def extract_patch(dir_name, suffix_name, patch_size, patch_step=1, flatten=False): ''' 提取指定类型病灶的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 = [] 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=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) mask_images = convert2depthlaster(mask_images) mhd_images = convert2depthlaster(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 patch_count += 1 if flatten: patches.append(np.array(cur_patch).flatten()) else: patches.append(cur_patch) if patch_count == 1: continue patches_arr.append(patches) return np.array(patches_arr)
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 / 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.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 load_patch(dir_path, return_roi=False, parent_dir=None): phasenames = ['NC', 'ART', 'PV'] mhd_images = [] mask_images = [] for phasename in phasenames: # print os.path.join(parent_dir, basename, phasename + '_Image*.mhd') # print os.path.join(dir_path, phasename + '_Image*.mhd') image_path = glob(os.path.join(dir_path, phasename + '_Image*.mhd'))[0] mask_path = os.path.join(dir_path, 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 visulization(data_dir): img = None for phasename in ['NC', 'ART', 'PV']: mhd_path = glob(os.path.join(data_dir, phasename + '_Image*.mhd'))[0] mask_path = glob( os.path.join(data_dir, phasename + '_Registration*.mhd'))[0] mhd_image = read_mhd_image(mhd_path) mask_image = read_mhd_image(mask_path) mhd_image = np.squeeze(mhd_image) mask_image = np.squeeze(mask_image) x_min, x_max, y_min, y_max = get_boundingbox(mask_image) ROI = mhd_image[x_min:x_max, y_min:y_max] print np.shape(ROI) if img is None: img = [] img.append(ROI) img = convert2depthlaster(img) print np.shape(img) img = Image.fromarray(np.asarray(img, np.uint8)) img.show() img.save('./multi-phase_ROI.png')
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