def preprocess_dir(in_dir, out_dir, GT=False): utils.makeDirectory(out_dir) cases = os.listdir(in_dir) # sizes = np.zeros([len(cases) - 1, 3]) i = 0 for case in cases: if not case.startswith('.'): img = sitk.ReadImage(os.path.join(in_dir, case)) # label = sitk.ReadImage(os.path.join(train_dir, case)) if not GT: img = normalizeIntensities(img)[0] img = utils.pad_volume(img, target_size_x=48, target_size_y=64, target_size_z=48, padValue=0) filename = case[:-7] sitk.WriteImage(img, os.path.join(out_dir, filename + '.nrrd')) np.save(os.path.join(out_dir, filename + '.npy'), sitk.GetArrayFromImage(img))
def get_final_roi(img, start_x, start_y, start_z, size_x, size_y, size_z): img_size = img.GetSize() check_x = True if (start_x + size_x) < img_size[0] else False check_y = True if (start_y + size_y) < img_size[1] else False check_z = True if (start_z + size_z) < img_size[2] else False size = [int(size_x), int(size_y), int(size_z)] start = [int(start_x), int(start_y), int(start_z)] if check_x and check_y and check_z: out_img = sitk.RegionOfInterest(img, size, start) else: out_img = utils.pad_volume(img, target_size_x=start_x + size_x, target_size_y=start_y + size_y, target_size_z=start_z + size_z, padValue=utils.getMinimum(img)) out_img = sitk.RegionOfInterest(out_img, size, start) return out_img
def preprocess_unlabeled(files_dir, preprocessed_dir): utils.makedir(preprocessed_dir) # bb_size = [80, 200, 200] # will be extended by [10,20,20] per side for augmented # get_left_and_right_BB() cases = sorted(os.listdir(files_dir)) cases = [x for x in cases if 'case' in x] for i in range(0, len(cases)): # if cases[i] == 'case_00223': out_dir = os.path.join(preprocessed_dir, cases[i]) utils.makedir(out_dir) print(cases[i]) img = sitk.ReadImage( os.path.join(files_dir, cases[i], 'imaging.nii.gz')) img = utils.resampleImage(img, SPACING, sitk.sitkLinear, 0, sitk.sitkInt16) # apply region of interest that is guided by bones bb, smaller_bb = get_threshold_bounding_box(img, 500) if img.GetSize()[0] > BB_SIZE[0]: # if img.GetSize()[0] > BB_SIZE[1]+50: # if x-axis is very long, it might be a whole body scan cropped_img = sitk.RegionOfInterest( img, [smaller_bb[3], smaller_bb[4], smaller_bb[5]], [smaller_bb[0], smaller_bb[1], smaller_bb[2]]) sitk.WriteImage(cropped_img, 'cropped_' + str(i) + '.nrrd') lung_img = sitk.BinaryThreshold(cropped_img, -900, -700, 1, 0) # lung -900, -700 sitk.WriteImage(lung_img, 'lung_img_thresh' + str(i) + '.nrrd') lung_img = utils.getLargestConnectedComponents(lung_img) sitk.WriteImage(lung_img, 'lung_img_cc' + str(i) + '.nrrd') # get boundingbox of lung: bb_lung = getBB(lung_img, 1) start_y_w_bb_lung = cropped_img.TransformIndexToPhysicalPoint( [bb_lung[0], bb_lung[1], bb_lung[2]]) start_y_w_img = img.TransformPhysicalPointToIndex( start_y_w_bb_lung) # print('bb_lung: ', bb_lung) # values for final ROI start_X = max( 0, bb_lung[0] + bb_lung[3] ) # - 0.1*BB_SIZE[0]) # start 20 voxel above bottom of lung size_X = BB_SIZE[0] if start_X + BB_SIZE[0] > img.GetSize()[0]: start_X = img.GetSize()[0] - size_X - 1 start_y = max(0, (start_y_w_img[1] + bb_lung[4] - BB_SIZE[1])) ## according to bones bounding box # start_y = max(0, bb[1] + bb[4] - BB_SIZE[1]) start_z_r = int((bb[2] + bb[5]) / 2) start_z_l = max(0, int((bb[2] + bb[5]) / 2) - BB_SIZE[2]) else: ### pad volume in x direction img = utils.pad_volume(img, target_size_x=BB_SIZE[0], padValue=utils.getMinimum(img)) # values for final ROI start_X = 0 # start 20 voxel above bottom of lung start_y = max(0, (bb[1] + bb[4] + 20 - BB_SIZE[1])) ## according to bones bounding box start_z_r = int((bb[2] + bb[5]) / 2) start_z_l = max(0, int((bb[2] + bb[5]) / 2) - BB_SIZE[2]) left_img = get_final_roi(img, start_X, start_y, start_z_l, BB_SIZE[0], BB_SIZE[1], BB_SIZE[2]) right_img = get_final_roi(img, start_X, start_y, start_z_r, BB_SIZE[0], BB_SIZE[1], BB_SIZE[2]) sitk.WriteImage(left_img, os.path.join(out_dir, 'img_left.nrrd')) sitk.WriteImage(right_img, os.path.join(out_dir, 'img_right.nrrd'))
def preprocess(): utils.makedir(preprocessed_dir) bb_size = [80, 200, 200] # will be extended by [10,20,20] per side for augmented # get_left_and_right_BB() cases = sorted(os.listdir(files_dir)) cases = [x for x in cases if 'case' in x] bbs_left = np.load('bbs_left.npy') bbs_right = np.load('bbs_right.npy') for i in range(0, len(cases)): # if cases[i] == 'case_00002': out_dir = os.path.join(preprocessed_dir, cases[i]) utils.makedir(out_dir) print(cases[i]) img = sitk.ReadImage( os.path.join(files_dir, cases[i], 'imaging.nii.gz')) img = utils.resampleImage(img, [3.0, 0.75, 0.75], sitk.sitkLinear, 0, sitk.sitkInt16) segm = sitk.ReadImage( os.path.join(files_dir, cases[i], 'segmentation.nii.gz')) # apply region of interest that is guided by bones bb, smaller_bb = get_threshold_bounding_box(img, 500) if bb[3] > 100: cropped_img = sitk.RegionOfInterest( img, [smaller_bb[3], smaller_bb[4], smaller_bb[5]], [smaller_bb[0], smaller_bb[1], smaller_bb[2]]) # sitk.WriteImage(cropped_img, 'cropped_' + str(i) + '.nrrd') lung_img = sitk.BinaryThreshold(cropped_img, -900, -700, 1, 0) # lung -900, -700 # sitk.WriteImage(lung_img, 'lung_img_thresh' + str(i) + '.nrrd') lung_img = utils.getLargestConnectedComponents(lung_img) # sitk.WriteImage(lung_img, 'lung_img_cc' + str(i) + '.nrrd') # get boundingbox of lung: bb_lung = getBB(lung_img, 1) # print('bb_lung: ', bb_lung) # values for final ROI start_X = max(0, bb_lung[0] + bb_lung[3] - 20) # start 20 voxel above bottom of lung size_X = 100 if start_X + 100 > img.GetSize()[0]: start_X = img.GetSize()[0] - size_X - 1 img = sitk.RegionOfInterest( img, [size_X, img.GetSize()[1], img.GetSize()[2]], [start_X, 0, 0]) # img = sitk.RegionOfInterest(img, [size_X, bb[4], bb[5]], # [start_X, bb[1], bb[2]]) sitk.WriteImage(img, 'final_cropped_' + str(i) + '.nrrd') else: ### pad volume in x direction img = utils.pad_volume(img, target_size_x=100, padValue=utils.getMinimum(img)) ## split image in left and right half [size_x, size_y, size_z] = img.GetSize() img_left = sitk.RegionOfInterest( img, [size_x, size_y, int(size_z / 2)], [0, 0, 0]) img_right = sitk.RegionOfInterest( img, [size_x, size_y, int(size_z / 2)], [0, 0, int(size_z / 2) - 1]) sitk.WriteImage(img_left, 'img_left_temp.nrrd') sitk.WriteImage(img_right, 'img_right_temp.nrrd') # get ROI for left and right kidney roi_left_start = [ max(0, bbs_left[i, 0] - 10), max(0, bbs_left[i, 1] - 10), max(0, bbs_left[i, 2] - 10) ] roi_right_start = [ max(0, bbs_right[i, 0] - 10), max(0, bbs_right[i, 1] - 10), max(0, bbs_right[i, 2] - 10) ] # apply ROI to image img_left_final = get_final_roi(img_left, roi_left_start[0], roi_left_start[1], roi_left_start[2], bb_size[0] + 20, bb_size[1] + 20, bb_size[2] + 20) img_right_final = get_final_roi(img_right, roi_right_start[0], roi_right_start[1], roi_right_start[2], bb_size[0] + 20, bb_size[1] + 20, bb_size[2] + 20) segm_left_final = utils.resampleToReference(segm, img_left_final, sitk.sitkNearestNeighbor, defaultValue=0, out_dType=sitk.sitkUInt8) segm_right_final = utils.resampleToReference(segm, img_right_final, sitk.sitkNearestNeighbor, defaultValue=0, out_dType=sitk.sitkUInt8) # write images sitk.WriteImage(img_left_final, os.path.join(out_dir, 'img_left.nrrd')) sitk.WriteImage(img_right_final, os.path.join(out_dir, 'img_right.nrrd')) sitk.WriteImage(segm_left_final, os.path.join(out_dir, 'segm_left.nrrd')) sitk.WriteImage(segm_right_final, os.path.join(out_dir, 'segm_right.nrrd'))
def get_left_and_right_BB(): cases = sorted(os.listdir(files_dir)) cases = [x for x in cases if 'case' in x] bbs_left = np.zeros([len(cases), 6]) bbs_right = np.zeros([len(cases), 6]) worried_cases = [ 'case_00048', 'case_00097', 'case_00178', 'case_00185', 'case_00191' ] for i in range(0, len(cases)): # if cases[i] in worried_cases: # if cases[i] == 'case_00001' or cases[i] == 'case_00011': print(cases[i]) img = sitk.ReadImage( os.path.join(files_dir, cases[i], 'imaging.nii.gz')) img = utils.resampleImage(img, [4.0, 1.0, 1.0], sitk.sitkLinear, 0, sitk.sitkInt16) segm = sitk.ReadImage( os.path.join(files_dir, cases[i], 'segmentation.nii.gz')) # apply region of interest that is guided by bones bb, smaller_bb = get_threshold_bounding_box(img, 500) if bb[3] > 100: cropped_img = sitk.RegionOfInterest( img, [smaller_bb[3], smaller_bb[4], smaller_bb[5]], [smaller_bb[0], smaller_bb[1], smaller_bb[2]]) # sitk.WriteImage(cropped_img, 'cropped_' + str(i) + '.nrrd') lung_img = sitk.BinaryThreshold(cropped_img, -900, -700, 1, 0) # lung -900, -700 # sitk.WriteImage(lung_img, 'lung_img_thresh' + str(i) + '.nrrd') lung_img = utils.getLargestConnectedComponents(lung_img) # sitk.WriteImage(lung_img, 'lung_img_cc' + str(i) + '.nrrd') # get boundingbox of lung: bb_lung = getBB(lung_img, 1) # print('bb_lung: ', bb_lung) # values for final ROI start_X = max(0, bb_lung[0] + bb_lung[3] - 20) # start 20 voxel above bottom of lung size_X = 100 if start_X + 100 > img.GetSize()[0]: start_X = img.GetSize()[0] - size_X - 1 # img_final = sitk.RegionOfInterest(img, [size_X, bb[4], bb[5]], # [start_X, bb[1], bb[2]]) img_final = sitk.RegionOfInterest( img, [size_X, img.GetSize()[1], img.GetSize()[2]], [start_X, 0, 0]) # sitk.WriteImage(cropped_img_final, 'final_cropped_' + str(i) + '.nrrd') else: ### pad volume in x direction # get minimum value for padding filter = sitk.StatisticsImageFilter() filter.Execute(img) minValue = filter.GetMinimum() img_final = utils.pad_volume(img, target_size_x=100, padValue=minValue) ## split image in left and right half [size_x, size_y, size_z] = img_final.GetSize() img_left = sitk.RegionOfInterest( img_final, [100, size_y, int(size_z / 2)], [0, 0, 0]) img_right = sitk.RegionOfInterest( img_final, [100, size_y, int(size_z / 2)], [0, 0, int(size_z / 2) - 1]) bb_left, bb_right = getBoundingBoxes(img_left, img_right, segm) bbs_left[i] = [ bb_left[0], bb_left[1], bb_left[2], bb_left[3], bb_left[4], bb_left[5] ] bbs_right[i] = [ bb_right[0], bb_right[1], bb_right[2], bb_right[3], bb_right[4], bb_right[5] ] print(bb_left, bb_right) np.save('bbs_left.npy', bbs_left) np.save('bbs_right.npy', bbs_right) print('max and min x: ', np.amin(bbs_left[:, 0]), np.amax(bbs_left[:, 3])) print('max and min y: ', np.amin(bbs_left[:, 1]), np.amax(bbs_left[:, 4])) print('max and min z: ', np.amin(bbs_left[:, 2]), np.amax(bbs_left[:, 5])) print('max and min x: ', np.amin(bbs_right[:, 0]), np.amax(bbs_right[:, 3])) print('max and min y: ', np.amin(bbs_right[:, 1]), np.amax(bbs_right[:, 4])) print('max and min z: ', np.amin(bbs_right[:, 2]), np.amax(bbs_right[:, 5]))