def find_downsized_info(training_data_files, input_shape): foreground = get_complete_foreground(training_data_files) crop_slices = crop_img(foreground, return_slices=True, copy=True) cropped = crop_img_to(foreground, crop_slices, copy=True) final_image = resize(cropped, new_shape=input_shape, interpolation="nearest") return crop_slices, final_image.affine, final_image.header
def remove_black_frame(files): foreground = get_image_foreground(files) crop = get_index_of_slice(foreground) new_image_files = list() for image_file in files: image = nib.load(image_file) image_croped = crop_img_to(image, crop, copy=True) new_image_files.append(image_croped) return new_image_files
def read_image(in_file, image_shape=None, interpolation='linear', crop=None): print("Reading: {0}".format(in_file)) image = nib.load(os.path.abspath(in_file)) image = fix_shape(image) if crop: image = crop_img_to(image, crop, copy=True) if image_shape: return resize(image, new_shape=image_shape, interpolation=interpolation) else: return image
def crop_img(img, rtol=1e-8, copy=True, return_slices=False): """Crops img as much as possible Will crop img, removing as many zero entries as possible without touching non-zero entries. Will leave one voxel of zero padding around the obtained non-zero area in order to avoid sampling issues later on. Parameters ---------- img: Niimg-like object See http://nilearn.github.io/manipulating_images/input_output.html img to be cropped. rtol: float relative tolerance (with respect to maximal absolute value of the image), under which values are considered negligeable and thus croppable. copy: boolean Specifies whether cropped data is copied or not. return_slices: boolean If True, the slices that define the cropped image will be returned. Returns ------- cropped_img: image Cropped version of the input image """ img = check_niimg(img) data = img.get_data() infinity_norm = max(-data.min(), data.max()) # Discard voxels that are approximately zero or zero passes_threshold = np.logical_or(data < -rtol * infinity_norm, data > rtol * infinity_norm) if data.ndim == 4: passes_threshold = np.any(passes_threshold, axis=-1) # Return a numpy array with shape (3, num_column), each column is coordinates of voxel other than 0 coords = np.array(np.where(passes_threshold)) # Find min and max of rows => Determine min and max of coordinates => range for x, y, z for coordinates. start = coords.min(axis=1) end = coords.max(axis=1) + 1 # pad with one voxel to avoid resampling problems start = np.maximum(start - 1, 0) end = np.minimum(end + 1, data.shape[:3]) # Range for three dimension to crop MRI image, ex: (10, 100), (40, 120), (30, 130) slices = [slice(s, e) for s, e in zip(start, end)] # return_slices = True if return_slices: return slices return crop_img_to(img, slices, copy=copy)
def crop_img(img, rtol=1e-8, copy=True, return_slices=False): """Crops img as much as possible Will crop img, removing as many zero entries as possible without touching non-zero entries. Will leave one voxel of zero padding around the obtained non-zero area in order to avoid sampling issues later on. Parameters ---------- img: Niimg-like object See http://nilearn.github.io/manipulating_images/input_output.html img to be cropped. rtol: float relative tolerance (with respect to maximal absolute value of the image), under which values are considered negligeable and thus croppable. copy: boolean Specifies whether cropped data is copied or not. return_slices: boolean If True, the slices that define the cropped image will be returned. Returns ------- cropped_img: image Cropped version of the input image """ img = check_niimg(img) data = img.get_data() infinity_norm = max(-data.min(), data.max()) # Bo di cac voxel xap xi bang 0, hoac bang 0. passes_threshold = np.logical_or(data < -rtol * infinity_norm, data > rtol * infinity_norm) if data.ndim == 4: passes_threshold = np.any(passes_threshold, axis=-1) # Tra ve mang gom 3 dong, moi cot la cac thong so toa do cua voxel khac 0. coords = np.array(np.where(passes_threshold)) # Tim min va max cua cac dong => xac dinh min va max cua toa do => pham vi toa do. start = coords.min(axis=1) end = coords.max(axis=1) + 1 # pad with one voxel to avoid resampling problems start = np.maximum(start - 1, 0) end = np.minimum(end + 1, data.shape[:3]) # slices nay neu la 3D thi se la pham vi ung voi 3 dimension de crop hinh, vi du: (10, 100), (40, 120), (30, 130) slices = [slice(s, e) for s, e in zip(start, end)] if return_slices: return slices return crop_img_to(img, slices, copy=copy)
def crop_img(img, rtol=1e-8, copy=True, return_slices=False): img = check_niimg(img) data = img.get_data() infinity_norm = max(-data.min(), data.max()) passes_threshold = np.logical_or(data < -rtol * infinity_norm, data > rtol * infinity_norm) if data.ndim == 4: passes_threshold = np.any(passes_threshold, axis=-1) coords = np.array(np.where(passes_threshold)) start = coords.min(axis=1) end = coords.max(axis=1) + 1 # pad with one voxel to avoid resampling problems start = np.maximum(start - 1, 0) end = np.minimum(end + 1, data.shape[:3]) slices = [slice(s, e) for s, e in zip(start, end)] if return_slices: return slices return crop_img_to(img, slices, copy=copy)
# slices = [slice(s, e) for s, e in zip(min_index, max_index)] return slices def remove_black_frame(files): foreground = get_image_foreground(files) crop = get_index_of_slice(foreground) new_image_files = list() for image_file in files: image = nib.load(image_file) image_croped = crop_img_to(image, crop, copy=True) new_image_files.append(image_croped) return new_image_files if __name__ == "__main__": set_of_files = [ 'f_data/original/HGG/Brats18_2013_2_1/Brats18_2013_2_1_t1.nii.gz', 'f_data/original/HGG/Brats18_2013_2_1/Brats18_2013_2_1_t2.nii.gz' ] nii_foreground = get_image_foreground(set_of_files) crop = get_index_of_slice(nii_foreground) # print(crop) nii_image = nib.load( 'f_data/original/HGG/Brats18_2013_2_1/Brats18_2013_2_1_t1.nii.gz') nii_image_croped = crop_img_to(nii_image, crop, copy=True) nib.save(nii_image_croped, 'simpleitk_save5.nii.gz')
def cut_bounding_box(img, start, end): slices = [slice(s, e) for s, e in zip(start, end)] return crop_img_to(img, slices, copy=True)
def crop_img(img, rtol=1e-8, copy=True, return_slices=False, pad=True, percentile=None, return_affine=False): """Crops img as much as possible Will crop img, removing as many zero entries as possible without touching non-zero entries. Will leave one voxel of zero padding around the obtained non-zero area in order to avoid sampling issues later on. Parameters ---------- img: Niimg-like object See http://nilearn.github.io/manipulating_images/input_output.html img to be cropped. rtol: float relative tolerance (with respect to maximal absolute value of the image), under which values are considered negligeable and thus croppable. copy: boolean Specifies whether cropped data is copied or not. return_slices: boolean If True, the slices that define the cropped image will be returned. pad: boolean or integer If True, an extra slice in each direction will be added to the image. If integer > 0 then the pad width will be set to that integer. percentile: integer or None If not None, then the image will be crop out slices below the given percentile Returns ------- cropped_img: image Cropped version of the input image """ img = check_niimg(img) data = img.get_data() if percentile is not None: passes_threshold = data > np.percentile(data, percentile) else: infinity_norm = max(-data.min(), data.max()) passes_threshold = np.logical_or(data < -rtol * infinity_norm, data > rtol * infinity_norm) if data.ndim == 4: passes_threshold = np.any(passes_threshold, axis=-1) coords = np.array(np.where(passes_threshold)) start = coords.min(axis=1) end = coords.max(axis=1) + 1 if int(pad) > 0: pad_width = int(pad) # pad with one voxel to avoid resampling problems start = np.maximum(start - pad_width, 0) end = np.minimum(end + pad_width, data.shape[:3]) slices = [slice(s, e) for s, e in zip(start, end)] if return_slices: return slices if return_affine: return image_slices_to_affine(img, slices), end - start return crop_img_to(img, slices, copy=copy)