SciPy

scipy.ndimage.affine_transform

scipy.ndimage.affine_transform(input, matrix, offset=0.0, output_shape=None, output=None, order=3, mode='constant', cval=0.0, prefilter=True)[source]

Apply an affine transformation.

Given an output image pixel index vector o, the pixel value is determined from the input image at position np.dot(matrix, o) + offset.

Parameters:
input : array_like

The input array.

matrix : ndarray

The inverse coordinate transformation matrix, mapping output coordinates to input coordinates. If ndim is the number of dimensions of input, the given matrix must have one of the following shapes:

  • (ndim, ndim): the linear transformation matrix for each output coordinate.
  • (ndim,): assume that the 2D transformation matrix is diagonal, with the diagonal specified by the given value. A more efficient algorithm is then used that exploits the separability of the problem.
  • (ndim + 1, ndim + 1): assume that the transformation is specified using homogeneous coordinates [1]. In this case, any value passed to offset is ignored.
  • (ndim, ndim + 1): as above, but the bottom row of a homogeneous transformation matrix is always [0, 0, ..., 1], and may be omitted.
offset : float or sequence, optional

The offset into the array where the transform is applied. If a float, offset is the same for each axis. If a sequence, offset should contain one value for each axis.

output_shape : tuple of ints, optional

Shape tuple.

output : array or dtype, optional

The array in which to place the output, or the dtype of the returned array. By default an array of the same dtype as input will be created.

order : int, optional

The order of the spline interpolation, default is 3. The order has to be in the range 0-5.

mode : {‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’}, optional

The mode parameter determines how the input array is extended beyond its boundaries. Default is ‘constant’. Behavior for each valid value is as follows:

‘reflect’ (d c b a | a b c d | d c b a)

The input is extended by reflecting about the edge of the last pixel.

‘constant’ (k k k k | a b c d | k k k k)

The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter.

‘nearest’ (a a a a | a b c d | d d d d)

The input is extended by replicating the last pixel.

‘mirror’ (d c b | a b c d | c b a)

The input is extended by reflecting about the center of the last pixel.

‘wrap’ (a b c d | a b c d | a b c d)

The input is extended by wrapping around to the opposite edge.

cval : scalar, optional

Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

prefilter : bool, optional

Determines if the input array is prefiltered with spline_filter before interpolation. The default is True, which will create a temporary float64 array of filtered values if order > 1. If setting this to False, the output will be slightly blurred if order > 1, unless the input is prefiltered, i.e. it is the result of calling spline_filter on the original input.

Returns:
affine_transform : ndarray

The transformed input.

Notes

The given matrix and offset are used to find for each point in the output the corresponding coordinates in the input by an affine transformation. The value of the input at those coordinates is determined by spline interpolation of the requested order. Points outside the boundaries of the input are filled according to the given mode.

Changed in version 0.18.0: Previously, the exact interpretation of the affine transformation depended on whether the matrix was supplied as a one-dimensional or two-dimensional array. If a one-dimensional array was supplied to the matrix parameter, the output pixel value at index o was determined from the input image at position matrix * (o + offset).

References

[1](1, 2) https://en.wikipedia.org/wiki/Homogeneous_coordinates

Previous topic

scipy.ndimage.fourier_uniform

Next topic

scipy.ndimage.geometric_transform