def getSitkGradient(im): gradient = sitk.Gradient(im) gradientn = sitk.gafi(gradient) gradients = [sitk.Cast(sitk.gifa(gradientn[:,:,:,i]),6) for i in range(gradient.GetNumberOfComponentsPerPixel())] #gradients = imaging.sortAxes(gradient,[3,0,1,2]) return gradients
def getSmoothGradient(im,sigma=2): shape = im.shape im = sitk.gifa(im) gradient = sitk.GradientRecursiveGaussian(im,sigma) gradient = sitk.gafi(gradient) gradients = imaging.sortAxes(gradient,[len(shape)]+range(len(shape))) return gradients
def step(self): """Perform a single step of the morphological snake evolution.""" # Assign attributes to local variables for convenience. u = self._u gI = self._data dgI = self._ddata theta = self._theta v = self._v if u is None: raise ValueError, "the levelset is not set (use set_levelset)" # res = np.copy(u) res = u*1 # Balloon. if v > 0: aux = sitk.Cast(sitk.BinaryDilate(sitk.Cast(u,3)),self.dtype) # aux = binary_dilation(u, self.structure) elif v < 0: aux = sitk.Cast(sitk.BinaryErode(sitk.Cast(u,3)),self.dtype) # aux = binary_erosion(u, self.structure) if v!= 0: res = sitk.Cast(self._threshold_mask_v==0,self.dtype)*res+\ sitk.Cast(self._threshold_mask_v>00,self.dtype)*aux # res[self._threshold_mask_v] = aux[self._threshold_mask_v] # Image attachment. # aux = np.zeros_like(res) aux = sitk.gifa(np.zeros(res.GetSize()[::-1],dtype=self.dtype)) dres = getSitkGradient(res) for el1, el2 in zip(dgI, dres): aux += el1*el2 res[aux > 0] = 1 res[aux < 0] = 0 res = sitk.Cast(aux>0,self.dtype) # Smoothing. # Smoothing. res = sitk.gafi(res) for i in xrange(self.smoothing): res = curvop(res) res = sitk.gifa(res) self._u = res
def step(self): """Perform a single step of the morphological Chan-Vese evolution.""" # Assign attributes to local variables for convenience. u = self._u if u is None: raise ValueError, "the levelset function is not set (use set_levelset)" data = self.data # Determine c0 and c1. inside = sitk.Cast(u>0,self.dtype) outside = sitk.Cast(u<=0,self.dtype) c0 = sitkSum(data*outside)/float(sitkSum(outside)) c1 = sitkSum(data*inside)/float(sitkSum(inside)) # c0 = data[outside].sum() / float(outside.sum()) # c1 = data[inside].sum() / float(inside.sum()) # Image attachment. dres = getSitkGradient(u) # abs_dres = sitk.GradientMagnitude(u) # dres = np.array(np.gradient(u)) # abs_dres = np.abs(dres).sum(0) abs_dres = sitk.Cast(sitk.Add(sitk.Add(dres[0],dres[1]),dres[2]),self.dtype) #aux = abs_dres * (c0 - c1) * (c0 + c1 - 2*data) aux = abs_dres * (self.lambda1*(data - c1)**2 - self.lambda2*(data - c0)**2) # res = np.copy(u) res = u*1. res = sitk.Cast(aux<0,self.dtype)+res*sitk.Cast(aux==0,self.dtype) # res[aux < 0] = 1 # res[aux > 0] = 0 # Smoothing. res = sitk.gafi(res) for i in xrange(self.smoothing): res = curvop(res) res = sitk.Cast(sitk.gifa(res),self.dtype) self._u = res
import numpy as np import SimpleITK as sitk from reader.data_reader import _read_data if __name__ == "__main__": data_path = "/exports/lkeb-hpc/syousefi/Data/asl_pet/" '''read path of the images for train, test, and validation''' _rd = _read_data(data_path) train_data, validation_data, test_data = _rd.read_data_path() m_t1 = .00000000000000000000001 M_t1 = 1 for d in validation_data: # print(d) t1 = sitk.GetArrayFromImage(sitk.ReadImage(d['t1'])) mt1 = np.min(t1) Mt1 = np.max(t1) if mt1 < m_t1: m_t1 = mt1 if Mt1 > M_t1: M_t1 = Mt1 print('[' + str(mt1) + ' , ' + str(Mt1) + ']') print('[' + str(m_t1) + ' , ' + str(M_t1) + ']')
ret = map_coordinates(img, coords, order=0, \ mode=mode).astype(img.dtype) else: assert(img.shape[0] == 3) ret = np.zeros_like(array_image) for i in range(3): ret[i] = map_coordinates(img[i], coords, order=1, \ mode=mode).astype(img.dtype) return ret if __name__ == "__main__": Iters = 10 data_pth = 'data/FLAIR.nii.gz' sitk_image = sitk.ReadImage(data_pth) array_image = sitk.GetArrayFromImage(sitk_image).copy() elastic_time = 0. map_coordinates_time = 0. for i in range(Iters): start = time.time() coords = create_zero_centered_coordinate_mesh(array_image.shape) # alpha=(0., 1000.) # sigma=(10., 13.) # a = np.random.uniform(alpha[0], alpha[1]) # s = np.random.uniform(sigma[0], sigma[1]) # coords = elastic_deform_coordinates(coords, a, s) # angle_x=(0, 2 * np.pi)
def __call__(self, sample): random_state = np.random.RandomState(int(time.time())) if random_state.rand(1)[0] < self.ratio: img, seg = sample['image'], sample['label'] image_size = img.GetSize() image_spacing = img.GetSpacing() if self.rotation_center: rotation_center = self.rotation_center else: rotation_center = (np.array(image_size) // 2).tolist() rotation_center = img.TransformIndexToPhysicalPoint( rotation_center) rotation_radians_x = random_state.normal( 0, self.rotation_angles[0] / 2) * np.pi / 180 rotation_radians_y = random_state.normal( 0, self.rotation_angles[1] / 2) * np.pi / 180 rotation_radians_z = random_state.normal( 0, self.rotation_angles[2] / 2) * np.pi / 180 random_trans_x = random_state.normal( 0, self.translation[0] / 2) * image_spacing[0] random_trans_y = random_state.normal( 0, self.translation[1] / 2) * image_spacing[1] random_trans_z = random_state.normal( 0, self.translation[2] / 2) * image_spacing[2] # initialize a bspline transform rigid_transform = sitk.Euler3DTransform( rotation_center, rotation_radians_x, rotation_radians_y, rotation_radians_z, (random_trans_x, random_trans_y, random_trans_z)) # deform and resample image if self.mode == 'both': img_trans = resample(img, rigid_transform, interpolator=self.interpolator, default_value=0.1) seg_trans = resample(seg, rigid_transform, interpolator=sitk.sitkNearestNeighbor, default_value=0) elif self.mode == 'image': img_trans = resample(img, rigid_transform, interpolator=self.interpolator, default_value=0.1) seg_trans = seg elif self.mode == 'label': img_trans = img seg_trans = resample(seg, rigid_transform, interpolator=sitk.sitkNearestNeighbor, default_value=0) else: raise ValueError('Wrong rigid transformation mode :{}!'.format( self.mode)) sample['image'] = img_trans sample['label'] = seg_trans return sample
def read_image(self,path): reader = sitk.ImageFileReader() reader.SetFileName(path) return reader.Execute()
def sitk_ImageRead(image_dir): im = sitk.ReadImage(image_dir) im_array = sitk.GetArrayFromImage(im) return im_array
def loadFile(filename): ds = sitk.ReadImage(filename) img_array = sitk.GetArrayFromImage(ds) frame_num, width, height = img_array.shape return img_array
def srm(itk_image, q=25, three_dim=True, averages=False, fully_connected=True, smooth=None): temp_image_path_input = os.path.join(tempfile.gettempdir(), str(time.time()) + '.mhd') if smooth is not None: itk_image = SimpleITK.Median(itk_image, smooth) SimpleITK.WriteImage(itk_image, temp_image_path_input) temp_image_path_output = os.path.join(tempfile.gettempdir(), str(time.time()) + '.mhd') srm = SRM(save_img=(temp_image_path_output, False), q=q) srm.execute(temp_image_path_input) srm_itk_image = SimpleITK.ReadImage(temp_image_path_output) srm_itk_image.CopyInformation(itk_image) if fully_connected: return srm_itk_image img_out = SimpleITK.Image(srm_itk_image.GetSize(), SimpleITK.sitkUInt32) img_out.CopyInformation(srm_itk_image) SimpleITK.WriteImage(img_out, "/tmp/imou.mhd") fake_labelling = SimpleITK.LabelShapeStatisticsImageFilter() fake_labelling.Execute(srm_itk_image) max_offset = 0 for l in fake_labelling.GetLabels(): partial_img = SimpleITK.Cast( SimpleITK.ConnectedComponent(srm_itk_image == l, fullyConnected=True), SimpleITK.sitkUInt32) stupid_filter = SimpleITK.MinimumMaximumImageFilter() stupid_filter.Execute(partial_img) maxi = int(stupid_filter.GetMaximum()) img_out += (partial_img + max_offset) * partial_img if l % 500 == 0: SimpleITK.WriteImage(partial_img, "/tmp/partial_" + str(l) + ".mhd") SimpleITK.WriteImage(img_out, "/tmp/partial_out_" + str(l) + ".mhd") max_offset += maxi print maxi, max_offset, fake_labelling.GetNumberOfLabels() return img_out
def smooth(cls, segmentation: np.ndarray, arguments: dict) -> np.ndarray: return sitk.GetArrayFromImage( sitk.BinaryMorphologicalOpening( sitk.GetImageFromArray(segmentation), arguments["smooth_border_radius"]))
def data_to_array(base_path, store_path, img_rows, img_cols): clahe = cv2.createCLAHE(clipLimit=0.05, tileGridSize=(int(img_rows / 8), int(img_cols / 8))) fileList = os.listdir(os.path.join(base_path, 'TrainingData')) fileList = sorted((x for x in fileList if '.mhd' in x)) val_list = [5, 15, 25, 35, 45] train_list = list(set(range(50)) - set(val_list)) count = 0 for the_list in [train_list, val_list]: images = [] masks = [] filtered = [ file for file in fileList for ff in the_list if str(ff).zfill(2) in file ] for filename in filtered: itkimage = sitk.ReadImage( os.path.join(base_path, 'TrainingData', filename)) imgs = sitk.GetArrayFromImage(itkimage) if 'segm' in filename.lower(): imgs = img_resize(imgs, img_rows, img_cols, equalize=False) masks.append(imgs) else: imgs = img_resize(imgs, img_rows, img_cols, equalize=True) images.append(imgs) # images: slices x w x h ==> total number x w x h images = np.concatenate(images, axis=0).reshape(-1, img_rows, img_cols) masks = np.concatenate(masks, axis=0).reshape(-1, img_rows, img_cols) masks = masks.astype(np.uint8) # Smooth images using CurvatureFlow images = smooth_images(images) images = images.astype(np.float32) if count == 0: # no normalize mu = np.mean(images) sigma = np.std(images) images = (images - mu) / sigma np.save(os.path.join(store_path, 'X_train.npy'), images) np.save(os.path.join(store_path, 'y_train.npy'), masks) elif count == 1: images = (images - mu) / sigma np.save(os.path.join(store_path, 'X_val.npy'), images) np.save(os.path.join(store_path, 'y_val.npy'), masks) count += 1 fileList = os.listdir(os.path.join(base_path, 'TestData')) fileList = sorted([x for x in fileList if '.mhd' in x]) n_imgs = [] images = [] for filename in fileList: itkimage = sitk.ReadImage(os.path.join(base_path, 'TestData', filename)) imgs = sitk.GetArrayFromImage(itkimage) imgs = img_resize(imgs, img_rows, img_cols, equalize=True) images.append(imgs) n_imgs.append(len(imgs)) images = np.concatenate(images, axis=0).reshape(-1, img_rows, img_cols) images = smooth_images(images) images = images.astype(np.float32) images = (images - mu) / sigma np.save(os.path.join(store_path, 'X_test.npy'), images) np.save(os.path.join(store_path, 'test_n_imgs.npy'), np.array(n_imgs)) print('save file in {}'.format(store_path))
def writeResultsFromNumpyLabel(self, result, key, resultTag, ext, resultDir): ''' :param result: predicted mask :param key: sample id :return: register predicted mask (e.g. binary mask of size 96x96x48) to original image (e.g. CT volume of size 320x320x20), output the final mask of the same size as original image. ''' img = self.sitkImages[key] # original image print("original img shape{}".format(img.GetSize())) toWrite = sitk.Image(img.GetSize()[0], img.GetSize()[1], img.GetSize()[2], sitk.sitkFloat32) factor = np.asarray(img.GetSpacing()) / [ self.params['dstRes'][0], self.params['dstRes'][1], self.params['dstRes'][2] ] factorSize = np.asarray(img.GetSize() * factor, dtype=float) newSize = np.max([factorSize, self.params['VolSize']], axis=0) newSize = newSize.astype(dtype=int) T = sitk.AffineTransform(3) T.SetMatrix(img.GetDirection()) resampler = sitk.ResampleImageFilter() resampler.SetReferenceImage(img) resampler.SetOutputSpacing([ self.params['dstRes'][0], self.params['dstRes'][1], self.params['dstRes'][2] ]) resampler.SetSize(newSize.tolist()) resampler.SetInterpolator(sitk.sitkNearestNeighbor) if self.params['normDir']: resampler.SetTransform(T.GetInverse()) toWrite = resampler.Execute(toWrite) imgCentroid = np.asarray(newSize, dtype=float) / 2.0 imgStartPx = (imgCentroid - self.params['VolSize'] / 2.0).astype(dtype=int) for dstX, srcX in zip( range(0, result.shape[0]), range(imgStartPx[0], int(imgStartPx[0] + self.params['VolSize'][0]))): for dstY, srcY in zip( range(0, result.shape[1]), range(imgStartPx[1], int(imgStartPx[1] + self.params['VolSize'][1]))): for dstZ, srcZ in zip( range(0, result.shape[2]), range(imgStartPx[2], int(imgStartPx[2] + self.params['VolSize'][2]))): try: toWrite.SetPixel(int(srcX), int(srcY), int(srcZ), float(result[dstX, dstY, dstZ])) except: pass resampler.SetOutputSpacing( [img.GetSpacing()[0], img.GetSpacing()[1], img.GetSpacing()[2]]) resampler.SetSize(img.GetSize()) if self.params['normDir']: resampler.SetTransform(T) toWrite = resampler.Execute(toWrite) thfilter = sitk.BinaryThresholdImageFilter() thfilter.SetInsideValue(1) thfilter.SetOutsideValue(0) thfilter.SetLowerThreshold(0.5) toWrite = thfilter.Execute(toWrite) #connected component analysis (better safe than sorry) cc = sitk.ConnectedComponentImageFilter() toWritecc = cc.Execute(sitk.Cast(toWrite, sitk.sitkUInt8)) arrCC = np.transpose( sitk.GetArrayFromImage(toWritecc).astype(dtype=float), [2, 1, 0]) lab = np.zeros(int(np.max(arrCC) + 1), dtype=float) for i in range(1, int(np.max(arrCC) + 1)): lab[i] = np.sum(arrCC == i) activeLab = np.argmax(lab) toWrite = (toWritecc == activeLab) toWrite = sitk.Cast(toWrite, sitk.sitkUInt8) writer = sitk.ImageFileWriter() writer.SetFileName(join(resultDir, key + resultTag + ext)) writer.Execute(toWrite)
def load_itk_image(filename): itkimage = sitk.ReadImage(filename) numpyImage = sitk.GetArrayFromImage(itkimage) numpyOrigin = np.array(list(reversed(itkimage.GetOrigin()))) numpySpacing = np.array(list(reversed(itkimage.GetSpacing()))) return numpyImage, numpyOrigin, numpySpacing
modalities_list = os.listdir( jp(path_data, data, study_center, patient, timepoint)) modalities_list = remove_from_list_if_contains( modalities_list, "null") for modality in modalities_list: # For every modality if modality == "FLAIR": #If FLAIR 2D types_list = os.listdir( jp(path_data, data, study_center, patient, timepoint, modality)) #There is only mask folder images_list = os.listdir( jp(path_data, data, study_center, patient, timepoint, modality, types_list[0])) curr_flair_proc = sitk.ReadImage( jp(path_data, data, study_center, patient, timepoint, modality, types_list[0], images_list[0]) ) #Take first image which is 3D volume timepoints_orig = os.listdir( jp(path_data, original_data, study_center, patient)) if not os.path.exists( jp(path_data, original_data, study_center, patient, timepoints_orig[i_timepoint], 'FLAIR_T2')): break flair_orig_name = os.listdir( jp(path_data, original_data, study_center, patient, timepoints_orig[i_timepoint], 'FLAIR_T2')) curr_flair_orig = sitk.ReadImage( jp(path_data, original_data, study_center, patient,
def preprocessImage(input_image, itcounter, land3D, landmarkList): #################### Get useful image data #################### spacing = input_image.GetSpacing() size = input_image.GetSize() origin = input_image.GetOrigin() direction = input_image.GetDirection() extent = [size[j] * spacing[j] for j in range(3)] if (verbose): print('Input_size:', size) print('Input_spacing', spacing) print('Input_extent', extent) #################### Recalculate resampled image size and spacing #################### if (verbose): print('Resampling to Isotropic image', '...') new_size = [ int(size[0] * (spacing[0])), int(size[1] * (spacing[1])), int(size[2] * (spacing[2])) ] new_spacing = [1.0, 1.0, 1.0] isotropic_image = resampleImage(input_image, new_size, origin, direction, new_spacing) padded_image = padImage(isotropic_image, new_size) #################### Create image ROI from resampled image #################### if (verbose): print('Extracting ROI', '...') roi_image, roi_mask = extractROI(padded_image) #bone_image,bone_mask=extractBone(isotropic_image) #################### Create Ground-Truth segmentation #################### if (verbose): print('Generating Ground-Truth labels', '...') ##Test image with original size -> same results gt_image = extractGT(padded_image, roi_mask, dcmfilename, land3D, landmarkList) #################### Normalize Intensity Distribution #################### if (verbose): print('Normalizing Intensity Distribution', '...') ### Rescale intensity distribution ### normalized_image, normalized_masked_image = normalizeImg( padded_image, roi_mask) #################### Write NiFTi images ##################### name_file_ct = dcmfilename + '/' + dcmfilename + '_ct.nii.gz' name_file_gt = dcmfilename + '/' + dcmfilename + '_gt.nii.gz' name_file_roi = dcmfilename + '/' + dcmfilename + '_roi.nii.gz' if (verbose): print('Writing patient "' + dcmfilename + '" NiFTi images', '...') if not os.path.exists(os.path.dirname(outputDIR + name_file_ct)): try: os.makedirs(os.path.dirname(outputDIR + name_file_ct)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise sitk.WriteImage(normalized_masked_image, outputDIR + name_file_ct) sitk.WriteImage(roi_mask, outputDIR + name_file_roi) sitk.WriteImage(gt_image, outputDIR + name_file_gt) return 1, normalized_image.GetSize()
### Ask user if train/validation/test folders are to be removed ### cleanTrainingFolder() log_file = open('processed_files.log', "w") #################### Parse CSV file with 3D landmarks #################### land3D, landmarkList, patientData = parseLandmarks(csvFILE) print('Reading 3D landmark coordinates', csvFILE, '...') #################### Parse Dicom Dataset directory #################### ### unique dcm folders ### subjects = os.walk(datasetDIR).next()[1] ### Randomize the database for file classification in train,test or validation sets ### #nSubjects=len(land3D) #guided by the .csv files is safer (as some images have no landmarks) nSubjects = len(subjects) #guided by the number of images reader = sitk.ImageSeriesReader() counter = -1 preprocessed_images = [] preprocessed_sizes = [] for itsubject, subject in enumerate(subjects): scans, directories = searchPath(datasetDIR + subject) print('Reading', len(directories), 'Dicom images', directories, '...') idealvolume = '' optionalvolumes = [] optionalsizes = [] ### Iterate subject directory series in order to find the suitable volume ### for itfile, dirname in enumerate(directories): string = dirname.replace(datasetDIR, '') string = string.strip('.').split('/') dcmfilename = string[0] dcmserie = string[1]
def multi_label_mask(itk_image, labels): img_out = SimpleITK.Image(itk_image.GetSize(), SimpleITK.sitkUInt8) img_out.CopyInformation(itk_image) for l in labels: img_out += itk_image == l return img_out
def read_dicom(path): reader = SimpleITK.ImageSeriesReader() reader.SetFileNames(reader.GetGDCMSeriesFileNames(path)) return reader.Execute()
def main(args): temp = 0 data_path = args.path sequences = args.sequence for sequence in sequences: print('\nBegin to preprocess sequence {}'.format(sequence)) img_path = os.path.join(data_path, sequence) seg_path = os.path.join(data_path, '{}_GT'.format(sequence), 'SEG') img_names = os.listdir(img_path) img_names.sort() seg_names = os.listdir(seg_path) seg_names.sort() total = len(seg_names) for i in tqdm(range(total)): seg_name = seg_names[i] img_name = 't' + str(seg_name[7:]) itk_in_img = sitk.ReadImage(os.path.join(img_path, img_name)) in_img = sitk.GetArrayFromImage(itk_in_img).astype(np.float32) # sitk.WriteImage(itk_in_img, os.path.join(data_path, 'image', 't{:03d}.tif'.format(i + temp))) itk_seg_img = sitk.ReadImage(os.path.join(seg_path, seg_name)) seg_img = sitk.GetArrayFromImage(itk_seg_img).astype(np.float32) # sitk.WriteImage(itk_seg_img, os.path.join(data_path, 'mask', 'man_seg{:03d}.tif'.format(i + temp))) depth, width, height = seg_img.shape img = np.zeros((64, 256, 256), dtype=np.float32) seg = np.zeros((64, 256, 256), dtype=np.float32) img[0, :, :] = Image.fromarray(in_img[0, :, :]).resize((256, 256)) seg[0, :, :] = Image.fromarray(seg_img[0, :, :]).resize((256, 256)) img[63, :, :] = Image.fromarray(in_img[depth - 1, :, :]).resize( (256, 256)) seg[63, :, :] = Image.fromarray(seg_img[depth - 1, :, :]).resize( (256, 256)) for k in range(1, 63): y1 = np.zeros((width, height), dtype=np.float32) y2 = np.zeros((width, height), dtype=np.float32) # We have (x1, y1) and (x2, y2) # where x1 = int(k/63*(depth-1)), x2 = int(k/63*(depth-1)) + 1 # y1 = in_img[x1, :, :], y2 = in_img[x2, :, :] # line: Y = (y2-y1)/(x2-x1) * (X - x1) + y1 x1 = int(k / 63 * (depth - 1)) x2 = int(k / 63 * (depth - 1)) + 1 y1[:, :] = in_img[x1, :, :] y2[:, :] = in_img[x2, :, :] kk = k / 63 * (depth - 1) temp_img = Image.fromarray((y2 - y1) / (x2 - x1) * (kk - x1) + y1).resize((256, 256)) img[k, :, :] = temp_img y1[:, :] = seg_img[x1, :, :] y2[:, :] = seg_img[x2, :, :] temp_seg = Image.fromarray((y2 - y1) / (x2 - x1) * (kk - x1) + y1).resize((256, 256)) seg[k, :, :] = temp_seg ''' x = np.arange(depth) for ii in range(256): for jj in range(256): y1 = in_img[:, ii, jj] li = interp1d(x, y1, kind='cubic') y2 = seg_img[:, ii, jj] ls = interp1d(x, y2, kind='cubic') for k in range(64): img[k, ii, jj] = li(k/63*(depth-1)) seg[k, ii, jj] = ls(k/63*(depth-1)) ''' sitk.WriteImage( sitk.GetImageFromArray(img.astype(np.uint16)), os.path.join(data_path, 'image', 't{:03d}.tif'.format(i + temp))) sitk.WriteImage( sitk.GetImageFromArray(seg.astype(np.uint16)), os.path.join(data_path, 'mask', 'man_seg{:03d}.tif'.format(i + temp))) temp += total
def test_get_resampling_space_properties(self): for dim in [2, 3]: path_to_image = os.path.join( DIR_DATA, "%dD_Brain_Target.nii.gz" % dim) image_sitk = sitk.ReadImage(path_to_image) # image size is (181, 217). Division by 2 problematic otherwise if dim == 2: image_sitk = image_sitk[0:180, 0:200] spacing_in = np.array(image_sitk.GetSpacing()) size_in = np.array(image_sitk.GetSize()).astype("int") origin = np.array(image_sitk.GetOrigin()) direction = image_sitk.GetDirection() factor_spacing = 13. factor_add_to_grid = -3.5 spacing = factor_spacing * spacing_in add_to_grid = factor_add_to_grid * spacing size = np.round(size_in / factor_spacing + 2 * factor_add_to_grid) size_out, origin_out, spacing_out, direction_out = \ res.Resampler.get_space_resampling_properties( image_sitk, spacing, add_to_grid) if dim == 3: a_x = image_sitk.TransformIndexToPhysicalPoint( (1, 0, 0)) - origin a_y = image_sitk.TransformIndexToPhysicalPoint( (0, 1, 0)) - origin a_z = image_sitk.TransformIndexToPhysicalPoint( (0, 0, 1)) - origin e_x = a_x / np.linalg.norm(a_x) e_y = a_y / np.linalg.norm(a_y) e_z = a_z / np.linalg.norm(a_z) offset = (e_x + e_y + e_z) * add_to_grid origin -= offset else: a_x = image_sitk.TransformIndexToPhysicalPoint((1, 0)) - origin a_y = image_sitk.TransformIndexToPhysicalPoint((0, 1)) - origin e_x = a_x / np.linalg.norm(a_x) e_y = a_y / np.linalg.norm(a_y) offset = (e_x + e_y) * add_to_grid origin -= offset self.assertEqual(np.sum(np.abs(spacing_out - spacing)), 0) self.assertEqual(np.sum(np.abs(size_out - size)), 0) self.assertEqual(np.sum(np.abs(direction_out - direction)), 0) self.assertEqual(np.sum(np.abs(origin_out - origin)), 0) # check whether extension/cropping does not change 'pixel position' resampled_image_sitk = sitk.Resample( image_sitk, size_out, getattr(sitk, "Euler%dDTransform" % dim)(), sitk.sitkNearestNeighbor, origin_out, spacing_out, direction_out, 0, image_sitk.GetPixelIDValue() ) image_sitk = sitk.Resample( image_sitk, resampled_image_sitk, getattr(sitk, "Euler%dDTransform" % dim)(), sitk.sitkNearestNeighbor, ) nda_diff = sitk.GetArrayFromImage( image_sitk - resampled_image_sitk) self.assertEqual(np.sum(np.abs(nda_diff)), 0)
in_img_dir = os.path.join(base_path_in, dir) out_img_dir = os.path.join(base_path_out, dir + "_crop") if not os.path.exists(out_img_dir): os.makedirs(out_img_dir) if dir == '00005': print(dir) images = os.listdir(in_img_dir) for img in images: print(img) try: l_index = 0 r_index = 0 high = 0 dark = 0 name, dummy = img.split('.jpg') im_in = sitk.ReadImage(os.path.join(in_img_dir, img)) img_array = sitk.GetArrayFromImage(im_in) r_index = find_right_edge(img_array) if r_index > 2400: dark = (img_array[5800:6200, r_index - 50:r_index] < 10).sum() high = np.max(img_array[5800:6200, r_index - 50:r_index]) #brightness = np.mean(img_array[5800:6200,r_index-50:r_index]) #print(brightness) if (high < 30 and dark > 19000) or r_index < 2400: #img_array = sitk.GetArrayFromImage(im_in) l_index = find_left_edge(img_array) if (l_index != -2000 and l_index < 1200): img_left = name + '_l_inx' + str(l_index) + '.jpg' out_img = crop_image(im_in, l_index, LEFT) #sitk.Show(out_img)
re_sampler.SetInterpolator(re_sample_method) itk_img_re_sampled = re_sampler.Execute(itk_image) # 得到重新采样后的图像 return itk_img_re_sampled def mapping_hu_to_pixel(hu_img, wl, ws): ct_min = wl - ws / 2 ct_max = wl + ws / 2 hu_img[hu_img <= ct_min] = ct_min hu_img[hu_img >= ct_max] = ct_max return (((hu_img - ct_min) / (ct_max - ct_min)) * 255).astype(np.uint8) reader = sitk.ImageFileReader() reader.SetImageIO("NiftiImageIO") reader.SetFileName("./COVID19_A_1_1.nii.gz") image = reader.Execute() image_re_sampled = resize_image_itk(image, (512, 512, 512)) img_hu = sitk.GetArrayFromImage(image_re_sampled) img = mapping_hu_to_pixel(img_hu, -645, 4756) x, y, z = img.shape for i in range(x): io.imsave("./output/transverse_plane/transverse_plane" + str(i) + ".png", img[i, :, :]) for i in range(y): io.imsave("./output/sagittal_plane/sagittal_plane" + str(i) + ".png", img[:, i, :])
def find_left_edge(scipy_img): """ """ scipy_img.astype('int32') #sobel(scipy_img, axis=0, output=scipy_img) #sobel(scipy_img, axis=1, output=scipy_img) gaussian_filter1d(scipy_img, 1, axis=0, order=1, output=scipy_img, mode="reflect") gaussian_filter1d(scipy_img, 1, axis=1, order=1, output=scipy_img, mode="reflect") scipy_img.astype('uint8') edge_img = sitk.GetImageFromArray(scipy_img) edge_img = sitk.TriangleThreshold(edge_img, 0, 255) #edge_img = sitk.Median(edge_img,(5,1)) edge_img = sitk.BinaryErode(edge_img, (3, 2), 2, 0, 255) #sitk.Show(edge_img) img = sitk.GetArrayFromImage(edge_img) lines = transform.probabilistic_hough_line(img[:, :2048], 0.5, 6000, 500, angles) height = 6000 while (lines == []): if (height < 0): break else: print(lines) height = height - 1000 lines = transform.probabilistic_hough_line(img[:, :2048], 0.5, height, 500, angles) if (lines == []): print("Failed to find any edge") return -2000 print(lines) l0, l1 = zip(*lines) l0 = list(l0) l1 = list(l1) l00, l01 = zip(*l0) l10, l11 = zip(*l1) l01 = list(l01) l11 = list(l11) d = [a - b for a, b in zip(l01, l11)] inx = l00[np.argmax(d)] img_slice = img[:, inx:inx + 30] m = 0 for i in range(20): sl1 = img[:, inx + i - 1] sl2 = img[:, inx + i] sl3 = img[:, inx + i + 1] c1 = (sl1 != 0).sum(0) c2 = (sl2 != 0).sum(0) c3 = (sl3 != 0).sum(0) if c1 > c2 and c3 > c2: m = inx + i break #array = sitk.GetArrayFromImage(edge_img) #array1,array0 = np.nonzero(array) #array = Counter(array0).most_common() #array.sort(key=lambda x:x[0]) #Sort the tuples (x,occ) from lowest to highest x #array_new = array[0:50] #x,o = zip(*array) #n = [x[i] for i in range(len(x)) if x[i]>875] #print(n) #print(x.index(n[0])) #slice_s = x.index(n[0]) #x,o = zip(*array[slice_s:slice_s+50]) ##inx = np.argmax(o) ##array = array[inx:inx+50] ##x,o = zip(*array) #s_inx = np.argmax(o) ##x = x[inx:] ##o = o[inx:] #for h in range(s_inx+int(s_inx==0),len(o)-1): #if((o[h] > o[h+1]) and (o[h] > o[h-1])):#(o[0]-o[h]>THRESHOLD_PIX_CNT) ): #inx = h #print(h) #break ##if(inx == 0): ##for h in range(1,len(o)-1): ##if(o[h] < o[h+1]): ##inx = h ##print(h) ##break ##plt.plot(x,o,'ro') ##plt.show() print(m) return m
def image_patch_padding (im_dir_list, central_point, dim_range_sup, reference_image, outputPrefix): im_ref = sitk.ReadImage(reference_image) PatchIndice = np.zeros((6,)) PatchIndice[0:3] = central_point - dim_range_sup PatchIndice[3:6] = central_point + dim_range_sup PatchIndice = PatchIndice.astype(int) centralPointFinal = np.zeros((3,len(im_dir_list))) outputlist = [] for i in range(len(im_dir_list)): im = sitk.ReadImage(im_dir_list[i]) im_array = sitk.GetArrayFromImage(im) im_z, im_x, im_y = im_array.shape z_begin = PatchIndice[0] z_end = PatchIndice[3] x_begin = PatchIndice[1] x_end = PatchIndice[4] y_begin = PatchIndice[2] y_end = PatchIndice[5] if z_begin < 0: pad_z = np.abs(0 - z_begin) patch_im_array = im_array[(z_begin+pad_z):(z_end+pad_z), :, :] centralPointFinal[:,i] = central_point + np.array((pad_z,0,0)) elif z_end > im_z: pad_z = np.abs(z_end - im_z) patch_im_array = im_array[(z_begin-pad_z):(z_end-pad_z), :, :] centralPointFinal[:,i] = central_point - np.array((pad_z,0,0)) else: patch_im_array = im_array[z_begin:z_end, :, :] centralPointFinal[:,i] = central_point if x_begin < 0: pad_x = np.abs(0 - x_begin) patch_im_array = patch_im_array[:, (x_begin+pad_x):(x_end+pad_x), :] centralPointFinal[:,i] = central_point + np.array((0,pad_x,0)) elif x_end > im_x: pad_x = np.abs(x_end - im_x) patch_im_array = patch_im_array[:, (x_begin-pad_x):(x_end-pad_x), :] centralPointFinal[:,i] = central_point - np.array((0,pad_x,0)) else: patch_im_array = patch_im_array[:, x_begin:x_end, :] centralPointFinal[:,i] = central_point if y_begin < 0: pad_y = np.abs(0 - y_begin) patch_im_array = patch_im_array[:, :, (y_begin+pad_y):(y_end+pad_y)] centralPointFinal[:,i] = central_point + np.array((0,0,pad_y)) elif y_end > im_y: pad_y = np.abs(y_end - im_y) patch_im_array = patch_im_array[:, :, (y_begin-pad_y):(y_end-pad_y)] centralPointFinal[:,i] = central_point - np.array((0,0,pad_y)) else: patch_im_array = patch_im_array[:, :, y_begin:y_end] centralPointFinal[:,i] = central_point img = sitk.GetImageFromArray(patch_im_array) img.SetOrigin(im_ref.GetOrigin()) img.SetSpacing(im_ref.GetSpacing()) img.SetDirection(im_ref.GetDirection()) name, ext = os.path.splitext(im_dir_list[i]) baseName = os.path.basename(name) fn = outputPrefix +'/patch_'+ baseName+ '.nrrd' outputlist.append(fn) sitk.WriteImage(img,fn) print "the %d th patch is finished" %(i) return outputlist, centralPointFinal
def segCut(data_path, save_path, format, hsize, wsize): file = os.listdir(data_path) # num = len(file) #fname = os.listdir(os.path.join(data_path, file[0])) fname = file[0] # 判断数据的类型 types = '' fn = fname print(fn) if '.mhd' in fn: types = '.mhd' if '.mha' in fn: types = '.mha' if '.nii.gz' in fn: types = '.nii.gz' elif '.nii' in fn: types = '.nii' for filename in file: print(filename) #r_nda3D = load_data_volume_as_array(os.path.join(data_path, filename) + '/'+filename + types) # 获取每个病例的3D数组 #s_nda3D = load_data_volume_as_array(os.path.join(data_path, filename) + '/'+filename + '_segmentation_segmentation' + types) r_nda3D = load_data_volume_as_array(os.path.join( data_path, filename)) # 获取每个病例的3D数组 s_nda3D = load_data_volume_as_array(os.path.join(data_path, filename)) t = np.nonzero(s_nda3D) x1 = np.max(t[0]) y1 = np.max(t[1]) z1 = np.max(t[2]) x2 = np.min(t[0]) y2 = np.min(t[1]) z2 = np.min(t[2]) # print("({}_{},{}_{},{}_{})".format(x2, x1, y2, y1, z2, z1)) if (x1 - x2 + 1 > hsize) or (y2 - 1): s_nda3D = s_nda3D[np.ix_(range(x2, x1 + 1), range(y2, y1 + 1), range(z2, z1 + 1))] # 切取器官所在区域 r_nda3D = r_nda3D[np.ix_(range(x2, x1 + 1), range(y2, y1 + 1), range(z2, z1 + 1))] n = x1 - x2 + 1 x = [] y = [] for j in range(n): s_nda3D_new = cv2.resize(s_nda3D[j], (hsize, wsize)) # 获取每张切片的2D数组 r_nda3D_new = cv2.resize(r_nda3D[j], (hsize, wsize)) x.append(r_nda3D_new) y.append(s_nda3D_new) s_nda3D = np.asarray(y) r_nda3D = np.asarray(x) else: s_nda3D = s_nda3D[np.ix_( range(x2, x1 + 1), range(int((y2 + y1 - hsize) / 2), int((y2 + y1 + hsize) / 2)), range(int((z2 + z1 - wsize) / 2), int( (z2 + z1 + wsize) / 2)))] # 切取器官所在区域 r_nda3D = r_nda3D[np.ix_( range(x2, x1 + 1), range(int((y2 + y1 - hsize) / 2), int((y2 + y1 + hsize) / 2)), range(int((z2 + z1 - wsize) / 2), int((z2 + z1 + wsize) / 2)))] if format == '3D': filename = os.path.splitext(filename)[0] sitk_img = sitk.GetImageFromArray(r_nda3D, isVector=False) sitk.WriteImage( sitk_img, os.path.join(save_path + '/images', filename + '.nii')) sitk_lab = sitk.GetImageFromArray(s_nda3D, isVector=False) sitk.WriteImage( sitk_lab, os.path.join(save_path + '/labels', filename + '_mask.nii')) if format == '2D': filename = os.path.splitext(filename)[0] for nums in range(r_nda3D.shape[0]): #imsave(os.path.join(save_path+'/images', filename + '_' + str(nums) + '.png'), r_nda3D[nums]) #np.save(os.path.join(save_path+'/labels', filename + '_'+ str(nums) + '_mask.npy'), s_nda3D[nums]) imsave( os.path.join(save_path + '/images', filename + '_' + str(nums) + '.png'), r_nda3D[nums]) np.save( os.path.join(save_path + '/labels', filename + '_' + str(nums) + '_mask.npy'), s_nda3D[nums]) print('The data of patient {} have been cut!'.format(filename))
RegionOutput=np.zeros(ImagePyramid[j].shape) RegionWeight=np.zeros(ImagePyramid[j].shape)+0.001 RoIs=Pred[2] for k in range(len(Pred[0])): Coord=RoIs[k]*np.array([2,4,4,2,4,4]) Weight=np.ones(np.asarray(Pred[0][k][0,1:2].shape)) RegionOutput[0,:,Coord[0]:Coord[3],Coord[1]:Coord[4],Coord[2]:Coord[5]]+=Pred[0][k][0,1:2] RegionWeight[0,:,Coord[0]:Coord[3],Coord[1]:Coord[4],Coord[2]:Coord[5]]+=Weight RegionOutput/=RegionWeight OutputWhole=np.zeros(Shape,dtype=np.uint8) OutputWhole[MaximumBbox[0]:MaximumBbox[3],MaximumBbox[1]:MaximumBbox[4],MaximumBbox[2]:MaximumBbox[5]]=(RegionOutput[0,0]*255).astype(np.uint8) Spacing = ToSpacing[ResRates[j]] NewSpacing = [1.0,1.0,4.0] Shape=Shape[::-1] OutputWhole=sitk.GetImageFromArray(OutputWhole) OutputWhole.SetSpacing(Spacing) Resample = sitk.ResampleImageFilter() Resample.SetSize(NewSize[::-1]) Resample.SetInterpolator(sitk.sitkLinear) Resample.SetOutputSpacing(NewSpacing) OutputWhole = Resample.Execute(OutputWhole) OutputWhole=sitk.GetArrayFromImage(OutputWhole) PredPyramid.append(OutputWhole) EnsemblePred=np.zeros(NewSize) for j in range(len(ResRates)): EnsemblePred+=PredPyramid[j].astype(np.float) EnsemblePred/=3 EnsemblePred[EnsemblePred>=128]=255 EnsemblePred[EnsemblePred<128]=0
def getSmoothGradientMagnitude(im,sigma=2): im = sitk.gifa(im) gm = sitk.GradientMagnitudeRecursiveGaussian(im,sigma) gm = sitk.gafi(gm) return gm
def augment_img(datas): dimension = datas[0].GetDimension() # Physical image size corresponds to the largest physical size in the training set, or any other arbitrary size. reference_physical_size = np.zeros(dimension) for img in datas: reference_physical_size[:] = [ (sz - 1) * spc if sz * spc > mx else mx for sz, spc, mx in zip( img.GetSize(), img.GetSpacing(), reference_physical_size) ] # Create the reference image with a zero origin, identity direction cosine matrix and dimension reference_origin = np.zeros(dimension) reference_direction = np.identity(dimension).flatten() # Select arbitrary number of pixels per dimension, smallest size that yields desired results # or the required size of a pretrained network (e.g. VGG-16 224x224), transfer learning. This will # often result in non-isotropic pixel spacing. reference_size = [256] * dimension reference_spacing = [ phys_sz / (sz - 1) for sz, phys_sz in zip(reference_size, reference_physical_size) ] # Another possibility is that you want isotropic pixels, then you can specify the image size for one of # the axes and the others are determined by this choice. Below we choose to set the x axis to 128 and the # spacing set accordingly. # Uncomment the following lines to use this strategy. #reference_size_x = 128 #reference_spacing = [reference_physical_size[0]/(reference_size_x-1)]*dimension #reference_size = [int(phys_sz/(spc) + 1) for phys_sz,spc in zip(reference_physical_size, reference_spacing)] reference_image = sitk.Image(reference_size, datas[0].GetPixelIDValue()) reference_image.SetOrigin(reference_origin) reference_image.SetSpacing(reference_spacing) reference_image.SetDirection(reference_direction) # Always use the TransformContinuousIndexToPhysicalPoint to compute an indexed point's physical coordinates as # this takes into account size, spacing and direction cosines. For the vast majority of images the direction # cosines are the identity matrix, but when this isn't the case simply multiplying the central index by the # spacing will not yield the correct coordinates resulting in a long debugging session. reference_center = np.array( reference_image.TransformContinuousIndexToPhysicalPoint( np.array(reference_image.GetSize()) / 2.0)) ######################################################## aug_transform = sitk.Similarity2DTransform( ) if dimension == 2 else sitk.Similarity3DTransform() all_images = [] for img in datas: # Transform which maps from the reference_image to the current img with the translation mapping the image # origins to each other. transform = sitk.AffineTransform(dimension) transform.SetMatrix(img.GetDirection()) transform.SetTranslation(np.array(img.GetOrigin()) - reference_origin) # Modify the transformation to align the centers of the original and reference image instead of their origins. centering_transform = sitk.TranslationTransform(dimension) img_center = np.array( img.TransformContinuousIndexToPhysicalPoint( np.array(img.GetSize()) / 2.0)) centering_transform.SetOffset( np.array(transform.GetInverse().TransformPoint(img_center) - reference_center)) centered_transform = sitk.Transform(transform) centered_transform.AddTransform(centering_transform) # Set the augmenting transform's center so that rotation is around the image center. aug_transform.SetCenter(reference_center) if dimension == 2: # The parameters are scale (+-10%), rotation angle (+-10 degrees), x translation, y translation transformation_parameters_list = parameter_space_regular_grid_sampling( np.linspace(0.9, 1.1, 5), np.linspace(-np.pi / 18.0, np.pi / 18.0, 5), np.linspace(-10, 10, 5), np.linspace(-10, 10, 5)) tmp = np.random.randint(len(transformation_parameters_list)) transformation_parameters_list = [ transformation_parameters_list[tmp] ] # transformation_parameters_list = parameter_space_random_sampling( # thetaX=(-np.pi / 18.0, np.pi / 18.0), # thetaY=(-np.pi / 18.0, np.pi / 18.0), # tx=(-10.0, 10.0), # ty=(-10.0, 10.0), # scale=(0.9, 1.1), # n=1) else: # transformation_parameters_list = similarity3D_parameter_space_random_sampling( # thetaX=(-np.pi / 18.0, np.pi / 18.0), # thetaY=(-np.pi / 18.0, np.pi / 18.0), # thetaZ=(-np.pi / 18.0, np.pi / 18.0), # tx=(-10.0, 10.0), # ty=(-10.0, 10.0), # tz=(-10.0, 10.0), # scale=(0.9, 1.1), # n=1) transformation_parameters_list = similarity3D_parameter_space_random_sampling( thetaX=(-np.pi / 18.0, np.pi / 18.0), thetaY=(-np.pi / 18.0, np.pi / 18.0), thetaZ=(-np.pi / 18.0, np.pi / 18.0), tx=(-10.0, 10.0), ty=(-10.0, 10.0), tz=(-10.0, 10.0), scale=(0.9, 1.1), n=1) # transformation_parameters_list = similarity3D_parameter_space_regular_sampling(np.linspace(-np.pi/18.0,np.pi/18.0,3), # np.linspace(-np.pi/18.0,np.pi/18.0,3), # np.linspace(-np.pi/18.0,np.pi/18.0,3), # np.linspace(-10,10,3), # np.linspace(-10,10,3), # np.linspace(-10,10,3), # np.linspace(0.9,1.1,3)) generated_images = augment_images_spatial( img, reference_image, centered_transform, aug_transform, transformation_parameters_list, os.path.join(OUTPUT_DIR, 'img_spatial_aug'), 'nii.gz') # if dimension == 2: # in 2D we join all of the images into a 3D volume which we use for display. # all_images.append(sitk.JoinSeries(generated_images)) return generated_images
tn_epi] #attention:[0:num_train) with open(os.path.join(TVTcsv, train_csv_list[epi]), 'w') as file: w = csv.writer(file) for name in train_lists: ct_name = os.path.join(savedct_path, name) seg_name = os.path.join(savedseg_path, 'segmentation-' + name.split('-')[-1]) w.writerow( (ct_name, seg_name)) #attention: the first row defult to tile with open(os.path.join(TVTcsv, valid_csv), 'w') as file: w = csv.writer(file) for name in valid_lists: ct_name = os.path.join(savedct_path, name) seg_name = os.path.join(savedseg_path, 'segmentation-' + name.split('-')[-1]) w.writerow((ct_name, seg_name)) if if_test: test_lists = os.listdir(saved_test) test_lists.sort(key=natural_keys) with open(os.path.join(TVTcsv, test_csv), 'w') as file: w = csv.writer(file) for name in test_lists: ct_name = os.path.join(saved_test, name) seg_name = os.path.join(saved_test, name) ct = sitk.GetArrayFromImage(sitk.ReadImage(ct_name)) w.writerow((ct_name, seg_name)) print('total=', total, 'train=', tn, '(', tn_epi, '*', episode, ')', 'val=', len(valid_lists))
def load_data_volume_as_array(filename): img = sitk.ReadImage(filename) nda = sitk.GetArrayFromImage(img) return nda
df_node["file"] = df_node["seriesuid"].map( lambda file_name: get_filename(file_list, file_name)) df_node = df_node.dropna() df_node.head(5) print('数据条数:%d' % df_node.shape[0]) count_true = 0 for fcount, img_file in enumerate(tqdm(file_list)): mini_df = df_node[df_node["file"] == img_file] # get all nodules associate with file if mini_df.shape[0] > 0: # some files may not have a nodule--skipping those print(mini_df.shape[0]) count_true = 1 # load the data once itk_img = sitk.ReadImage(img_file) img_array = sitk.GetArrayFromImage( itk_img) # indexes are z,y,x (notice the ordering) num_z, height, width = img_array.shape # heightXwidth constitute the transverse plane origin = np.array( itk_img.GetOrigin()) # x,y,z Origin in world coordinates (mm) spacing = np.array( itk_img.GetSpacing()) # spacing of voxels in world coor. (mm) # go through all nodes (why just the biggest?) for node_idx, cur_row in mini_df.iterrows(): node_x = cur_row["coordX"] node_y = cur_row["coordY"] node_z = cur_row["coordZ"] diam = cur_row["diameter_mm"] # just keep 3 slices imgs = np.ndarray([3, height, width], dtype=np.float32)
def correct_raw_data(raw_data_path, channel, subsample_factor=2, log_s3_path=None, background_correction=True): total_n_jobs = cpu_count() # overwrite existing raw data with corrected data outdir = raw_data_path # get list of all tiles to correct for given channel all_files = np.sort(glob.glob(f'{raw_data_path}/*/*.tiff')) if background_correction: background_val = get_background_value(raw_data_path) total_files = len(all_files) bias_path = f'{outdir}/CHN0{channel}_bias.tiff' if os.path.exists(bias_path): bias = tf.imread(bias_path) else: # subsample tiles files_cb = all_files[::subsample_factor] num_files = len(files_cb) # compute running sums in parallel sums = Parallel(total_n_jobs, verbose=10)( delayed(sum_tiles)(f) for f in chunks(files_cb, math.ceil(num_files // (total_n_jobs)) + 1)) sums = [i[:, :, None] for i in sums] mean_tile = np.squeeze(np.sum(np.concatenate(sums, axis=2), axis=2)) / num_files if background_correction: # subtract background out from bias correction mean_tile -= background_val mean_tile = sitk.GetImageFromArray(mean_tile) # get the bias correction tile using N4ITK bias = sitk.GetArrayFromImage(get_bias_field(mean_tile, scale=1.0)) # save bias tile to local directory tf.imsave(bias_path, bias.astype('float32')) # save bias tile to S3 if log_s3_path: s3 = boto3.resource('s3') img = Image.fromarray(bias) fp = BytesIO() img.save(fp, format='TIFF') # reset pointer to beginning of file fp.seek(0) log_s3_url = S3Url(log_s3_path.strip('/')) bias_path = f'{log_s3_url.key}/CHN0{channel}_bias.tiff' s3.Object(log_s3_url.bucket, bias_path).upload_fileobj(fp) # correct all the files and save them files_per_proc = math.ceil(total_files / total_n_jobs) + 1 work = chunks(all_files, files_per_proc) with tqdm_joblib(tqdm(desc="Correcting tiles", total=total_n_jobs)) as progress_bar: Parallel(n_jobs=total_n_jobs, verbose=10)( delayed(correct_tiles)(files, outdir, bias, background_val) for files in work)
def augment_imgs_labs(imgs, labs, img_size=96): dimension = imgs[0].GetDimension() # Physical image size corresponds to the largest physical size in the training set, or any other arbitrary size. reference_physical_size = np.zeros(dimension) for img in imgs: reference_physical_size[:] = [ (sz - 1) * spc if sz * spc > mx else mx for sz, spc, mx in zip( img.GetSize(), img.GetSpacing(), reference_physical_size) ] reference_origin = np.zeros(dimension) reference_direction = np.identity(dimension).flatten() reference_size = [img_size] * dimension reference_spacing = [ phys_sz / (sz - 1) for sz, phys_sz in zip(reference_size, reference_physical_size) ] reference_image = sitk.Image(reference_size, imgs[0].GetPixelIDValue()) reference_image.SetOrigin(reference_origin) reference_image.SetSpacing(reference_spacing) reference_image.SetDirection(reference_direction) reference_lab = sitk.Image(reference_size, labs[0].GetPixelIDValue()) reference_lab.SetOrigin(reference_origin) reference_lab.SetSpacing(reference_spacing) reference_lab.SetDirection(reference_direction) # Always use the TransformContinuousIndexToPhysicalPoint to compute an indexed point's physical coordinates as # this takes into account size, spacing and direction cosines. For the vast majority of images the direction # cosines are the identity matrix, but when this isn't the case simply multiplying the central index by the # spacing will not yield the correct coordinates resulting in a long debugging session. reference_center = np.array( reference_image.TransformContinuousIndexToPhysicalPoint( np.array(reference_image.GetSize()) / 2.0)) aug_transform = sitk.Similarity2DTransform( ) if dimension == 2 else sitk.Similarity3DTransform() ######################################################## img = imgs[0] ####create spatial transform transform = sitk.AffineTransform(dimension) transform.SetMatrix(img.GetDirection()) transform.SetTranslation(np.array(img.GetOrigin()) - reference_origin) # Modify the transformation to align the centers of the original and reference image instead of their origins. centering_transform = sitk.TranslationTransform(dimension) img_center = np.array( img.TransformContinuousIndexToPhysicalPoint( np.array(img.GetSize()) / 2.0)) centering_transform.SetOffset( np.array(transform.GetInverse().TransformPoint(img_center) - reference_center)) centered_transform = sitk.Transform(transform) centered_transform.AddTransform(centering_transform) # Set the augmenting transform's center so that rotation is around the image center. aug_transform.SetCenter(reference_center) if dimension == 2: range = np.linspace(0.8, 1.2, 20) scale = range[np.random.randint(len(range))] range = np.linspace(-np.pi / 18.0, np.pi / 18.0, 24) rot = range[np.random.randint(len(range))] range = np.linspace(-15, 15, 20) deltax = range[np.random.randint(len(range))] range = np.linspace(-15, 15, 20) deltay = range[np.random.randint(len(range))] transformation_parameters_list = [[scale, rot, deltax, deltay]] else: transformation_parameters_list = similarity3D_parameter_space_random_sampling( thetaX=(-np.pi / 28.0, np.pi / 28.0), thetaY=(-np.pi / 28.0, np.pi / 28.0), thetaZ=(-np.pi / 28.0, np.pi / 28.0), tx=(-10.0, 10.0), ty=(-10.0, 10.0), tz=(-10.0, 10.0), scale=(0.85, 1.45), n=1) tmp = np.random.randint(len(transformation_parameters_list)) transformation_parameters_list = [transformation_parameters_list[tmp]] #### trans_imgs = [] trans_labs = [] for img in imgs: # Transform which maps from the reference_image to the current img with the translation mapping the image # origins to each other. gen_img = augment_images_spatial( img, reference_image, centered_transform, aug_transform, transformation_parameters_list, os.path.join(OUTPUT_DIR, 'img_spatial_aug'), 'nii.gz') trans_imgs.append(gen_img[0]) for lab in labs: gen_lab = augment_images_spatial(lab, reference_lab, centered_transform, aug_transform, transformation_parameters_list, os.path.join(OUTPUT_DIR, 'lab_spatial_aug'), 'nii.gz', interpolator=sitk.sitkNearestNeighbor, default_intensity_value=0) trans_labs.append(gen_lab[0]) return trans_imgs, trans_labs