def read_from_dir(data_dir, phasename='PV'): mhd_path = glob(os.path.join(data_dir, phasename + '_Image*.mhd'))[0] mhd_image = read_mhd_image(mhd_path) mask_path = os.path.join(data_dir, phasename + '_Registration.mhd') mask_image = read_mhd_image(mask_path) mhd_image = np.squeeze(mhd_image) mask_image = np.squeeze(mask_image) return mask_image, mhd_image
def load_patch(patch_path, return_roi=False, parent_dir=None): if not return_roi: if patch_path.endswith('.jpg'): return Image.open(patch_path) if patch_path.endswith('.npy'): return np.load(patch_path) else: phasenames = ['NC', 'ART', 'PV'] if patch_path.endswith('.jpg'): basename = os.path.basename(patch_path) basename = basename[: basename.rfind('_')] mask_images = [] mhd_images = [] for phasename in phasenames: image_path = glob(os.path.join(parent_dir, basename, phasename + '_Image*.mhd'))[0] mask_path = os.path.join(parent_dir, basename, phasename + '_Registration.mhd') mhd_image = read_mhd_image(image_path, rejust=True) mhd_image = np.squeeze(mhd_image) # show_image(mhd_image) mask_image = read_mhd_image(mask_path) mask_image = np.squeeze(mask_image) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image) # xmin -= 15 # xmax += 15 # ymin -= 15 # ymax += 15 mask_image = mask_image[xmin: xmax, ymin: ymax] mhd_image = mhd_image[xmin: xmax, ymin: ymax] mhd_image[mask_image != 1] = 0 mask_images.append(mask_image) mhd_images.append(mhd_image) mhd_images = convert2depthlaster(mhd_images) return mhd_images if patch_path.endswith('.npy'): basename = os.path.basename(patch_path) basename = basename[: basename.rfind('_')] mask_images = [] mhd_images = [] for phasename in phasenames: image_path = glob(os.path.join(parent_dir, basename, phasename + '_Image*.mhd'))[0] mask_path = os.path.join(parent_dir, basename, phasename + '_Registration.mhd') mhd_image = read_mhd_image(image_path, rejust=False) # 因为存储的是npy格式,所以不进行窗宽窗位的调整 mhd_image = np.squeeze(mhd_image) # show_image(mhd_image) mask_image = read_mhd_image(mask_path) mask_image = np.squeeze(mask_image) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image) # xmin -= 15 # xmax += 15 # ymin -= 15 # ymax += 15 mask_image = mask_image[xmin: xmax, ymin: ymax] mhd_image = mhd_image[xmin: xmax, ymin: ymax] mhd_image[mask_image != 1] = 0 mask_images.append(mask_image) mhd_images.append(mhd_image) mhd_images = convert2depthlaster(mhd_images) return mhd_images
def extract_patch_npy(dir_name, suffix_name, save_dir, patch_size, patch_step=1): ''' 提取指定类型病灶的patch 保存原始像素值,存成npy的格式 :param patch_size: 提取patch的大小 :param dir_name: 目前所有病例的存储路径 :param suffix_name: 指定的病灶类型的后缀,比如说cyst 就是0 :param save_dir: 提取得到的patch的存储路径 :param patch_step: 提取patch的步长 :return: None ''' count = 0 names = os.listdir(dir_name) for name in names: if name.endswith(suffix_name): # 只提取指定类型病灶的patch mask_images = [] mhd_images = [] for phasename in phasenames: image_path = glob(os.path.join(dir_name, name, phasename + '_Image*.mhd'))[0] mask_path = os.path.join(dir_name, name, phasename + '_Registration.mhd') mhd_image = read_mhd_image(image_path) mhd_image = np.squeeze(mhd_image) # show_image(mhd_image) mask_image = read_mhd_image(mask_path) mask_image = np.squeeze(mask_image) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image) mask_image = mask_image[xmin: xmax, ymin: ymax] mhd_image = mhd_image[xmin: xmax, ymin: ymax] mhd_image[mask_image != 1] = 0 mask_images.append(mask_image) mhd_images.append(mhd_image) # show_image(mhd_image) mask_images = convert2depthlaster(mask_images) mhd_images = convert2depthlaster(mhd_images) count += 1 [width, height, depth] = list(np.shape(mhd_images)) patch_count = 1 if width * height >= 900: patch_step = int(math.sqrt(width * height / 160)) for i in range(patch_size / 2, width - patch_size / 2, patch_step): for j in range(patch_size / 2, height - patch_size / 2, patch_step): cur_patch = mhd_images[i - patch_size / 2:i + patch_size / 2, j - patch_size / 2: j + patch_size / 2, :] if (np.sum(mask_images[i - patch_size / 2:i + patch_size / 2, j - patch_size / 2: j + patch_size / 2, :]) / ( (patch_size - 1) * (patch_size - 1) * 3)) < 0.9: continue save_path = os.path.join(save_dir, name + '_' + str(patch_count) + '.npy') # print save_path np.save(save_path, np.array(cur_patch)) patch_count += 1 if patch_count == 1: continue # save_path = os.path.join(save_dir, name + '_' + str(patch_count) + '.png') # roi_image = Image.fromarray(np.asarray(mhd_images, np.uint8)) # roi_image.save(save_path) print count
def extract_liver_density(dir_name, suffix_name, save_dir, subclass, type): ''' 提取肝脏的平均密度 :param dir_name: 目前所有病例的存储路径 :param suffix_name: 指定的病灶类型的后缀,比如说cyst 就是0 :param save_dir: 提取得到的patch的存储路径 :return: None ''' count = 0 names = os.listdir(dir_name) liver_density_dict = {} for name in names: if name.endswith(suffix_name): # 只提取指定类型病灶的patch liver_density = [] for phasename in phasenames: image_path = glob( os.path.join(dir_name, name, phasename + '_Image*.mhd'))[0] liver_mask_path = glob( os.path.join(dir_name, name, phasename + '*Liver*.mhd'))[0] mask_path = os.path.join(dir_name, name, phasename + '_Registration.mhd') mhd_image = read_mhd_image(image_path, rejust=False) mhd_image = np.squeeze(mhd_image) # show_image(mhd_image) mask_image = read_mhd_image(mask_path) mask_image = np.squeeze(mask_image) liver_mask_image = read_mhd_image(liver_mask_path) liver_mask_image = np.squeeze(liver_mask_image) # 计算肝脏的平均密度 value_sum = np.sum(mhd_image[liver_mask_image != 0]) pixel_num = np.sum(liver_mask_image != 0) average_value = (1.0 * value_sum) / (1.0 * pixel_num) liver_density.append(average_value) print name, liver_density liver_density_dict[name] = liver_density scio.savemat( os.path.join( save_dir, 'raw_liver_density_' + str(subclass) + '_' + str(type) + '.mat'), liver_density_dict) return liver_density_dict
def load_ROI(dir_name): phasenames = ['NC', 'ART', 'PV'] mask_images = [] mhd_images = [] for phasename in phasenames: image_path = glob(os.path.join(dir_name, phasename + '_Image*.mhd'))[0] mask_path = os.path.join(dir_name, phasename + '_Registration.mhd') mhd_image = read_mhd_image(image_path, rejust=True) mhd_image = np.squeeze(mhd_image) # show_image(mhd_image) mask_image = read_mhd_image(mask_path) mask_image = np.squeeze(mask_image) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image) # xmin -= 15 # xmax += 15 # ymin -= 15 # ymax += 15 mask_image = mask_image[xmin:xmax, ymin:ymax] mhd_image = mhd_image[xmin:xmax, ymin:ymax] mhd_image[mask_image != 1] = 0 mask_images.append(mask_image) mhd_images.append(mhd_image) mhd_images = convert2depthlaster(mhd_images) return mhd_images
def extract_patch_npy_for_JI(dir_name, suffix_name, save_path, patch_size, patch_step=1, erode_size=1): ''' 提取指定类型病灶的patch 保存原始像素值,存成npy的格式 :param patch_size: 提取patch的大小 :param dir_name: 目前所有病例的存储路径 :param suffix_name: 指定的病灶类型的后缀,比如说cyst 就是0 :param save_path: 提取得到的patch的存储路径 :param patch_step: 提取patch的步长 :param erode_size: 向内缩的距离,因为我们需要确定内部区域,所以为了得到内部区域,我们就将原区域向内缩以得到内部区域 :return: None ''' count = 0 names = os.listdir(dir_name) patches = [] for name in names: if name.endswith(suffix_name): # 只提取指定类型病灶的patch mask_images = [] mhd_images = [] flag = True for phasename in phasenames: image_path = glob(os.path.join(dir_name, name, phasename + '_Image*.mhd'))[0] mask_path = os.path.join(dir_name, name, phasename + '_Registration.mhd') mhd_image = read_mhd_image(image_path) mhd_image = np.squeeze(mhd_image) # show_image(mhd_image) mask_image = read_mhd_image(mask_path) mask_image = np.squeeze(mask_image) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image) if (xmax - xmin) <= erode_size or (ymax - ymin) <= erode_size: flag = False continue mask_image = image_erode(mask_image, erode_size) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image) mask_image = mask_image[xmin: xmax, ymin: ymax] mhd_image = mhd_image[xmin: xmax, ymin: ymax] # mhd_image[mask_image != 1] = 0 mask_images.append(mask_image) mhd_images.append(mhd_image) # show_image(mhd_image) if not flag: continue mask_images = convert2depthlaster(mask_images) mhd_images = convert2depthlaster(mhd_images) count += 1 [width, height, depth] = list(np.shape(mhd_images)) patch_count = 1 # if width * height >= 900: # patch_step = int(math.sqrt(width * height / 100)) for i in range(patch_size / 2, width - patch_size / 2, patch_step): for j in range(patch_size / 2, height - patch_size / 2, patch_step): cur_patch = mhd_images[i - patch_size / 2:i + patch_size / 2 + 1, j - patch_size / 2: j + patch_size / 2 + 1, :] if (np.sum(mask_images[i - patch_size / 2:i + patch_size / 2, j - patch_size / 2: j + patch_size / 2, :]) / ( (patch_size - 1) * (patch_size - 1) * 3)) < 0.5: continue # save_path = os.path.join(save_dir, name + '_' + str(patch_count) + '.npy') # print save_path # np.save(save_path, np.array(cur_patch)) cur_patch = np.reshape(cur_patch, [patch_size*patch_size*3]) patches.append(cur_patch) patch_count += 1 if patch_count == 1: continue print np.shape(patches) patches = np.concatenate([patches, np.expand_dims([int(suffix_name)] * len(patches), axis=1)], axis=1) print np.shape(patches) np.save(save_path, patches) print count
def extract_patches_multifiles(data_dir, names, target_label, patch_size, patch_step, save_dir): patches = [] labeles = [] coding_labeles = [] for name in names: if name is not None and not name.endswith(target_label): continue cur_data_dir = os.path.join(data_dir, name) patches_save_path = os.path.join(cur_data_dir, 'patches.npy') coding_labeles_path = os.path.join(cur_data_dir, 'coding_labeles.npy') labeles_path = os.path.join(cur_data_dir, 'labeles.npy') mask_images = [] mhd_images = [] if not os.path.exists(patches_save_path): for phasename in ['NC', 'ART', 'PV']: image_path = glob(os.path.join(data_dir, name, phasename + '_Image*.mhd'))[0] mask_path = os.path.join(data_dir, name, phasename + '_Registration.mhd') mhd_image = read_mhd_image(image_path, rejust=True) mhd_image = np.squeeze(mhd_image) # show_image(mhd_image) mask_image = read_mhd_image(mask_path) mask_image = np.squeeze(mask_image) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image) mask_image = mask_image[xmin: xmax, ymin: ymax] mhd_image = mhd_image[xmin: xmax, ymin: ymax] mhd_image[mask_image != 1] = 0 mask_images.append(mask_image) mhd_images.append(mhd_image) # show_image(mhd_image) cur_patches = [] cur_coding_labeles = [] mask_images = convert2depthlaster(mask_images) coding_image = convert_coding(cur_data_dir) mhd_images = convert2depthlaster(mhd_images) [width, height, _] = list(np.shape(mhd_images)) print 'extract patches from ', cur_data_dir, ' at ', str( os.getpid()), ' corresponding size is [', width, height, ']' for i in range(patch_size / 2, width - patch_size / 2, patch_step): for j in range(patch_size / 2, height - patch_size / 2, patch_step): cur_patch = mhd_images[i - patch_size / 2:i + patch_size / 2 + 1, j - patch_size / 2: j + patch_size / 2 + 1, :] cur_label = target_label cur_coding_label = coding_image[i, j] if save_dir is not None: save_path_dir = os.path.join(save_dir, str(cur_coding_label), str(cur_label)) if os.path.exists(save_path_dir): cur_id = len(os.listdir(save_path_dir)) else: cur_id = 0 save_path = os.path.join(save_dir, str(cur_coding_label), str(cur_label), str(cur_id) + '.npy') check_save_path(save_path) np.save(save_path, cur_patch) else: cur_patches.append(flatten_multiphase(np.array(cur_patch))) cur_coding_labeles.append(cur_coding_label) if save_dir is None: if len(cur_patches) == 0: continue patches.append(cur_patches) coding_labeles.append(cur_coding_labeles) labeles.append(int(target_label)) np.save(patches_save_path, cur_patches) np.save(coding_labeles_path, cur_coding_labeles) else: cur_patches = np.load(patches_save_path) cur_coding_labeles = np.load(coding_labeles_path) coding_labeles.append(cur_coding_labeles) patches.append(cur_patches) labeles.append(int(target_label)) print len(patches), len(coding_labeles), len(labeles) return patches, coding_labeles, labeles
def extract_patch(dir_name, suffix_name, patch_size, patch_step=1): ''' 提取指定类型病灶的patch :param patch_size: 提取patch的大小 :param dir_name: 目前所有病例的存储路径 :param suffix_name: 指定的病灶类型的后缀,比如说cyst 就是0 :param patch_step: 提取patch的步长 :return: patch_arr (图像的个数, patch的个数) ''' count = 0 names = os.listdir(dir_name) patches_arr = [] paths = [] return_mhd_images = [] for name in names: if name.endswith(suffix_name): # 只提取指定类型病灶的patch mask_images = [] mhd_images = [] paths.append(os.path.join(dir_name, name)) for phasename in phasenames: image_path = glob( os.path.join(dir_name, name, phasename + '_Image*.mhd'))[0] mask_path = os.path.join(dir_name, name, phasename + '_Registration.mhd') mhd_image = read_mhd_image(image_path, rejust=mhd_adjust) mhd_image = np.squeeze(mhd_image) # show_image(mhd_image) mask_image = read_mhd_image(mask_path) mask_image = np.squeeze(mask_image) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image) # xmin -= 15 # xmax += 15 # ymin -= 15 # ymax += 15 mask_image = mask_image[xmin:xmax, ymin:ymax] mhd_image = mhd_image[xmin:xmax, ymin:ymax] mhd_image[mask_image != 1] = 0 mask_images.append(mask_image) mhd_images.append(mhd_image) # show_image(mhd_image) mask_images = convert2depthlaster(mask_images) mhd_images = convert2depthlaster(mhd_images) return_mhd_images.append(mhd_images) # show_image(mhd_images) count += 1 [width, height, depth] = list(np.shape(mhd_images)) patch_count = 1 # if width * height >= 400: # patch_step = int(math.sqrt(width * height / 100)) patches = [] for i in range(patch_size / 2, width - patch_size / 2, patch_step): for j in range(patch_size / 2, height - patch_size / 2, patch_step): cur_patch = mhd_images[i - patch_size / 2:i + patch_size / 2, j - patch_size / 2:j + patch_size / 2, :] if (np.sum(mask_images[i - patch_size / 2:i + patch_size / 2, j - patch_size / 2:j + patch_size / 2, :]) / ((patch_size - 1) * (patch_size - 1) * 3)) < 0.9: continue patches.append(cur_patch) patches_arr.append(patches) if patch_count == 1: continue print len(patches_arr) return patches_arr, paths, return_mhd_images
def generate_heatmap_version2(data_path, pointed_phase_index, patch_size, save_path): ''' 生成label map,生成的label map严格按照ROI的边界来生成 :param data_path: :param pointed_phase_index: :param patch_size: :param save_path: :return: ''' start_time = time.time() mhd_images = [] mask_images = [] for phase_name in ['NC', 'ART', 'PV']: image_path = glob(os.path.join(data_path, phase_name + '_Image*.mhd'))[0] mask_path = os.path.join(data_path, phase_name + '_Registration.mhd') mhd_image = read_mhd_image(image_path) mask_image = read_mhd_image(mask_path) mhd_image = np.squeeze(mhd_image) mask_image = np.squeeze(mask_image) mhd_images.append(mhd_image) mask_images.append(mask_image) mask_images = convert2depthlaster(mask_images) mhd_images = convert2depthlaster(mhd_images) print np.shape(mask_images) new_mask_image = np.zeros([512, 512], np.uint8) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_images[:, :, pointed_phase_index]) roi_image = mask_images[xmin:xmax, ymin:ymax, :] patches = [] for i in range(xmin, xmax): for j in range(ymin, ymax): if mask_images[i, j, pointed_phase_index] == 0: continue cur_patch = mhd_images[i - patch_size / 2:i + patch_size / 2, j - patch_size / 2:j + patch_size / 2, :] patches.append(cur_patch) start_index = 0 end_index = 0 predicted_labels = [] while True: end_index = start_index + net_config.BATCH_SIZE print end_index, '/', len(patches) if end_index >= len(patches): end_index = len(patches) cur_patches = patches[start_index:end_index] # expand_patches = patches[start_index: end_index] expand_patches = [roi_image] * len( cur_patches) # 使用完整的ROI作为expand 的patch roi_images_values = resize_images(cur_patches, net_config.ROI_SIZE_W, rescale=(not divided_liver)) expand_roi_images_values = resize_images(expand_patches, net_config.EXPAND_SIZE_W, rescale=(not divided_liver)) predicted_label_value = sess.run(predicted_label_tensor, feed_dict={ roi_images: roi_images_values, expand_roi_images: expand_roi_images_values, batch_size_tensor: len(roi_images_values), is_training_tensor: False }) predicted_labels.extend(predicted_label_value) if end_index == len(patches): break start_index = end_index start_index = 0 statics = [0, 0, 0, 0] for i in range(xmin, xmax): for j in range(ymin, ymax): if mask_images[i, j, pointed_phase_index] == 0: continue print(i - xmin) * (xmax - xmin) + (j - ymin) new_mask_image[i, j] = predicted_labels[start_index] + 2 statics[predicted_labels[start_index]] += 1 start_index += 1 print statics end_time = time.time() print 'cost time is ', (end_time - start_time) save_mhd_image(new_mask_image, save_path)
def extract_interior_patch_npy(dir_name, suffix_name, save_dir, patch_size, patch_step=1, erode_size=1): ''' 提取指定类型病灶的patch 保存原始像素值,存成npy的格式 :param patch_size: 提取patch的大小 :param dir_name: 目前所有病例的存储路径 :param suffix_name: 指定的病灶类型的后缀,比如说cyst 就是0 :param save_dir: 提取得到的patch的存储路径 :param patch_step: 提取patch的步长 :param erode_size: 向内缩的距离,因为我们需要确定内部区域,所以为了得到内部区域,我们就将原区域向内缩以得到内部区域 :return: None ''' count = 0 names = os.listdir(dir_name) patches = [] for name in names: if name.endswith(suffix_name): # 只提取指定类型病灶的patch mask_images = [] mhd_images = [] flag = True for phasename in phasenames: image_path = glob( os.path.join(dir_name, name, phasename + '_Image*.mhd'))[0] mask_path = os.path.join(dir_name, name, phasename + '_Registration.mhd') mhd_image = read_mhd_image(image_path) mhd_image = np.squeeze(mhd_image) # show_image(mhd_image) mask_image = read_mhd_image(mask_path) mask_image = np.squeeze(mask_image) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image) if (xmax - xmin) <= 5 or (ymax - ymin) <= 5: flag = False continue mask_image = image_erode(mask_image, erode_size) [xmin, xmax, ymin, ymax] = get_boundingbox(mask_image) mask_image = mask_image[xmin:xmax, ymin:ymax] mhd_image = mhd_image[xmin:xmax, ymin:ymax] # mhd_image[mask_image != 1] = 0 mask_images.append(mask_image) mhd_images.append(mhd_image) # show_image(mhd_image) if not flag: continue mask_images = convert2depthlaster(mask_images) mhd_images = convert2depthlaster(mhd_images) count += 1 [width, height, depth] = list(np.shape(mhd_images)) patch_count = 1 cur_patches = [] # if width * height >= 900: # patch_step = int(math.sqrt(width * height / 100)) for i in range(patch_size / 2, width - patch_size / 2, patch_step): for j in range(patch_size / 2, height - patch_size / 2, patch_step): cur_patch = mhd_images[i - patch_size / 2:i + patch_size / 2, j - patch_size / 2:j + patch_size / 2, :] if (np.sum(mask_images[i - patch_size / 2:i + patch_size / 2, j - patch_size / 2:j + patch_size / 2, :]) / ((patch_size - 1) * (patch_size - 1) * 3)) < 0.5: continue if save_dir is not None: save_path = os.path.join( save_dir, name + '_' + str(patch_count) + '.npy') np.save(save_path, np.array(cur_patch)) else: cur_patches.append(flatten_arr(np.asarray(cur_patch))) patch_count += 1 if save_dir is None: if len(cur_patches) == 0: continue patches.append(cur_patches) if patch_count == 1: continue # save_path = os.path.join(save_dir, name + '_' + str(patch_count) + '.png') # roi_image = Image.fromarray(np.asarray(mhd_images, np.uint8)) # roi_image.save(save_path) print count if save_dir is None: return patches