machinevisiontoolbox.Image.warp_affine

Image.warp_affine(M, inverse=False, size=None, bgcolor=None)

Affine warp of image

Parameters:
  • M (ndarray(2,3), SE2) – affine matrix

  • inverse (bool, optional) – warp with inverse of M, defaults to False

  • size (array_like(2), optional) – size of output image, defaults to size of input image

  • bgcolor (scalar, str, array_like, optional) – background color, defaults to None

Returns:

warped image

Return type:

Image

Apply an affine warp to the image. Pixels in the output image that correspond to pixels outside the input image are set to bgcol.

\[\begin{split}Y_{u,v} = X_{u^\prime, v^\prime} \mbox{, where } \begin{pmatrix} u^\prime \\ v^\prime \end{pmatrix} = \mat{M} \begin{pmatrix} u \\ v \\ 1 \end{pmatrix}\end{split}\]

Example:

>>> from machinevisiontoolbox import Image
>>> import numpy as np
>>> from spatialmath import SE2
>>> img = Image.Read('monalisa.png')
>>> M = np.diag([0.25, 0.25, 1]) * SE2(100, 200)  # scale and translate
>>> M
array([[  0.25,   0.  , 100.  ],
       [  0.  ,   0.25, 200.  ],
       [  0.  ,   0.  ,   1.  ]])
>>> out = img.warp_affine(M, bgcolor=np.nan)  # unmapped pixels are NaNs
>>> out.disp(badcolor="r")  # display warped image with NaNs as red
<matplotlib.image.AxesImage object at 0x7f43bc047340>
Note:

Only the first two rows of M are used.

Seealso:

warp opencv.warpAffine