atldld.base module

Fundamental building blocks of the project.

class DisplacementField(delta_x, delta_y)[source]

Bases: object

A class representing a 2D displacement vector field.

Notes

The dtype is enforced to be single-precision (float32) since opencv’s remap function (used for warping) does not accept double-precision (float64).

delta_x

A 2D array of dtype float32 that represents the displacement field in the x coordinate (columns). Positive values move the pixel to the right, negative move it to the left.

Type:

np.ndarray

delta_y

A 2D array of dtype float32 that represents the displacement field in the y coordinate (rows). Positive values move the pixel down, negative pixels move the pixels up.

Type:

np.ndarray

classmethod from_file(file_path)[source]

Load displacement field from a file.

Parameters:

file_path (str or pathlib.Path) – Path to where the file is located.

Returns:

Instance of the Displacement field.

Return type:

DisplacementField

classmethod from_transform(f_x, f_y)[source]

Instantiate displacement field from actual transformations.

Parameters:
  • f_x (np.array) – 2D array of shape (h, w) representing the x coordinate of the transformation.

  • f_y (np.array) – 2D array of shape (h, w) representing the y coordinate of the transformation.

Returns:

Instance of the Displacement field.

Return type:

DisplacementField

property norm

Norm for each pixel.

save(path)[source]

Save displacement field as a .npy file.

Notes

Can be loaded via DisplacementField.from_file class method.

Parameters:

path (str or pathlib.Path) – Path to the file. Needs to end with .npy.

property transformation

Output the actual transformation rather than the displacement field.

Returns:

  • f_x (np.ndarray) – A 2D array of dtype float32. For each pixel in the fixed image what is the corresponding x coordinate in the moving image.

  • f_y (np.ndarray) – A 2D array of dtype float32. For each pixel in the fixed image what is the corresponding y coordinate in the moving image.

warp(img, interpolation='linear', border_mode='constant', c=0)[source]

Warp an input image based on the inner displacement field.

Parameters:
  • img (np.ndarray) – Input image to which we will apply the transformation. Note that certain dtypes (e.g. np.float64) are not supported by OpenCV.

  • interpolation (str, {'nearest', 'linear', 'cubic', 'area', 'lanczos'}) – Regular grid interpolation method to be used.

  • border_mode (str, {'constant', 'replicate', 'reflect',) –

  • 'wrap' – How to fill outside of the range values. See references for detailed explanation.

  • 'reflect101' – How to fill outside of the range values. See references for detailed explanation.

  • 'transparent'} – How to fill outside of the range values. See references for detailed explanation.

  • c (float) – Only used if border_mode=’constant’ and represents the fill value.

Returns:

warped_img – Warped image.

Return type:

np.ndarray

affine(shape, matrix)[source]

Affine transformation encoded in a 2 x 3 matrix.

Parameters:
  • shape (tuple) – Of the form (height, width).

  • matrix (np.ndarray) – Transformation matrix of the shape 2 x 3.

Raises:

ValueError – In case the transformation matrix has a wrong shape.

Returns:

  • delta_x (np.ndarray) – Displacement vector field of the x coordinates.

  • delta_y (np.ndarray) – Displacement vector field of the y coordinates.

affine_simple(shape, scale_x=1, scale_y=1, rotation=0, translation_x=0, translation_y=0, shear=0, apply_centering=True)[source]

Just a human version of affine mapping.

Notes

Instead of specifying the whole matrix one can just specify all the understandable quantities.

Parameters:
  • shape (tuple) – Of the form (height, width).

  • scale_x (float) – Scale on the x axis. If scale_x < 1 then zoom out, if scale_x > 1 zoom in.

  • scale_y (float) – Scale on the y axis. If scale_y < 1 then zoom out, if scale_y > 1 zoom in.

  • rotation (float) – Rotation angle in counter-clockwise direction as radians.

  • translation_x (float) – Translation in the x direction. If translation_x > 0 then to the right, else to the left.

  • translation_y (float) – Translation in the y direction. If translation_y > 0 then down, else to the up.

  • shear (float) – Shear angle in counter-clockwise direction as radians.

  • apply_centering (bool) – If True then (h // 2 - 0.5, w // 2 - 0.5) is considered a center of the image. And before performing all the other operations the image is first shifted so that the center corresponds to (0, 0). Then the actual transformation is applied and after that the image is shifted into the original center.

Returns:

  • delta_x (np.ndarray) – Displacement vector field of the x coordinates.

  • delta_y (np.ndarray) – Displacement vector field of the y coordinates.

Source

source/api/atldld.base.rst