affine_transform

sunpy.image.transform.affine_transform(image, rmatrix, order=3, scale=1.0, image_center=None, recenter=False, missing=nan, use_scipy=None, *, method='scipy', clip=True)[source]

Rotates, shifts and scales an image.

This function supports NaN values in the input image and supports using NaN for pixels in the output image that are beyond the extent of the input image. Handling NaN values in the input image requires additional computation time.

Parameters
  • image (numpy.ndarray) – 2D image to be rotated.

  • rmatrix (numpy.ndarray that is 2x2) – Linear transformation rotation matrix.

  • order (int 0-5, optional) – Interpolation order to be used, defaults to 3. The precise meaning depends on the rotation method specifed by method.

  • scale (float) – A scale factor for the image with the default being no scaling.

  • image_center (tuple, optional) – The point in the image to rotate around (axis of rotation). Defaults to the center of the array.

  • recenter (bool or array-like, optional) – Move the axis of rotation to the center of the array or recenter coords. Defaults to False.

  • missing (float, optional) – The value to use for pixels in the output image that are beyond the extent of the input image. Defaults to numpy.nan.

  • method ({'scipy', 'scikit-image', 'opencv'}, optional) – Rotation function to use. Defaults to 'scipy'.

  • clip (bool, optional) – If True, clips the pixel values of the output image to the range of the input image (including the value of missing, if used). Defaults to True.

Returns

numpy.ndarray – New rotated, scaled and translated image.

Notes

For each NaN pixel in the input image, one or more pixels in the output image will be set to NaN, with the size of the pixel region affected depending on the interpolation order. All currently implemented rotation methods require a convolution step to handle image NaNs. This convolution normally uses scipy.signal.convolve2d(), but if OpenCV is installed, the faster cv2.filter2D() is used instead.

See add_rotation_function() for how to add a different rotation function.

Specific notes for the ‘scipy’ rotation method:

Specific notes for the ‘scikit-image’ rotation method:

  • Rotates using skimage.transform.warp()

  • The order parameter selects from the following interpolation algorithms:

    • 0: nearest-neighbor

    • 1: bi-linear

    • 2: bi-quadratic

    • 3: bi-cubic

    • 4: bi-quartic

    • 5: bi-quintic

  • The implementation for higher orders of interpolation means that the pixels in the output image that are beyond the extent of the input image may not have exactly the value of the missing parameter.

  • An input image with byte ordering that does not match the native byte order of the system (e.g., big-endian values on a little-endian system) will be copied and byte-swapped prior to rotation.

  • An input image with integer data is cast to floats prior to passing to warp(). The output image can be re-cast using numpy.ndarray.astype() if desired.

  • Does not let warp() handle clipping due to inconsistent handling across interpolation orders

  • Does not let warp() handle image NaNs because they are not handled properly for some interpolation orders

  • Does not pass NaN as missing to warp() due to inconsistent handling across interpolation orders

Specific notes for the ‘opencv’ rotation method:

  • Rotates using cv2.warpAffine() from OpenCV

  • The order parameter selects from the following interpolation algorithms:

    • 0: nearest-neighbor interpolation

    • 1: bilinear interpolation

    • 3: bicubic interpolation

  • An input image with byte ordering that does not match the native byte order of the system (e.g., big-endian values on a little-endian system) will be copied and byte-swapped prior to rotation.

  • An input image with integer data is cast to floats prior to passing to cv2.warpAffine(). The output image can be re-cast using numpy.ndarray.astype() if desired.