Deeplearn
- extract_image_patches(image, patch_size, max_number_of_patches='all', stride_length=1, mask_image=None, random_seed=None, return_as_array=False, randomize=True)[source]
Extract 2-D or 3-D image patches.
- Parameters:
image (
ants.core.ANTsImage) – Input image with one or more components.patch_size (
n-D tuple (depending on dimensionality).) – Width, height, and depth (if 3-D) of patches.max_number_of_patches (
intorstr) – Maximum number of patches returned. If “all” is specified, then all patches in sequence (defined by the stride_length are extracted.stride_length (
intortyping.Tuple) – Defines the sequential patch overlap for max_number_of_patches = “all”. Can be a image-dimensional vector or a scalar.mask_image (
ANTsImage (optional)) – Optional image specifying the sampling region for the patches when max_number_of_patches does not equal “all”. The way we constrain patch selection using a mask is by forcing each returned patch to have a masked voxel at its center.random_seed (
integer (optional)) – Seed value that allows reproducible patch extraction across runs.return_as_array (
bool) – Specifies the return type of the function. If False (default) the return type is a list where each element is a single patch. Otherwise the return type is an array of size dim( number_of_patches, patch_size ).randomize (
bool) – Boolean controlling whether we randomize indices when masking.
- Return type:
A list (or array)ofpatches.
Example
>>> import ants >>> image = ants.image_read(ants.get_ants_data('r16')) >>> image_patches = extract_image_patches(image, patch_size=(32, 32))
- reconstruct_image_from_patches(patches, domain_image, stride_length=1, domain_image_is_mask=False)[source]
Reconstruct image from a list of patches.
- Parameters:
patches (
listornumpy.ndarrayofpatches) – List or array of patches defining an image. Patches are assumed to have the same format as returned by extract_image_patches.domain_image (
ANTs image) – Image or mask to define the geometric information of the reconstructed image. If this is a mask image, the reconstruction will only use patches in the mask.stride_length (
intortyping.Tuple) – Defines the sequential patch overlap for max_number_of_patches = “all”. Can be a image-dimensional vector or a scalar.domain_image_is_mask (
bool) – Boolean specifying whether the domain image is a mask used to limit the region of reconstruction from the patches.
- Return type:
An ANTs image.
Example
>>> import ants >>> image = ants.image_read(ants.get_ants_data('r16')) >>> image_patches = extract_image_patches(image, patch_size=(16, 16), stride_length=4) >>> reconstructed_image = reconstruct_image_from_patches(image_patches, image, stride_length=4)
- regression_match_image(source_image, reference_image, mask=None, poly_order=1, truncate=True)[source]
Image intensity normalization using linear regression.
- Parameters:
source_image (
ants.core.ANTsImage) – Image whose intensities are matched to the reference image.reference_image (
ants.core.ANTsImage) – Defines the reference intensity function.poly_order (
int) – Polynomial order of fit. Default is 1 (linear fit).mask (
ants.core.ANTsImage) – Defines voxels for regression modeling.truncate (
bool) – Turns on/off the clipping of intensities.
- Return type:
ANTs image (i.e.,source_image) matchedtothe (reference_image).
Example
>>> import ants >>> source_image = ants.image_read(ants.get_ants_data('r16')) >>> reference_image = ants.image_read(ants.get_ants_data('r64')) >>> matched_image = regression_match_image(source_image, reference_image)
- crop_image_center(image, crop_size)[source]
Crop the center of an image.
- Parameters:
image (
ants.core.ANTsImage) – Input imagecrop_size (
typing.Tuple) – Width, height, depth (if 3-D), and time (if 4-D) of crop region.
- Return type:
ANTs image.
Example
>>> import ants >>> image = ants.image_read(ants.get_ants_data('r16')) >>> cropped_image = crop_image_center(image, crop_size=(64, 64))
- pad_or_crop_image_to_size(image, size)[source]
Pad or crop an image to a specified size
- Parameters:
image (
ants.core.ANTsImage) – Input imagesize (
tuple) – size of output image
- Return type:
A croppedorpadded image
Example
>>> import ants >>> image = ants.image_read(ants.get_ants_data('r16')) >>> padded_image = pad_or_crop_image_to_size(image, (333, 333))
- pad_image_by_factor(image, factor)[source]
Pad an image based on a factor.
Pad image of size (x, y, z) to (x’, y’, z’) where (x’, y’, z’) is a divisible by a user-specified factor.
- Parameters:
image (
ants.core.ANTsImage) – Input imagefactor (
scalarortyping.Tuple) – Padding factor
- Return type:
ANTs image.
Example
>>> import ants >>> image = ants.image_read(ants.get_ants_data('r16')) >>> padded_image = pad_image_by_factor(image, factor=4)
- histogram_warp_image_intensities(image, break_points=(0.25, 0.5, 0.75), displacements=None, clamp_end_points=(False, False), sd_displacements=0.05, transform_domain_size=20)[source]
Transform image intensities based on histogram mapping.
Apply B-spline 1-D maps to an input image for intensity warping.
- Parameters:
image (
ants.core.ANTsImage) – Input image.break_points (
intortuple) – Parametric points at which the intensity transform displacements are specified between [0, 1]. Alternatively, a single number can be given and the sequence is linearly spaced in [0, 1].displacements (
tuple) – displacements to define intensity warping. Length must be equal to the breakPoints. Alternatively, if None random displacements are chosen (random normal: mean = 0, sd = sd_displacements).sd_displacements (
float) – Characterize the randomness of the intensity displacement.clamp_end_points (
2-element tupleofbooleans) – Specify non-zero intensity change at the ends of the histogram.transform_domain_size (
int) – Defines the sampling resolution of the B-spline warping.
- Return type:
ANTs image
Example
>>> import ants >>> image = ants.image_read(ants.get_ants_data("r64")) >>> transformed_image = histogram_warp_image_intensities( image )
- simulate_bias_field(domain_image, number_of_points=10, sd_bias_field=1.0, number_of_fitting_levels=4, mesh_size=1)[source]
Simulate random bias field
Low frequency, spatial varying simulated random bias field using random points and B-spline fitting.
- Parameters:
domain_image (
ants.core.ANTsImage) – Image to define the spatial domain of the bias field.number_of_points (
int) – Number of randomly defined points to define the bias field (default = 10).sd_bias_field (
float) – Characterize the standard deviation of the amplitude (default = 1).number_of_fitting_levels (
int) – B-spline fitting parameter.
- Return type:
ANTs image
Example
>>> import ants >>> import numpy as np >>> image = ants.image_read(ants.get_ants_data("r64")) >>> log_field = ants.simulate_bias_field(image, number_of_points=10, sd_bias_field=1.0, ... number_of_fitting_levels=2, mesh_size=10) >>> log_field = log_field.iMath("Normalize") >>> field_array = np.power(np.exp(log_field.numpy()), 4) >>> image = image * ants.from_numpy(field_array, origin=image.origin, ... spacing=image.spacing, direction=image.direction)
- randomly_transform_image_data(reference_image, input_image_list, segmentation_image_list=None, number_of_simulations=10, transform_type='affine', sd_affine=0.02, deformation_transform_type='bspline', number_of_random_points=1000, sd_noise=10.0, number_of_fitting_levels=4, mesh_size=1, sd_smoothing=4.0, input_image_interpolator='linear', segmentation_image_interpolator='nearestNeighbor')[source]
Randomly transform image data (optional: with corresponding segmentations).
Apply rigid, affine and/or deformable maps to an input set of training images. The reference image domain defines the space in which this happens.
- Parameters:
reference_image (
ants.core.ANTsImage) – Defines the spatial domain for all output images. If the input images do not match the spatial domain of the reference image, we internally resample the target to the reference image. This could have unexpected consequences. Resampling to the reference domain is performed by testing using ants.image_physical_space_consistency then calling ants.resample_image_to_target with failure.input_image_list (
listoflistsofANTsImages) – List of lists of input images to warp. The internal list sets contain one or more images (per subject) which are assumed to be mutually aligned. The outer list contains multiple subject lists which are randomly sampled to produce output image list.segmentation_image_list (
listofANTsImages) – List of segmentation images corresponding to the input image list (optional).number_of_simulations (
int) – Number of simulated output image sets.transform_type (
str) – One of the following options: “translation”, “rotation”, “rigid”, “scaleShear”, “affine”, “deformation”, “affineAndDeformation”.sd_affine (
float) – Parameter dictating deviation amount from identity for random linear transformations.deformation_transform_type (
str) – “bspline” or “exponential”.number_of_random_points (
int) – Number of displacement points for the deformation field.sd_noise (
float) – Standard deviation of the displacement field.number_of_fitting_levels (
int) – Number of fitting levels (bspline deformation only).mesh_size (
intortyping.Tuple) – Determines fitting resolution (bspline deformation only).sd_smoothing (
float) – Standard deviation of the Gaussian smoothing in mm (exponential field only).input_image_interpolator (
str) – One of the following options: “nearestNeighbor”, “linear”, “gaussian”, “bSpline”.segmentation_image_interpolator (
str) – Only “nearestNeighbor” is currently available.
- Return type:
listoflistsoftransformed images
Example
>>> import ants >>> image1_list = list() >>> image1_list.append(ants.image_read(ants.get_ants_data("r16"))) >>> image2_list = list() >>> image2_list.append(ants.image_read(ants.get_ants_data("r64"))) >>> input_segmentations = list() >>> input_segmentations.append(ants.threshold_image(image1, "Otsu", 3)) >>> input_segmentations.append(ants.threshold_image(image2, "Otsu", 3)) >>> input_images = list() >>> input_images.append(image1_list) >>> input_images.append(image2_list) >>> data = antspynet.randomly_transform_image_data(image1, >>> input_images, input_segmentations, sd_affine=0.02, >>> transform_type = "affineAndDeformation" )
- data_augmentation(input_image_list, segmentation_image_list=None, pointset_list=None, number_of_simulations=10, reference_image=None, transform_type='affineAndDeformation', noise_model='additivegaussian', noise_parameters=(0.0, 0.05), sd_simulated_bias_field=1.0, sd_histogram_warping=0.05, sd_affine=0.05, sd_deformation=0.2, output_numpy_file_prefix=None, verbose=False)[source]
Randomly transform image data.
Given an input image list (possibly multi-modal) and an optional corresponding segmentation image list, this function will perform data augmentation with the following augmentation possibilities:
spatial transformations
added image noise
simulated bias field
histogram warping
- Parameters:
input_image_list (
listoflistsofANTsImages) – List of lists of input images to warp. The internal list sets contain one or more images (per subject) which are assumed to be mutually aligned. The outer list contains multiple subject lists which are randomly sampled to produce output image list.segmentation_image_list (
listofANTsImages) – List of segmentation images corresponding to the input image list (optional).pointset_list (
listofpointsets) – Numpy arrays corresponding to the input image list (optional). If using this option, the transform_type must be invertible.number_of_simulations (
int) – Number of simulated output image sets. Default = 10.reference_image (
ants.core.ANTsImage) – Defines the spatial domain for all output images. If one is not specified, we used the first image in the input image list.transform_type (
str) – One of the following options: “translation”, “rigid”, “scaleShear”, “affine”, “deformation”, “affineAndDeformation”.noise_model (
str) – ‘additivegaussian’, ‘saltandpepper’, ‘shot’, and ‘speckle’. Alternatively, one can specify a tuple or list of one or more of the options and one is selected at random with reasonable, randomized parameters. Note that the “speckle” model takes much longer than the others.noise_parameters (
tupleornumpy.ndarrayorfloat) – ‘additivegaussian’: (mean, standardDeviation) ‘saltandpepper’: (probability, saltValue, pepperValue) ‘shot’: scale ‘speckle’: standardDeviation Note that the standard deviation, scale, and probability values are max values and are randomly selected in the range [0, noise_parameter]. Also, the “mean”, “saltValue” and “pepperValue” are assumed to be in the intensity normalized range of [0, 1].sd_simulated_bias_field (
float) – Characterize the standard deviation of the amplitude.sd_histogram_warping (
float) – Determines the strength of the bias field.sd_affine (
float) – Determines the amount of affine transformation.sd_deformation (
float) – Determines the amount of deformable transformation.output_numpy_file_prefix (
str) – Filename of output numpy array containing all the simulated images and segmentations.
- Return type:
listoflistsoftransformed images and/or outputstoa numpy array.
Example
>>> image1_list = list() >>> image1_list.append(ants.image_read(ants.get_ants_data("r16"))) >>> image2_list = list() >>> image2_list.append(ants.image_read(ants.get_ants_data("r64"))) >>> segmentation1 = ants.threshold_image(image1_list[0], "Otsu", 3) >>> segmentation2 = ants.threshold_image(image2_list[0], "Otsu", 3) >>> input_segmentations = list() >>> input_segmentations.append(segmentation1) >>> input_segmentations.append(segmentation2) >>> points1 = ants.get_centroids(segmentation1)[:,0:2] >>> points2 = ants.get_centroids(segmentation2)[:,0:2] >>> input_points = list() >>> input_points.append(points1) >>> input_points.append(points2) >>> input_images = list() >>> input_images.append(image1_list) >>> input_images.append(image2_list) >>> data = data_augmentation(input_images, input_segmentations, input_points, tranform_type="scaleShear")