def extractPatch_2D_patchs_landmark(self): """ This function extraccts 2D patches using land marks from an XML file""" if self.appply_on_batch_files is False: # single image img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] # convert image to 8 bit self.data = self.to_8bit_(self.data) self.do_2D_patch_extraction() else: assert len(os.listdir(self.img_source_folder)) or \ len(os.listdir(self.xml_source_folder)), 'source folder is empty' assert len(os.listdir(self.xml_source_folder)) == len(os.listdir(self.img_source_folder)), \ 'number of xml file and image files shoud be same, BUT, found different' img_files = os.listdir(self.img_source_folder) xml_files = os.listdir(self.xml_source_folder) for images_name, xml_name in zip(img_files, xml_files): self.image_name = images_name # get image extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] img_extension = img_name_split[-1] name_ = os.path.join(self.img_source_folder, images_name) if img_extension == 'dcm': self.data = read_dicom(name_) elif img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.data = read_nii(name_) else: self.data = read_image(name_) # convert image to 8 bit self.data = self.to_8bit_(self.data) # read XML file self.landmark_path = os.path.join(self.xml_source_folder, xml_name) lmark = get_type_center_from_XML(self.landmark_path) self.landmark = lmark # apply augmentation self.do_2D_patch_extraction() print('Patch Generation done!')
def load_reference_image(self): filter = "All Files (*);;image files (*.nii.gz *.png *.tif *.PNG *.dcm)" title = "Select refernce image" ref_image_name = self.openFileNameDialog(filter, title) # self.ref_image = io.imread(image_name) # get image extension img_name = ref_image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") img_extension = img_name_split[-1] if img_extension == 'dcm': self.ref_image = read_dicom(ref_image_name) elif img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.ref_image = read_nii(ref_image_name) else: self.ref_image = read_image(ref_image_name)
def apply_otherpreprocessing(self): """ this function cheks if it the operation is requested on a single image or to abatch files and call the respective function :return: """ if self.appply_on_batch_files is False: # single image img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] self.do_preprocessing() else: # apply to all images in the folder assert len(os.listdir()), 'source folder is empty' files = os.listdir(self.source_folder) for images_name in files: self.image_name = images_name # get image extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] img_extension = img_name_split[-1] name_ = os.path.join(self.source_folder, images_name) if img_extension == 'dcm': self.input_image = read_dicom(name_) elif img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.input_image = read_nii(name_) else: self.input_image = read_image(name_) self.do_preprocessing() print('All requested preprcessing are done!')
def loadImageimage_data(self, apply_to_folder_files=False): if apply_to_folder_files is True: self.source_folder = str( QFileDialog.getExistingDirectory( self, "Select a directory containing images")) if os.path.isdir(self.source_folder) is False or len( os.listdir(self.source_folder)) is 0: QMessageBox.warning( self, "Warning", "Please select a folder or the directory is empty") else: self.image_data = None self.image_name = None else: filter = "All Files (*);;image files (*.nii.gz *.png *.tif *.PNG *.dcm)" title = "Select image or folder" self.image_name = self.openFileNameDialog(filter, title) if os.path.isdir(self.image_name) is True: print('you have selected a folder, please select a file!') else: # get image extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") img_extension = img_name_split[-1] # allowed image formats image_format = ('.png', '.PNG', '.tif', '.dcm', '.nii', 'nii.gz') assert self.image_name.endswith(image_format) is True, 'selected file doesnot have supported image extension. allowed image ' \ 'image extensions are (.png, .PNG, .tif, .dcm, .nii, nii.gz)' if img_extension == 'dcm': self.image_data = read_dicom(self.image_name) elif img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.image_data = read_nii(self.image_name) else: self.image_data = read_image(self.image_name) # display image # imaage shape S = self.image_data.shape if len(S) == 2: # 2d image self.slider.setMinimum(0) self.slider.setMaximum(0) self.displayNDArray(self.image_data) else: # 3D image N = S[-1] self.slider.setMinimum(0) self.slider.setMaximum(N - 1) self.slider.setValue(int(N / 2)) self.slider.setTickInterval(1) self.displayNDArray(self.image_data[:, :, int(N / 2)])
def extractPatch_2D5_patchs_mask_gt(self): """ Extract 2.5D patches from 3D image with ground truth image provided. """ # the size of the patch will only depend on the first patchg dimension patch_size_temp = (self.patch_dim[0], self.patch_dim[0], self.patch_dim[0]) if self.appply_on_batch_files is False: # single image # get image name and extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] # for gt image gt_image_name = self.gt_img_path # get image extension gt_image_name = os.path.split(gt_image_name) gt_image_name = gt_image_name[-1] gt_img_name_split = gt_image_name.split(".") gt_img_extension = gt_img_name_split[-1] name_ = os.path.join(self.gt_img_path) if gt_img_extension == 'dcm': self.gt_data = read_dicom(name_) elif gt_img_extension == 'nii' or gt_img_extension == 'gz' or gt_img_extension == 'nii.gz': self.gt_data = read_nii(name_) else: self.gt_data = read_image(name_) # convert to 8 bit self.data = self.to_8bit_(self.data) # extract and save patchs im_patches, gt_patches = extract_3d_patches(self.data, patch_size_temp, self.stride, gt_data=self.gt_data) output_patches = self.make_2D5_patch_from_3d(im_patches) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, output_patches) output_patches_gt = self.make_2D5_patch_from_3d(gt_patches) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_gt_.npy') np.save(path_, output_patches_gt) else: # for batch of files assert len(os.listdir(self.img_source_folder)) or \ len(os.listdir(self.gt_path_source_folder)), 'source folder is empty' assert len(os.listdir(self.gt_path_source_folder)) == len(os.listdir(self.img_source_folder)), \ 'number of xml file and image files shoud be same, BUT, found different' img_files = os.listdir(self.img_source_folder) gt_files = os.listdir(self.gt_path_source_folder) for images_name, gt_name in zip(img_files, gt_files): self.image_name = images_name # get image extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] img_extension = img_name_split[-1] name_ = os.path.join(self.img_source_folder, images_name) if img_extension == 'dcm': self.data = read_dicom(name_) elif img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.data = read_nii(name_) else: self.data = read_image(name_) # convert to 8-bit self.data = self.to_8bit_(self.data) # for gt image gt_image_name = gt_name # get image extension gt_image_name = os.path.split(gt_image_name) gt_image_name = gt_image_name[-1] gt_img_name_split = gt_image_name.split(".") gt_image_name = gt_img_name_split[0] gt_img_extension = gt_img_name_split[-1] name_ = os.path.join(self.gt_path_source_folder, gt_name) if gt_img_extension == 'dcm': self.gt_data = read_dicom(name_) elif gt_img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.gt_data = read_nii(name_) else: self.gt_data = read_image(name_) # extract and save patchs im_patches, gt_patches = extract_3d_patches( self.data, patch_size_temp, self.stride, gt_data=self.gt_data) output_patches = self.make_2D5_patch_from_3d(im_patches) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, output_patches) output_patches_gt = self.make_2D5_patch_from_3d(gt_patches) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_gt_.npy') np.save(path_, output_patches_gt) print('Patch Generation done!')
def extractPatch_2D5_patchs_no_gt(self): """ Extract 2.5D patches from 3D image; when there is no ground truth provided. """ patch_size_temp = (self.patch_dim[0], self.patch_dim[0], self.patch_dim[0]) if self.appply_on_batch_files is False: # single image img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] self.data = self.to_8bit_(self.data) im_patches = extract_3d_patches(self.data, patch_size_temp, self.stride, gt_data=None) output_patches = self.make_2D5_patch_from_3d(im_patches) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, output_patches) else: # for batch of files assert len(os.listdir( self.img_source_folder)), 'source folder is empty' img_files = os.listdir(self.img_source_folder) for images_name in img_files: self.image_name = images_name # get image extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] img_extension = img_name_split[-1] name_ = os.path.join(self.img_source_folder, images_name) if img_extension == 'dcm': self.data = read_dicom(name_) elif img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.data = read_nii(name_) else: self.data = read_image(name_) # convert to 8-bit self.data = self.to_8bit_(self.data) # extract and save patchs im_patches = extract_3d_patches(self.data, patch_size_temp, self.stride, gt_data=None) output_patches = self.make_2D5_patch_from_3d(im_patches) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, output_patches) print('Patch Generation done!')
def extractPatch_3D_patchs_mask_gt(self): """ This function it extarcts 3D patches using mask or ground truth or ROI image into consideration """ if self.appply_on_batch_files is False: # single image # get image name and extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] # for gt image gt_image_name = self.gt_img_path # get image extension gt_image_name = os.path.split(gt_image_name) gt_image_name = gt_image_name[-1] gt_img_name_split = gt_image_name.split(".") gt_img_extension = gt_img_name_split[-1] name_ = os.path.join(self.gt_img_path) if gt_img_extension == 'dcm': self.gt_data = read_dicom(name_) elif gt_img_extension == 'nii' or gt_img_extension == 'gz' or gt_img_extension == 'nii.gz': self.gt_data = read_nii(name_) else: self.gt_data = read_image(name_) # convert to 8-bit self.data = self.to_8bit_(self.data) # extract patch im_patches, gt_patches = extract_3d_patches(self.data, self.patch_dim, self.stride, gt_data=self.gt_data) # save patch path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, im_patches) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_gt_.npy') np.save(path_, gt_patches) else: # for batch of files assert len(os.listdir(self.img_source_folder)) or \ len(os.listdir(self.gt_path_source_folder)), 'source folder is empty' assert len(os.listdir(self.gt_path_source_folder)) == len(os.listdir(self.img_source_folder)), \ 'number of xml file and image files shoud be same, BUT, found different' img_files = os.listdir(self.img_source_folder) gt_files = os.listdir(self.gt_path_source_folder) for images_name, gt_name in zip(img_files, gt_files): self.image_name = images_name # get image extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] img_extension = img_name_split[-1] name_ = os.path.join(self.img_source_folder, images_name) if img_extension == 'dcm': self.data = read_dicom(name_) elif img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.data = read_nii(name_) else: self.data = read_image(name_) # convert to 8-bit self.data = self.to_8bit_(self.data) # for gt image gt_image_name = gt_name # get image extension gt_image_name = os.path.split(gt_image_name) gt_image_name = gt_image_name[-1] gt_img_name_split = gt_image_name.split(".") gt_image_name = gt_img_name_split[0] gt_img_extension = gt_img_name_split[-1] name_ = os.path.join(self.gt_path_source_folder, gt_name) if gt_img_extension == 'dcm': self.gt_data = read_dicom(name_) elif gt_img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.gt_data = read_nii(name_) else: self.gt_data = read_image(name_) # extract 3D patches im_patches, gt_patches = extract_3d_patches( self.data, self.patch_dim, self.stride, gt_data=self.gt_data) # Save patches path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, im_patches) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_gt_.npy') np.save(path_, gt_patches) print('Patch Generation done!')
def extractPatch_2D_patchs_mask_gt(self): if self.appply_on_batch_files is False: # single image # get image extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] # for gt image gt_image_name = self.gt_img_path # get image extension gt_image_name = os.path.split(gt_image_name) gt_image_name = gt_image_name[-1] gt_img_name_split = gt_image_name.split(".") # gt_image_name = img_name_split[0] gt_img_extension = gt_img_name_split[-1] # Read an image name_ = os.path.join(self.gt_img_path) if gt_img_extension == 'dcm': self.gt_data = read_dicom(name_) elif gt_img_extension == 'nii' or gt_img_extension == 'gz' or gt_img_extension == 'nii.gz': self.gt_data = read_nii(name_) else: self.gt_data = read_image(name_) Dim = self.data.shape if len(Dim) is 2: # 2D image # extract patches im_patches, gt_patches = extract_2d_patches( self.data, self.patch_dim[:2], self.stride, gt_data=self.gt_data) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, im_patches) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_gt_.npy') np.save(path_, gt_patches) else: # image is 3D and apply per slice P_im = np.empty( shape=[0, self.patch_dim[0], self.patch_dim[1]], dtype='int16') P_gt = np.empty( shape=[0, self.patch_dim[0], self.patch_dim[1]], dtype='int16') for i in range(Dim[2]): # extract slices per slice im_patches, gt_patches = extract_2d_patches( self.data[:, :, i], self.patch_dim[:2], self.stride, gt_data=self.gt_data[:, :, i]) P_im = np.append(P_im, im_patches, axis=0) P_gt = np.append(P_gt, gt_patches, axis=0) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, P_im) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_gt_.npy') np.save(path_, P_gt) else: # for batch of files assert len(os.listdir(self.img_source_folder)) or \ len(os.listdir(self.gt_path_source_folder)), 'source folder is empty' assert len(os.listdir(self.gt_path_source_folder)) == len(os.listdir(self.img_source_folder)), \ 'number of ground truth file and image files shoud be same, BUT, found different!' img_files = os.listdir(self.img_source_folder) gt_files = os.listdir(self.gt_path_source_folder) for images_name, gt_name in zip(img_files, gt_files): self.image_name = images_name # get image extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] img_extension = img_name_split[-1] name_ = os.path.join(self.img_source_folder, images_name) if img_extension == 'dcm': self.data = read_dicom(name_) elif img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.data = read_nii(name_) else: self.data = read_image(name_) # Convert to 8 bit self.data = self.to_8bit_(self.data) # for gt image gt_image_name = gt_name # get image extension gt_image_name = os.path.split(gt_image_name) gt_image_name = gt_image_name[-1] gt_img_name_split = gt_image_name.split(".") gt_image_name = gt_img_name_split[0] gt_img_extension = gt_img_name_split[-1] name_ = os.path.join(self.gt_path_source_folder, gt_name) if gt_img_extension == 'dcm': self.gt_data = read_dicom(name_) elif gt_img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.gt_data = read_nii(name_) else: self.gt_data = read_image(name_) Dim = self.data.shape # 2D image if len(Dim) is 2: im_patches, gt_patches = extract_2d_patches( self.data, self.patch_dim[:2], self.stride, gt_data=self.gt_data) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, im_patches) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_gt_.npy') np.save(path_, gt_patches) else: # image is 3D and apply per slice P_im = np.empty( shape=[0, self.patch_dim[0], self.patch_dim[1]], dtype='int16') P_gt = np.empty( shape=[0, self.patch_dim[0], self.patch_dim[1]], dtype='int16') for i in range(Dim[2]): # extract slices per slice im_patches, gt_patches = extract_2d_patches( self.data[:, :, i], self.patch_dim[:2], self.stride, gt_data=self.gt_data[:, :, i]) P_im = np.append(P_im, im_patches, axis=0) P_gt = np.append(P_gt, gt_patches, axis=0) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, P_im) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_gt_.npy') np.save(path_, P_gt) print('Patch Generation done!')
def extractPatch_2D_patchs_no_gt(self): if self.appply_on_batch_files is False: # single image # get image name and extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] # check dimenation of the data Dim = self.data.shape self.data = self.to_8bit_(self.data) # 2D image if len(Dim) is 2: # extract patches im_patches = extract_2d_patches(self.data, self.patch_dim[:2], self.stride, gt_data=None) # patch name path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') #save as npy file np.save(path_, im_patches) else: # image is 3D and apply per slice P = np.empty(shape=[0, self.patch_dim[0], self.patch_dim[1]], dtype='int16') for i in range(Dim[2]): # extract slices per slice data_ = self.to_8bit_(self.data[:, :, i]) im_patches = extract_2d_patches(data_, self.patch_dim[:2], self.stride, gt_data=None) P = np.append(P, im_patches, axis=0) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, P) else: # for batch of files # check if folder contains file assert len(os.listdir( self.img_source_folder)), 'source folder is empty' # read all files img_files = os.listdir(self.img_source_folder) for images_name in img_files: self.image_name = images_name # get image extension img_name = self.image_name img_name = os.path.split(img_name) img_name = img_name[-1] img_name_split = img_name.split(".") self.img_name_ = img_name_split[0] img_extension = img_name_split[-1] name_ = os.path.join(self.img_source_folder, images_name) if img_extension == 'dcm': self.data = read_dicom(name_) elif img_extension == 'nii' or img_extension == 'gz' or img_extension == 'nii.gz': self.data = read_nii(name_) else: self.data = read_image(name_) # check dimenation of the data Dim = self.data.shape # 2D image if len(Dim) is 2: im_patches = extract_2d_patches(self.data, self.patch_dim[:2], self.stride, gt_data=None) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, im_patches) else: # image is 3D and apply per slice P = np.empty( shape=[0, self.patch_dim[0], self.patch_dim[1]], dtype='int16') for i in range(Dim[2]): # extract slices per slice im_patches = extract_2d_patches(self.data[:, :, i], self.patch_dim[:2], self.stride, gt_data=None) P = np.append(P, im_patches, axis=0) path_ = os.path.join(self.generated_patches_folder, self.img_name_ + '_images_.npy') np.save(path_, P)