def removekidneysmallobj(): """ 去除Vnet肾脏结果的小物体 :return: """ kits_path = "D:\Data\kits19\kits19test\kidney_modify" result_path = "D:\Data\kits19\kits19test\\kidney_modify\\" """ load itk image,change z Spacing value to 1,and save image ,liver mask ,tumor mask :return:None """ # step2 get all train image path_list = file_name_path(kits_path) # step3 get signal train image and mask for subsetindex in range(len(path_list)): kits_subset_path = kits_path + "/" + str(path_list[subsetindex]) + "/" images = [] index = 0 for _ in os.listdir(kits_subset_path): image = cv2.imread(kits_subset_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) images.append(image) index += 1 ys_pd_array = np.array(images) ys_pd_array = np.reshape(ys_pd_array, (index, 512, 512)) ys_pd_sitk = sitk.GetImageFromArray(ys_pd_array) ys_pd_array = removesmallConnectedCompont(ys_pd_sitk, 0.2) ys_pd_array = np.clip(ys_pd_array, 0, 255).astype('uint8') sub_src_path = result_path + path_list[subsetindex] if not os.path.exists(sub_src_path): os.makedirs(sub_src_path) for i in range(np.shape(ys_pd_array)[0]): cv2.imwrite(sub_src_path + "/" + str(i) + ".bmp", ys_pd_array[i])
def remove2d3dtumorsmallobj(): """ 去除2d和3dVnet的肿瘤的小物体 :return: """ tumor3d_path = "D:\Data\kits19\kits19test\\tumor3d" tumor2d_path = "D:\Data\kits19\kits19test\\tumor2d" """ load itk image,change z Spacing value to 1,and save image ,liver mask ,tumor mask :return:None """ # step2 get all train image path_list = file_name_path(tumor3d_path) # step3 get signal train image and mask for subsetindex in range(len(path_list)): kits_subset_path = tumor3d_path + "/" + str(path_list[subsetindex]) + "/" tumor2d_subset_path = tumor2d_path + "/" + str(path_list[subsetindex]) + "/" images = [] tumor2dmasks = [] index = 0 for _ in os.listdir(kits_subset_path): image = cv2.imread(kits_subset_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) mask = cv2.imread(tumor2d_subset_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) images.append(image) tumor2dmasks.append(mask) index += 1 tumor3d_array = np.array(images) tumor3d_array = np.reshape(tumor3d_array, (index, 512, 512)) tumor2d_array = np.array(tumor2dmasks) tumor2d_array = np.reshape(tumor2d_array, (index, 512, 512)) tumor2d_array_sitk = sitk.GetImageFromArray(tumor2d_array) tumor2d_array = removesmallConnectedCompont(tumor2d_array_sitk, 0.2) tumor2d_array_sitk = sitk.GetImageFromArray(tumor3d_array) tumor3d_array = removesmallConnectedCompont(tumor2d_array_sitk, 0.2) tumor2d_array = np.clip(tumor2d_array, 0, 255).astype('uint8') tumor3d_array = np.clip(tumor3d_array, 0, 255).astype('uint8') sub_tumor3d_path = tumor3d_path + "/" + path_list[subsetindex] sub_tumor2d_path = tumor2d_path + "/" + path_list[subsetindex] if not os.path.exists(sub_tumor3d_path): os.makedirs(sub_tumor3d_path) if not os.path.exists(sub_tumor2d_path): os.makedirs(sub_tumor2d_path) for i in range(np.shape(tumor3d_array)[0]): cv2.imwrite(sub_tumor3d_path + "/" + str(i) + ".bmp", tumor3d_array[i]) cv2.imwrite(sub_tumor2d_path + "/" + str(i) + ".bmp", tumor2d_array[i])
def tumor2d3dinkidney(): """ 保留肾脏区域范围内的肿瘤2d和3d结果 :return: """ tumor3d_path = "D:\Data\kits19\kits19test\\tumor3d" tumor2d_path = "D:\Data\kits19\kits19test\\tumor2d" kidney_path = "D:\Data\kits19\kits19test\kidney_modify" result_path = "D:\Data\kits19\kits19test\\tumor_modify1\\" """ load itk image,change z Spacing value to 1,and save image ,liver mask ,tumor mask :return:None """ # step2 get all train image path_list = file_name_path(tumor3d_path) # step3 get signal train image and mask for subsetindex in range(len(path_list)): kidney_subset_path = kidney_path + "/" + str(path_list[subsetindex]) + "/" kits_subset_path = tumor3d_path + "/" + str(path_list[subsetindex]) + "/" tumor2d_subset_path = tumor2d_path + "/" + str(path_list[subsetindex]) + "/" kidneys = [] images = [] tumor2dmasks = [] index = 0 for _ in os.listdir(kits_subset_path): kidney = cv2.imread(kidney_subset_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) image = cv2.imread(kits_subset_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) mask = cv2.imread(tumor2d_subset_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) kidneys.append(kidney) images.append(image) tumor2dmasks.append(mask) index += 1 kidneys_array = np.array(kidneys) kidneys_array = np.reshape(kidneys_array, (index, 512, 512)) kidneys_array_sitk = sitk.GetImageFromArray(kidneys_array) kidneys_array = morphologicaloperation(kidneys_array_sitk, 5, "dilate") tumor3d_array = np.array(images) tumor3d_array = np.reshape(tumor3d_array, (index, 512, 512)) tumor2d_array = np.array(tumor2dmasks) tumor2d_array = np.reshape(tumor2d_array, (index, 512, 512)) tumor3d_array = tumor3d_array & kidneys_array tumor2d_array = tumor2d_array & kidneys_array tumor_array = tumor3d_array | tumor2d_array tumor_array_sitk = sitk.GetImageFromArray(tumor_array) tumor_array = removesmallConnectedCompont(tumor_array_sitk, 0.1) tumor_array = np.clip(tumor_array, 0, 255).astype('uint8') sub_tumor_path = result_path + path_list[subsetindex] if not os.path.exists(sub_tumor_path): os.makedirs(sub_tumor_path) for i in range(np.shape(tumor_array)[0]): cv2.imwrite(sub_tumor_path + "/" + str(i) + ".bmp", tumor_array[i])
def inference(): depth_z = 48 Vnet3d = Vnet3dModule(512, 512, depth_z, channels=1, costname=("dice coefficient",), inference=True, model_path="log\segmeation\model\Vnet3d.pd-20000") test_path = "E:\junqiangchen\data\kits19\kits19process\\" image_path = "Image" mask_path = "Mask" dice_values = [] for num in range(200, 210, 1): index = 0 batch_xs = [] batch_ys = [] test_image_path = test_path + image_path + "/" + str(num) test_mask_path = test_path + mask_path + "/" + str(num) for _ in os.listdir(test_image_path): image = cv2.imread(test_image_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) label = cv2.imread(test_mask_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) batch_xs.append(image) batch_ys.append(label) index += 1 xs_array = np.array(batch_xs) ys_array = np.array(batch_ys) xs_array = np.reshape(xs_array, (index, 512, 512)) ys_array = np.reshape(ys_array, (index, 512, 512)) ys_pd_array = np.empty((index, 512, 512), np.uint8) last_depth = 0 for depth in range(0, index // depth_z, 1): patch_xs = xs_array[depth * depth_z:(depth + 1) * depth_z, :, :] pathc_pd = Vnet3d.prediction(patch_xs) ys_pd_array[depth * depth_z:(depth + 1) * depth_z, :, :] = pathc_pd last_depth = depth if index != depth_z * last_depth: patch_xs = xs_array[(index - depth_z):index, :, :] pathc_pd = Vnet3d.prediction(patch_xs) ys_pd_array[(index - depth_z):index, :, :] = pathc_pd ys_pd_sitk = sitk.GetImageFromArray(ys_pd_array) ys_pd_array = removesmallConnectedCompont(ys_pd_sitk, 0.4) ys_pd_array = np.clip(ys_pd_array, 0, 255).astype('uint8') dice_value = calcu_dice(ys_pd_array, ys_array) print("num,dice:", (num, dice_value)) dice_values.append(dice_value) for depth in range(0, index, 1): cv2.imwrite(test_mask_path + "/" + str(depth) + "predict.bmp", ys_pd_array[depth]) average = sum(dice_values) / len(dice_values) print("average dice:", average)
def tumor2d3dmerge(): """ 求2d和3d交集的区域分别与2d和3d相连接的区域都保留下来 :return: """ tumor3d_path = "D:\Data\kits19\kits19test\\tumor3d" tumor2d_path = "D:\Data\kits19\kits19test\\tumor2d" tumor2d3d_path = "D:\Data\kits19\kits19test\\tumor2_3d" result_path = "D:\Data\kits19\kits19test\\tumor_modify2\\" """ load itk image,change z Spacing value to 1,and save image ,liver mask ,tumor mask :return:None """ # step2 get all train image path_list = file_name_path(tumor3d_path) # step3 get signal train image and mask for subsetindex in range(len(path_list)): kits_subset_path = tumor3d_path + "/" + str(path_list[subsetindex]) + "/" tumor2d_subset_path = tumor2d_path + "/" + str(path_list[subsetindex]) + "/" tumor2d3d_subset_path = tumor2d3d_path + "/" + str(path_list[subsetindex]) + "/" images = [] tumor2dmasks = [] tumo2d3dmasks = [] index = 0 for _ in os.listdir(kits_subset_path): image = cv2.imread(kits_subset_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) mask = cv2.imread(tumor2d_subset_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) tumor = cv2.imread(tumor2d3d_subset_path + "/" + str(index) + ".bmp", cv2.IMREAD_GRAYSCALE) images.append(image) tumor2dmasks.append(mask) tumo2d3dmasks.append(tumor) index += 1 tumor3d_array = np.array(images) tumor3d_array = np.reshape(tumor3d_array, (index, 512, 512)) tumor2d_array = np.array(tumor2dmasks) tumor2d_array = np.reshape(tumor2d_array, (index, 512, 512)) tumor2d3d_array = np.array(tumo2d3dmasks) tumor2d3d_array = np.reshape(tumor2d3d_array, (index, 512, 512)) tumor_array = np.zeros((index, 512, 512), np.int) for z in range(index): tumor2d3d = tumor2d3d_array[z] if np.max(tumor2d3d) != 0: tumor2d3dlabels, tumor2d3dout = cv2.connectedComponents(tumor2d3d) tumor3d = tumor3d_array[z] tumor3dlabels, tumor3dout = cv2.connectedComponents(tumor3d) tumor2d = tumor2d_array[z] tumor2dlabels, tumor2dout = cv2.connectedComponents(tumor2d) for i in range(1, tumor2d3dlabels): tumor2d3doutmask = np.zeros(tumor2d3dout.shape, np.int) tumor2d3doutmask[tumor2d3dout == i] = 255 for j in range(1, tumor3dlabels): tumor3doutmask = np.zeros(tumor3dout.shape, np.int) tumor3doutmask[tumor3dout == j] = 255 if cv2.countNonZero(tumor2d3doutmask & tumor3doutmask): tumor_array[z] = tumor_array[z] + tumor3doutmask for k in range(1, tumor2dlabels): tumor2doutmask = np.zeros(tumor2dout.shape, np.int) tumor2doutmask[tumor2dout == k] = 255 if cv2.countNonZero(tumor2d3doutmask & tumor2doutmask): tumor_array[z] = tumor_array[z] + tumor2doutmask tumor2d_array_sitk = sitk.GetImageFromArray(tumor_array) tumor2d_array = removesmallConnectedCompont(tumor2d_array_sitk, 0.2) tumor_array = np.clip(tumor2d_array, 0, 255).astype('uint8') sub_tumor_path = result_path + path_list[subsetindex] if not os.path.exists(sub_tumor_path): os.makedirs(sub_tumor_path) for i in range(np.shape(tumor_array)[0]): cv2.imwrite(sub_tumor_path + "/" + str(i) + ".bmp", tumor_array[i])
def inference(): """ Vnet network segmentation kidney fine segmatation :return: """ depth_z = 32 Vnet3d = Vnet3dModule(512, 512, depth_z, channels=1, costname=("dice coefficient", ), inference=True, model_path="log\segmeation\VNet\model\Vnet3d.pd") kits_path = "D:\Data\kits19\kits19\\test" image_name = "imaging.nii.gz" result_path = "D:\Data\kits19\kits19test" """ load itk image,change z Spacing value to 1,and save image ,liver mask ,tumor mask :return:None """ # step2 get all train image path_list = file_name_path(kits_path) read = open("kidneyrang.txt", 'r') # step3 get signal train image and mask for subsetindex in range(len(path_list)): line = read.readline() line = line.split(',') casename = line[0] start = int(line[1]) end = int(line[2][0:-1]) kits_subset_path = kits_path + "/" + str(path_list[subsetindex]) + "/" file_image = kits_subset_path + image_name # 1 load itk image and truncate value with upper and lower and get rang kideny region src = load_itkfilewithtrucation(file_image, 300, -200) originSpacing = src.GetSpacing() src_array = sitk.GetArrayFromImage(src) sub_src_array = src_array[:, :, start:end] sub_src = sitk.GetImageFromArray(sub_src_array) sub_src.SetSpacing(originSpacing) print(sub_src.GetSize()) thickspacing, widthspacing = originSpacing[0], originSpacing[1] # 2 change z spacing >1.0 to 1.0 if thickspacing > 1.0: _, sub_src = resize_image_itk( sub_src, newSpacing=(1.0, widthspacing, widthspacing), originSpcaing=(thickspacing, widthspacing, widthspacing), resamplemethod=sitk.sitkLinear) xs_array = sitk.GetArrayFromImage(sub_src) xs_array = np.swapaxes(xs_array, 0, 2) index = np.shape(xs_array)[0] ys_pd_array = np.zeros(np.shape(xs_array), np.uint8) last_depth = 0 for depth in range(0, index // depth_z, 1): patch_xs = xs_array[depth * depth_z:(depth + 1) * depth_z, :, :] pathc_pd = Vnet3d.prediction(patch_xs) ys_pd_array[depth * depth_z:(depth + 1) * depth_z, :, :] = pathc_pd last_depth = depth if index != depth_z * last_depth: patch_xs = xs_array[(index - depth_z):index, :, :] pathc_pd = Vnet3d.prediction(patch_xs) ys_pd_array[(index - depth_z):index, :, :] = pathc_pd ys_pd_sitk = sitk.GetImageFromArray(ys_pd_array) ys_pd_array = removesmallConnectedCompont(ys_pd_sitk, 0.2) ys_pd_array = np.clip(ys_pd_array, 0, 255).astype('uint8') sub_src_path = result_path + "/src/" + casename sub_pred_path = result_path + "/kidney_modify/" + casename if not os.path.exists(sub_src_path): os.makedirs(sub_src_path) if not os.path.exists(sub_pred_path): os.makedirs(sub_pred_path) for i in range(np.shape(xs_array)[0]): cv2.imwrite(sub_src_path + "/" + str(i) + ".bmp", xs_array[i]) cv2.imwrite(sub_pred_path + "/" + str(i) + ".bmp", ys_pd_array[i])