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
:math:`(x, y)` of the destination image, the functions compute coordinates of the corresponding "donor" pixel in the source image and copy the pixel value, that is:
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
:math:`(x, y)` of the destination image, the functions compute coordinates of the corresponding "donor" pixel in the source image and copy the pixel value:
In the case when the user specifies the forward mapping:
:math:`\left<g_x, g_y\right>:\texttt{src} \rightarrow \texttt{dst}` , the OpenCV functions first compute the corresponding inverse mapping:
In case when you specify the forward mapping
:math:`\left<g_x, g_y\right>:\texttt{src} \rightarrow \texttt{dst}` , the OpenCV functions first compute the corresponding inverse mapping
:math:`\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
:ref:`Remap` and to the simplest and the fastest
:ref:`Resize` , need to solve the 2 main problems with the above formula:
:ref:`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
:math:`(x,y)` one of
:math:`f_x(x,y)` or
:math:`f_y(x,y)` , 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.
*
Extrapolation of non-existing pixels. Similarly to the filtering functions described in the previous section, for some
:math:`(x,y)`, either one of
:math:`f_x(x,y)`, or
:math:`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
*
Interpolation of pixel values. Usually
:math:`f_x(x,y)` and
:math:`f_y(x,y)` are floating-point numbers (i.e.
:math:`\left<f_x, f_y\right>` 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 <http://en.wikipedia.org/wiki/Multivariate_interpolation>`_
:math:`f_y(x,y)` are floating-point numbers. This means that
:math:`\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 <http://en.wikipedia.org/wiki/Multivariate_interpolation>`_
, where a polynomial function is fit into some neighborhood of the computed pixel
:math:`(f_x(x,y), f_y(x,y))` and then the value of the polynomial at
:math:`(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
:ref:`Resize`.
:math:`(f_x(x,y), f_y(x,y))`, and then the value of the polynomial at
:math:`(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
Converts image transformation maps from one representation to another
Converts image transformation maps from one representation to another.
:param map1:The first input map of type ``CV_16SC2`` or ``CV_32FC1`` or ``CV_32FC2``
:param map1:The first input map of type ``CV_16SC2`` , ``CV_32FC1`` , or ``CV_32FC2`` .
:param map2:The second input map of type ``CV_16UC1`` or ``CV_32FC1`` or none (empty matrix), respectively
:param map2:The second input map of type ``CV_16UC1`` , ``CV_32FC1`` , or none (empty matrix), respectively.
:param dstmap1:The first output map; will have type ``dstmap1type`` and the same size as ``src``
:param dstmap1:The first output map that has the type ``dstmap1type`` and the same size as ``src`` .
:param dstmap2:The second output map
:param dstmap2:The second output map.
:param dstmap1type:The type of the first output map; should be ``CV_16SC2`` , ``CV_32FC1`` or ``CV_32FC2``
:param dstmap1type:Type of the first output map that should be ``CV_16SC2`` , ``CV_32FC1`` , or ``CV_32FC2`` .
:param nninterpolation:Indicates whether the fixed-point maps will be used for nearest-neighbor or for more complex interpolation
:param nninterpolation:Flag indicatingwhether the fixed-point maps are used for the nearest-neighbor or for a more complex interpolation.
The function converts a pair of maps for
:func:`remap` from one representation to another. The following options ( ``(map1.type(), map2.type())``:math:`\rightarrow```(dstmap1.type(), dstmap2.type())`` ) are supported:
#.
*
:math:`\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
:func:`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.
:func:`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.
#.
:math:`\texttt{(CV\_32FC2)} \rightarrow \texttt{(CV\_16SC2, CV\_16UC1)}` . The same as above, but the original maps are stored in one 2-channel matrix.
*
:math:`\texttt{(CV\_32FC2)} \rightarrow \texttt{(CV\_16SC2, CV\_16UC1)}` . The same as above but the original maps are stored in one 2-channel matrix.
#.
the reverse conversion. Obviously, the reconstructed floating-point maps will not be exactly the same as the originals.
*
Reverse conversion. Obviously, the reconstructed floating-point maps will not be exactly the same as the originals.
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
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
:func:`borderInterpolate` ) is used to extrapolate
the pixel values outside of the image.
See also:
:func:`warpAffine`,:func:`warpPerspective`
See Also:
:func:`warpAffine`,
:func:`warpPerspective`
..index:: getRotationMatrix2D
@ -179,13 +185,13 @@ getRotationMatrix2D
-----------------------
..c:function:: Mat getRotationMatrix2D( Point2f center, double angle, double scale )
Calculates the affine matrix of 2d rotation.
Calculates an affine matrix of 2D rotation.
:param center:Center of the rotation in the source image
:param center:Center of the rotation in the source image.
:param angle:The rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner)
:param angle:Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner).
:math:`2 \times 3` matrix of the same type as ``M`` .
..index:: remap
@ -239,18 +247,18 @@ remap
Applies a generic geometrical transformation to an image.
:param src:Source image
:param src:Source image.
:param dst:Destination image. It will have the same size as ``map1`` and the same type as ``src``
:param map1:The first map of either ``(x,y)`` points or just ``x`` values having type ``CV_16SC2`` , ``CV_32FC1`` or ``CV_32FC2`` . See :func:`convertMaps` for converting floating point representation to fixed-point for speed.
:param dst:Destination image. It has the same size as ``map1`` and the same type as ``src`` .
:param map1:The first map of either ``(x,y)`` points or just ``x`` values having the type ``CV_16SC2`` , ``CV_32FC1`` , or ``CV_32FC2`` . See :func:`convertMaps` for details on converting a floating point representation to fixed-point for speed.
:param map2:The second map of ``y`` values having type ``CV_16UC1`` , ``CV_32FC1`` or none (empty map if map1 is ``(x,y)`` points), respectively
:param 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.
:param interpolation:The interpolation method, see :func:`resize` . The method ``INTER_AREA`` is not supported by this function
:param interpolation:Interpolation method (see :func:`resize` ). The method ``INTER_AREA`` is not supported by this function.
:param borderMode:The pixel extrapolation method, see :func:`borderInterpolate` . When the \ ``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
:param borderMode:Pixel extrapolation method (see :func:`borderInterpolate` ). 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.
:param borderValue:A value used in the case of a constant border. By default it is 0
:param borderValue:Value used in case of a constant border. By default, it is 0.
The function ``remap`` transforms the source image using the specified map:
@ -258,20 +266,20 @@ The function ``remap`` transforms the source image using the specified map:
:param dst:Destination image. It will have size ``dsize`` (when it is non-zero) or the size computed from ``src.size()`` and ``fx`` and ``fy`` . The type of ``dst`` will be the same as of ``src`` .
:param dst:Destination image. It has 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`` .
:param dsize:The destination image size. If it is zero, then it is computed as:
:param dsize:Destination image size. If it is zero, it is computed as:
* **INTER_NEAREST** - a nearest-neighbor interpolation
* **INTER_LINEAR** bilinear interpolation (used by default)
* **INTER_LINEAR** - a bilinear interpolation (used by default)
* **INTER_AREA** resampling using pixel area relation. It may be the 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_AREA** - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives freer?? results. But when the image is zoomed, it is similar to the ``INTER_NEAREST`` method.
* **INTER_CUBIC** bicubic interpolation over 4x4 pixel neighborhood
* **INTER_CUBIC** - a bicubic interpolation over 4x4 pixel neighborhood
* **INTER_LANCZOS4** Lanczos interpolation over 8x8 pixel neighborhood
* **INTER_LANCZOS4** - a Lanczos interpolation over 8x8 pixel neighborhood
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: ::
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.
:param dst:Destination image; will have size ``dsize`` and the same type as ``src``
:param dst:Destination image that has the size ``dsize`` and the same type as ``src`` .
:param M::math:`2\times 3` transformation matrix
:param M::math:`2\times 3` transformation matrix.
:param dsize:Size of the destination image
:param dsize:Size of the destination image.
:param flags:A combination of interpolation methods, see :func:`resize` , and the optional flag ``WARP_INVERSE_MAP`` that means that ``M`` is the inverse transformation ( :math:`\texttt{dst}\rightarrow\texttt{src}` )
:param flags:Combination of interpolation methods (see :func:`resize` ) and the optional flag ``WARP_INVERSE_MAP`` that means that ``M`` is the inverse transformation ( :math:`\texttt{dst}\rightarrow\texttt{src}` ).
:param borderMode:The pixel extrapolation method, see :func:`borderInterpolate` . When the \ ``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
:param borderMode:Pixel extrapolation method (see :func:`borderInterpolate` ). 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.
:param borderValue:A value used in case of a constant border. By default it is 0
:param borderValue:Value used in case of a constant border. By default, it is 0.
The function ``warpAffine`` transforms the source image using the specified matrix:
@ -369,10 +379,14 @@ The function ``warpAffine`` transforms the source image using the specified matr
when the flag ``WARP_INVERSE_MAP`` is set. Otherwise, the transformation is first inverted with
:func:`invertAffineTransform` and then put in the formula above instead of ``M`` .
:param flags:A combination of interpolation methods, see :func:`resize` , and the optional flag ``WARP_INVERSE_MAP`` that means that ``M`` is the inverse transformation ( :math:`\texttt{dst}\rightarrow\texttt{src}` )
:param flags:Combination of interpolation methods (see :func:`resize` ) and the optional flag ``WARP_INVERSE_MAP`` that means that ``M`` is the inverse transformation ( :math:`\texttt{dst}\rightarrow\texttt{src}` ).
:param borderMode:The pixel extrapolation method, see :func:`borderInterpolate` . When the \ ``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
:param borderMode:Pixel extrapolation method (see :func:`borderInterpolate` ). 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.
:param borderValue:A value used in case of a constant border. By default it is 0
:param borderValue:Value used in case of a constant border. By default, it is 0.
The function ``warpPerspective`` transforms the source image using the specified matrix:
@ -406,8 +421,12 @@ The function ``warpPerspective`` transforms the source image using the specified
when the flag ``WARP_INVERSE_MAP`` is set. Otherwise, the transformation is first inverted with
:func:`invert` and then put in the formula above instead of ``M`` .