Registration/Alignment utilities

This code contains helper methods in using Elastix to perform section to section aligment. Elastix takes in many many different parameter settings. Below are some notes regarding one particular parameter.

Notes from the manual regarding MOMENTS vs GEOMETRY:

The CenteredTransformInitializer parameter supports two modes of operation. In the first mode, the centers of the images are computed as space coordinates using the image origin, size and spacing. The center of the fixed image is assigned as the rotational center of the transform while the vector going from the fixed image center to the moving image center is passed as the initial translation of the transform. In the second mode, the image centers are not computed geometrically but by using the moments of the intensity gray levels.

Keep in mind that the scale of units in rotation and translation is quite different. For example, here we know that the first element of the parameters array corresponds to the angle that is measured in radians, while the other parameters correspond to the translations that are measured in millimeters

library.utilities.utilities_registration.align_elastix(fixed, moving)

This takes the moving and fixed images runs Elastix on them. Note the huge list of parameters Elastix uses here.

Parameters:
  • fixed – sitk float array for the fixed image (the image behind the moving).

  • moving – sitk float array for the moving image.

Returns:

the Elastix transformation results that get parsed into the rigid transformation

library.utilities.utilities_registration.align_image_to_affine(file_key)

This is the method that takes the rigid transformation and uses PIL to align the image. This method takes about 20 seconds to run as compared to scikit’s version which takes 220 seconds to run on a full scale image.

Parameters:

file_key – tuple of file input and output

Returns:

nothing

library.utilities.utilities_registration.convert_2d_transform_forms(arr)

Helper method used by create_downsampled_transforms

Parameters:

arr – an array of data to vertically stack

Returns:

a numpy array

library.utilities.utilities_registration.create_downsampled_transforms(transforms: dict, downsample: bool) dict

Changes the dictionary of transforms to the correct resolution

Parameters:
  • animal – prep_id of animal we are working on animal

  • transforms – dictionary of filename:array of transforms

  • downsample – boolean: either true for thumbnails, false for full resolution images

Returns:

corrected dictionary of filename: array of transforms

library.utilities.utilities_registration.create_rigid_parameters(elastixImageFilter)

Creates the rigid paramaters used by Elastix. This sets lots of parameters in this dictionary and it used multiple places.

Parameters:

elastixImageFilter – object set in previous method for Elastix.

Returns:

dictionary of parameters

library.utilities.utilities_registration.create_scaled_transform(T)

Creates a transform (T) to the correct resolution

library.utilities.utilities_registration.parameters_to_rigid_transform(rotation, xshift, yshift, center)

Takes the rotation, xshift, yshift that were created by Elastix and stored in the elastix_transformation table. Creates a matrix of the rigid transformation.

Parameters:
  • rotation – a float designating the rotation

  • xshift – a float for showing how much the moving image shifts in the X direction.

  • yshift – a float for showing how much the moving image shifts in the Y direction.

  • center – tuple of floats showing the center of the image.

Returns:

the 3x3 rigid transformation

library.utilities.utilities_registration.rigid_transform_to_parmeters(transform, center)

convert a 2d transformation matrix (3*3) to the rotation angles, rotation center and translation This is used in the manual aligner notebook.

Args:

transform (array like): 3*3 array that stores the 2*2 transformation matrix and the 1*2 translation vector for a 2D image. the third row of the array is a place holder of values [0,0,1].

Returns:

float: x translation float: y translation float: rotation angle in arc list: lisf of x and y for rotation center

library.utilities.utilities_registration.tif_to_png(file_key)

This method creates a PNG from a TIF :param file_key: tuple of file input and output :return: nothing