本文整理汇总了Python中SimpleITK.Transform方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleITK.Transform方法的具体用法?Python SimpleITK.Transform怎么用?Python SimpleITK.Transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SimpleITK
的用法示例。
在下文中一共展示了SimpleITK.Transform方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resample_image
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Transform [as 别名]
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)
示例2: resample_image
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Transform [as 别名]
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)
示例3: _get_sitk_transform_from_filepath
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Transform [as 别名]
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
#
示例4: interpolate_image
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Transform [as 别名]
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())
示例5: reorient_image
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Transform [as 别名]
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)
示例6: resample_image
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Transform [as 别名]
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))
示例7: sitk_resample_to_image
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Transform [as 别名]
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)
示例8: create_composite
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Transform [as 别名]
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
示例9: get
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Transform [as 别名]
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)
示例10: apply_affine_transform
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Transform [as 别名]
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