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 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_mask_image(image_path, mask_dir_name=['LiverMask', 'TumorMask']): mask_image = {} liver_mask = {} tumors_mask = [] tumor_files_num = len(os.listdir(os.path.join(image_path, mask_dir_name[1]))) / 6 for phase in Config.QIXIANGS: mask_file_name = glob.glob(os.path.join(image_path, mask_dir_name[0]) + '\\*'+phase+'.mhd')[0] liver_mask[phase] = read_mhd_image(mask_file_name) for i in range(tumor_files_num): tumor_mask_image = {} for phase in Config.QIXIANGS: tumor_mask_file_path = \ glob.glob(os.path.join(image_path, mask_dir_name[1]) + '\\*' + phase + '_' + str(i + 1) + '.mhd')[0] tumor_mask_image[phase] = np.asarray(read_mhd_image(tumor_mask_file_path), np.uint8) tumors_mask.append(tumor_mask_image) mask_image['LiverMask'] = liver_mask mask_image['TumorMask'] = tumors_mask return mask_image
def extract_ROI_image(mhd_path, tumor_path, liver_path, image_size): mhd_image = read_mhd_image(mhd_path, rejust=True) # show_image(mhd_image) tumor_mask_image = read_mhd_image(tumor_path) liver_mask_image = read_mhd_image(liver_path) mhd_image = compress22dim(mhd_image) tumor_mask_image = compress22dim(tumor_mask_image) liver_mask_image = compress22dim(liver_mask_image) # 注意记得填充图像 tumor_bounding_box = get_boundingbox(tumor_mask_image) liver_average = cal_liver_average(mhd_image, liver_mask_image) mhd_image_copy = copy(mhd_image) mhd_image_copy[tumor_mask_image != 1] = 0 roi_image = mhd_image_copy[tumor_bounding_box[0]:tumor_bounding_box[1], tumor_bounding_box[2]:tumor_bounding_box[3]] roi_image = resize_image(roi_image, size=image_size) roi_image = np.asarray(roi_image, np.float32) roi_image = roi_image / liver_average return roi_image
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 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_image(image_path): images = {} dirs = os.listdir(image_path) exists = False for dir_name in dirs: if dir_name in ['ART', 'NC', 'PV']: exists = True mhd_path = os.path.join(image_path, dir_name, dir_name+'.mhd') if os.path.exists(mhd_path): images[dir_name] = read_mhd_image(mhd_path) else: images[dir_name] = read_dicom_series(os.path.join(image_path, dir_name)) if Config.ADJUST_WW_WC: images[dir_name] = rejust_pixel_value(images[dir_name]) if not exists: print image_path, 'happened error' return images
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 copy_mhd_image(mhd_path, save_path): mhd_image = read_mhd_image(mhd_path) save_mhd_image(mhd_image, save_path)
feed_dict={ x: validation_images, y_: validation_labels } ) _, _, _, error_indexs, error_record = calculate_acc_error( logits=np.argmax(logits, 1), label=validation_labels, show=True ) print 'accuracy is %g' % \ (validation_accuracy) return error_indexs, error_record if __name__ == '__main__': # va_size, image_w, image_h, channal dataset = ValDataSet(data_path='/home/give/Documents/dataset/MedicalImage/MedicalImage/ROI/val', phase='ART', new_size=[sub_Config.IMAGE_W, sub_Config.IMAGE_H], shuffle=False) for one_sample_name in dataset.image_names: original_image = read_mhd_image(one_sample_name) operations = [ [96, 64, 1.0], [64, 64, 1.0], [64, 64, 0.8] ] error_indexs, error_record = val( dataset, load_model_path='/home/give/PycharmProjects/MedicalImage/Net/BaseNet/LeNet/model_finetuing/model_art/', ) dataset.show_error_name(error_indexs, error_record)
from Tools import read_mhd_image, show_image, save_image_with_mask, compress22dim import os from glob import glob import numpy as np data_dir = '/home/give/Documents/dataset/MedicalImage/MedicalImage/SL_TrainAndVal/train' names = os.listdir(data_dir) for name in names: cur_dir = os.path.join(data_dir, name) for phasename in ['NC', 'ART', 'PV']: image_path = glob(os.path.join(cur_dir, phasename + '_Image*.mhd'))[0] mask_paths = glob(os.path.join(cur_dir, phasename + '_Mask.mhd')) if len(mask_paths) == 0: mask_paths = glob(os.path.join(cur_dir, phasename + '_Mask_*.mhd')) for path in mask_paths: if path.endswith('Expand.mhd'): continue else: mask_path = path break else: mask_path = mask_paths[0] mhd_image = read_mhd_image(image_path, rejust=True) mhd_image = compress22dim(mhd_image) print mask_path tumor_mask_image = read_mhd_image(mask_path) tumor_mask_image = compress22dim(tumor_mask_image) print np.max(tumor_mask_image) save_image_with_mask(mhd_image, tumor_mask_image, None) input('input %d')