Python SimpleITK.Transform() Examples
The following are 12
code examples of SimpleITK.Transform().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
SimpleITK
, or try the search function
.
Example #1
Source File: download_IXI_Guys.py From DLTK with Apache License 2.0 | 8 votes |
def resample_image(itk_image, out_spacing=[1.0, 1.0, 1.0], is_label=False): original_spacing = itk_image.GetSpacing() original_size = itk_image.GetSize() out_size = [ int(np.round(original_size[0] * (original_spacing[0] / out_spacing[0]))), int(np.round(original_size[1] * (original_spacing[1] / out_spacing[1]))), int(np.round(original_size[2] * (original_spacing[2] / out_spacing[2]))) ] resample = sitk.ResampleImageFilter() resample.SetOutputSpacing(out_spacing) resample.SetSize(out_size) resample.SetOutputDirection(itk_image.GetDirection()) resample.SetOutputOrigin(itk_image.GetOrigin()) resample.SetTransform(sitk.Transform()) resample.SetDefaultPixelValue(itk_image.GetPixelIDValue()) if is_label: resample.SetInterpolator(sitk.sitkNearestNeighbor) else: resample.SetInterpolator(sitk.sitkBSpline) return resample.Execute(itk_image)
Example #2
Source File: download_IXI_HH.py From DLTK with Apache License 2.0 | 6 votes |
def resample_image(itk_image, out_spacing=(1.0, 1.0, 1.0), is_label=False): original_spacing = itk_image.GetSpacing() original_size = itk_image.GetSize() out_size = [int(np.round(original_size[0] * (original_spacing[0] / out_spacing[0]))), int(np.round(original_size[1] * (original_spacing[1] / out_spacing[1]))), int(np.round(original_size[2] * (original_spacing[2] / out_spacing[2])))] resample = sitk.ResampleImageFilter() resample.SetOutputSpacing(out_spacing) resample.SetSize(out_size) resample.SetOutputDirection(itk_image.GetDirection()) resample.SetOutputOrigin(itk_image.GetOrigin()) resample.SetTransform(sitk.Transform()) resample.SetDefaultPixelValue(itk_image.GetPixelIDValue()) if is_label: resample.SetInterpolator(sitk.sitkNearestNeighbor) else: resample.SetInterpolator(sitk.sitkBSpline) return resample.Execute(itk_image)
Example #3
Source File: data_reader.py From NiftyMIC with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _get_sitk_transform_from_filepath(self, path_to_sitk_transform): # Read transform as type sitk.Transform transform_sitk = sitk.ReadTransform(path_to_sitk_transform) # Convert transform to respective type, e.g. Euler, Affine etc # Third line in *.tfm file contains information on the transform type with open(path_to_sitk_transform) as f: content = f.readlines() transform_type = content[2] transform_type = re.sub("\n", "", transform_type) transform_type = transform_type.split(" ")[1] transform_sitk = self._transform_type[transform_type](transform_sitk) return transform_sitk ## # Reads slice transformations stored in the format 'filename_slice#.tfm'. # # Rationale: Read only slice transformations associated with # 'motion_correction' export achieved by the volumetric reconstruction # algorithm # \date 2018-01-31 19:16:00+0000 #
Example #4
Source File: processing.py From istn with Apache License 2.0 | 6 votes |
def reorient_image(image): """Reorients an image to standard radiology view.""" dir = np.array(image.GetDirection()).reshape(len(image.GetSize()), -1) ind = np.argmax(np.abs(dir), axis=0) new_size = np.array(image.GetSize())[ind] new_spacing = np.array(image.GetSpacing())[ind] new_extent = new_size * new_spacing new_dir = dir[:, ind] flip = np.diag(new_dir) < 0 flip_diag = flip * -1 flip_diag[flip_diag == 0] = 1 flip_mat = np.diag(flip_diag) new_origin = np.array(image.GetOrigin()) + np.matmul(new_dir, (new_extent * flip)) new_dir = np.matmul(new_dir, flip_mat) resample = sitk.ResampleImageFilter() resample.SetOutputSpacing(new_spacing.tolist()) resample.SetSize(new_size.tolist()) resample.SetOutputDirection(new_dir.flatten().tolist()) resample.SetOutputOrigin(new_origin.tolist()) resample.SetTransform(sitk.Transform()) resample.SetDefaultPixelValue(image.GetPixelIDValue()) resample.SetInterpolator(sitk.sitkNearestNeighbor) return resample.Execute(image)
Example #5
Source File: preprocessing.py From gdl-fire-4d with GNU General Public License v3.0 | 6 votes |
def interpolate_image(image, spacing_new, default_voxel_value=0): original_spacing = image.GetSpacing() original_size = image.GetSize() new_size = [ round(original_size[0] * (original_spacing[0] / spacing_new[0])), round(original_size[1] * (original_spacing[1] / spacing_new[1])), round(original_size[2] * (original_spacing[2] / spacing_new[2])) ] log.debug(f'Got image with spacing {original_spacing} and size ' \ f'{original_size}. New spacing is {spacing_new}, new size ' \ f'is {new_size} (before padding).') return sitk.Resample(image, new_size, sitk.Transform(), sitk.sitkLinear, image.GetOrigin(), spacing_new, image.GetDirection(), default_voxel_value, image.GetPixelID())
Example #6
Source File: processing.py From istn with Apache License 2.0 | 5 votes |
def resample_image(image, out_spacing=(1.0, 1.0, 1.0), out_size=None, is_label=False, pad_value=0): """Resamples an image to given element spacing and output size.""" original_spacing = np.array(image.GetSpacing()) original_size = np.array(image.GetSize()) if out_size is None: out_size = np.round(np.array(original_size * original_spacing / np.array(out_spacing))).astype(int) else: out_size = np.array(out_size) original_direction = np.array(image.GetDirection()).reshape(len(original_spacing),-1) original_center = (np.array(original_size, dtype=float) - 1.0) / 2.0 * original_spacing out_center = (np.array(out_size, dtype=float) - 1.0) / 2.0 * np.array(out_spacing) original_center = np.matmul(original_direction, original_center) out_center = np.matmul(original_direction, out_center) out_origin = np.array(image.GetOrigin()) + (original_center - out_center) resample = sitk.ResampleImageFilter() resample.SetOutputSpacing(out_spacing) resample.SetSize(out_size.tolist()) resample.SetOutputDirection(image.GetDirection()) resample.SetOutputOrigin(out_origin.tolist()) resample.SetTransform(sitk.Transform()) resample.SetDefaultPixelValue(pad_value) if is_label: resample.SetInterpolator(sitk.sitkNearestNeighbor) else: #resample.SetInterpolator(sitk.sitkBSpline) resample.SetInterpolator(sitk.sitkLinear) return resample.Execute(sitk.Cast(image, sitk.sitkFloat32))
Example #7
Source File: sitk_utils.py From 3DUnetCNN with MIT License | 5 votes |
def sitk_resample_to_image(image, reference_image, default_value=0., interpolator=sitk.sitkLinear, transform=None, output_pixel_type=None): if transform is None: transform = sitk.Transform() transform.SetIdentity() if output_pixel_type is None: output_pixel_type = image.GetPixelID() resample_filter = sitk.ResampleImageFilter() resample_filter.SetInterpolator(interpolator) resample_filter.SetTransform(transform) resample_filter.SetOutputPixelType(output_pixel_type) resample_filter.SetDefaultPixelValue(default_value) resample_filter.SetReferenceImage(reference_image) return resample_filter.Execute(image)
Example #8
Source File: sitk_utils.py From 3D-CNNs-for-Liver-Classification with Apache License 2.0 | 5 votes |
def sitk_resample_to_image(image, reference_image, default_value=0., interpolator=sitk.sitkLinear, transform=None, output_pixel_type=None): if transform is None: transform = sitk.Transform() transform.SetIdentity() if output_pixel_type is None: output_pixel_type = image.GetPixelID() resample_filter = sitk.ResampleImageFilter() resample_filter.SetInterpolator(interpolator) resample_filter.SetTransform(transform) resample_filter.SetOutputPixelType(output_pixel_type) resample_filter.SetDefaultPixelValue(default_value) resample_filter.SetReferenceImage(reference_image) return resample_filter.Execute(image)
Example #9
Source File: common.py From MedicalDataAugmentationTool with GNU General Public License v3.0 | 5 votes |
def create_composite(dim, transformations, merge_affine=False): """ Creates a composite sitk transform based on a list of sitk transforms. :param dim: The dimension of the transformation. :param transformations: A list of sitk transforms. :param merge_affine: If true, merge affine transformations before calculating the composite transformation. :return: The composite sitk transform. """ if merge_affine: merged_transformations = [] combined_matrix = None for transformation in transformations: if isinstance(transformation, sitk.AffineTransform): if combined_matrix is None: combined_matrix = np.eye(dim + 1) current_matrix = get_affine_homogeneous_matrix(dim, transformation) combined_matrix = current_matrix @ combined_matrix else: if combined_matrix is not None: matrix, translation = get_affine_matrix_and_translation(dim, combined_matrix) combined_affine_transform = sitk.AffineTransform(dim) combined_affine_transform.SetMatrix(matrix) combined_affine_transform.SetTranslation(translation) merged_transformations.append(combined_affine_transform) merged_transformations.append(transformation) combined_matrix = None if combined_matrix is not None: matrix, translation = get_affine_matrix_and_translation(dim, combined_matrix) combined_affine_transform = sitk.AffineTransform(dim) combined_affine_transform.SetMatrix(matrix) combined_affine_transform.SetTranslation(translation) merged_transformations.append(combined_affine_transform) transformations = merged_transformations compos = sitk.Transform(dim, sitk.sitkIdentity) for transformation in transformations: compos.AddTransform(transformation) return compos
Example #10
Source File: center_line_at_y_axis.py From MedicalDataAugmentationTool with GNU General Public License v3.0 | 5 votes |
def get(self, **kwargs): """ Returns the sitk transform based on the given parameters. :param kwargs: Must contain 'image', which defines the input image. Must contain 'line', which defines the lin in the input image to center and scale. Must contain 'output_size' and 'output_spacing', which define the output image physical space. :return: The sitk.Transform(). """ if self.dim == 2: return self.get_2d(**kwargs) elif self.dim == 3: return self.get_3d(**kwargs)
Example #11
Source File: sitk_utils.py From Keras-Brats-Improved-Unet3d with MIT License | 5 votes |
def sitk_resample_to_image(image, reference_image, default_value=0., interpolator=sitk.sitkLinear, transform=None, output_pixel_type=None): if transform is None: transform = sitk.Transform() transform.SetIdentity() if output_pixel_type is None: output_pixel_type = image.GetPixelID() resample_filter = sitk.ResampleImageFilter() resample_filter.SetInterpolator(interpolator) resample_filter.SetTransform(transform) resample_filter.SetOutputPixelType(output_pixel_type) resample_filter.SetDefaultPixelValue(default_value) resample_filter.SetReferenceImage(reference_image) return resample_filter.Execute(image)
Example #12
Source File: random_affine.py From torchio with MIT License | 4 votes |
def apply_affine_transform( self, tensor: torch.Tensor, affine: np.ndarray, scaling_params: List[float], rotation_params: List[float], translation_params: List[float], interpolation: Interpolation, center_lps: Optional[TypeTripletFloat] = None, ) -> torch.Tensor: assert tensor.ndim == 4 assert len(tensor) == 1 image = self.nib_to_sitk(tensor[0], affine) floating = reference = image scaling_transform = self.get_scaling_transform( scaling_params, center_lps=center_lps, ) rotation_transform = self.get_rotation_transform( rotation_params, translation_params, center_lps=center_lps, ) transform = sitk.Transform(3, sitk.sitkComposite) transform.AddTransform(scaling_transform) transform.AddTransform(rotation_transform) if self.default_pad_value == 'minimum': default_value = tensor.min().item() elif self.default_pad_value == 'mean': default_value = get_borders_mean(image, filter_otsu=False) elif self.default_pad_value == 'otsu': default_value = get_borders_mean(image, filter_otsu=True) else: default_value = self.default_pad_value resampler = sitk.ResampleImageFilter() resampler.SetInterpolator(get_sitk_interpolator(interpolation)) resampler.SetReferenceImage(reference) resampler.SetDefaultPixelValue(float(default_value)) resampler.SetOutputPixelType(sitk.sitkFloat32) resampler.SetTransform(transform) resampled = resampler.Execute(floating) np_array = sitk.GetArrayFromImage(resampled) np_array = np_array.transpose() # ITK to NumPy tensor[0] = torch.from_numpy(np_array) return tensor