def get_croped_data_roi(data_path, roi_path, store_path=None): data_array = get_array_from_path(data_path) # data_array = np.flipud(data_array) # 注意这里的数据要进行翻转的 roi_array = get_array_from_path(roi_path) data_array = standard(data_array) crop_array = crop_data_array(roi_array, data_array) def resize_data(data_array, resized_shape): resized_data = cv2.resize(data_array, resized_shape, interpolation=cv2.INTER_NEAREST) return resized_data roi_new = crop_data_array(roi_array, roi_array) new_array = resize_data(crop_array, (224, 224)) roi_new = resize_data(roi_new, (224, 224)) # 选择只含有roi的层进行保存 index = [] for i in range(roi_new.shape[-1]): if np.sum(roi_new[..., i]) != 0: index.append(i) roi_new = roi_new[..., index] data_new = new_array[..., index] Imshow3DArray(data_new, roi_new)
def get_result(): data_path = r"Y:\DYB\shidaoai_vibenii\649302\BLADE42_256_3mm\026_BLADE42_256_3mm.nii" roi_path = r"Y:\DYB\shidaoai_vibenii\649302\BLADE42_256_3mm\026_BLADE42_256_3mm_ROI.mha" data_array = standard(get_array_from_path(data_path)) roi_array = get_array_from_path(roi_path) new_roi = binary_dilation(roi_array) Imshow3DArray(data_array, [roi_array, new_roi*1])
def show_origin_roi(self, value): """展示最原始的ROI,并去掉半月板""" for case in os.listdir(self.root_path): data_path = os.path.join(self.root_path, case, "new_data.nii") data_array = standard(get_array_from_path(data_path)) roi_path = os.path.join(self.root_path, case, "roi.nii.gz") """展示原始ROI""" roi_array = get_array_from_path(roi_path) roi_list = self.get_single_roi(roi_array) # Imshow3DArray(data_array, roi_list) """展示软骨和下骨ROI""" bone_roi_path = os.path.join(self.root_path, case, "moved_new_roi.nii.gz") # 整数 bone_roi_array = get_array_from_path(bone_roi_path) bone_roi_list = self.get_single_roi(bone_roi_array) Imshow3DArray(data_array, bone_roi_list[value]) Imshow3DArray(data_array, roi_list[value]) print(np.unique(bone_roi_array)) """展示计算图像""" four_array = os.path.join(self.root_path, case, "four_T2_array", "T2_3D_array.npy") four_array = np.load(four_array) img_mean = cv2.blur(four_array, (5, 5)) # for i in range(four_array.shape[-1]): # sns.set() # ax = sns.heatmap(img_mean[...,i]) # plt.show() break
def show_3d_roi(): case_path = r"Y:\DYB\data_and_result\doctor tao\data\all_data\Normal control\10007807" ruan_roi_list = ["new_ruan_roi_" + str(i) for i in range(1, 8)] xia_roi_list = ["roi_" + str(i) for i in range(1, 8)] ruan_roi_path_list = [os.path.join(case_path, i) for i in ruan_roi_list] xia_roi_path_list = [os.path.join(case_path, i) for i in xia_roi_list] ruan_roi_array_list = [get_array_from_path(i) for i in ruan_roi_path_list] xia_roi_array_list = [get_array_from_path(i) for i in xia_roi_path_list] data_array = get_array_from_path(os.path.join(case_path, "data.nii"))
def show(): """经过显示发现图像并不是很好,可能需要配准一下""" root_path = r"Y:\DYB\2020832DATA\doctor_gao\pet_ct_egfr\EGFR-" for case in os.listdir(root_path): ct_path = os.path.join(root_path, case, "ct.nii") pet_path = os.path.join(root_path, case, "pet.nii") roi_path = os.path.join(root_path, case, "no_name.nii") ct_array = get_array_from_path(ct_path) pet_array = get_array_from_path(pet_path) roi_array = get_array_from_path(roi_path) Imshow3DArray(standard(ct_array), roi_array) Imshow3DArray(standard(pet_array), roi_array)
def check_resample_result(self): """检查重采样的结果""" resample_result = os.path.join(self.case_path, "pet_Resize.nii") pet_array = get_array_from_path(resample_result) sum_array = np.sum(pet_array) pet_array = standard(pet_array) roi_path = os.path.join(self.case_path, "no_name.nii") roi_array = get_array_from_path(roi_path) pet_array = np.flipud(pet_array) Imshow3DArray(pet_array, roi_array) print(sum_array)
def check_data(): case_path = r"Y:\DYB\PETCT_EGFR\EGFR+\004+" ct_path = os.path.join(case_path, "ct.nii") pet_path = os.path.join(case_path, "pet_Resize.nii") roi_path = os.path.join(case_path, "ct_roi.nii") ct_array = get_array_from_path(ct_path) pet_array = get_array_from_path(pet_path) roi_array = get_array_from_path(roi_path) ct_array = standard(ct_array) pet_array = standard(pet_array) pet_array = np.flipud(pet_array) Imshow3DArray(ct_array, roi_array)
def show_T2_and_ROI(self): T2_path = os.path.join(self.case_path, "T2.nii") roi_list = [ "xia_gu_down_1.nii", "xia_gu_down_2.nii", "xia_gu_up_1.nii", "xia_gu_up_2.nii" ] T2_array = standard(get_array_from_path(T2_path)) T2_array = cv2.blur(T2_array, (5, 5)) for roi in roi_list: roi_path = os.path.join(self.case_path, roi) roi_array = get_array_from_path(roi_path) Imshow3DArray(T2_array, roi_array)
def get_roi(self): for group in self.normal_and_instability: for case in os.listdir(os.path.join(self.root_path, group)): data_path = os.path.join(self.root_path, group, case, "data.nii") data_array = standard(get_array_from_path(data_path)) data = sitk.ReadImage(data_path) big_roi = 0 for i in range(1, 9): roi_path = os.path.join(self.root_path, group, case, "roi_" + str(i) + ".nii") roi_array = get_array_from_path(roi_path) big_roi += roi_array Imshow3DArray(data_array, big_roi)
def show_resample_image(): data_path = r"Y:\DYB\PETCT_EGFR\EGFR-\001-\pet_Resize.nii" roi_path = r"Y:\DYB\PETCT_EGFR\EGFR-\001-\no_name.nii" roi_array = get_array_from_path(roi_path) data_array = get_array_from_path(data_path) data_array = np.flipud(data_array) ct_path = r"Y:\DYB\PETCT_EGFR\EGFR-\001-\ct.nii" ct_array = get_array_from_path(ct_path) ct_pet_array = ct_array + data_array ct_pet_array = standard(ct_pet_array) ct_pet_array = np.flipud(ct_pet_array) ct_array = np.flipud(ct_array) # Imshow3DArray(standard(ct_array), roi_array) Imshow3DArray(ct_pet_array, roi_array)
def check_roi(): """检查之前ROI中的数值""" case_path = r"Y:\DYB\data_and_result\doctor_xie\knee_data\CAI NAI JI-20140605" data_path = os.path.join(case_path, "data_1.nii") roi_path = os.path.join(case_path, "roi.nii.gz") data_array = standard(get_array_from_path(data_path)) data_array = np.flipud(data_array) roi_array = get_array_from_path(roi_path) roi_1 = np.where(roi_array == 1, 1, 0) # 上软骨后面的层 roi_2 = np.where(roi_array==2, 1, 0) # 上软骨前面的层 roi_3 = np.where(roi_array==3, 1, 0) # 下软骨后面的层 roi_4 = np.where(roi_array==4, 1, 0) roi_5 = np.where(roi_array==5, 1, 0) roi_6 = np.where(roi_array==6, 1, 0) Imshow3DArray(standard(data_array), [roi_1, roi_2, roi_3, roi_4])
def get_T2_ninety(self, roi_name): roi_array = get_array_from_path( os.path.join(self.case_path, roi_name + ".nii")) old_array = self.T2_array * roi_array array = np.unique(old_array) ninety_value = np.percentile(array, 90) return ninety_value
def get_T2_mean(self, roi_name): # 对应软骨名称 roi_array = get_array_from_path( os.path.join(self.case_path, roi_name + ".nii")) old_array = self.T2_array * roi_array array = old_array.flatten() new_array = [i for i in array if i != 0] return sum(new_array) / len(new_array)
def get_image(self): """ 该函数用于:存放计算的三维T2 四回波 :return: """ for case in os.listdir(self.root_path): store_data_path = os.path.join(self.root_path, case, "T2_array") new_store_data = self.sorted_file(folder_path=store_data_path) roi_path = os.path.join(self.root_path, case, "xia_gu_down_1.nii") # 原始ROI roi_array = get_array_from_path(roi_path) # 将ROI的形状作为参考,以及保存为nii的参考图像 ref_roi = sitk.ReadImage(roi_path) T_2_array = [] # 三维 for file in reversed(new_store_data): # 这里进行反转的原因感觉是因为reshape方法的问题 file_path = os.path.join(self.root_path, case, "T2_array", file) image_2D = np.load(file_path) T_2_array.append(image_2D) change_roi = np.transpose(roi_array, (2, 1, 0)) T_2_array = np.reshape(T_2_array, change_roi.shape) T_2_array = np.transpose(T_2_array, (1, 2, 0)) import cv2 T_2_array_filtered = cv2.blur(T_2_array, (5,5)) T_2_array_filtered = np.asarray(T_2_array_filtered) # print(T_2_array) # SaveNumpyToImageByRef(os.path.join(self.root_path, case, "T2_filtered.nii"), T_2_array_filtered, ref_roi) SaveNumpyToImageByRef(os.path.join(self.root_path, case, "T2.nii"), T_2_array, ref_roi) print("case {} is saved".format(case))
def get_data_array(self): for file in os.listdir(self.case_path): if file.split("_")[-1] == "src.nii": data_path = os.path.join(self.case_path, file) data_array = get_array_from_path(data_path) data_array = standard(data_array) return data_array
def show(self): for file in os.listdir(self.case_path): if file.split("_")[-1] == "src.nii": data_path = os.path.join(self.case_path, file) data_array = standard(get_array_from_path(data_path)) roi_array_list = self.get_roi_list() Imshow3DArray(data_array, roi_array_list)
def get_roi(): root_path = r"Y:\DYB\2020832DATA\doctor_xie\Normal_data_nii_and_T2" for case in os.listdir(root_path)[4:]: case_path = os.path.join(root_path, case) data_path = os.path.join(case_path, "data.nii") data = sitk.ReadImage(data_path) data_array = get_array_from_path(data_path) data_array = standard(data_array) data_array = np.flipud(data_array) roi_name = ["roi_" + str(i) + ".npy"for i in range(1, 5)] roi_path = [os.path.join(case_path, i) for i in roi_name] roi_array_list = [np.load(i) for i in roi_path] # roi_array_list = [get_big_roi(i) for i in roi_array_list] roi_1 = np.where(roi_array_list[0] == 1, 1, 0) roi_2 = np.where(roi_array_list[1] == 1, 2, 0) roi_3 = np.where(roi_array_list[2] == 1, 3, 0) roi_4 = np.where(roi_array_list[3] == 1, 4, 0) # roi_1 = get_big_roi(roi_1) # roi_2 = get_big_roi(roi_2) # roi_3 = get_big_roi(roi_3) # roi_4 = get_big_roi(roi_4) # big_roi = roi_1 + roi_2 + roi_3 + roi_4 # print(np.sum(roi_1), np.sum(roi_2), np.sum(roi_3), np.sum(roi_4)) Imshow3DArray(data_array, roi_array_list) # SaveArrayToNiiByRef(store_path = os.path.join(case_path, "roi.nii.gz"), # array=big_roi, # ref_image=data) print(case)
def get_image(): data_path = r"Y:\DYB\chang\BAO CAI ZHEN_2769351_sag_Resample.nii" data_array = get_array_from_path(data_path) data_array = standard(data_array) th = filters.threshold_otsu(data_array) roi_array = np.where(data_array > th / 6.1, 1, 0) roi_array = cv2.blur(roi_array, (12, 12)) Imshow3DArray(data_array, roi_array)
def get_data_roi(): root_path = r"Y:\DYB\PETCT_EGFR" for folder in os.listdir(root_path): folder_path = os.path.join(root_path, folder) for case in os.listdir(folder_path): case_path = os.path.join(folder_path, case) ct_path = os.path.join(case_path, "ct.nii") roi_path = os.path.join(case_path, "no_name.nii") data = sitk.ReadImage(ct_path) ct_array = get_array_from_path(ct_path) roi_array = get_array_from_path(roi_path) roi_array = np.flipud(roi_array) SaveArrayToNiiByRef(store_path=os.path.join( case_path, "ct_roi.nii"), array=roi_array, ref_image=data) print(case)
def get_2d_roi(): root_path = r"Y:\LS\liusheng _shidaoai_train_and_test" store_path = r"Y:\LS\xxxxxxxxxxxx" for folder in os.listdir(root_path): folder_path = os.path.join(root_path, folder) store_folder_path = make_folder(os.path.join(store_path, folder)) for case in os.listdir(folder_path): case_path = os.path.join(folder_path, case) data_path = os.path.join(case_path, "data.nii.gz") roi_path = os.path.join(case_path, "ROI.nii.gz") data_array = get_array_from_path(data_path) roi_array = get_array_from_path(roi_path) # 存放一个方向的结果 for i in range(roi_array.shape[-1]): if np.sum(roi_array[..., i]) != 0: # 开始存放: # data 1 是从一个方向看的结果 data_1 = data_array[..., i-1:i+2] roi_1 = roi_array[..., i-1:i+2] store_case_path = make_folder(os.path.join(store_folder_path, str(i)+"_1_"+case)) np.save(os.path.join(store_case_path, "data.npy"), data_1) np.save(os.path.join(store_case_path, "roi.npy"), roi_1) print("---1---") # 存放另一个方向的结果 for j in range(roi_array.shape[0]): if np.sum(roi_array[j, ...]) != 0: # 开始存放 # data 2 是从第二个方向看的结果 data_2 = data_array[j-1:j+2, ...] roi_2 = roi_array[j-1:j+2, ...] store_case_path = make_folder(os.path.join(store_folder_path, str(j) + "_2_" + case)) np.save(os.path.join(store_case_path, "data.npy"), data_2) np.save(os.path.join(store_case_path, "roi.npy"), roi_2) print("---2---") # 存放第三个方向看的结果 for k in range(roi_array.shape[1]): if np.sum(roi_array[:, k, :]) != 0: # 开始存放第三个方向的结果 data 3 data_3 = data_array[:, k-1:k+2, :] roi_3 = roi_array[:, k-1:k+2, :] store_case_path = make_folder(os.path.join(store_folder_path, str(k) + "_3_" + case)) np.save(os.path.join(store_case_path, "data.npy"), data_3) np.save(os.path.join(store_case_path, "roi.npy"), roi_3) print("---3---") print("case {} is finished!".format(case))
def get_new_pet(root_path): for case in os.listdir(root_path): pet_path = os.path.join(root_path, case, "pet.nii") ct_path = os.path.join(root_path, case, "ct.nii") roi_path = os.path.join(root_path, case, "no_name.nii") roi_array = get_array_from_path(roi_path) reg, result = Registrator_image(fix_path=ct_path, moving_path=pet_path) Imshow3DArray(standard(reg), roi_array)
def __init__(self, dicom_path, roi_path, store_path): self.dicom_path = dicom_path # 一个case, 存放dicom文件的路径 self.roi_path = roi_path self.roi_array = get_array_from_path(self.roi_path) self.roi = sitk.ReadImage(self.roi_path) self.store_path = store_path self.oirgin_roi_array = sitk.GetArrayFromImage( sitk.ReadImage(self.roi_path)) print(self.oirgin_roi_array.shape)
def get_roi_array_list(self): roi_array_list = [] roi_name_list = [] for file in os.listdir(self.all_roi_path): roi_path = os.path.join(self.all_roi_path, file) roi_array = get_array_from_path(roi_path) roi_array_list.append(roi_array) roi_name_list.append(file) return roi_array_list, roi_name_list
def get_roi_array(self): roi_array_list = [] for file in os.listdir(self.case_path): if file.split("_")[-1] != "src.nii": roi_path = os.path.join(self.case_path, file) print(roi_path) roi_array = get_array_from_path(roi_path) roi_array_list.append(roi_array) return roi_array_list
def check_data_roi(): """检查数据和ROI""" root_path = r"Y:\DYB\data_and_result\doctor_xie\knee_data" store_path = r"Y:\DYB\data_and_result\doctor_xie\knn_segment_all_roi" for case in os.listdir(root_path): case_path = os.path.join(root_path, case) store_case_path = make_folder(os.path.join(store_path, case)) # 选择分割的数据是的一个回波的数据 data_path = os.path.join(case_path, "data_1.nii") roi_path = os.path.join(case_path, "roi.nii.gz") data_array = standard(get_array_from_path(data_path)) data_array = np.flipud(data_array) roi_array = get_array_from_path(roi_path) roi_array_list = [ np.where(roi_array == i, 1, 0) for i in np.unique(roi_array)[1:] ] np.save(os.path.join(store_case_path, "data.npy"), data_array) np.save(os.path.join(store_case_path, "all_roi.npy"), roi_array_list) print(case, " is finished!")
def show_T2_mampping(self): t2_mapping_path = os.path.join(self.case_path, "new_T2.nii") t2_mapping_array = get_array_from_path(t2_mapping_path) # sns.set() # sns.heatmap(t2_mapping_array[..., 5]) # plt.axis('off') # plt.show() ruan_roi_path = r"Y:\DYB\data_and_result\doctor_xie\knee_data\CAI NAI JI-20140605\ruan_gu_down_1.nii" xia_roi_path = r"Y:\DYB\data_and_result\doctor_xie\knee_data\CAI NAI JI-20140605\xia_gu_down_1.nii" ruan_array = get_array_from_path(ruan_roi_path) xia_array = get_array_from_path(xia_roi_path) for i in range(t2_mapping_array.shape[-1]): if np.sum(ruan_array[..., i]) != 0 and np.sum(xia_array[..., i]) != 0: sns.heatmap(t2_mapping_array[..., i]) plt.contour(ruan_array[..., i], colors="blue", linewidths=0.45) plt.contour(xia_array[..., i], linewidths=0.45) plt.axis('off') plt.show()
def get_roi_list(self): roi_array_list = [] roi_name = [ "roi_1.nii", "roi_2.nii", "roi_3.nii", "roi_4.nii", "roi_5.nii", "roi_6.nii", "roi_7.nii", "roi_8.nii" ] for name in roi_name: roi_path = os.path.join(self.case_path, name) roi_array = get_array_from_path(roi_path) roi_array_list.append(roi_array) return roi_array_list
def get_croped_data_roi(data_path, roi_path, store_path): data_array = get_array_from_path(data_path) # data_array = np.flipud(data_array) # 注意这里的数据要进行翻转的 roi_array = get_array_from_path(roi_path) data_array = standard(data_array) crop_array = crop_data_array(roi_array, data_array) def resize_data(data_array, resized_shape): resized_data = cv2.resize(data_array, resized_shape, interpolation=cv2.INTER_NEAREST) return resized_data roi_new = crop_data_array(roi_array, roi_array) new_array = resize_data(crop_array, (224, 224)) roi_new = resize_data(roi_new, (224, 224)) # Imshow3DArray(standard(new_array), roi_new) np.save(os.path.join(store_path, "croped_pet.npy"), new_array) np.save(os.path.join(store_path, "croped_roi.npy"), roi_new)
def show_knee_array(self): five_echo_name_list = ["data_" + str(i) for i in range(1, 6)] five_echo_path_list = [ os.path.join(self.case_path, i) for i in five_echo_name_list ] five_array_list = [ np.flipud(get_array_from_path(i)) for i in five_echo_path_list ] # # 显示五个回波的图像 for echo, i in zip(five_array_list[1:], range(1, 6)): plt.subplot(4, 1, i) self.show_array(echo) plt.tight_layout(pad=0.5, h_pad=0.5, w_pad=0.5) plt.show()
def get_ROI(self, value, store_name): # value用来选择 ruangu_roi = os.path.join(self.case_path, "roi.nii.gz") # 软骨路径 xiagu_roi = os.path.join(self.case_path, "moved_new_roi.nii.gz") # 下骨路径 data_path = os.path.join(self.case_path, "data_1.nii") data_array = standard(np.flipud(get_array_from_path(data_path))) data = sitk.ReadImage(data_path) ruangu_roi_array = get_array_from_path(ruangu_roi) single_ruangu_array = np.where(ruangu_roi_array == value, 1, 0) # 软骨 xiagu_roi_array = get_array_from_path(xiagu_roi) single_xiagu_array = np.where(xiagu_roi_array == value, 1, 0) # 下骨 # 显示检查 # Imshow3DArray(data_array, [single_ruangu_array, single_xiagu_array]) SaveArrayToNiiByRef( os.path.join(self.case_path, "ruan_gu_" + store_name + ".nii"), single_ruangu_array, data) SaveArrayToNiiByRef( os.path.join(self.case_path, "xia_gu_" + store_name + ".nii"), single_xiagu_array, data)