The functions in this section perform various geometrical transformations of 2D images. That is, 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
of the destination image, the functions compute coordinates of the corresponding “donor” pixel in the source image and copy the pixel value, that is:

In the case when the user specifies the forward mapping:
, the OpenCV functions first compute the corresponding inverse mapping:
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 the 2 main problems with the above formula:
one of
or
, or they both, may fall outside of the image, in which case some extrapolation method needs to be used. OpenCV provides the same selection of the extrapolation methods as in the filtering functions, but also an additional method
BORDER_TRANSPARENT
, which means that the corresponding pixels in the destination image will not be modified at all.
and
are floating-point numbers (i.e.
can be an affine or perspective transformation, or radial lens distortion correction etc.), so a pixel values 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 used, which is called 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
and then the value of the polynomial at
is taken as the interpolated pixel value. In OpenCV you can choose between several interpolation methods, see
Resize
.Converts image transformation maps from one representation to another
| Parameters: |
|
|---|
The function converts a pair of maps for
remap()
from one representation to another. The following options (
(map1.type(), map2.type())
(dstmap1.type(), dstmap2.type())
) are supported:
. This is the most frequently used conversion operation, in which the original floating-point maps (see
remap()
) are converted to more compact and much faster fixed-point representation. The first output array will contain the rounded coordinates and the second array (created only when
nninterpolation=false
) will contain indices in the interpolation tables.
. The same as above, but the original maps are stored in one 2-channel matrix.See also: remap() , undisort() , initUndistortRectifyMap()
Calculates the affine transform from 3 pairs of the corresponding points
| Parameters: |
|
|---|
The function calculates the
matrix of an affine transform such that:

where

See also: warpAffine() , transform()
Calculates the perspective transform from 4 pairs of the corresponding points
| Parameters: |
|
|---|
The function calculates the
matrix of a perspective transform such that:

where

See also: findHomography() , warpPerspective() , perspectiveTransform()
Retrieves the pixel rectangle from an image with sub-pixel accuracy
| Parameters: |
|
|---|
The function getRectSubPix extracts pixels from src :

where the values of the pixels at non-integer coordinates are retrieved using bilinear interpolation. Every channel of multiple-channel images is processed independently. While the rectangle center 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.
See also: warpAffine() , warpPerspective()
Calculates the affine matrix of 2d rotation.
| Parameters: |
|
|---|
The function calculates the following matrix:

where

The transformation maps the rotation center to itself. If this is not the purpose, the shift should be adjusted.
See also: getAffineTransform() , warpAffine() , transform()
Inverts an affine transformation
| Parameters: |
|
|---|
The function computes inverse affine transformation represented by
matrix
M
:

The result will also be a
matrix of the same type as
M
.
Applies a generic geometrical transformation to an image.
| Parameters: |
|
|---|
The function remap transforms the source image using the specified map:

Where values of pixels with non-integer coordinates are computed using one of the available interpolation methods.
and
can be encoded as separate floating-point maps in
and
respectively, or interleaved floating-point maps of
in
, or
fixed-point maps made by using
convertMaps()
. The reason you might want to convert from floating to fixed-point
representations of a map is that they can yield much faster (~2x) remapping operations. In the converted case,
contains pairs
(cvFloor(x), cvFloor(y))
and
contains indices in a table of interpolation coefficients.
This function can not operate in-place.
Resizes an image
| Parameters: |
|
|---|
The function resize resizes an image src down to or up to the specified size. Note that the initial dst type or size are not taken into account. Instead the size and type are derived from the src , dsize , fx and fy . If you want to resize src so that it fits the pre-created dst , you may call the function as:
// explicitly specify dsize=dst.size(); fx and fy will be computed from that.
resize(src, dst, dst.size(), 0, 0, interpolation);
If you want to decimate the image by factor of 2 in each direction, you can call the function this way:
// specify fx and fy and let the function to compute the destination image size.
resize(src, dst, Size(), 0.5, 0.5, interpolation);
See also: warpAffine() , warpPerspective() , remap() .
Applies an affine transformation to an image.
| Parameters: |
|
|---|
The function warpAffine transforms the source image using the specified matrix:

when the flag WARP_INVERSE_MAP is set. Otherwise, the transformation is first inverted with invertAffineTransform() and then put in the formula above instead of M . The function can not operate in-place.
See also: warpPerspective() , resize() , remap() , getRectSubPix() , transform()
Applies a perspective transformation to an image.
| Parameters: |
|
|---|
The function warpPerspective transforms the source image using the specified matrix:

when the flag WARP_INVERSE_MAP is set. Otherwise, the transformation is first inverted with invert() and then put in the formula above instead of M . The function can not operate in-place.
See also: warpAffine() , resize() , remap() , getRectSubPix() , perspectiveTransform()