mymesh.register.Image2Image#
- mymesh.register.Image2Image(img1, img2, T0=None, bounds=None, center='image', transform='rigid', metric='dice', method='direct', scale=1, interpolation_order=1, threshold=None, transform_args=None, optimizer_args=None, decimation=1, verbose=True, point_method='boundary')[source]#
Image registration for 2D or 3D images.
img2will be registered toimg1- Parameters:
img1 (array_like) – Image array of the fixed image. Two or three dimensional numpy array of image data
img2 (array_like) – image array of the moving image. Two or three dimensional numpy array of image data
T0 (array_like or NoneType, optional) – Initial transformation to apply to img2, by default, None. This can serve as an ‘initial guess’ of the alignment.
bounds (array_like or NoneType, optional) – Optimization bounds, formatted as [(min,max),…] for each parameter. If None, bounds are selected that should cover most possible transformations, by default None. Not used by all optimizers.
center (str, array_like, optional) –
Location of the center of rotation of the image. This will be used as the “center” input to the transformation model (e.g.
rigid(),affine()). If “center” is given in transform_args, that value will be used instead.’image’: Rotation about the center of the image,
np.shape(img)/2.[x,y,z] or [x,y]: A tow or three element list or array specifies the location, in voxels/pixels, of where to place to place the center of rotation
transform (str, optional) –
Transformation model, by default ‘rigid’. If 2d images are given, the transformation model will automatically be modified to the two dimensional variant (e.g. ‘rigid’ -> ‘rigid2d’)
’translation’: translations
’rotation’: rotations
’rigid’: translations and rotations
’similarity: translations, rotations, and uniform scaling
’affine’: translations, rotations, triaxial scaling, shearing
’translation2d’: translations in two dimensions
’rotation2d’: rotations in two dimensions
’rigid2’: translations and rotations in two dimensions
’similarity: translations, rotations, and uniform scaling in two dimensions
’affine’: translations, rotations, triaxial scaling, shearing in two dimensions
metric (str, optional) – Similarity metric to compare the two point clouds, by default ‘hausdorff’
method (str, optional) – Optimization method, by default ‘direct’. See
optimize()for details.scale (float, optional) – Scale factor used to resample the image to either reduce (scale < 1) or increase (scale > 1) the size/resolution of the image, by default 1. The returned image will still be of the original resolution.
interpolation_order (int, optional) – Interpolation order used in image transformation (see scipy.ndimage.affine_transform) and scaling (if used). Must be an integer in the range 0-5. Lower order is more efficient at the cost of quality. By default, 1.
threshold (NoneType, float, or tuple, optional) – Threshold value(s) to binarize the images. Images are binarized by img > threshold. If given as a float or scalar value, this threshold value is applied to both images. If given as a two-element tuple (or array_like), the first value is applied to img1 and the second value is paplied to img2. If None, the image is assumed to already be binarized (or doesn’t require binarization, depending on which similarity metric is chosen). Images can be binarized arbitrarily, consisting of True/False, 1/0, 255/0, etc. If the image is not already binarized and no threshold is given, the threshold value will be the midpoint of the range of values for each image (which may not give the intended result). By default, None.
transform_args (dict, optional) –
Optional input arguments passed to scipy.ndimage.affine_transform, by default dict(mode=’constant’, order=interpolation_order).
optimizer_args (dict, optional) – Additional arguments for the chosen optimizer, by default None. See
optimize()for details.verbose (bool, optional) – Verbosity, by default True. If True, iteration progress will be printed.
point_method (str, optional) –
Method for converting image to points if using a point-based method, by default ‘boundary’.
’boundary’ : The voxels of the boundaries of the thresholeded image wil be converted to points
’binary’ : All thresholded voxels will be converted to points
’skeleton’ : The morphological skeleton of the thresholded image will be converted to points
- Returns:
new_img (np.ndarray) – Transformed image array of img2 registered to img1.
x (np.ndarray) – Transformation parameters for the transformation that registers img2 to img1.
Examples
import mymesh from mymesh import register import numpy as np import matplotlib.pyplot as plt # Load two the CT scan of the Stanford Bunny img1 = mymesh.demo_image('bunny') thresh = 100 # Perform an arbitrary rotation to the copy R = register.rotation([np.pi/6, -np.pi/6, np.pi/6], center=np.array(img1.shape)/2) img2 = register.transform_image(img1, R) # Align the two meshes using the iterative closest point (ICP) algorithm img_aligned, T = register.Image2Image(img1, img2, method='icp', threshold=thresh)
Note
An overlay image can be displayed using
ImageOverlay(), e.g.import matplotlib.pyplot as plt overlay = register.ImageOverlay(img1, img_aligned, threshold=thresh) plt.imshow(overlay[:,250], cmap='inferno')