CLAHE Python bindings
[profile/ivi/opencv.git] / modules / imgproc / doc / geometric_transformations.rst
1 Geometric Image Transformations
2 ===============================
3 .. highlight:: cpp
4
5 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:
6
7 .. math::
8
9     \texttt{dst} (x,y)= \texttt{src} (f_x(x,y), f_y(x,y))
10
11 In case when you specify the forward mapping
12 :math:`\left<g_x, g_y\right>: \texttt{src} \rightarrow \texttt{dst}` , the OpenCV functions first compute the corresponding inverse mapping
13 :math:`\left<f_x, f_y\right>: \texttt{dst} \rightarrow \texttt{src}` and then use the above formula.
14
15 The actual implementations of the geometrical transformations, from the most generic
16 :ocv:func:`remap` and to the simplest and the fastest
17 :ocv:func:`resize` , need to solve two main problems with the above formula:
18
19 *
20     Extrapolation of non-existing pixels. Similarly to the filtering functions described in the previous section, for some
21     :math:`(x,y)`  ,   either one of
22     :math:`f_x(x,y)`   ,  or
23     :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.
24
25 *
26     Interpolation of pixel values. Usually
27     :math:`f_x(x,y)`     and
28     :math:`f_y(x,y)`     are floating-point numbers. This means that
29     :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>`_
30     , where a polynomial function is fit into some neighborhood of the computed pixel
31     :math:`(f_x(x,y), f_y(x,y))`   ,  and then the value of the polynomial at
32     :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
33     :ocv:func:`resize`   for details.
34
35 convertMaps
36 -----------
37 Converts image transformation maps from one representation to another.
38
39 .. ocv:function:: void convertMaps( InputArray map1, InputArray map2, OutputArray dstmap1, OutputArray dstmap2, int dstmap1type, bool nninterpolation=false )
40
41 .. ocv:pyfunction:: cv2.convertMaps(map1, map2, dstmap1type[, dstmap1[, dstmap2[, nninterpolation]]]) -> dstmap1, dstmap2
42
43     :param map1: The first input map of type  ``CV_16SC2``  ,  ``CV_32FC1`` , or  ``CV_32FC2`` .
44
45     :param map2: The second input map of type  ``CV_16UC1``  , ``CV_32FC1``  , or none (empty matrix), respectively.
46
47     :param dstmap1: The first output map that has the type  ``dstmap1type``  and the same size as  ``src`` .
48
49     :param dstmap2: The second output map.
50
51     :param dstmap1type: Type of the first output map that should be  ``CV_16SC2`` , ``CV_32FC1`` , or  ``CV_32FC2`` .
52
53     :param nninterpolation: Flag indicating whether the fixed-point maps are used for the nearest-neighbor or for a more complex interpolation.
54
55 The function converts a pair of maps for
56 :ocv:func:`remap` from one representation to another. The following options ( ``(map1.type(), map2.type())`` :math:`\rightarrow` ``(dstmap1.type(), dstmap2.type())`` ) are supported:
57
58 *
59     :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
60     :ocv: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.
61
62 *
63     :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.
64
65 *
66     Reverse conversion. Obviously, the reconstructed floating-point maps will not be exactly the same as the originals.
67
68 .. seealso::
69
70     :ocv:func:`remap`,
71     :ocv:func:`undistort`,
72     :ocv:func:`initUndistortRectifyMap`
73
74
75
76 getAffineTransform
77 ----------------------
78 Calculates an affine transform from three pairs of the corresponding points.
79
80 .. ocv:function:: Mat getAffineTransform( InputArray src, InputArray dst )
81
82 .. ocv:function:: Mat getAffineTransform( const Point2f src[], const Point2f dst[] )
83
84 .. ocv:pyfunction:: cv2.getAffineTransform(src, dst) -> retval
85
86 .. ocv:cfunction:: CvMat* cvGetAffineTransform( const CvPoint2D32f * src, const CvPoint2D32f * dst, CvMat * map_matrix )
87
88 .. ocv:pyoldfunction:: cv.GetAffineTransform(src, dst, mapMatrix)-> None
89
90     :param src: Coordinates of triangle vertices in the source image.
91
92     :param dst: Coordinates of the corresponding triangle vertices in the destination image.
93
94 The function calculates the :math:`2 \times 3` matrix of an affine transform so that:
95
96 .. math::
97
98     \begin{bmatrix} x'_i \\ y'_i \end{bmatrix} = \texttt{map\_matrix} \cdot \begin{bmatrix} x_i \\ y_i \\ 1 \end{bmatrix}
99
100 where
101
102 .. math::
103
104     dst(i)=(x'_i,y'_i),
105     src(i)=(x_i, y_i),
106     i=0,1,2
107
108 .. seealso::
109
110     :ocv:func:`warpAffine`,
111     :ocv:func:`transform`
112
113
114
115 getPerspectiveTransform
116 ---------------------------
117 Calculates a perspective transform from four pairs of the corresponding points.
118
119 .. ocv:function:: Mat getPerspectiveTransform( InputArray src, InputArray dst )
120
121 .. ocv:function:: Mat getPerspectiveTransform( const Point2f src[], const Point2f dst[] )
122
123 .. ocv:pyfunction:: cv2.getPerspectiveTransform(src, dst) -> retval
124
125 .. ocv:cfunction:: CvMat* cvGetPerspectiveTransform( const CvPoint2D32f* src, const CvPoint2D32f* dst, CvMat* map_matrix )
126
127 .. ocv:pyoldfunction:: cv.GetPerspectiveTransform(src, dst, mapMatrix)-> None
128
129     :param src: Coordinates of quadrangle vertices in the source image.
130
131     :param dst: Coordinates of the corresponding quadrangle vertices in the destination image.
132
133 The function calculates the :math:`3 \times 3` matrix of a perspective transform so that:
134
135 .. math::
136
137     \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}
138
139 where
140
141 .. math::
142
143     dst(i)=(x'_i,y'_i),
144     src(i)=(x_i, y_i),
145     i=0,1,2,3
146
147 .. seealso::
148
149     :ocv:func:`findHomography`,
150     :ocv:func:`warpPerspective`,
151     :ocv:func:`perspectiveTransform`
152
153
154 getRectSubPix
155 -----------------
156 Retrieves a pixel rectangle from an image with sub-pixel accuracy.
157
158 .. ocv:function:: void getRectSubPix( InputArray image, Size patchSize, Point2f center, OutputArray patch, int patchType=-1 )
159
160 .. ocv:pyfunction:: cv2.getRectSubPix(image, patchSize, center[, patch[, patchType]]) -> patch
161
162 .. ocv:cfunction:: void cvGetRectSubPix( const CvArr* src, CvArr* dst, CvPoint2D32f center )
163 .. ocv:pyoldfunction:: cv.GetRectSubPix(src, dst, center)-> None
164
165     :param src: Source image.
166
167     :param patchSize: Size of the extracted patch.
168
169     :param center: Floating point coordinates of the center of the extracted rectangle within the source image. The center must be inside the image.
170
171     :param dst: Extracted patch that has the size  ``patchSize``  and the same number of channels as  ``src`` .
172
173     :param patchType: Depth of the extracted pixels. By default, they have the same depth as  ``src`` .
174
175 The function ``getRectSubPix`` extracts pixels from ``src`` :
176
177 .. math::
178
179     dst(x, y) = src(x +  \texttt{center.x} - ( \texttt{dst.cols} -1)*0.5, y +  \texttt{center.y} - ( \texttt{dst.rows} -1)*0.5)
180
181 where the values of the pixels at non-integer coordinates are retrieved
182 using bilinear interpolation. Every channel of multi-channel
183 images is processed independently. While the center of the rectangle
184 must be inside the image, parts of the rectangle may be
185 outside. In this case, the replication border mode (see
186 :ocv:func:`borderInterpolate` ) is used to extrapolate
187 the pixel values outside of the image.
188
189 .. seealso::
190
191     :ocv:func:`warpAffine`,
192     :ocv:func:`warpPerspective`
193
194
195 getRotationMatrix2D
196 -----------------------
197 Calculates an affine matrix of 2D rotation.
198
199 .. ocv:function:: Mat getRotationMatrix2D( Point2f center, double angle, double scale )
200
201 .. ocv:pyfunction:: cv2.getRotationMatrix2D(center, angle, scale) -> retval
202
203 .. ocv:cfunction:: CvMat* cv2DRotationMatrix( CvPoint2D32f center, double angle, double scale, CvMat* map_matrix )
204
205 .. ocv:pyoldfunction:: cv.GetRotationMatrix2D(center, angle, scale, mapMatrix)-> None
206
207     :param center: Center of the rotation in the source image.
208
209     :param angle: Rotation angle in degrees. Positive values mean counter-clockwise rotation (the coordinate origin is assumed to be the top-left corner).
210
211     :param scale: Isotropic scale factor.
212
213     :param map_matrix: The output affine transformation, 2x3 floating-point matrix.
214
215 The function calculates the following matrix:
216
217 .. math::
218
219     \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}
220
221 where
222
223 .. math::
224
225     \begin{array}{l} \alpha =  \texttt{scale} \cdot \cos \texttt{angle} , \\ \beta =  \texttt{scale} \cdot \sin \texttt{angle} \end{array}
226
227 The transformation maps the rotation center to itself. If this is not the target, adjust the shift.
228
229 .. seealso::
230
231     :ocv:func:`getAffineTransform`,
232     :ocv:func:`warpAffine`,
233     :ocv:func:`transform`
234
235
236
237 invertAffineTransform
238 -------------------------
239 Inverts an affine transformation.
240
241 .. ocv:function:: void invertAffineTransform(InputArray M, OutputArray iM)
242
243 .. ocv:pyfunction:: cv2.invertAffineTransform(M[, iM]) -> iM
244
245     :param M: Original affine transformation.
246
247     :param iM: Output reverse affine transformation.
248
249 The function computes an inverse affine transformation represented by
250 :math:`2 \times 3` matrix ``M`` :
251
252 .. math::
253
254     \begin{bmatrix} a_{11} & a_{12} & b_1  \\ a_{21} & a_{22} & b_2 \end{bmatrix}
255
256 The result is also a
257 :math:`2 \times 3` matrix of the same type as ``M`` .
258
259
260
261 LogPolar
262 --------
263 Remaps an image to log-polar space.
264
265 .. ocv:cfunction:: void cvLogPolar( const CvArr* src, CvArr* dst, CvPoint2D32f center, double M, int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS )
266
267 .. ocv:pyoldfunction:: cv.LogPolar(src, dst, center, M, flags=CV_INNER_LINEAR+CV_WARP_FILL_OUTLIERS)-> None
268
269     :param src: Source image
270
271     :param dst: Destination image
272
273     :param center: The transformation center; where the output precision is maximal
274
275     :param M: Magnitude scale parameter. See below
276
277     :param flags: A combination of interpolation methods and the following optional flags:
278
279             *  **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
280
281             *  **CV_WARP_INVERSE_MAP** See below
282
283 The function ``cvLogPolar`` transforms the source image using the following transformation:
284
285   *
286     Forward transformation (``CV_WARP_INVERSE_MAP`` is not set):
287
288         .. math::
289
290             dst( \phi , \rho ) = src(x,y)
291
292
293   *
294     Inverse transformation (``CV_WARP_INVERSE_MAP`` is set):
295
296         .. math::
297
298             dst(x,y) = src( \phi , \rho )
299
300
301 where
302
303     .. math::
304
305         \rho = M  \cdot \log{\sqrt{x^2 + y^2}} , \phi =atan(y/x)
306
307
308 The function emulates the human "foveal" vision and can be used for fast scale and rotation-invariant template matching, for object tracking and so forth. The function can not operate in-place.
309
310
311 remap
312 -----
313 Applies a generic geometrical transformation to an image.
314
315 .. ocv:function:: void remap( InputArray src, OutputArray dst, InputArray map1, InputArray map2, int interpolation, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
316
317 .. ocv:pyfunction:: cv2.remap(src, map1, map2, interpolation[, dst[, borderMode[, borderValue]]]) -> dst
318
319 .. ocv:cfunction:: void cvRemap( const CvArr* src, CvArr* dst, const CvArr* mapx, const CvArr* mapy, int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, CvScalar fillval=cvScalarAll(0) )
320 .. ocv:pyoldfunction:: cv.Remap(src, dst, mapx, mapy, flags=CV_INNER_LINEAR+CV_WARP_FILL_OUTLIERS, fillval=(0, 0, 0, 0))-> None
321
322     :param src: Source image.
323
324     :param dst: Destination image. It has the same size as  ``map1``  and the same type as  ``src`` .
325     :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  :ocv:func:`convertMaps`  for details on converting a floating point representation to fixed-point for speed.
326
327     :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.
328
329     :param interpolation: Interpolation method (see  :ocv:func:`resize` ). The method  ``INTER_AREA``  is not supported by this function.
330
331     :param borderMode: Pixel extrapolation method (see  :ocv: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.
332
333     :param borderValue: Value used in case of a constant border. By default, it is 0.
334
335 The function ``remap`` transforms the source image using the specified map:
336
337 .. math::
338
339     \texttt{dst} (x,y) =  \texttt{src} (map_x(x,y),map_y(x,y))
340
341 where values of pixels with non-integer coordinates are computed using one of available interpolation methods.
342 :math:`map_x` and
343 :math:`map_y` can be encoded as separate floating-point maps in
344 :math:`map_1` and
345 :math:`map_2` respectively, or interleaved floating-point maps of
346 :math:`(x,y)` in
347 :math:`map_1` , or
348 fixed-point maps created by using
349 :ocv:func:`convertMaps` . The reason you might want to convert from floating to fixed-point
350 representations of a map is that they can yield much faster (~2x) remapping operations. In the converted case,
351 :math:`map_1` contains pairs ``(cvFloor(x), cvFloor(y))`` and
352 :math:`map_2` contains indices in a table of interpolation coefficients.
353
354 This function cannot operate in-place.
355
356
357
358 resize
359 ------
360 Resizes an image.
361
362 .. ocv:function:: void resize( InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR )
363
364 .. ocv:pyfunction:: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst
365
366 .. ocv:cfunction:: void cvResize( const CvArr* src, CvArr* dst, int interpolation=CV_INTER_LINEAR )
367 .. ocv:pyoldfunction:: cv.Resize(src, dst, interpolation=CV_INTER_LINEAR)-> None
368
369     :param src: input image.
370
371     :param 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``.
372
373     :param dsize: output image size; if it equals zero, it is computed as:
374
375         .. math::
376
377             \texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}
378
379
380         Either  ``dsize``  or both  ``fx``  and  ``fy``  must be non-zero.
381
382     :param fx: scale factor along the horizontal axis; when it equals 0, it is computed as
383
384         .. math::
385
386             \texttt{(double)dsize.width/src.cols}
387
388     :param fy: scale factor along the vertical axis; when it equals 0, it is computed as
389
390         .. math::
391
392             \texttt{(double)dsize.height/src.rows}
393
394     :param interpolation: interpolation method:
395
396             * **INTER_NEAREST** - a nearest-neighbor interpolation
397
398             * **INTER_LINEAR** - a bilinear interpolation (used by default)
399
400             * **INTER_AREA** - 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.
401
402             * **INTER_CUBIC**  - a bicubic interpolation over 4x4 pixel neighborhood
403
404             * **INTER_LANCZOS4** - a Lanczos interpolation over 8x8 pixel neighborhood
405
406 The function ``resize`` resizes the image ``src`` down to or up to the specified size.
407 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: ::
408
409     // explicitly specify dsize=dst.size(); fx and fy will be computed from that.
410     resize(src, dst, dst.size(), 0, 0, interpolation);
411
412
413 If you want to decimate the image by factor of 2 in each direction, you can call the function this way: ::
414
415     // specify fx and fy and let the function compute the destination image size.
416     resize(src, dst, Size(), 0.5, 0.5, interpolation);
417
418 To shrink an image, it will generally look best with CV_INTER_AREA interpolation, whereas to enlarge an image, it will generally look best with CV_INTER_CUBIC (slow) or CV_INTER_LINEAR (faster but still looks OK).
419
420 .. seealso::
421
422     :ocv:func:`warpAffine`,
423     :ocv:func:`warpPerspective`,
424     :ocv:func:`remap`
425
426
427 warpAffine
428 ----------
429 Applies an affine transformation to an image.
430
431 .. ocv:function:: void warpAffine( InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
432
433 .. ocv:pyfunction:: cv2.warpAffine(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) -> dst
434
435 .. ocv:cfunction:: void cvWarpAffine( const CvArr* src, CvArr* dst, const CvMat* map_matrix, int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, CvScalar fillval=cvScalarAll(0) )
436
437 .. ocv:pyoldfunction:: cv.WarpAffine(src, dst, mapMatrix, flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, fillval=(0, 0, 0, 0))-> None
438
439 .. ocv:cfunction:: void cvGetQuadrangleSubPix( const CvArr* src, CvArr* dst, const CvMat* map_matrix )
440
441 .. ocv:pyoldfunction:: cv.GetQuadrangleSubPix(src, dst, mapMatrix)-> None
442
443     :param src: input image.
444
445     :param dst: output image that has the size  ``dsize``  and the same type as  ``src`` .
446
447     :param M: :math:`2\times 3` transformation matrix.
448
449     :param dsize: size of the output image.
450
451     :param flags: combination of interpolation methods (see  :ocv:func:`resize` ) and the optional flag ``WARP_INVERSE_MAP`` that means that ``M`` is the inverse transformation ( :math:`\texttt{dst}\rightarrow\texttt{src}` ).
452
453     :param borderMode: pixel extrapolation method (see :ocv: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.
454
455     :param borderValue: value used in case of a constant border; by default, it is 0.
456
457 The function ``warpAffine`` transforms the source image using the specified matrix:
458
459 .. math::
460
461     \texttt{dst} (x,y) =  \texttt{src} ( \texttt{M} _{11} x +  \texttt{M} _{12} y +  \texttt{M} _{13}, \texttt{M} _{21} x +  \texttt{M} _{22} y +  \texttt{M} _{23})
462
463 when the flag ``WARP_INVERSE_MAP`` is set. Otherwise, the transformation is first inverted with
464 :ocv:func:`invertAffineTransform` and then put in the formula above instead of ``M`` .
465 The function cannot operate in-place.
466
467 .. seealso::
468
469     :ocv:func:`warpPerspective`,
470     :ocv:func:`resize`,
471     :ocv:func:`remap`,
472     :ocv:func:`getRectSubPix`,
473     :ocv:func:`transform`
474
475
476 .. note:: ``cvGetQuadrangleSubPix`` is similar to ``cvWarpAffine``, but the outliers are extrapolated using replication border mode.
477
478 warpPerspective
479 ---------------
480 Applies a perspective transformation to an image.
481
482 .. ocv:function:: void warpPerspective( InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
483
484 .. ocv:pyfunction:: cv2.warpPerspective(src, M, dsize[, dst[, flags[, borderMode[, borderValue]]]]) -> dst
485
486 .. ocv:cfunction:: void cvWarpPerspective( const CvArr* src, CvArr* dst, const CvMat* map_matrix, int flags=CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, CvScalar fillval=cvScalarAll(0) )
487
488 .. ocv:pyoldfunction:: cv.WarpPerspective(src, dst, mapMatrix, flags=CV_INNER_LINEAR+CV_WARP_FILL_OUTLIERS, fillval=(0, 0, 0, 0))-> None
489
490     :param src: input image.
491
492     :param dst: output image that has the size  ``dsize``  and the same type as  ``src`` .
493
494     :param M: :math:`3\times 3`  transformation matrix.
495
496     :param dsize: size of the output image.
497
498     :param flags: combination of interpolation methods (``INTER_LINEAR`` or ``INTER_NEAREST``) and the optional flag  ``WARP_INVERSE_MAP``, that sets ``M`` as the inverse transformation ( :math:`\texttt{dst}\rightarrow\texttt{src}` ).
499
500     :param borderMode: pixel extrapolation method (``BORDER_CONSTANT`` or ``BORDER_REPLICATE``).
501
502     :param borderValue: value used in case of a constant border; by default, it equals 0.
503
504 The function ``warpPerspective`` transforms the source image using the specified matrix:
505
506 .. math::
507
508     \texttt{dst} (x,y) =  \texttt{src} \left ( \frac{M_{11} x + M_{12} y + M_{13}}{M_{31} x + M_{32} y + M_{33}} ,
509          \frac{M_{21} x + M_{22} y + M_{23}}{M_{31} x + M_{32} y + M_{33}} \right )
510
511 when the flag ``WARP_INVERSE_MAP`` is set. Otherwise, the transformation is first inverted with
512 :ocv:func:`invert` and then put in the formula above instead of ``M`` .
513 The function cannot operate in-place.
514
515 .. seealso::
516
517     :ocv:func:`warpAffine`,
518     :ocv:func:`resize`,
519     :ocv:func:`remap`,
520     :ocv:func:`getRectSubPix`,
521     :ocv:func:`perspectiveTransform`
522
523
524
525
526 initUndistortRectifyMap
527 -----------------------
528 Computes the undistortion and rectification transformation map.
529
530 .. ocv:function:: void initUndistortRectifyMap( InputArray cameraMatrix, InputArray distCoeffs, InputArray R, InputArray newCameraMatrix, Size size, int m1type, OutputArray map1, OutputArray map2 )
531
532 .. ocv:pyfunction:: cv2.initUndistortRectifyMap(cameraMatrix, distCoeffs, R, newCameraMatrix, size, m1type[, map1[, map2]]) -> map1, map2
533
534 .. ocv:cfunction:: void cvInitUndistortRectifyMap( const CvMat* camera_matrix, const CvMat* dist_coeffs, const CvMat * R, const CvMat* new_camera_matrix, CvArr* mapx, CvArr* mapy )
535 .. ocv:cfunction:: void cvInitUndistortMap( const CvMat* camera_matrix, const CvMat* distortion_coeffs, CvArr* mapx, CvArr* mapy )
536
537 .. ocv:pyoldfunction:: cv.InitUndistortRectifyMap(cameraMatrix, distCoeffs, R, newCameraMatrix, map1, map2)-> None
538 .. ocv:pyoldfunction:: cv.InitUndistortMap(cameraMatrix, distCoeffs, map1, map2)-> None
539
540     :param cameraMatrix: Input camera matrix  :math:`A=\vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}` .
541
542     :param distCoeffs: Input vector of distortion coefficients  :math:`(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]])`  of 4, 5, or 8 elements. If the vector is NULL/empty, the zero distortion coefficients are assumed.
543
544     :param R: Optional rectification transformation in the object space (3x3 matrix).  ``R1``  or  ``R2`` , computed by  :ocv:func:`stereoRectify`  can be passed here. If the matrix is empty, the identity transformation is assumed. In ``cvInitUndistortMap`` R assumed to be an identity matrix.
545
546     :param newCameraMatrix: New camera matrix  :math:`A'=\vecthreethree{f_x'}{0}{c_x'}{0}{f_y'}{c_y'}{0}{0}{1}` .
547
548     :param size: Undistorted image size.
549
550     :param m1type: Type of the first output map that can be  ``CV_32FC1``  or  ``CV_16SC2`` . See  :ocv:func:`convertMaps` for details.
551
552     :param map1: The first output map.
553
554     :param map2: The second output map.
555
556 The function computes the joint undistortion and rectification transformation and represents the result in the form of maps for
557 :ocv:func:`remap` . The undistorted image looks like original, as if it is captured with a camera using the camera matrix ``=newCameraMatrix`` and zero distortion. In case of a monocular camera, ``newCameraMatrix`` is usually equal to ``cameraMatrix`` , or it can be computed by
558 :ocv:func:`getOptimalNewCameraMatrix` for a better control over scaling. In case of a stereo camera, ``newCameraMatrix`` is normally set to ``P1`` or ``P2`` computed by
559 :ocv:func:`stereoRectify` .
560
561 Also, this new camera is oriented differently in the coordinate space, according to ``R`` . That, for example, helps to align two heads of a stereo camera so that the epipolar lines on both images become horizontal and have the same y- coordinate (in case of a horizontally aligned stereo camera).
562
563 The function actually builds the maps for the inverse mapping algorithm that is used by
564 :ocv:func:`remap` . That is, for each pixel
565 :math:`(u, v)` in the destination (corrected and rectified) image, the function computes the corresponding coordinates in the source image (that is, in the original image from camera). The following process is applied:
566
567 .. math::
568
569     \begin{array}{l} x  \leftarrow (u - {c'}_x)/{f'}_x  \\ y  \leftarrow (v - {c'}_y)/{f'}_y  \\{[X\,Y\,W]} ^T  \leftarrow R^{-1}*[x \, y \, 1]^T  \\ x'  \leftarrow X/W  \\ y'  \leftarrow Y/W  \\ x"  \leftarrow x' (1 + k_1 r^2 + k_2 r^4 + k_3 r^6) + 2p_1 x' y' + p_2(r^2 + 2 x'^2)  \\ y"  \leftarrow y' (1 + k_1 r^2 + k_2 r^4 + k_3 r^6) + p_1 (r^2 + 2 y'^2) + 2 p_2 x' y'  \\ map_x(u,v)  \leftarrow x" f_x + c_x  \\ map_y(u,v)  \leftarrow y" f_y + c_y \end{array}
570
571 where
572 :math:`(k_1, k_2, p_1, p_2[, k_3])` are the distortion coefficients.
573
574 In case of a stereo camera, this function is called twice: once for each camera head, after
575 :ocv:func:`stereoRectify` , which in its turn is called after
576 :ocv:func:`stereoCalibrate` . But if the stereo camera was not calibrated, it is still possible to compute the rectification transformations directly from the fundamental matrix using
577 :ocv:func:`stereoRectifyUncalibrated` . For each camera, the function computes homography ``H`` as the rectification transformation in a pixel domain, not a rotation matrix ``R`` in 3D space. ``R`` can be computed from ``H`` as
578
579 .. math::
580
581     \texttt{R} =  \texttt{cameraMatrix} ^{-1}  \cdot \texttt{H} \cdot \texttt{cameraMatrix}
582
583 where ``cameraMatrix`` can be chosen arbitrarily.
584
585
586
587
588 getDefaultNewCameraMatrix
589 -------------------------
590 Returns the default new camera matrix.
591
592 .. ocv:function:: Mat getDefaultNewCameraMatrix(InputArray cameraMatrix, Size imgsize=Size(), bool centerPrincipalPoint=false )
593
594 .. ocv:pyfunction:: cv2.getDefaultNewCameraMatrix(cameraMatrix[, imgsize[, centerPrincipalPoint]]) -> retval
595
596     :param cameraMatrix: Input camera matrix.
597
598     :param imgsize: Camera view image size in pixels.
599
600     :param centerPrincipalPoint: Location of the principal point in the new camera matrix. The parameter indicates whether this location should be at the image center or not.
601
602 The function returns the camera matrix that is either an exact copy of the input ``cameraMatrix`` (when ``centerPrinicipalPoint=false`` ), or the modified one (when ``centerPrincipalPoint=true``).
603
604 In the latter case, the new camera matrix will be:
605
606 .. math::
607
608     \begin{bmatrix} f_x && 0 && ( \texttt{imgSize.width} -1)*0.5  \\ 0 && f_y && ( \texttt{imgSize.height} -1)*0.5  \\ 0 && 0 && 1 \end{bmatrix} ,
609
610 where
611 :math:`f_x` and
612 :math:`f_y` are
613 :math:`(0,0)` and
614 :math:`(1,1)` elements of ``cameraMatrix`` , respectively.
615
616 By default, the undistortion functions in OpenCV (see
617 :ocv:func:`initUndistortRectifyMap`,
618 :ocv:func:`undistort`) do not move the principal point. However, when you work with stereo, it is important to move the principal points in both views to the same y-coordinate (which is required by most of stereo correspondence algorithms), and may be to the same x-coordinate too. So, you can form the new camera matrix for each view where the principal points are located at the center.
619
620
621
622
623 undistort
624 -------------
625 Transforms an image to compensate for lens distortion.
626
627 .. ocv:function:: void undistort( InputArray src, OutputArray dst, InputArray cameraMatrix, InputArray distCoeffs, InputArray newCameraMatrix=noArray() )
628
629 .. ocv:pyfunction:: cv2.undistort(src, cameraMatrix, distCoeffs[, dst[, newCameraMatrix]]) -> dst
630
631 .. ocv:cfunction:: void cvUndistort2( const CvArr* src, CvArr* dst, const CvMat* camera_matrix, const CvMat* distortion_coeffs, const CvMat* new_camera_matrix=0 )
632
633 .. ocv:pyoldfunction:: cv.Undistort2(src, dst, cameraMatrix, distCoeffs)-> None
634
635     :param src: Input (distorted) image.
636
637     :param dst: Output (corrected) image that has the same size and type as  ``src`` .
638
639     :param cameraMatrix: Input camera matrix  :math:`A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}` .
640
641     :param distCoeffs: Input vector of distortion coefficients  :math:`(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]])`  of 4, 5, or 8 elements. If the vector is NULL/empty, the zero distortion coefficients are assumed.
642
643     :param newCameraMatrix: Camera matrix of the distorted image. By default, it is the same as  ``cameraMatrix``  but you may additionally scale and shift the result by using a different matrix.
644
645 The function transforms an image to compensate radial and tangential lens distortion.
646
647 The function is simply a combination of
648 :ocv:func:`initUndistortRectifyMap` (with unity ``R`` ) and
649 :ocv:func:`remap` (with bilinear interpolation). See the former function for details of the transformation being performed.
650
651 Those pixels in the destination image, for which there is no correspondent pixels in the source image, are filled with zeros (black color).
652
653 A particular subset of the source image that will be visible in the corrected image can be regulated by ``newCameraMatrix`` . You can use
654 :ocv:func:`getOptimalNewCameraMatrix` to compute the appropriate ``newCameraMatrix``  depending on your requirements.
655
656 The camera matrix and the distortion parameters can be determined using
657 :ocv:func:`calibrateCamera` . If the resolution of images is different from the resolution used at the calibration stage,
658 :math:`f_x, f_y, c_x` and
659 :math:`c_y` need to be scaled accordingly, while the distortion coefficients remain the same.
660
661
662
663
664 undistortPoints
665 -------------------
666 Computes the ideal point coordinates from the observed point coordinates.
667
668 .. ocv:function:: void undistortPoints( InputArray src, OutputArray dst, InputArray cameraMatrix, InputArray distCoeffs, InputArray R=noArray(), InputArray P=noArray())
669
670 .. ocv:cfunction:: void cvUndistortPoints( const CvMat* src, CvMat* dst, const CvMat* camera_matrix, const CvMat* dist_coeffs, const CvMat* R=0, const CvMat* P=0 )
671 .. ocv:pyoldfunction:: cv.UndistortPoints(src, dst, cameraMatrix, distCoeffs, R=None, P=None)-> None
672
673     :param src: Observed point coordinates, 1xN or Nx1 2-channel (CV_32FC2 or CV_64FC2).
674
675     :param dst: Output ideal point coordinates after undistortion and reverse perspective transformation. If matrix ``P`` is identity  or omitted, ``dst`` will contain normalized point coordinates.
676
677     :param cameraMatrix: Camera matrix  :math:`\vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}` .
678
679     :param distCoeffs: Input vector of distortion coefficients  :math:`(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]])`  of 4, 5, or 8 elements. If the vector is NULL/empty, the zero distortion coefficients are assumed.
680
681     :param R: Rectification transformation in the object space (3x3 matrix).  ``R1``  or  ``R2``  computed by  :ocv:func:`stereoRectify`  can be passed here. If the matrix is empty, the identity transformation is used.
682
683     :param P: New camera matrix (3x3) or new projection matrix (3x4).  ``P1``  or  ``P2``  computed by  :ocv:func:`stereoRectify`  can be passed here. If the matrix is empty, the identity new camera matrix is used.
684
685 The function is similar to
686 :ocv:func:`undistort` and
687 :ocv:func:`initUndistortRectifyMap`  but it operates on a sparse set of points instead of a raster image. Also the function performs a reverse transformation to
688 :ocv:func:`projectPoints` . In case of a 3D object, it does not reconstruct its 3D coordinates, but for a planar object, it does, up to a translation vector, if the proper ``R`` is specified. ::
689
690     // (u,v) is the input point, (u', v') is the output point
691     // camera_matrix=[fx 0 cx; 0 fy cy; 0 0 1]
692     // P=[fx' 0 cx' tx; 0 fy' cy' ty; 0 0 1 tz]
693     x" = (u - cx)/fx
694     y" = (v - cy)/fy
695     (x',y') = undistort(x",y",dist_coeffs)
696     [X,Y,W]T = R*[x' y' 1]T
697     x = X/W, y = Y/W
698     // only performed if P=[fx' 0 cx' [tx]; 0 fy' cy' [ty]; 0 0 1 [tz]] is specified
699     u' = x*fx' + cx'
700     v' = y*fy' + cy',
701
702 where ``undistort()`` is an approximate iterative algorithm that estimates the normalized original point coordinates out of the normalized distorted point coordinates ("normalized" means that the coordinates do not depend on the camera matrix).
703
704 The function can be used for both a stereo camera head or a monocular camera (when R is empty).
705
706