def raw_reader(path, i, lung=True): """ :param path: path to mhd file i: the id of slice in raw file lung=True: lung windowing lung=False: bone,tissue,muscle windowing :return: a 3 channel numpy array """ # file_name = tool_packages.get_filename(path) img_set, origin, spacing = tool_packages.raw_image_reader(path) masked_img = [] if i == 0: masked_img.append(img_set[0]) masked_img.append(img_set[0]) masked_img.append(img_set[1]) elif i == len(img_set) - 1: masked_img.append(img_set[-2]) masked_img.append(img_set[-1]) masked_img.append(img_set[-1]) else: for j in range(i - 1, i + 2, 1): image = np.squeeze(img_set[j]) masked_img.append(image) masked_img, _slice_num = img_seg.image_segmentor(np.array(masked_img), lung) three_channels = np.array(masked_img).transpose((1, 2, 0)) return three_channels
def segmentation_interface(mhd_dir, out_dir, button): mhd_path_list = tool_packages.get_path(mhd_dir, 'mhd') length = len(mhd_path_list) count = 0 for path in mhd_path_list: # a = time.time() img_set, _, __ = tool_packages.raw_image_reader(path) file_name = tool_packages.get_filename(path) np_array, slicenum = image_segmentor(img_set, button=button) tool_packages.raw_image_writer(np_array, out_dir + file_name + '.mhd') # b = time.time() # print('elapse: ' + str(b-a)) # print('slices:' + str(slicenum)) count += 1 print('file processed: ' + str(count) + '/' + str(length)) # print(samples) return None
def file_parser_interface(mhdfile_path_list, out_path, csv_handle): """ :Function a raw format file parser, to generate npy files and corresponding labels for dl training :param mhdfile_path_list: a list of mhd file path anno_path: path to the annotation file :return: None """ length = len(mhdfile_path_list) count = 0 for path in mhdfile_path_list: file_name = tool_packages.get_filename(path) img_set, origin, spacing = tool_packages.raw_image_reader(path) slice_num, width, height = img_set.shape # can be optimized HERE # param1 = 1 # if width != 512: # param1 = label_normalization_parameter(width) for i in range(slice_num): label_parser(out_path + file_name, i, path, slice_num - 1, csv_handle) count += 1 print('file processed: ' + str(count) + '/' + str(length))
plt visualization below """ # plt.figure() # plt.subplot(2, 2, 1), plt.imshow(image, 'gray'), plt.title('original') # plt.subplot(2, 2, 2), plt.imshow(result_, 'gray'), plt.title('result') # plt.subplot(2, 2, 3), plt.imshow(opening, 'gray'), plt.title('opening') # plt.subplot(2, 2, 4), plt.imshow(lca, 'gray'), plt.title('lca') # plt.show() if img_set.shape[1] != 512: img_resize = cv2.resize(result_, (512, 512)) rt.append(img_resize) else: rt.append(result_) return rt, slice_num if __name__ == '__main__': samples = '../chestCT_round1/test/320831.mhd' img_set, _, __ = tool_packages.raw_image_reader(samples) file_name = tool_packages.get_filename(samples) result, s_num = image_segmentor( img_set, True) # true->lung. false->muscle, tissue, bone # plt.figure() # plt.subplot(2, 2, 1), # plt.imshow(result[10], 'gray'), plt.title('test1') # plt.subplot(2, 2, 2), plt.imshow(result[5], 'gray'), plt.title('test2') # plt.subplot(2, 2, 3), plt.imshow(result[10], 'gray'), plt.title('test3') # plt.subplot(2, 2, 4), plt.imshow(result[15], 'gray'), plt.title('test4') # plt.show()
tool_packages.write_csv(out_path, [ line[0], min_coord[0], min_coord[1], min_coord[2], max_coord[0], max_coord[1], max_coord[2], line[7] ]) return None def label_normalization_parameter(w): coefficient = 512 / int(w) return coefficient if __name__ == '__main__': pass img_set, origin, spacing = tool_packages.raw_image_reader( '../chestCT_round1/test/318818.mhd') slices, width, height = img_set.shape name = tool_packages.get_filename('chestCT_round1/test/318818.mhd') # csv_file = tool_packages.read_csv('../do_not_git/chestCT_round1_annotation.csv') annotation_path_world = '../do_not_git/chestCT_round1_annotation.csv' annotation_path_pixel = '../chestCT_round1/test.csv' if os.path.exists(annotation_path_pixel): print('the pixel coordinates file exists') csv_file_handle = tool_packages.read_csv(annotation_path_pixel) else: print('generating pixel coordinates file') annotation_csv_translator(annotation_path_world, annotation_path_pixel) print('done') csv_file_handle = tool_packages.read_csv(annotation_path_pixel) file_parser_interface(['../chestCT_round1/test/318818.mhd'], '',