Geometric Image Transformations — OpenCV 2.4.13.7 documentation

Geometric Image Transformations

The functions in this section perform various geometrical transformations of 2D images. They do not change the image content but deform the pixel grid and map this deformed grid to the destination image. In fact, to avoid sampling artifacts, the mapping is done in the reverse order, from destination to the source. That is, for each pixel (x, y) of the destination image, the functions compute coordinates of the corresponding “donor” pixel in the source image and copy the pixel value:

\texttt{dst} (x,y)= \texttt{src} (f_x(x,y), f_y(x,y))

In case when you specify the forward mapping \left<g_x, g_y\right>: \texttt{src} \rightarrow \texttt{dst} , the OpenCV functions first compute the corresponding inverse mapping \left<f_x, f_y\right>: \texttt{dst} \rightarrow \texttt{src} and then use the above formula.

The actual implementations of the geometrical transformations, from the most generic remap() and to the simplest and the fastest resize() , need to solve two main problems with the above formula:

  • Extrapolation of non-existing pixels. Similarly to the filtering functions described in the previous section, for some (x,y) , either one of f_x(x,y) , or f_y(x,y) , or both of them may fall outside of the image. In this case, an extrapolation method needs to be used. OpenCV provides the same selection of extrapolation methods as in the filtering functions. In addition, it provides the method BORDER_TRANSPARENT . This means that the corresponding pixels in the destination image will not be modified at all.
  • Interpolation of pixel values. Usually f_x(x,y) and f_y(x,y) are floating-point numbers. This means that \left<f_x, f_y\right> can be either an affine or perspective transformation, or radial lens distortion correction, and so on. So, a pixel value at fractional coordinates needs to be retrieved. In the simplest case, the coordinates can be just rounded to the nearest integer coordinates and the corresponding pixel can be used. This is called a nearest-neighbor interpolation. However, a better result can be achieved by using more sophisticated interpolation methods , where a polynomial function is fit into some neighborhood of the computed pixel (f_x(x,y), f_y(x,y)) , and then the value of the polynomial at (f_x(x,y), f_y(x,y)) is taken as the interpolated pixel value. In OpenCV, you can choose between several interpolation methods. See resize() for details.

convertMaps

Converts image transformation maps from one representation to another.

C++: void convertMaps(InputArray map1, InputArray map2, OutputArray dstmap1, OutputArray dstmap2, int dstmap1type, bool nninterpolation=false )
Python: cv2.convertMaps(map1, map2, dstmap1type[, dstmap1[, dstmap2[, nninterpolation]]]) → dstmap1, dstmap2
Parameters:
  • map1 – The first input map of type CV_16SC2 , CV_32FC1 , or CV_32FC2 .
  • map2 – The second input map of type CV_16UC1 , CV_32FC1 , or none (empty matrix), respectively.
  • dstmap1 – The first output map that has the type dstmap1type and the same size as src .
  • dstmap2 – The second output map.
  • dstmap1type – Type of the first output map that should be CV_16SC2 , CV_32FC1 , or CV_32FC2 .
  • nninterpolation – Flag indicating whether the fixed-point maps are used for the nearest-neighbor or for a more complex interpolation.

The function converts a pair of maps for remap() from one representation to another. The following options ( (map1.type(), map2.type()) \rightarrow (dstmap1.type(), dstmap2.type()) ) are supported:

  • \texttt{(CV\_32FC1, CV\_32FC1)} \rightarrow \texttt{(CV\_16SC2, CV\_16UC1)} . This is the most frequently used conversion operation, in which the original floating-point maps (see remap() ) are converted to a more compact and much faster fixed-point representation. The first output array contains the rounded coordinates and the second array (created only when nninterpolation=false ) contains indices in the interpolation tables.
  • \texttt{(CV\_32FC2)} \rightarrow \texttt{(CV\_16SC2, CV\_16UC1)} . The same as above but the original maps are stored in one 2-channel matrix.
  • Reverse conversion. Obviously, the reconstructed floating-point maps will not be exactly the same as the originals.

getAffineTransform

Calculates an affine transform from three pairs of the corresponding points.

C++: Mat getAffineTransform(InputArray src, InputArray dst)
C++: Mat getAffineTransform(const Point2f src[], const Point2f dst[])
Python: cv2.getAffineTransform(src, dst) → retval
C: CvMat* cvGetAffineTransform(const CvPoint2D32f* src, const CvPoint2D32f* dst, CvMat* map_matrix)
Python: cv.GetAffineTransform(src, dst, mapMatrix) → None
Parameters:
  • src – Coordinates of triangle vertices in the source image.
  • dst – Coordinates of the corresponding triangle vertices in the destination image.

The function calculates the 2 \times 3 matrix of an affine transform so that:

\begin{bmatrix} x'_i \\ y'_i \end{bmatrix} = \texttt{map\_matrix} \cdot \begin{bmatrix} x_i \\ y_i \\ 1 \end{bmatrix}

where

dst(i)=(x'_i,y'_i),
src(i)=(x_i, y_i),
i=0,1,2

getPerspectiveTransform

Calculates a perspective transform from four pairs of the corresponding points.

C++: Mat getPerspectiveTransform(InputArray src, InputArray dst)
C++: Mat getPerspectiveTransform(const Point2f src[], const Point2f dst[])
Python: cv2.getPerspectiveTransform(src, dst) → retval
C: CvMat* cvGetPerspectiveTransform(const CvPoint2D32f* src, const CvPoint2D32f* dst, CvMat* map_matrix)
Python: cv.GetPerspectiveTransform(src, dst, mapMatrix) → None
Parameters:
  • src – Coordinates of quadrangle vertices in the source image.
  • dst – Coordinates of the corresponding quadrangle vertices in the destination image.

The function calculates the 3 \times 3 matrix of a perspective transform so that:

\begin{bmatrix} t_i x'_i \\ t_i y'_i \\ t_i \end{bmatrix} = \texttt{map\_matrix} \cdot \begin{bmatrix} x_i \\ y_i \\ 1 \end{bmatrix}

where

dst(i)=(x'_i,y'_i),
src(i)=(x_i, y_i),
i=0,1,2,3

getRectSubPix

Retrieves a pixel rectangle from an image with sub-pixel accuracy.

C++: void getRectSubPix(InputArray image, Size patchSize, Point2f center, OutputArray patch, int patchType=-1 )
Python: cv2.getRectSubPix(image, patchSize, center[, patch[, patchType]]) → patch
C: void cvGetRectSubPix(const CvArr* src, CvArr* dst, CvPoint2D32f center)
Python: cv.GetRectSubPix(src, dst, center) → None
Parameters:
  • src – Source image.
  • patchSize – Size of the extracted patch.
  • center – Floating point coordinates of the center of the extracted rectangle within the source image. The center must be inside the image.
  • dst – Extracted patch that has the size patchSize and the same number of channels as src .
  • patchType – Depth of the extracted pixels. By default, they have the same depth as src .

The function getRectSubPix extracts pixels from src :

dst(x, y) = src(x +  \texttt{center.x} - ( \texttt{dst.cols} -1)*0.5, y +  \texttt{center.y} - ( \texttt{dst.rows} -1)*0.5)

where the values of the pixels at non-integer coordinates are retrieved using bilinear interpolation. Every channel of multi-channel images is processed independently. While the center of the rectangle must be inside the image, parts of the rectangle may be outside. In this case, the replication border mode (see borderInterpolate() ) is used to extrapolate the pixel values outside of the image.

getRotationMatrix2D

Calculates an affine matrix of 2D rotation.

C++: Mat getRotationMatrix2D(Point2f center, double angle, double scale)
Python: cv2.getRotationMatrix2D(center, angle, scale) → retval
C: CvMat* cv2DRotationMatrix(CvPoint2D32f center, double angle, double scale, CvMat* map_matrix)
Python: cv.GetRotationMatrix2D(center, angle, scale, mapMatrix) → None
Parameters:
  • center – Center of the rotation in the source image.
  • angle – Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner).
  • scale – Isotropic scale factor.
  • map_matrix – The output affine transformation, 2x3 floating-point matrix.

The function calculates the following matrix:

\begin{bmatrix} \alpha &  \beta & (1- \alpha )  \cdot \texttt{center.x} -  \beta \cdot \texttt{center.y} \\ - \beta &  \alpha &  \beta \cdot \texttt{center.x} + (1- \alpha )  \cdot \texttt{center.y} \end{bmatrix}

where

\begin{array}{l} \alpha =  \texttt{scale} \cdot \cos \texttt{angle} , \\ \beta =  \texttt{scale} \cdot \sin \texttt{angle} \end{array}

The transformation maps the rotation center to itself. If this is not the target, adjust the shift.

invertAffineTransform

Inverts an affine transformation.

C++: void invertAffineTransform(InputArray M, OutputArray iM)
Python: cv2.invertAffineTransform(M[, iM]) → iM
Parameters:
  • M – Original affine transformation.
  • iM – Output reverse affine transformation.

The function computes an inverse affine transformation represented by 2 \times 3 matrix M :

\begin{bmatrix} a_{11} & a_{12} & b_1  \\ a_{21} & a_{22} & b_2 \end{bmatrix}

The result is also a 2 \times 3 matrix of the same type as M .

LinearPolar

Remaps an image to polar space.

C: void cvLinearPolar(const CvArr* src, CvArr* dst, CvPoint2D32f center, double maxRadius, int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS )
Parameters:
  • src – Source image
  • dst – Destination image. It will have same size and type as src.
  • center – The transformation center;
  • maxRadius – The radius of the bounding circle to transform. It determines the inverse magnitude scale parameter too. See below
  • flags

    A combination of interpolation methods and the following optional flags:

    • CV_WARP_FILL_OUTLIERS fills all of the destination image pixels. If some of them correspond to outliers in the source image, they are set to zero
    • CV_WARP_INVERSE_MAP See below

The function cvLinearPolar transforms the source image using the following transformation:

  • Forward transformation (CV_WARP_INVERSE_MAP is not set):

    dst( \rho , \phi ) = src(x,y)

  • Inverse transformation (CV_WARP_INVERSE_MAP is set):

    dst(x,y) = src( \rho , \phi )

where

\begin{array}{l}
I = (dx,dy) = (x - center.x,y - center.y) \\
\rho = Kx \cdot \texttt{magnitude} (I) ,\\
\phi = Ky \cdot \texttt{angle} (I)_{0..360 deg}
\end{array}

and

\begin{array}{l}
Kx = src.cols / maxRadius \\
Ky = src.rows / 360
\end{array}

Note

  • The function can not operate in-place.
  • To calculate magnitude and angle in degrees cvCartToPolar() is used internally thus angles are measured from 0 to 360 with accuracy about 0.3 degrees.
  • An example using the LinearPolar operation can be found at opencv_source_code/samples/c/polar_transforms.c

LogPolar

Remaps an image to log-polar space.

C: void cvLogPolar(const CvArr* src, CvArr* dst, CvPoint2D32f center, double M, int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS )