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, o) + offset.


input : ndarray

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 [R162]. 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 : ndarray or dtype, optional

The array in which to place the output, or the dtype of the returned array.

order : int, optional

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

mode : str, optional

Points outside the boundaries of the input are filled according to the given mode (‘constant’, ‘nearest’, ‘reflect’, ‘mirror’ or ‘wrap’). Default is ‘constant’.

cval : scalar, optional

Value used for points outside the boundaries of the input if mode='constant'. Default is 0.0

prefilter : bool, optional

The parameter prefilter determines if the input is pre-filtered with spline_filter before interpolation (necessary for spline interpolation of order > 1). If False, it is assumed that the input is already filtered. Default is True.


affine_transform : ndarray or None

The transformed input. If output is given as a parameter, None is returned.


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).


[R162](1, 2)