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:
dst(x,y)=src(fx(x,y),fy(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.
resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire'-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
INTER_LANCZOS4
Python: cv.INTER_LANCZOS4
Lanczos interpolation over 8x8 neighborhood
INTER_LINEAR_EXACT
Python: cv.INTER_LINEAR_EXACT
Bit exact bilinear interpolation
INTER_NEAREST_EXACT
Python: cv.INTER_NEAREST_EXACT
Bit exact nearest neighbor interpolation. This will produce same results as the nearest neighbor method in PIL, scikit-image or Matlab.
INTER_MAX
Python: cv.INTER_MAX
mask for interpolation codes
WARP_FILL_OUTLIERS
Python: cv.WARP_FILL_OUTLIERS
flag, fills all of the destination image pixels. If some of them correspond to outliers in the source image, they are set to zero
Converts image transformation maps from one representation to another.
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.
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.
The function resize resizes the 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 follows:
// 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:
where the values of the pixels at non-integer coordinates are retrieved using bilinear interpolation. Every channel of multi-channel images is processed independently. Also the image should be a single channel or three channel image. While the center of the rectangle must be inside the image, parts of the rectangle may be outside.
Parameters
image
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.
patch
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 .
where
α=scale⋅cosangle,β=scale⋅sinangle
The transformation maps the rotation center to itself. If this is not the target, adjust the shift.
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).
The result is also a \(2 \times 3\) matrix of the same type as M.
Parameters
M
Original affine transformation.
iM
Output reverse affine transformation.
with the WARP_RELATIVE_MAP flag :
dst(x,y)=src(x+mapx(x,y),y+mapy(x,y))
where values of pixels with non-integer coordinates are computed using one of available interpolation methods. \(map_x\) and \(map_y\) can be encoded as separate floating-point maps in \(map_1\) and \(map_2\) respectively, or interleaved floating-point maps of \((x,y)\) in \(map_1\), or fixed-point maps created 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, \(map_1\) contains pairs (cvFloor(x), cvFloor(y)) and \(map_2\) contains indices in a table of interpolation coefficients.
This function cannot operate in-place.
Parameters
src
Source image.
dst
Destination image. It has the same size as map1 and the same type as src .
map1
The first map of either (x,y) points or just x values having the type CV_16SC2 , CV_32FC1, or CV_32FC2. See convertMaps for details on converting a floating point representation to fixed-point for speed.
map2
The second map of y values having the type CV_16UC1, CV_32FC1, or none (empty map if map1 is (x,y) points), respectively.
interpolation
Interpolation method (see InterpolationFlags). The methods INTER_AREAINTER_LINEAR_EXACT and INTER_NEAREST_EXACT are not supported by this function. The extra flag WARP_RELATIVE_MAP can be ORed to the interpolation method (e.g. INTER_LINEAR | WARP_RELATIVE_MAP)
borderMode
Pixel extrapolation method (see BorderTypes). When borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image that corresponds to the "outliers" in the source image are not modified by the function.
borderValue
Value used in case of a constant border. By default, it is 0.
// specify fx and fy and let the function compute the destination image size.resize(src, dst, Size(), 0.5, 0.5, interpolation);
To shrink an image, it will generally look best with INTER_AREA interpolation, whereas to enlarge an image, it will generally look best with INTER_CUBIC (slow) or INTER_LINEAR (faster but still looks OK).
Parameters
src
input image.
dst
output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.
dsize
output image size; if it equals zero (None in Python), it is computed as:
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 cannot operate in-place.
Parameters
src
input image.
dst
output image that has the size dsize and the same type as src .
M
\(2\times 3\) transformation matrix.
dsize
size of the output image.
flags
combination of interpolation methods (see InterpolationFlags) and the optional flag WARP_INVERSE_MAP that means that M is the inverse transformation ( \(\texttt{dst}\rightarrow\texttt{src}\) ).
borderMode
pixel extrapolation method (see BorderTypes); when borderMode=BORDER_TRANSPARENT, it means that the pixels in the destination image corresponding to the "outliers" in the source image are not modified by the function.
borderValue
value used in case of a constant border; by default, it is 0.
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 cannot operate in-place.
Parameters
src
input image.
dst
output image that has the size dsize and the same type as src .
M
\(3\times 3\) transformation matrix.
dsize
size of the output image.
flags
combination of interpolation methods (INTER_LINEAR or INTER_NEAREST) and the optional flag WARP_INVERSE_MAP, that sets M as the inverse transformation ( \(\texttt{dst}\rightarrow\texttt{src}\) ).
Polar mapping can be linear or semi-log. Add one of WarpPolarMode to flags to specify the polar mapping mode.
Linear is the default mode.
The semilog mapping emulates the human "foveal" vision that permit very high acuity on the line of sight (central vision) in contrast to peripheral vision where acuity is minor.
Option on dsize:
if both values in dsize <=0 (default), the destination image will have (almost) same area of source bounding circle: