本文整理汇总了Python中SimpleITK.Image方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleITK.Image方法的具体用法?Python SimpleITK.Image怎么用?Python SimpleITK.Image使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SimpleITK
的用法示例。
在下文中一共展示了SimpleITK.Image方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resample_images
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def resample_images(
image: sitk.Image,
transforms: List[sitk.Euler3DTransform],
interpolation: Interpolation,
) -> List[sitk.Image]:
floating = reference = image
default_value = np.float64(sitk.GetArrayViewFromImage(image).min())
transforms = transforms[1:] # first is identity
images = [image] # first is identity
for transform in transforms:
resampler = sitk.ResampleImageFilter()
resampler.SetInterpolator(get_sitk_interpolator(interpolation))
resampler.SetReferenceImage(reference)
resampler.SetOutputPixelType(sitk.sitkFloat32)
resampler.SetDefaultPixelValue(default_value)
resampler.SetTransform(transform)
resampled = resampler.Execute(floating)
images.append(resampled)
return images
示例2: get_rigid_transforms
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def get_rigid_transforms(
self,
degrees_params: np.ndarray,
translation_params: np.ndarray,
image: sitk.Image,
) -> List[sitk.Euler3DTransform]:
center_ijk = np.array(image.GetSize()) / 2
center_lps = image.TransformContinuousIndexToPhysicalPoint(center_ijk)
identity = np.eye(4)
matrices = [identity]
for degrees, translation in zip(degrees_params, translation_params):
radians = np.radians(degrees).tolist()
motion = sitk.Euler3DTransform()
motion.SetCenter(center_lps)
motion.SetRotation(*radians)
motion.SetTranslation(translation.tolist())
motion_matrix = self.transform_to_matrix(motion)
matrices.append(motion_matrix)
transforms = [self.matrix_to_transform(m) for m in matrices]
return transforms
示例3: add_artifact
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def add_artifact(
self,
image: sitk.Image,
transforms: List[sitk.Euler3DTransform],
times: np.ndarray,
interpolation: Interpolation,
):
images = self.resample_images(image, transforms, interpolation)
arrays = [sitk.GetArrayViewFromImage(im) for im in images]
arrays = [array.transpose() for array in arrays] # ITK to NumPy
spectra = [self.fourier_transform(array) for array in arrays]
self.sort_spectra(spectra, times)
result_spectrum = np.empty_like(spectra[0])
last_index = result_spectrum.shape[2]
indices = (last_index * times).astype(int).tolist()
indices.append(last_index)
ini = 0
for spectrum, fin in zip(spectra, indices):
result_spectrum[..., ini:fin] = spectrum[..., ini:fin]
ini = fin
result_image = np.real(self.inv_fourier_transform(result_spectrum))
return result_image.astype(np.float32)
示例4: sitk_to_nib
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def sitk_to_nib(image: sitk.Image) -> Tuple[np.ndarray, np.ndarray]:
data = sitk.GetArrayFromImage(image).transpose()
spacing = np.array(image.GetSpacing())
direction = np.array(image.GetDirection())
origin = image.GetOrigin()
if len(direction) == 9:
rotation = direction.reshape(3, 3)
elif len(direction) == 4: # ignore first dimension if 2D (1, 1, H, W)
rotation_2d = direction.reshape(2, 2)
rotation = np.eye(3)
rotation[1:3, 1:3] = rotation_2d
spacing = 1, *spacing
origin = 0, *origin
rotation = np.dot(FLIP_XY, rotation)
rotation_zoom = rotation * spacing
translation = np.dot(FLIP_XY, origin)
affine = np.eye(4)
affine[:3, :3] = rotation_zoom
affine[:3, 3] = translation
return data, affine
示例5: _add_slice_contribution
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def _add_slice_contribution(slice, coverage_sitk):
#
slice_sitk = sitk.Image(slice.sitk)
spacing = np.array(slice_sitk.GetSpacing())
spacing[-1] = slice.get_slice_thickness()
slice_sitk.SetSpacing(spacing)
contrib_nda = sitk.GetArrayFromImage(slice_sitk)
contrib_nda[:] = 1
contrib_sitk = sitk.GetImageFromArray(contrib_nda)
contrib_sitk.CopyInformation(slice_sitk)
coverage_sitk += sitk.Resample(
contrib_sitk,
coverage_sitk,
sitk.Euler3DTransform(),
sitk.sitkNearestNeighbor,
0,
coverage_sitk.GetPixelIDValue(),
)
return coverage_sitk
示例6: from_stack
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def from_stack(cls,
stack,
compute_brain_image=False,
compute_brain_mask=True,
compute_skull_image=False,
dir_tmp=os.path.join(DIR_TMP, "BrainExtractionTool")):
self = cls(compute_brain_image=compute_brain_image,
compute_brain_mask=compute_brain_mask,
compute_skull_image=compute_skull_image,
dir_tmp=dir_tmp)
self._stack = stack
self._sitk = sitk.Image(stack.sitk)
return self
##
# Sets the sitk image for brain stripping
# \date 2016-10-12 15:46:20+0100
#
# \param self The object
# \param sitk_image The sitk image as sitk.Image object
#
#
示例7: get_input_image_sitk
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def get_input_image_sitk(self):
if self._sitk is None:
raise ValueError("Input image was not read yet.")
return sitk.Image(self._sitk)
##
# Gets the brain masked stack.
# \date 2018-01-18 00:44:49+0000
#
# \param self The object
# \param filename The filename
# \param extract_slices Extract slices of stack; boolean
#
# \return Returns image as Stack object holding obtained brain mask
#
示例8: __init__
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def __init__(self,
srr_mask_sitk,
suffix="_mask",
sigma=2,
lower=0.5,
upper=100,
):
if not isinstance(srr_mask_sitk, sitk.Image):
raise ValueError("Input must be of type sitk.Image")
self._srr_mask_sitk = srr_mask_sitk
self._suffix = suffix
self._sigma = sigma
self._lower = lower
self._upper = upper
self._mask_sitk = None
self._mask = None
示例9: verify_same_geometry
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def verify_same_geometry(img_1: sitk.Image, img_2: sitk.Image):
ori1, spacing1, direction1, size1 = img_1.GetOrigin(), img_1.GetSpacing(), img_1.GetDirection(), img_1.GetSize()
ori2, spacing2, direction2, size2 = img_2.GetOrigin(), img_2.GetSpacing(), img_2.GetDirection(), img_2.GetSize()
same_ori = np.all(np.isclose(ori1, ori2))
if not same_ori: print("the origin does not match between the images")
same_spac = np.all(np.isclose(spacing1, spacing2))
if not same_spac: print("the spacing does not match between the images")
same_dir = np.all(np.isclose(direction1, direction2))
if not same_dir: print("the direction does not match between the images")
same_size = np.all(np.isclose(size1, size2))
if not same_size: print("the size does not match between the images")
if same_ori and same_spac and same_dir and same_size:
return True
else:
return False
示例10: _get_subject_from_tensor
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def _get_subject_from_tensor(tensor: torch.Tensor) -> Subject:
subject_dict = {}
for channel_index, channel_tensor in enumerate(tensor):
name = f'channel_{channel_index}'
image = Image(tensor=channel_tensor, type=INTENSITY)
subject_dict[name] = image
subject = Subject(subject_dict)
dataset = ImagesDataset([subject])
sample = dataset[0]
return sample
示例11: sitk_to_nib
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def sitk_to_nib(image: sitk.Image):
return sitk_to_nib(image)
示例12: get_bspline_transform
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def get_bspline_transform(
image: sitk.Image,
num_control_points: Tuple[int, int, int],
coarse_field: np.ndarray,
) -> sitk.BSplineTransformInitializer:
mesh_shape = [n - SPLINE_ORDER for n in num_control_points]
bspline_transform = sitk.BSplineTransformInitializer(image, mesh_shape)
parameters = coarse_field.flatten(order='F').tolist()
bspline_transform.SetParameters(parameters)
return bspline_transform
示例13: as_sitk
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def as_sitk(self) -> sitk.Image:
return nib_to_sitk(self[DATA][0], self[AFFINE])
示例14: crop
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def crop(self, index_ini, index_fin):
new_origin = nib.affines.apply_affine(self.affine, index_ini)
new_affine = self.affine.copy()
new_affine[:3, 3] = new_origin
i0, j0, k0 = index_ini
i1, j1, k1 = index_fin
patch = self.data[0, i0:i1, j0:j1, k0:k1].clone()
return Image(tensor=patch, affine=new_affine, type=self.type)
示例15: apply_transform_to_file
# 需要导入模块: import SimpleITK [as 别名]
# 或者: from SimpleITK import Image [as 别名]
def apply_transform_to_file(
input_path: TypePath,
transform, # : Transform seems to create a circular import (TODO)
output_path: TypePath,
type: str = INTENSITY,
):
from . import Image, ImagesDataset, Subject
subject = Subject(image=Image(input_path, type))
dataset = ImagesDataset([subject], transform=transform)
transformed = dataset[0]
dataset.save_sample(transformed, dict(image=output_path))