lezargus.library.transform module#

Array or image transformations, typically affine transformations.

The transform of images and arrays are important, and here we separate many similar functions into this module.

lezargus.library.transform.affine_transform(array: hint.NDArray, matrix: hint.NDArray, offset: hint.NDArray | None = None, constant: float | tuple = nan) hint.NDArray[source]#

Execute an affine transformation on an array.

This function only handles images.

Parameters:
  • array (ndarray) – The input array to be transformed.

  • matrix (ndarray) – The transformation matrix. It may be homogenous, and if so, any input offset is ignored.

  • offset (ndarray) – The translation offset of the affine transformation, specified if a homogenous matrix is not provided.

  • constant (float | tuple, default = np.nan) – If the mode is constant, the constant value used is this value. Because we use OpenCV in the backend, a tuple representing a OpenCV Scalar may be provided.

Returns:

transformed_array – The affine transformed array/image.

Return type:

ndarray

lezargus.library.transform.calculate_affine_matrix(in_points: hint.NDArray, out_points: hint.NDArray) hint.NDArray[source]#

Calculate the homogeneous affine transformation matrix from points.

Provided a set of input and output point coordinates, and assuming an affine transformation between them, we calculate the optimal affine transformation as defined by a homogeneous affine transformation matrix. Generally, more than three pairs of points are provided and so we just find the best fit.

Parameters:
  • in_points (NDArray) – The set of input points, as an NxD array, for N number of points of D dimensions. Basically, the points transforming from to the output. Input and output should be parallel.

  • out_points (NDArray) – The set of output points, as an NxD array, for N number of points of D dimensions. Basically, the points after the transform, from the input. Input and output should be parallel.

Returns:

homogeneous_matrix – The best fitting homogeneous affine transformation matrix.

Return type:

NDArray

lezargus.library.transform.crop_2d(array: hint.NDArray, new_shape: tuple, location: str | tuple = 'center', use_pillow: bool = False) hint.NDArray[source]#

Crop a 2D image array.

Parameters:
  • array (ndarray) – The input array to be cropped.

  • new_shape (tuple) – The new shape of the array after cropping.

  • location (str | tuple, default = "center") –

    The central location of the crop, provided as either a pixel coordinate or an instruction as follows:

    • center : The center of the array.

  • use_pillow (bool, default = False) – If True, we use the PIL/Pillow module to determine the crop.

Returns:

crop – The cropped array.

Return type:

ndarray

lezargus.library.transform.crop_3d(array: hint.NDArray, new_shape: tuple, location: str | tuple = 'center', use_pillow: bool = False) hint.NDArray[source]#

Crop a 3D image array.

Parameters:
  • array (ndarray) – The input array to be cropped.

  • new_shape (tuple) – The new shape of the array after cropping.

  • location (str | tuple, default = "center") –

    The central location of the crop, provided as either a pixel coordinate or an instruction as follows:

    • center : The center of the array.

  • use_pillow (bool, default = False) – If True, we use the PIL/Pillow module to determine the crop.

Returns:

crop – The cropped array.

Return type:

ndarray

lezargus.library.transform.rotate_2d(array: hint.NDArray, rotation: float, mode: str = 'constant', constant: float = nan) hint.NDArray[source]#

Rotate a 2D image array.

This function is a connivent wrapper around scipy’s function.

Parameters:
  • array (ndarray) – The input array to be rotated.

  • rotation (float) – The rotation angle, in radians.

  • mode (str, default = "constant") – The padding mode of the translation. It must be one of the following. The implementation detail is similar to Scipy’s. See scipy.ndimage.shift() for more information.

  • constant (float, default = np.nan) – If the mode is constant, the constant value used is this value.

Returns:

rotated_array – The rotated array/image.

Return type:

ndarray

lezargus.library.transform.translate_2d(array: hint.NDArray, x_shift: float, y_shift: float, mode: str = 'constant', constant: float = nan) hint.NDArray[source]#

Translate a 2D image array.

This function is a convenient wrapper around Scipy’s function.

Parameters:
  • array (ndarray) – The input array to be translated.

  • x_shift (float) – The number of pixels that the array is shifted in the x-axis.

  • y_shift (float) – The number of pixels that the array is shifted in the y-axis.

  • mode (str, default = "constant") – The padding mode of the translation. It must be one of the following. The implimentation detail is similar to Scipy’s. See scipy.ndimage.shift() for more information.

  • constant (float, default = np.nan) – If the mode is constant, the constant value used is this value.

Returns:

translated – The translated array/image.

Return type:

ndarray