]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/doc/cv_calibration_3d.tex
added partitioned docs
[opencv.git] / opencv / doc / cv_calibration_3d.tex
1 \section{Camera Calibration and 3D Reconstruction}
2
3 The functions in this section use the so-called pinhole camera model. That
4 is, a scene view is formed by projecting 3D points into the image plane
5 using a perspective transformation.
6
7 \[
8 s \; m' = A [R|t] M'
9 \]
10
11 or
12
13 \[
14 s \vecthree{u}{v}{1} = \vecthreethree
15 {fx}{0}{cx}
16 {0}{fy}{cy}
17 {0}{0}{1}
18 \begin{bmatrix}
19  r_{11} & r_{12} & r{13} & t_1 \\
20  r_{21} & r_{22} & r{23} & t_2 \\
21  r_{31} & r_{32} & r{33} & t_3
22 \end{bmatrix}
23 \begin{bmatrix}X\\Y\\Z\\1 \end{bmatrix}
24 \]
25
26 Where $(X, Y, Z)$ are the coordinates of a 3D point in the world
27 coordinate space, $(u, v)$ are the coordinates of the projection point
28 in pixels. $A$ is called a camera matrix, or a matrix of
29 intrinsic parameters. $(cx, cy)$ is a principal point (that is
30 usually at the image center), and $fx, fy$ are the focal lengths
31 expressed in pixel-related units. Thus, if an image from camera is
32 scaled by some factor, all of these parameters should
33 be scaled (multiplied/divided, respectively) by the same factor. The
34 matrix of intrinsic parameters does not depend on the scene viewed and,
35 once estimated, can be re-used (as long as the focal length is fixed (in
36 case of zoom lens)). The joint rotation-translation matrix $[R|t]$
37 is called a matrix of extrinsic parameters. It is used to describe the
38 camera motion around a static scene, or vice versa, rigid motion of an
39 object in front of still camera. That is, $[R|t]$ translates
40 coordinates of a point $(X, Y, Z)$ to some coordinate system,
41 fixed with respect to the camera. The transformation above is equivalent
42 to the following (when $z \ne 0$):
43
44 \[
45 \begin{array}{l}
46 \vecthree{x}{y}{z} = R \vecthree{X}{Y}{Z} + t\\
47 x' = x/z\\
48 y' = y/z\\
49 u = f_x*x' + c_x\\
50 v = f_y*y' + c_y
51 \end{array}
52 \]
53
54 Real lenses usually have some distortion, mostly
55 radial distorion and slight tangential distortion. So, the above model
56 is extended as:
57
58 \[
59 \begin{array}{l}
60 \vecthree{x}{y}{z} = R \vecthree{X}{Y}{Z} + t\\
61 x' = x/z\\
62 y' = y/z\\
63 x'' = x' (1 + k_1 r^2 + k_2 r^4 + k_3 r^6) + 2 p_1 x' y' + p_2(r^2 + 2 x'^2) \\
64 y'' = 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' \\
65 \text{where} \quad r^2 = x'^2 + y'^2 \\
66 u = f_x*x'' + c_x\\
67 v = f_y*y'' + c_y
68 \end{array}
69 \]
70
71 $k_1$, $k_2$, $k_3$ are radial distortion coefficients, $p_1$, $p_2$ are tangential distortion coefficients.
72 Higher-order coefficients are not considered in OpenCV.
73 The distortion coefficients do not depend on the scene viewed, thus they also belong to the intrinsic camera parameters.
74 \emph{And they remain the same regardless of the captured image resolution.}
75 That is, if, for example, a camera has been calibrated on images of $320
76 \times 240$ resolution, absolutely the same distortion coefficients can
77 be used for images of $640 \times 480$ resolution from the same camera (while $f_x$,
78 $f_y$, $c_x$ and $c_y$ need to be scaled appropriately).
79
80 The functions below use the above model to
81
82 \begin{itemize}
83  \item Project 3D points to the image plane given intrinsic and extrinsic parameters
84  \item Compute extrinsic parameters given intrinsic parameters, a few 3D points and their projections.
85  \item Estimate intrinsic and extrinsic camera parameters from several views of a known calibration pattern (i.e. every view is described by several 3D-2D point correspodences).
86 \end{itemize}
87
88 \ifCPy
89
90 \cvCPyFunc{CalcImageHomography}
91 Calculates the homography matrix for an oblong planar object (e.g. arm).
92
93 \cvcodeC{
94 void cvCalcImageHomography( \par float* line,\par CvPoint3D32f* center,\par float* intrinsic,\par float* homography );
95 }\cvcodePy{CalcImageHomography(line,points)-> intrinsic,homography}
96
97 \begin{description}
98 \cvarg{line}{the main object axis direction (vector (dx,dy,dz))}
99 \cvarg{center}{object center ((cx,cy,cz))}
100 \cvarg{intrinsic}{intrinsic camera parameters (3x3 matrix)}
101 \cvarg{homography}{output homography matrix (3x3)}
102 \end{description}
103
104 The function calculates the homography
105 matrix for the initial image transformation from image plane to the
106 plane, defined by a 3D oblong object line (See \_\_Figure 6-10\_\_
107 in the OpenCV Guide 3D Reconstruction Chapter).
108
109
110 \cvCPyFunc{CalibrateCamera2}
111 Finds the intrinsic and extrinsic camera parameters using a calibration pattern.
112
113 \cvcodeC{
114 void cvCalibrateCamera2( \par const CvMat* object\_points,\par const CvMat* image\_points,\par const CvMat* point\_counts,\par CvSize image\_size,\par CvMat* intrinsic\_matrix,\par CvMat* distortion\_coeffs,\par CvMat* rotation\_vectors=NULL,\par CvMat* translation\_vectors=NULL,\par int flags=0 );
115 }\cvcodePy{CalibrateCamera2(object\_points,image\_points,point\_counts,image\_size,intrinsic\_matrix,distortion\_coeffs,rotation\_vectors,translation\_vectors,flags=0)-> None}
116
117 \begin{description}
118 \cvarg{object\_points}{The joint matrix of object points, 3xN or Nx3, where N is the total number of points in all views}
119 \cvarg{image\_points}{The joint matrix of corresponding image points, 2xN or Nx2, where N is the total number of points in all views}
120 \cvarg{point\_counts}{Vector containing the number of points in each particular view, 1xM or Mx1, where M is the number of points in a scene}
121 \cvarg{image\_size}{Size of the image, used only to initialize the intrinsic camera matrix}
122 \cvarg{intrinsic\_matrix}{The output camera matrix $A = \vecthreethree{fx}{0}{cx}{0}{fy}{cy}{0}{0}{1} $. If \texttt{CV\_CALIB\_USE\_INTRINSIC\_GUESS} and/or \texttt{CV\_CALIB\_FIX\_ASPECT\_RATION} are specified, some or all of \texttt{fx, fy, cx, cy} must be initialized}
123 \cvarg{distortion\_coeffs}{The output 4x1 or 1x4 vector of distortion coefficients $k_1, k_2, k_3, k_4$}
124 \cvarg{rotation\_vectors}{The output 3xM or Mx3 array of rotation vectors (compact representation of rotation matrices, \cvCPyCross{Rodrigues2})}
125 \cvarg{translation\_vectors}{The output 3xM or Mx3 array of translation vectors}
126 \cvarg{flags}{Different flags, may be 0 or combination of the following values:
127 \begin{description}
128 \cvarg{CV\_CALIB\_USE\_INTRINSIC\_GUESS}{\texttt{intrinsic\_matrix} contains the valid initial values of \texttt{fx, fy, cx, cy} that are optimized further. Otherwise, \texttt{(cx, cy)} is initially set to the image center (\texttt{image\_size} is used here), and focal distances are computed in some least-squares fashion. Note, that if intrinsic parameters are known, there is no need to use this function. Use \cvCPyCross{FindExtrinsicCameraParams2} instead.}
129 \cvarg{CV\_CALIB\_FIX\_PRINCIPAL\_POINT}{The principal point is not changed during the global optimization, it stays at the center and at the other location specified (when \texttt{CV\_CALIB\_USE\_INTRINSIC\_GUESS} is set as well)}
130 \cvarg{CV\_CALIB\_FIX\_ASPECT\_RATIO}{The optimization procedure considers only one of \texttt{fx} and \texttt{fy} as independent variables and keeps the aspect ratio \texttt{fx/fy} the same as it was set initially in \texttt{intrinsic\_matrix}. In this case the actual initial values of \texttt{(fx, fy)} are either taken from the matrix (when \texttt{CV\_CALIB\_USE\_INTRINSIC\_GUESS} is set) or estimated somehow (in the latter case \texttt{fx, fy} may be set to arbitrary values, only their ratio is used).}
131 \cvarg{CV\_CALIB\_ZERO\_TANGENT\_DIST}{Tangential distortion coefficients are set to zeros and do not change during the optimization.}}
132 \end{description}
133 \end{description}
134
135 The function estimates the intrinsic camera
136 parameters and extrinsic parameters for each of the views. The
137 coordinates of 3D object points and their correspondent 2D projections
138 in each view must be specified. That may be achieved by using an
139 object with known geometry and easily detectable feature points.
140 Such an object is called a calibration rig or calibration pattern,
141 and OpenCV has built-in support for a chessboard as a calibration
142 rig (see \cvCPyCross{FindChessboardCornerGuesses}). Currently, initialization
143 of intrinsic parameters (when \texttt{CV\_CALIB\_USE\_INTRINSIC\_GUESS}
144 is not set) is only implemented for planar calibration rigs
145 (z-coordinates of object points must be all 0's or all 1's). 3D
146 rigs can still be used as long as initial \texttt{intrinsic\_matrix}
147 is provided. After the initial values of intrinsic and extrinsic
148 parameters are computed, they are optimized to minimize the total
149 back-projection error - the sum of squared differences between the
150 actual coordinates of image points and the ones computed using
151 \cvCPyCross{ProjectPoints2}.
152
153 Note: if you're using a non-square (=non-NxN) grid and
154 \cvCPyCross{FindChessboardCorners} for calibration, and cvCalibrateCamera2 returns
155 bad values (i.e. zero distortion coefficients, an image center of
156 (w/2-0.5,h/2-0.5), and / or large differences between $fx$ and $fy$ (ratios of
157 10:1 or more)), then you've probaby  used pattern\_size=cvSize(rows,cols),
158 but should use pattern\_size=cvSize(cols,rows) in \cvCPyCross{FindChessboardCorners}.
159
160 \cvCPyFunc{ComputeCorrespondEpilines}
161 For points in one image of a stereo pair, computes the corresponding epilines in the other image.
162
163 \cvcodeC{
164 void cvComputeCorrespondEpilines( \par const CvMat* points,\par int which\_image,\par const CvMat* fundamental\_matrix,\par CvMat* correspondent\_lines);
165 }\cvcodePy{ComputeCorrespondEpilines(points, which\_image, fundamental\_matrix, correspondent\_lines) -> None}
166
167 \begin{description}
168 \cvarg{points}{The input points. \texttt{2xN, Nx2, 3xN} or \texttt{Nx3} array (where \texttt{N} number of points). Multi-channel \texttt{1xN} or \texttt{Nx1} array is also acceptable}
169 \cvarg{which\_image}{Index of the image (1 or 2) that contains the \texttt{points}}
170 \cvarg{fundamental\_matrix}{Fundamental matrix}
171 \cvarg{correspondent\_lines}{Computed epilines, a \texttt{3xN} or \texttt{Nx3} array}
172 \end{description}
173
174 For every point in one of the two images of a stereo-pair the function
175 \texttt{ComputeCorrespondEpilines} finds the equation of a line that
176 contains the corresponding point (i.e. projection of the same 3D
177 point) in the other image. Each line is encoded by a vector of 3
178 elements $l = \vecthree{a}{b}{c}$ so that:
179
180 \[ l^T \vecthree{x}{y}{1} = 0 \]
181 or
182 \[ a x + b y + c = 0 \]
183
184 From the fundamental matrix definition (see \cvCPyCross{FindFundamentalMatrix}
185 discussion), line $l_1$ for a point $p_1$ in the first image
186 $(\texttt{which\_image} =1)$ can be computed as:
187
188 \[ l_2 = F p_1 \]
189
190 and the line $l_1$ for a point $p_2$ in the second image $(\texttt{which\_image} =1)$ can be computed as:
191
192 \[ l_1 = F^T p_2 \]
193
194 Line coefficients are defined up to a scale. They are normalized $(a^2+b^2=1)$ are stored into \texttt{correspondent\_lines}.
195
196 \cvCPyFunc{ConvertPointsHomogenious}
197 Convert points to/from homogenious coordinates.
198
199 \cvcodeC{
200 void cvConvertPointsHomogenious( \par const CvMat* src,\par CvMat* dst );
201 }
202
203 \begin{description}
204 \cvarg{src}{The input point array, \texttt{2xN, Nx2, 3xN, Nx3, 4xN or Nx4 (where \texttt{N} is the number of points)}. Multi-channel \texttt{1xN} or \texttt{Nx1} array is also acceptable}
205 \cvarg{dst}{The output point array, must contain the same number of points as the input; The dimensionality must be the same, 1 less or 1 more than the input, and also within 2 to 4}
206 \end{description}
207
208 The function converts 2D or 3D points from/to homogenious coordinates, or simply copies or transposes the array. If the input array dimensionality is larger than the output, each coordinate is divided by the last coordinate:
209
210 \[
211 \begin{array}{l}
212 (x,y[,z],w) -> (x',y'[,z'])\\
213 \text{where} \\
214 x' = x/w \\
215 y' = y/w \\
216 z' = z/w \quad \text{(if output is 3D)}
217 \end{array}
218 \]
219
220 If the output array dimensionality is larger, an extra 1 is appended to each point.  Otherwise, the input array is simply copied (with optional tranposition) to the output.
221
222 \textbf{Note} because the function accepts a large variety of array layouts, it may report an error when input/output array dimensionality is ambiguous. It is always safe to use the function with number of points $\texttt{N} \ge 5$, or to use multi-channel \texttt{Nx1} or \texttt{1xN} arrays.
223
224 \cvCPyFunc{CreatePOSITObject}
225 Initializes a structure containing object information.
226
227 \cvcodeC{
228 CvPOSITObject* cvCreatePOSITObject( \par CvPoint3D32f* points,\par int point\_count );
229 }\cvcodePy{CreatePOSITObject(points)-> POSITObject}
230
231 \begin{description}
232 \cvarg{points}{Pointer to the points of the 3D object model}
233 \cvarg{point\_count}{Number of object points}
234 \end{description}
235
236 The function allocates memory for the object structure and computes the object inverse matrix.
237
238 The preprocessed object data is stored in the structure \cvCPyCross{CvPOSITObject}, internal for OpenCV, which means that the user cannot directly access the structure data. The user may only create this structure and pass its pointer to the function.
239
240 An object is defined as a set of points given in a coordinate system. The function \cvCPyCross{POSIT} computes a vector that begins at a camera-related coordinate system center and ends at the \texttt{points[0]} of the object.
241
242 Once the work with a given object is finished, the function \cvCPyCross{ReleasePOSITObject} must be called to free memory.
243
244 \cvCPyFunc{CreateStereoBMState}
245 Creates block matching stereo correspondence structure.
246
247 \begin{lstlisting}
248 #define CV_STEREO_BM_BASIC 0
249 #define CV_STEREO_BM_FISH_EYE 1
250 #define CV_STEREO_BM_NARROW 2
251 \end{lstlisting}
252
253 \cvcodeC{
254
255 CvStereoBMState* cvCreateStereoBMState( int preset=CV\_STEREO\_BM\_BASIC,
256                                         int numberOfDisparities=0 );
257
258 }\cvcodePy{CreateStereoBMState(preset=CV\_STEREO\_BM\_BASIC,numberOfDisparities=0)-> StereoBMState}
259
260 \begin{description}
261 \cvarg{preset}{ID of one of the pre-defined parameter sets. Any of the parameters can be overridden after creating the structure.}
262 \cvarg{numberOfDisparities}{The number of disparities. If the parameter is 0, it is taken from the preset, otherwise the supplied value overrides the one from preset.}
263 \end{description}
264
265 The function creates the stereo correspondence structure and initializes it. It is possible to override any of the parameters at any time between the calls to \cvCPyCross{cvFindStereoCorrespondenceBM}.
266
267 \cvCPyFunc{CreateStereoGCState}
268 Creates the state of graph cut-based stereo correspondence algorithm.
269
270 \cvcodeC{
271
272 CvStereoGCState* cvCreateStereoGCState( int numberOfDisparities,
273                                         int maxIters );
274
275 }\cvcodePy{CreateStereoGCState(numberOfDispaities,maxIters)-> StereoGCState}
276
277 \begin{description}
278 \cvarg{numberOfDisparities}{The number of disparities. The disparity search range will be $\texttt{state->minDisparity} \le disparity < \texttt{state->minDisparity} + \texttt{state->numberOfDisparities}$}
279 \cvarg{maxIters}{Maximum number of iterations. On each iteration all possible (or reasonable) alpha-expansions are tried. The algorithm may terminate earlier if it could not find an alpha-expansion that decreases the overall cost function value. See \href{\#Kolmogorov03}{[Kolmogorov03]}  for details. }
280 \end{description}
281
282 The function creates the stereo correspondence structure and initializes it. It is possible to override any of the parameters at any time between the calls to \cvCPyCross{cvFindStereoCorrespondenceGC}.
283
284 \cvCPyFunc{CvStereoBMState}
285 The structure for block matching stereo correspondence algorithm.
286
287 \begin{lstlisting}
288 typedef struct CvStereoBMState
289 {
290     //pre filters (normalize input images):
291     int       preFilterType; // 0 for now
292     int       preFilterSize; // ~5x5..21x21
293     int       preFilterCap;  // up to ~31
294     //correspondence using Sum of Absolute Difference (SAD):
295     int       SADWindowSize; // Could be 5x5..21x21
296     int       minDisparity;  // minimum disparity (=0)
297     int       numberOfDisparities; // maximum disparity - minimum disparity
298     //post filters (knock out bad matches):
299     int       textureThreshold; // areas with no texture are ignored
300     float     uniquenessRatio;// filter out pixels if there are other close matches
301                               // with different disparity
302     int       speckleWindowSize;// Disparity variation window (not used)
303     int       speckleRange; // Acceptable range of variation in window (not used)
304     // internal buffers, do not modify (!)
305     CvMat* preFilteredImg0;
306     CvMat* preFilteredImg1;
307     CvMat* slidingSumBuf;
308 }
309 CvStereoBMState;
310 \end{lstlisting}
311
312 The block matching stereo correspondence algorithm, by Kurt Konolige, is very fast one-pass stereo matching algorithm that uses sliding sums of absolute differences between pixels in the left image and the pixels in the right image, shifted by some varying amount of pixels (from \texttt{minDisparity} to \texttt{minDisparity+numberOfDisparities}). On a pair of images WxH the algorithm computes disparity in \texttt{O(W*H*numberOfDisparities)} time. In order to improve quality and reability of the disparity map, the algorithm includes pre-filtering and post-filtering procedures.
313
314 Note that the algorithm searches for the corresponding blocks in x direction only. It means that the supplied stereo pair should be rectified. Vertical stereo layout is not directly supported, but in such a case the images could be transposed by user.
315
316 \cvCPyFunc{CvStereoGCState}
317 The structure for graph cuts-based stereo correspondence algorithm
318
319 \begin{lstlisting}
320 typedef struct CvStereoGCState
321 {
322     int Ithreshold; // threshold for piece-wise linear data cost function (5 by default)
323     int interactionRadius; // radius for smoothness cost function (1 by default; means Potts model)
324     float K, lambda, lambda1, lambda2; // parameters for the cost function
325                                        // (usually computed adaptively from the input data)
326     int occlusionCost; // 10000 by default
327     int minDisparity; // 0 by default; see CvStereoBMState
328     int numberOfDisparities; // defined by user; see CvStereoBMState
329     int maxIters; // number of iterations; defined by user.
330
331     // internal buffers
332     CvMat* left;
333     CvMat* right;
334     CvMat* dispLeft;
335     CvMat* dispRight;
336     CvMat* ptrLeft;
337     CvMat* ptrRight;
338     CvMat* vtxBuf;
339     CvMat* edgeBuf;
340 }
341 CvStereoGCState;
342 \end{lstlisting}
343
344 The graph cuts stereo correspondence algorithm, described in \href{\#Kolmogrov03}{[Kolmogorov03]} (as \textbf{KZ1}), is non-realtime stereo correpsondence algorithm that usually gives very accurate depth map with well-defined object boundaries. The algorithm represents stereo problem as a sequence of binary optimization problems, each of those is solved using maximum graph flow algorithm. The state structure above should not be allocated and initialized manually; instead, use \cvCPyCross{cvCreateStereoGCState} and then override necessary parameters if needed.
345
346 \cvCPyFunc{DecomposeProjectionMatrix}
347 Computes the `RQ' decomposition of 3x3 matrices.
348
349 \cvcodeC{
350 void cvDecomposeProjectionMatrix( \par const CvMat *projMatr,\par CvMat *calibMatr,\par CvMat *rotMatr,\par CvMat *posVect,\par CvMat *rotMatrX=NULL,\par CvMat *rotMatrY=NULL,\par CvMat *rotMatrZ=NULL,\par CvPoint3D64f *eulerAngles=NULL);
351 }\cvcodePy{DecomposeProjectionMatrix(projMatr, calibMatr, rotMatr, posVect, rotMatrX = None, rotMatrY = None, rotMatrZ = None) -> eulerAngles}
352
353 \begin{description}
354 \cvarg{projMatr}{The 3x4 input projection matrix P}
355 \cvarg{calibMatr}{The output 3x3 internal calibration matrix K}
356 \cvarg{rotMatr}{The output 3x3 external rotation matrix R}
357 \cvarg{posVect}{The output 4x1 external homogenious position vector C}
358 \cvarg{rotMatrX}{Optional 3x3 rotation matrix around x-axis}
359 \cvarg{rotMatrY}{Optional 3x3 rotation matrix around y-axis}
360 \cvarg{rotMatrZ}{Optional 3x3 rotation matrix around z-axis}
361 \cvarg{eulerAngles}{Optional 3 points containing the three Euler angles of rotation}
362 \end{description}
363
364 The function computes a decomposition of a projection matrix into a calibration and a rotation matrix and the position of the camera.
365
366 It optionally returns three rotation matrices, one for each axis, and the three Euler angles that could be used in OpenGL.
367
368
369 \cvCPyFunc{DrawChessBoardCorners}
370 Renders the detected chessboard corners.
371
372 \cvcodeC{
373 void cvDrawChessboardCorners( \par CvArr* image,\par CvSize pattern\_size,\par CvPoint2D32f* corners,\par int count,\par int pattern\_was\_found );
374 }\cvcodePy{DrawChessboardCorners(image,pattern\_size,corners,pattern\_was\_found)-> None}
375
376 \begin{description}
377 \cvarg{image}{The destination image; it must be an 8-bit color image}
378 \cvarg{pattern\_size}{The number of inner corners per chessboard row and column. ( pattern\_size = cvSize(points\_per\_row,points\_per\_colum) = cvSize(columns,rows) )}
379 \cvarg{corners}{The array of corners detected}
380 \cvarg{count}{The number of corners}
381 \cvarg{pattern\_was\_found}{Indicates whether the complete board was found $(\ne 0)$ or not $(=0)$. One may just pass the return value \cvCPyCross{FindChessboardCorners} here}
382 \end{description}
383
384 The function draws the individual chessboard corners detected as red circles if the board was not found $(\texttt{pattern\_was\_found} =0)$ or as colored corners connected with lines if the board was found $(\texttt{pattern\_was\_found} \ne 0)$.
385
386
387 \cvCPyFunc{FindChessboardCorners}
388 Finds the positions of the internal corners of the chessboard.
389
390 \cvcodeC{
391 int cvFindChessboardCorners( \par const void* image,\par CvSize pattern\_size,\par CvPoint2D32f* corners,\par int* corner\_count=NULL,\par int flags=CV\_CALIB\_CB\_ADAPTIVE\_THRESH );
392 }\cvcodePy{FindChessboardCorners(image, pattern\_size, flags=CV\_CALIB\_CB\_ADAPTIVE\_THRESH) -> corners}
393
394 \begin{description}
395 \cvarg{image}{Source chessboard view; it must be an 8-bit grayscale or color image}
396 \cvarg{pattern\_size}{The number of inner corners per chessboard row and column}
397 ( pattern\_size = cvSize(points\_per\_row,points\_per\_colum) = cvSize(columns,rows) )
398 \cvarg{corners}{The output array of corners detected}
399 \cvC{\cvarg{corner\_count}{The output corner counter. If it is not NULL, it stores the number of corners found}}
400 \cvarg{flags}{Various operation flags, can be 0 or a combination of the following values:
401 \begin{description}
402  \cvarg{CV\_CALIB\_CB\_ADAPTIVE\_THRESH}{use adaptive thresholding to convert the image to black and white, rather than a fixed threshold level (computed from the average image brightness).}
403  \cvarg{CV\_CALIB\_CB\_NORMALIZE\_IMAGE}{normalize the image using \cvCPyCross{NormalizeHist} before applying fixed or adaptive thresholding.}
404  \cvarg{CV\_CALIB\_CB\_FILTER\_QUADS}{use additional criteria (like contour area, perimeter, square-like shape) to filter out false quads that are extracted at the contour retrieval stage.}
405 \end{description}}
406 \end{description}
407
408 The function attempts to determine
409 whether the input image is a view of the chessboard pattern and
410 locate the internal chessboard corners. The function returns a non-zero
411 value if all of the corners have been found and they have been placed
412 in a certain order (row by row, left to right in every row),
413 otherwise, if the function fails to find all the corners or reorder
414 them, it returns 0. For example, a regular chessboard has 8 x 8
415 squares and 7 x 7 internal corners, that is, points, where the black
416 squares touch each other. The coordinates detected are approximate,
417 and to determine their position more accurately, the user may use
418 the function \cvCPyCross{FindCornerSubPix}.
419
420 \cvCPyFunc{FindExtrinsicCameraParams2}
421 Finds the extrinsic camera parameters for a particular view.
422
423 \cvcodeC{
424 void cvFindExtrinsicCameraParams2( \par const CvMat* object\_points,\par const CvMat* image\_points,\par const CvMat* intrinsic\_matrix,\par const CvMat* distortion\_coeffs,\par CvMat* rotation\_vector,\par CvMat* translation\_vector );
425 }\cvcodePy{FindExtrinsicCameraParams2(object\_points,image\_points,intrinsic\_matrix,distortion\_coeffs,rotation\_vector,translation\_vector)-> None}
426
427 \begin{description}
428 \cvarg{object\_points}{The array of object points, 3xN or Nx3, where N is the number of points in the view}
429 \cvarg{image\_points}{The array of corresponding image points, 2xN or Nx2, where N is the number of points in the view}
430 \cvarg{intrinsic\_matrix}{The input camera matrix $A = \vecthreethree{fx}{0}{cx}{0}{fy}{cy}{0}{0}{1} $}
431 \cvarg{distortion\_coeffs}{The input 4x1 or 1x4 vector of distortion coefficients $k_1, k_2, k_3, k_4$. If it is NULL, all of the distortion coefficients are set to 0}
432 \cvarg{rotation\_vector}{The output 3x1 or 1x3 rotation vector (compact representation of a rotation matrix, \cvCPyCross{Rodrigues2}}
433 \cvarg{translation\_vector}{The output 3x1 or 1x3 translation vector}
434 \end{description}
435
436 The function estimates the extrinsic camera parameters using known intrinsic parameters and extrinsic parameters for each view. The coordinates of 3D object points and their correspondent 2D projections must be specified. This function also minimizes back-projection error.
437
438 \cvCPyFunc{FindFundamentalMat}
439 Calculates the fundamental matrix from the corresponding points in two images.
440
441 \cvcodeC{
442 int cvFindFundamentalMat( \par const CvMat* points1,\par const CvMat* points2,\par CvMat* fundamental\_matrix,\par int    method=CV\_FM\_RANSAC,\par double param1=1.,\par double param2=0.99,\par CvMat* status=NULL);
443 }\cvcodePy{FindFundamentalMat(points1, points2, fundamental\_matrix, method=CV\_FM\_RANSAC, param1=1., double param2=0.99, status = None) -> None}
444
445 \begin{description}
446 \cvarg{points1}{Array of the first image points of \texttt{2xN, Nx2, 3xN} or \texttt{Nx3} size (where \texttt{N} is number of points). Multi-channel \texttt{1xN} or \texttt{Nx1} array is also acceptable. The point coordinates should be floating-point (single or double precision)}
447 \cvarg{points2}{Array of the second image points of the same size and format as \texttt{points1}}
448 \cvarg{fundamental\_matrix}{The output fundamental matrix or matrices. The size should be 3x3 or 9x3 (7-point method may return up to 3 matrices)}
449 \cvarg{method}{Method for computing the fundamental matrix
450 \begin{description}
451   \cvarg{CV\_FM\_7POINT}{for a 7-point algorithm. $N = 7$}
452   \cvarg{CV\_FM\_8POINT}{for an 8-point algorithm. $N \ge 8$}
453   \cvarg{CV\_FM\_RANSAC}{for the RANSAC algorithm. $N \ge 8$}
454   \cvarg{CV\_FM\_LMEDS}{for the LMedS algorithm. $N \ge 8$}
455 \end{description}}
456 \cvarg{param1}{The parameter is used for RANSAC or LMedS methods only. It is the maximum distance from point to epipolar line in pixels, beyond which the point is considered an outlier and is not used for computing the final fundamental matrix. Usually it is set to 0.5 or 1.0}
457 \cvarg{param2}{The parameter is used for RANSAC or LMedS methods only. It denotes the desirable level of confidence that the matrix is correct}
458 \cvarg{status}{The optional output array of N elements, every element of which is set to 0 for outliers and to 1 for the other points. The array is computed only in RANSAC and LMedS methods. For other methods it is set to 1}
459 \end{description}
460
461 The epipolar geometry is described by the following equation:
462
463 \[ p_2^T F p1=0 \]
464
465 where $F$ is fundamental matrix, $p_1$ and $p_2$ are corresponding points in the first and the second images, respectively.
466
467 The function calculates the fundamental
468 matrix using one of four methods listed above and returns the number
469 of fundamental matrices found (1 or 3) and 0, if no matrix is found.
470
471 The calculated fundamental matrix may be passed further to
472 \texttt{cvComputeCorrespondEpilines} that finds the epipolar lines
473 corresponding to the specified points.
474
475 \cvfunc{Example. Estimation of fundamental matrix using RANSAC algorithm}
476 \begin{lstlisting}
477 int point_count = 100;
478 CvMat* points1;
479 CvMat* points2;
480 CvMat* status;
481 CvMat* fundamental_matrix;
482
483 points1 = cvCreateMat(1,point_count,CV_32FC2);
484 points2 = cvCreateMat(1,point_count,CV_32FC2);
485 status = cvCreateMat(1,point_count,CV_8UC1);
486
487 /* Fill the points here ... */
488 for( i = 0; i < point_count; i++ )
489 {
490     points1->data.fl[i*2] = <x,,1,i,,>;
491     points1->data.fl[i*2+1] = <y,,1,i,,>;
492     points2->data.fl[i*2] = <x,,2,i,,>;
493     points2->data.fl[i*2+1] = <y,,2,i,,>;
494 }
495
496 fundamental_matrix = cvCreateMat(3,3,CV_32FC1);
497 int fm_count = cvFindFundamentalMat( points1,points2,fundamental_matrix,
498                                      CV_FM_RANSAC,1.0,0.99,status );
499 \end{lstlisting}
500
501 \cvCPyFunc{FindHomography}
502 Finds the perspective transformation between two planes.
503
504 \cvcodeC{
505 void cvFindHomography( \par const CvMat* src\_points,\par const CvMat* dst\_points,\par CvMat* homography \par
506 int method=0, \par double ransacReprojThreshold=0, \par CvMat* mask=NULL);
507 }\cvcodePy{FindHomography(src\_points,dst\_points)-> homography}
508
509 \begin{description}
510 \cvarg{src\_points}{Point coordinates in the original plane, 2xN, Nx2, 3xN or Nx3 array (the latter two are for representation in homogenious coordinates), where N is the number of points}
511 \cvarg{dst\_points}{Point coordinates in the destination plane, 2xN, Nx2, 3xN or Nx3 array (the latter two are for representation in homogenious coordinates)}
512 \cvarg{homography}{Output 3x3 homography matrix}
513 \cvarg{method}{ The method used to computed homography matrix; one of the following:
514 \begin{description}
515 \cvarg{0}{regular method using all the point pairs}
516 \cvarg{CV\_RANSAC}{RANSAC-based robust method}
517 \cvarg{CV\_LMEDS}{Least-Median robust method}
518 \end{description}}
519 \cvarg{ransacReprojThreshold}{The maximum allowed reprojection error to treat a point pair as an inlier. The parameter is only used in RANSAC-based homography estimation. E.g. if \texttt{dst\_points} coordinates are measured in pixels with pixel-accurate precision, it makes sense to set this parameter somewhere in the range 1 to 3. }
520 \cvarg{mask}{The optional output mask set by a robust method (\texttt{CV\_RANSAC} or \texttt{CV\_LMEDS}).}
521 \end{description}
522
523 The function finds the perspective transformation $H$ between the source and the destination planes:
524
525 \[
526 s_i \vecthree{x'_i}{y'_i}{1} \sim H \vecthree{x_i}{y_i}{1}
527 \]
528
529 So that the back-projection error is minimized:
530
531 \[
532 \sum_i
533 \left( x'_i-\frac{h_{11} x_i + h_{12} y_i + h_{13}}{h_{31} x_i + h_{32} y_i + h_{33}} \right)^2+
534 \left( y'_i-\frac{h_{21} x_i + h_{22} y_i + h_{23}}{h_{31} x_i + h_{32} y_i + h_{33}} \right)^2
535 \]
536
537 If the parameter method is set to the default value 0, the function
538 uses all the point pairs and estimates the best suitable homography
539 matrix. However, if not all of the point pairs ($src\_points_i$,
540 $dst\_points_i$) fit the rigid perspective transformation (i.e. there
541 can be outliers), it is still possible to estimate the correct
542 transformation using one of the robust methods available. Both
543 methods, \texttt{CV\_RANSAC} and \texttt{CV\_LMEDS}, try many different random subsets
544 of the corresponding point pairs (of 5 pairs each), estimate
545 the homography matrix using this subset and a simple least-square
546 algorithm and then compute the quality/goodness of the computed homography
547 (which is the number of inliers for RANSAC or the median reprojection
548 error for LMeDs). The best subset is then used to produce the initial
549 estimate of the homography matrix and the mask of inliers/outliers.
550
551 Regardless of the method, robust or not, the computed homography
552 matrix is refined further (using inliers only in the case of a robust
553 method) with the Levenberg-Marquardt method in order to reduce the
554 reprojection error even more.
555
556 The method \texttt{CV\_RANSAC} can handle practically any ratio of outliers,
557 but it needs the threshold to distinguish inliers from outliers.
558 The method \texttt{CV\_LMEDS} does not need any threshold, but it works
559 correctly only when there are more than 50\% of inliers. Finally,
560 if you are sure in the computed features and there can be only some
561 small noise, but no outliers, the default method could be the best
562 choice.
563
564 The function is used to find initial intrinsic and extrinsic matrices.
565 Homography matrix is determined up to a scale, thus it is normalized
566 to make $h_{33} =1$.
567
568 \cvCPyFunc{FindStereoCorrespondenceBM}
569 Computes the disparity map using block matching algorithm.
570
571 \cvcodeC{
572
573 void cvFindStereoCorrespondenceBM( \par const CvArr* left, \par const CvArr* right,
574                                    \par CvArr* disparity, \par CvStereoBMState* state );
575
576 }\cvcodePy{FindStereoCorrespondenceBM(left,right,disparity,state)-> None}
577
578 \begin{description}
579 \cvarg{left}{The left single-channel, 8-bit image.}
580 \cvarg{right}{The right image of the same size and the same type.}
581 \cvarg{disparity}{The output single-channel 16-bit signed disparity map of the same size as input images. Its elements will be the computed disparities, multiplied by 16 and rounded to integers.}
582 \cvarg{state}{Stereo correspondence structure.}
583 \end{description}
584
585 The function cvFindStereoCorrespondenceBM computes disparity map for the input rectified stereo pair.
586
587 \cvCPyFunc{FindStereoCorrespondenceGC}
588 Computes the disparity map using graph cut-based algorithm.
589
590 \cvcodeC{
591
592 void cvFindStereoCorrespondenceGC( \par const CvArr* left, \par const CvArr* right,
593                                    \par CvArr* dispLeft, \par CvArr* dispRight,
594                                    \par CvStereoGCState* state,
595                                    \par int useDisparityGuess = CV\_DEFAULT(0) );
596
597 }\cvcodePy{FindStereoCorrespondenceGC(\par left,\par right,\par dispLeft,\par dispRight,\par state,\par useDisparityGuess=CV\_DEFAULT(0))-> None}
598
599 \begin{description}
600 \cvarg{left}{The left single-channel, 8-bit image.}
601 \cvarg{right}{The right image of the same size and the same type.}
602 \cvarg{dispLeft}{The optional output single-channel 16-bit signed left disparity map of the same size as input images.}
603 \cvarg{dispRight}{The optional output single-channel 16-bit signed right disparity map of the same size as input images.}
604 \cvarg{state}{Stereo correspondence structure.}
605 \cvarg{useDisparityGuess}{If the parameter is not zero, the algorithm will start with pre-defined disparity maps. Both dispLeft and dispRight should be valid disparity maps. Otherwise, the function starts with blank disparity maps (all pixels are marked as occlusions).}
606 \end{description}
607
608 The function computes disparity maps for the input rectified stereo pair. Note that the left disparity image will contain values in the following range: 
609
610 \[
611 -\texttt{state->numberOfDisparities}-\texttt{state->minDisparity}
612 < dispLeft(x,y) \le -\texttt{state->minDisparity},
613 \]
614
615 or
616 \[
617 dispLeft(x,y) == \texttt{CV\_STEREO\_GC\_OCCLUSION}
618 \]
619
620 and for the right disparity image the following will be true: 
621
622 \[
623 \texttt{state->minDisparity} \le dispRight(x,y) 
624 < \texttt{state->minDisparity} + \texttt{state->numberOfDisparities}
625 \]
626
627 or
628
629 \[
630 dispRight(x,y) == \texttt{CV\_STEREO\_GC\_OCCLUSION}
631 \]
632
633 that is, the range for the left disparity image will be inversed,
634 and the pixels for which no good match has been found, will be marked
635 as occlusions.
636
637 Here is how the function can be called:
638
639 \begin{lstlisting}
640 // image_left and image_right are the input 8-bit single-channel images
641 // from the left and the right cameras, respectively
642 CvSize size = cvGetSize(image_left);
643 CvMat* disparity_left = cvCreateMat( size.height, size.width, CV_16S );
644 CvMat* disparity_right = cvCreateMat( size.height, size.width, CV_16S );
645 CvStereoGCState* state = cvCreateStereoGCState( 16, 2 );
646 cvFindStereoCorrespondenceGC( image_left, image_right,
647     disparity_left, disparity_right, state, 0 );
648 cvReleaseStereoGCState( &state );
649 // now process the computed disparity images as you want ...
650 \end{lstlisting}
651
652 and this is the output left disparity image computed from the well-known Tsukuba stereo pair and multiplied by -16 (because the values in the left disparity images are usually negative): 
653
654 \begin{lstlisting}
655 CvMat* disparity_left_visual = cvCreateMat( size.height, size.width, CV_8U );
656 cvConvertScale( disparity_left, disparity_left_visual, -16 );
657 cvSave( "disparity.png", disparity_left_visual );
658 \end{lstlisting}
659
660 \includegraphics{pics/disparity.png}
661
662 \cvCPyFunc{POSIT}
663 Implements the POSIT algorithm.
664
665 \cvcodeC{
666 void cvPOSIT( \par CvPOSITObject* posit\_object,\par CvPoint2D32f* image\_points,\par double focal\_length,\par CvTermCriteria criteria,\par CvMatr32f rotation\_matrix,\par CvVect32f translation\_vector );
667 }\cvcodePy{POSIT(posit\_object,image\_points,focal\_length,criteria)-> rotation\_matrix,translation\_vector}
668
669 \begin{description}
670 \cvarg{posit\_object}{Pointer to the object structure}
671 \cvarg{image\_points}{Pointer to the object points projections on the 2D image plane}
672 \cvarg{focal\_length}{Focal length of the camera used}
673 \cvarg{criteria}{Termination criteria of the iterative POSIT algorithm}
674 \cvarg{rotation\_matrix}{Matrix of rotations}
675 \cvarg{translation\_vector}{Translation vector}
676 \end{description}
677
678 The function implements the POSIT algorithm. Image coordinates are given in a camera-related coordinate system. The focal length may be retrieved using the camera calibration functions. At every iteration of the algorithm a new perspective projection of the estimated pose is computed.
679
680 Difference norm between two projections is the maximal distance between corresponding points. The parameter \texttt{criteria.epsilon} serves to stop the algorithm if the difference is small.
681
682 \ifC
683 \cvCPyFunc{ProjectPoints2}
684 Projects 3D points on to an image plane.
685
686 \cvcodeC{
687 void cvProjectPoints2( \par const CvMat* object\_points,\par const CvMat* rotation\_vector,\par const CvMat* translation\_vector,\par const CvMat* intrinsic\_matrix,\par const CvMat* distortion\_coeffs,\par CvMat* image\_points,\par CvMat* dpdrot=NULL,\par CvMat* dpdt=NULL,\par CvMat* dpdf=NULL,\par CvMat* dpdc=NULL,\par CvMat* dpddist=NULL );
688 }\cvcodePy{ProjectPoints2(object\_points,rotation\_vector,translation\_vector,intrinsic\_matrix,distortion\_coeffs, image\_points,dpdrot=NULL,dpdt=NULL,dpdf=NULL,dpdc=NULL,dpddist=NULL)-> None}
689
690 \begin{description}
691 \cvarg{object\_points}{The array of object points, 3xN or Nx3, where N is the number of points in the view}
692 \cvarg{rotation\_vector}{The rotation vector, 1x3 or 3x1}
693 \cvarg{translation\_vector}{The translation vector, 1x3 or 3x1}
694 \cvarg{intrinsic\_matrix}{The camera matrix $A = \vecthreethree{fx}{0}{cx}{0}{fy}{cy}{0}{0}{1} $}
695 \cvarg{distortion\_coeffs}{The vector of distortion coefficients, 4x1 or 1x4 $k_1, k_2, k_3, k_4$. If it is \texttt{NULL}, all of the distortion coefficients are considered 0's}
696 \cvarg{image\_points}{The output array of image points, 2xN or Nx2, where N is the total number of points in the view}
697 \cvarg{dpdrot}{Optional Nx3 matrix of derivatives of image points with respect to components of the rotation vector}
698 \cvarg{dpdt}{Optional Nx3 matrix of derivatives of image points with respect to components of the translation vector}
699 \cvarg{dpdf}{Optional Nx2 matrix of derivatives of image points with respect to $fx$ and $fy$}
700 \cvarg{dpdc}{Optional Nx2 matrix of derivatives of image points with respect to $cx$ and $cy$}
701 \cvarg{dpddist}{Optional Nx4 matrix of derivatives of image points with respect to distortion coefficients}
702 \end{description}
703
704 The function computes projections of 3D
705 points to the image plane given intrinsic and extrinsic camera
706 parameters. Optionally, the function computes jacobians - matrices
707 of partial derivatives of image points as functions of all the
708 input parameters with respect to the particular parameters, intrinsic and/or
709 extrinsic. The jacobians are used during the global optimization
710 in \cvCPyCross{CalibrateCamera2} and
711 \cvCPyCross{FindExtrinsicCameraParams2}. The
712 function itself is also used to compute back-projection error for with
713 current intrinsic and extrinsic parameters.
714
715 Note, that with intrinsic and/or extrinsic parameters set to special
716 values, the function can be used to compute just an extrinsic transformation
717 or just an intrinsic transformation (i.e. distortion of a sparse set
718 of points).
719
720 \cvCPyFunc{RQDecomp3x3}
721 Computes the `RQ' decomposition of 3x3 matrices.
722
723 \cvcodeC{
724 void cvRQDecomp3x3( \par const CvMat *matrixM,\par CvMat *matrixR,\par CvMat *matrixQ,\par CvMat *matrixQx=NULL,\par CvMat *matrixQy=NULL,\par CvMat *matrixQz=NULL,\par CvPoint3D64f *eulerAngles=NULL);
725 }\cvcodePy{RQDecomp3x3(matrixM, matrixR, matrixQ, matrixQx = None, matrixQy = None, matrixQz = None) -> eulerAngles}
726
727 \begin{description}
728 \cvarg{matrixM}{The 3x3 input matrix M}
729 \cvarg{matrixR}{The output 3x3 upper-triangular matrix R}
730 \cvarg{matrixQ}{The output 3x3 orthogonal matrix Q}
731 \cvarg{matrixQx}{Optional 3x3 rotation matrix around x-axis}
732 \cvarg{matrixQy}{Optional 3x3 rotation matrix around y-axis}
733 \cvarg{matrixQz}{Optional 3x3 rotation matrix around z-axis}
734 \cvarg{eulerAngles}{Optional 3 points containing the three Euler angles of rotation}
735 \end{description}
736
737 The function computes a RQ decomposition using the given rotations. This function is used in \cvCPyCross{DecomposeProjectionMatrix} to decompose the left 3x3 submatrix of a projection matrix into a calibration and a rotation matrix.
738
739 It optionally returns three rotation matrices, one for each axis, and the three Euler angles that could be used in OpenGL.
740
741
742 \cvCPyFunc{ReleasePOSITObject}
743 Deallocates a 3D object structure.
744
745 \cvcodeC{
746 void cvReleasePOSITObject( \par CvPOSITObject** posit\_object );
747 }
748
749 \begin{description}
750 \cvarg{posit\_object}{Double pointer to \texttt{CvPOSIT} structure}
751 \end{description}
752
753 The function releases memory previously allocated by the function \cvCPyCross{CreatePOSITObject}.
754
755 \fi
756
757 \cvCPyFunc{ReleaseStereoBMState}
758 Releases block matching stereo correspondence structure.
759
760 \cvcodeC{
761
762 void cvReleaseStereoBMState( CvStereoBMState** state );
763
764 }\cvcodePy{ReleaseStereoBMState(state)-> None}
765
766 \begin{description}
767 \cvarg{state}{Double pointer to the released structure.}
768 \end{description}
769
770 The function releases the stereo correspondence structure and all the associated internal buffers. 
771
772 \cvCPyFunc{ReleaseStereoGCState}
773 Releases the state structure of the graph cut-based stereo correspondence algorithm.
774
775 \cvcodeC{
776
777 void cvReleaseStereoGCState( CvStereoGCState** state );
778
779 }\cvcodePy{ReleaseStereoGCState(state)-> None}
780
781 \begin{description}
782 \cvarg{state}{Double pointer to the released structure.}
783 \end{description}
784
785 The function releases the stereo correspondence structure and all the associated internal buffers. 
786
787
788 \cvCPyFunc{Rodrigues2}
789 Converts a rotation matrix to a rotation vector or vice versa.
790
791 \cvcodeC{
792 int  cvRodrigues2( \par const CvMat* src,\par CvMat* dst,\par CvMat* jacobian=0 );
793 }\cvcodePy{Rodrigues2(src,dst,jacobian=0)-> None}
794
795 \begin{description}
796 \cvarg{src}{The input rotation vector (3x1 or 1x3) or rotation matrix (3x3)}
797 \cvarg{dst}{The output rotation matrix (3x3) or rotation vector (3x1 or 1x3), respectively}
798 \cvarg{jacobian}{Optional output Jacobian matrix, 3x9 or 9x3 - partial derivatives of the output array components with respect to the input array components}
799 \end{description}
800
801 The function converts a rotation vector to a rotation matrix or vice versa. A rotation vector is a compact representation of rotation matrix. Direction of the rotation vector is the rotation axis and the length of the vector is the rotation angle around the axis. The rotation matrix $R$, corresponding to the rotation vector $r$, is computed as following:
802
803 \[
804 \begin{array}{l}
805 \theta \leftarrow norm(r)\\
806 r \leftarrow r/\theta\\
807 R = \cos{\theta} I + (1-\cos{\theta}) r r^T + \sin{\theta}
808 \vecthreethree
809 {0}{-r_z}{r_y}
810 {r_z}{0}{-r_x}
811 {-r_y}{r_x}{0}
812 \end{array}
813 \]
814
815 Inverse transformation can also be done easily as
816
817 \[
818 \sin(\theta)
819 \vecthreethree
820 {0}{-r_z}{r_y}
821 {r_z}{0}{-r_x}
822 {-r_y}{r_x}{0}
823 =
824 \frac{R - R^T}{2}
825 \]
826
827 A rotation vector is a convenient representation of a rotation matrix
828 as a matrix with only 3 degrees of freedom. The representation is
829 used in the global optimization procedures inside
830 \cvCPyCross{FindExtrinsicCameraParams2}
831 and \cvCPyCross{CalibrateCamera2}.
832
833
834 \cvCPyFunc{StereoCalibrate}
835 Calibrates stereo camera.
836
837 \cvcodeC{
838
839 void cvStereoCalibrate( \par const CvMat* object\_points, \par const CvMat* image\_points1,
840                         \par const CvMat* image\_points2, \par const CvMat* point\_counts,
841                         \par CvMat* camera\_matrix1, \par CvMat* dist\_coeffs1,
842                         \par CvMat* camera\_matrix2, \par CvMat* dist\_coeffs2,
843                        \par CvSize image\_size, \par CvMat* R, \par CvMat* T,
844                         \par CvMat* E=0, \par CvMat* F=0,
845                         \par CvTermCriteria term\_crit=cvTermCriteria(
846                                \par CV\_TERMCRIT\_ITER+CV\_TERMCRIT\_EPS,30,1e-6),
847                         \par int flags=CV\_CALIB\_FIX\_INTRINSIC );
848
849 }\cvcodePy{StereoCalibrate(\par object\_points,\par image\_points1,\par image\_points2,\par point\_counts,\par camera\_matrix1,\par dist\_coeffs1,\par camera\_matrix2,\par dist\_coeffs2,\par image\_size,\par R,\par T,\par E=NULL,\par F=NULL,\par term\_crit=cvTermCriteria(CV\_TERMCRIT\_ITER+CV\_TERMCRIT\_EPS,30,1e-6),\par flags=CV\_CALIB\_FIX\_INTRINSIC)-> None}
850
851 \begin{description}
852 \cvarg{object\_points}{The joint matrix of object points, 3xN or Nx3, where N is the total number of points in all views.}
853 \cvarg{image\_points1}{The joint matrix of corresponding image points in the views from the 1st camera, 2xN or Nx2, where N is the total number of points in all views.}
854 \cvarg{image\_points2}{The joint matrix of corresponding image points in the views from the 2nd camera, 2xN or Nx2, where N is the total number of points in all views.}
855 \cvarg{point\_counts}{Vector containing numbers of points in each view, 1xM or Mx1, where M is the number of views.}
856 \cvarg{camera\_matrix1, camera\_matrix2}{The input/output camera matrices [${fx}_k 0 {cx}_k; 0 {fy}_k {cy}_k; 0 0 1$]. If \texttt{CV\_CALIB\_USE\_INTRINSIC\_GUESS} or \texttt{CV\_CALIB\_FIX\_ASPECT\_RATIO} are specified, some or all of the elements of the matrices must be initialized.}
857 \cvarg{dist\_coeffs1, dist\_coeffs2}{The input/output vectors of distortion coefficients for each camera, \href{\#Pinhole Camera Model, Distortion}{4x1, 1x4, 5x1 or 1x5.}}
858 \cvarg{image\_size}{Size of the image, used only to initialize intrinsic camera matrix.} 
859 \cvarg{R}{The rotation matrix between the 1st and the 2nd cameras' coordinate systems.}
860 \cvarg{T}{The translation vector between the cameras' coordinate systems.}
861 \cvarg{E}{The optional output essential matrix.}
862 \cvarg{F}{The optional output fundamental matrix.}
863 \cvarg{term\_crit}{Termination criteria for the iterative optimiziation algorithm.}
864 \cvarg{flags}{Different flags, may be 0 or combination of the following values:
865 \begin{description}
866 \cvarg{CV\_CALIB\_FIX\_INTRINSIC}{If it is set, \texttt{camera\_matrix1,2}, as well as \texttt{dist\_coeffs1,2} are fixed, so that only extrinsic parameters are optimized.}
867 \cvarg{CV\_CALIB\_USE\_INTRINSIC\_GUESS}{The flag allows the function to optimize some or all of the intrinsic parameters, depending on the other flags, but the initial values are provided by the user.}
868 \cvarg{CV\_CALIB\_FIX\_PRINCIPAL\_POINT}{The principal points are fixed during the optimization.}
869 \cvarg{CV\_CALIB\_FIX\_FOCAL\_LENGTH}{${fx}_k$ and ${fy}_k$ are fixed.}
870 \cvarg{CV\_CALIB\_FIX\_ASPECT\_RATIO}{${fy}_k$ is optimized, but the ratio ${fx}_k/{fy}_k$ is fixed.}
871 \cvarg{CV\_CALIB\_SAME\_FOCAL\_LENGTH}{Enforces ${fx}_0={fx}_1$ and ${fy}_0={fy}_1$. \texttt{CV\_CALIB\_ZERO\_TANGENT\_DIST} - Tangential distortion coefficients for each camera are set to zeros and fixed there.}
872 \cvarg{CV\_CALIB\_FIX\_K1}{The 0-th distortion coefficients (k1) are fixed.}
873 \cvarg{CV\_CALIB\_FIX\_K2}{The 1-st distortion coefficients (k2) are fixed.}
874 \cvarg{CV\_CALIB\_FIX\_K3}{The 4-th distortion coefficients (k3) are fixed.}
875 \end{description}}
876 \end{description}
877
878 The function estimates transformation between the 2 cameras making a stereo pair. If we have a stereo camera, where the relative position and orientatation of the 2 cameras is fixed, and if we computed poses of an object relative to the fist camera and to the second camera, (R1, T1) and (R2, T2), respectively (that can be done with \cvCPyCross{cvFindExtrinsicCameraParams2}), obviously, those poses will relate to each other, i.e. given ($R_1$, $T_1$) it should be possible to compute ($R_2$, $T_2$) - we only need to know the position and orientation of the 2nd camera relative to the 1st camera. That's what the described function does. It computes ($R$, $T$) such that:
879
880 \[
881 R_2=R*R_1
882 T_2=R*T_1 + T,
883 \]
884
885 Optionally, it computes the essential matrix E:
886
887 \[
888 E=
889 \vecthreethree
890 {0}{-T_2}{T_1}
891 {T_2}{0}{-T_0}
892 {-T_1}{T_0}{0}
893 *R
894 \]
895
896 where $T_i$ are components of the translation vector $T$: $T=[T_0, T_1, T_2]^T$. And also the function can compute the fundamental matrix F:
897
898 $F = inv(camera\_matrix2)^T*E*inv(camera\_matrix1)$
899
900 Besides the stereo-related information, the function can also perform full calibration of each of the 2 cameras. However, because of the high dimensionality of the parameter space and noise in the input data the function can diverge from the correct solution. Thus, if intrinsic parameters can be estimated with high accuracy for each of the cameras individually (e.g. using \cvCPyCross{cvCalibrateCamera2}), it is recommended to do so and then pass \texttt{CV\_CALIB\_FIX\_INTRINSIC} flag to the function along with the computed intrinsic parameters. Otherwise, if all the parameters are estimated at once, it makes sense to restrict some parameters, e.g. pass \texttt{CV\_CALIB\_SAME\_FOCAL\_LENGTH} and \texttt{CV\_CALIB\_ZERO\_TANGENT\_DIST} flags, which are usually reasonable assumptions. 
901
902 \cvCPyFunc{StereoRectify}
903 Computes rectification transform for stereo camera.
904
905 \cvcodeC{
906
907 void cvStereoRectify( \par const CvMat* camera\_matrix1, \par const CvMat* camera\_matrix2,
908                       \par const CvMat* dist\_coeffs1, \par const CvMat* dist\_coeffs2,
909                       \par CvSize image\_size, \par const CvMat* R, \par const CvMat* T,
910                       \par CvMat* R1, \par CvMat* R2, \par CvMat* P1, \par CvMat* P2,
911                       \par CvMat* Q=0, \par int flags=CV\_CALIB\_ZERO\_DISPARITY );
912
913 }\cvcodePy{StereoRectify(\par camera\_matrix1,\par camera\_matrix2,\par dist\_coeffs1,\par dist\_coeffs2,\par image\_size,\par R,\par T,\par R1,\par R2,\par P1,\par P2,\par Q=NULL,\par flags=CV\_CALIB\_ZERO\_DISPARITY)-> None}
914
915 \begin{description}
916 \cvarg{camera\_matrix1, camera\_matrix2}{The camera matrices [${fx}_k$ 0 ${cx}_k$; 0 ${fy}_k$ ${cy}_k$; 0 0 1].}
917 \cvarg{dist\_coeffs1, dist\_coeffs2}{The vectors of distortion coefficients for each camera, \href{\#Pinhole Camera Model, Distortion}{4x1, 1x4, 5x1 or 1x5.}}
918 \cvarg{image\_size}{Size of the image used for stereo calibration.}
919 \cvarg{R}{The rotation matrix between the 1st and the 2nd cameras' coordinate systems.}
920 \cvarg{T}{The translation vector between the cameras' coordinate systems.}
921 \cvarg{R1, R2}{3x3 Rectification transforms (rotation matrices) for the first and the second cameras, respectively.}
922 \cvarg{P1, P2}{3x4 Projection matrices in the new (rectified) coordinate systems.}
923 \cvarg{Q}{The optional output disparity-to-depth mapping matrix, 4x4, see \cvCPyCross{cvReprojectImageTo3D}.}
924 \cvarg{flags}{The operation flags; may be 0 or \texttt{CV\_CALIB\_ZERO\_DISPARITY}. If the flag is set, the function makes the principal points of each camera have the same pixel coordinates in the rectified views. And if the flag is not set, the function can shift one of the image in horizontal or vertical direction (depending on the orientation of epipolar lines) in order to maximise the useful image area. }
925 \end{description}
926
927 The function computes the rotation matrices for each camera that (virtually) make both camera image planes the same plane. Consequently, that makes all the epipolar lines parallel and thus simplifies the dense stereo correspondence problem. On input the function takes the matrices computed by \cvCPyCross{cvStereoCalibrate} and on output it gives 2 rotation matrices and also 2 projection matrices in the new coordinates. The function is normally called after \cvCPyCross{cvStereoCalibrate} that computes both camera matrices, the distortion coefficients, R and T. The 2 cases are distinguished by the function: 
928
929 \begin{enumerate}
930 \item{Horizontal stereo, when 1st and 2nd camera views are shifted relative to each other mainly along the x axis (with possible small vertical shift). Then in the rectified images the corresponding epipolar lines in left and right cameras will be horizontal and have the same y-coordinate. P1 and P2 will look as: 
931
932 \[
933 P1=
934 \begin{array}{cccc}
935 f & 0 & cx1 & 0\\
936 0 & f & cy & 0\\
937 0 & 0 & 1 & 0
938 \end{array}
939 \]
940 \[
941 P2=
942 \begin{array}{cccc}
943 f & 0 & cx2 & Tx*f\\
944 0 & f & cy & 0\\
945 0 & 0 & 1 & 0
946 \end{array}
947 ,
948 \]
949
950 where $T_x$ is horizontal shift between the cameras and cx1=cx2 if \texttt{CV\_CALIB\_ZERO\_DISPARITY} is set.}
951 \item{Vertical stereo, when 1st and 2nd camera views are shifted relative to each other mainly in vertical direction (and probably a bit in the horizontal direction too). Then the epipolar lines in the rectified images will be vertical and have the same x coordinate. P2 and P2 will look as:
952
953 \[
954 P1=
955 \begin{array}{cccc}
956 f & 0 & cx & 0\\
957 0 & f & cy1 & 0\\
958 0 & 0 & 1 & 0
959 \end{array}
960 \]
961 \[
962 P2=
963 \begin{array}{cccc}
964 f & 0 & cx & 0\\
965 0 & f & cy2 & Ty*f\\
966 0 & 0 & 1 & 0
967 \end{array}
968 ,
969 \]
970
971 where $T_y$ is vertical shift between the cameras and cy1=cy2 if \texttt{CV\_CALIB\_ZERO\_DISPARITY} is set.}
972 \end{enumerate} 
973
974 As you can see, the first 3 columns of P1 and P2 will effectively be the new "rectified" camera matrices. 
975
976 \cvCPyFunc{StereoRectifyUncalibrated}
977 Computes rectification transform for uncalibrated stereo camera.
978
979 \cvcodeC{
980
981 void cvStereoRectifyUncalibrated( \par const CvMat* points1, \par const CvMat* points2,
982                                   \par const CvMat* F, \par CvSize image\_size,
983                                   \par CvMat* H1, \par CvMat* H2,
984                                   \par double threshold=5 );
985
986 }\cvcodePy{StereoRectifyUncalibrated(points1,points2,F,image\_size,H1,H2,threshold=5)-> None}
987
988 \begin{description}
989 \cvarg{points1, points2}{The 2 arrays of corresponding 2D points.}
990 \cvarg{F}{Fundamental matrix. It can be computed using the same set of point pairs points1 and points2  using \cvCPyCross{cvFindFundamentalMat}.}
991 \cvarg{image\_size}{Size of the image.}
992 \cvarg{H1, H2}{The rectification homography matrices for the first and for the second images.}
993 \cvarg{threshold}{Optional threshold used to filter out the outliers. If the parameter is greater than zero, then all the point pairs that do not comply the epipolar geometry well enough (that is, the points for which $fabs(points2[i]^T*F*points1[i])>threshold$) are rejected prior to computing the homographies. }
994 \end{description}
995
996 The function computes the rectification transformations without knowing intrinsic parameters of the cameras and their relative position in space, hence the suffix "Uncalibrated". Another related difference from \cvCPyCross{cvStereoRectify} is that the function outputs not the rectification transformations in the object (3D) space, but the planar perspective transformations, encoded by the homography matrices H1 and H2. The function implements the following algorithm \href{\#Hartly99}{[Hartley99]}. 
997
998 Note that while the algorithm does not need to know the intrinsic parameters of the cameras, it heavily depends on the epipolar geometry. Therefore, if the camera lenses have significant distortion, it would better be corrected before computing the fundamental matrix and calling this function. For example, distortion coefficients can be estimated for each head of stereo camera separately by using \cvCPyCross{cvCalibrateCamera2} and then the images can be corrected using \cvCPyCross{cvUndistort2}. 
999
1000 \cvCPyFunc{UndistortPoints}
1001 Computes the ideal point coordinates from the observed point coordinates.
1002
1003 \cvcodeC{
1004
1005 void cvUndistortPoints( \par const CvMat* src, \par CvMat* dst,
1006                         \par const CvMat* camera\_matrix,
1007                         \par const CvMat* dist\_coeffs,
1008                         \par const CvMat* R=NULL,
1009                         \par const CvMat* P=NULL);
1010
1011 }\cvcodePy{UndistortPoints(src,dst,camera\_matrix,dist\_coeffs,R=NULL,P=NULL)-> None}
1012
1013 \begin{description}
1014 \cvarg{src}{The observed point coordinates}
1015 \cvarg{dst}{The ideal point coordinates, after undistortion and reverse perspective transformation}
1016 \cvarg{camera\_matrix}{The camera matrix $A=[fx 0 cx; 0 fy cy; 0 0 1]$}
1017 \cvarg{dist\_coeffs}{he vector of distortion coefficients, \cvCPyCross{4x1, 1x4, 5x1 or 1x5}}
1018 \cvarg{R}{The rectification transformation in object space (3x3 matrix). \texttt{R1} or \texttt{R2}, computed by \cvCPyCross{StereoRectify} can be passed here. If the parameter is NULL, the identity matrix is used}
1019 \cvarg{P}{The new camera matrix (3x3) or the new projection matrix (3x4). \texttt{P1} or \texttt{P2}, computed by \cvCPyCross{StereoRectify} can be passed here. If the parameter is NULL, the identity matrix is used}
1020 \end{description}
1021
1022 The function is similar to \cvCPyCross{InitUndistortRectifyMap} and is opposite to it at the same time. The functions are similar in that they both are used to correct lens distortion and to perform the optional perspective (rectification) transformation. They are opposite because the function \cvCPyCross{InitUndistortRectifyMap} does actually perform the reverse transformation in order to initialize the maps properly, while this function does the forward transformation. That is, in pseudo-code it can be expressed as:
1023
1024 \begin{lstlisting}
1025 // (u,v) is the input point, (u', v') is the output point
1026 // camera_matrix=[fx 0 cx; 0 fy cy; 0 0 1]
1027 // P=[fx' 0 cx' tx; 0 fy' cy' ty; 0 0 1 tz]
1028 x" = (u - cx)/fx
1029 y" = (v - cy)/fy
1030 (x',y') = undistort(x",y",dist_coeffs)
1031 [X,Y,W]T = R*[x' y' 1]T
1032 x = X/W, y = Y/W
1033 u' = x*fx' + cx'
1034 v' = y*fy' + cy',
1035 \end{lstlisting}
1036
1037 where undistort() is 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).
1038
1039 The function can be used as for stereo cameras, as well as for individual cameras when R=NULL. 
1040
1041 \fi
1042
1043
1044 \ifCpp
1045
1046 \cvCppFunc{calibrateCamera}
1047 Finds the camera matrix and the camera poses from several views of the calibration pattern.
1048
1049 \begin{lstlisting}
1050 void calibrateCamera( const vector<vector<Point3f> >& objectPoints,
1051                       const vector<vector<Point2f> >& imagePoints,
1052                       Size imageSize,
1053                       Mat& cameraMatrix, Mat& distCoeffs,
1054                       vector<Mat>& rvecs, vector<Mat>& tvecs,
1055                       int flags=0 );
1056 enum
1057 {
1058   CALIB_USE_INTRINSIC_GUESS = CV_CALIB_USE_INTRINSIC_GUESS,
1059   CALIB_FIX_ASPECT_RATIO = CV_CALIB_FIX_ASPECT_RATIO,
1060   CALIB_FIX_PRINCIPAL_POINT = CV_CALIB_FIX_PRINCIPAL_POINT,
1061   CALIB_ZERO_TANGENT_DIST = CV_CALIB_ZERO_TANGENT_DIST,
1062   CALIB_FIX_FOCAL_LENGTH = CV_CALIB_FIX_FOCAL_LENGTH,
1063   CALIB_FIX_K1 = CV_CALIB_FIX_K1,
1064   CALIB_FIX_K2 = CV_CALIB_FIX_K2,
1065   CALIB_FIX_K3 = CV_CALIB_FIX_K3,
1066   // only for stereo
1067   CALIB_FIX_INTRINSIC = CV_CALIB_FIX_INTRINSIC,
1068   CALIB_SAME_FOCAL_LENGTH = CV_CALIB_SAME_FOCAL_LENGTH,
1069   // for stereo rectification
1070   CALIB_ZERO_DISPARITY = CV_CALIB_ZERO_DISPARITY
1071 };
1072 \end{lstlisting}
1073
1074 \begin{description}
1075 \cvarg{objectPoints}{The vector of vectors of points on the calibration rig in its coordinate system, one vector per a view of the rig. If the the same calibration rig is shown in each view and it's fully visible, all the vectors can be the same (though, you may change the numbering from one view to another). The points are 3D, but since they are in the rig coordinate system, then if the rig is planar, it may have sense to put the model to the XY coordinate plane, so that Z-coordinate of each input object point is 0}
1076 \cvarg{imagePoints}{The vector of vectors of the object point projections on the calibration rig views, one vector per a view. The projections must be in the same order as the corresponding object points.}
1077 \cvarg{imageSize}{Size of the image, used only to initialize the intrinsic camera matrix}
1078 \cvarg{cameraMatrix}{The input/output matrix of intrinsic camera parameters $A = \vecthreethree{fx}{0}{cx}{0}{fy}{cy}{0}{0}{1}$. If any of \texttt{CALIB\_USE\_INTRINSIC\_GUESS}, \texttt{CALIB\_FIX\_ASPECT\_RATIO}, \texttt{CALIB\_FIX\_FOCAL\_LENGTH} are specified, some or all of \texttt{fx, fy, cx, cy} must be initialized}
1079 \cvarg{distCoeffs}{The input/output lens distortion coefficients, 4x1, 5x1, 1x4 or 1x5 floating-point vector $k_1, k_2, p_1, p_2[, k_3]$. If any of \texttt{CALIB\_FIX\_K1}, \texttt{CALIB\_FIX\_K2} or \texttt{CALIB\_FIX\_K3} is specified, then the corresponding elements of \texttt{distCoeffs} must be initialized.}
1080 \cvarg{rvecs}{The output vector of rotation vectors (see \cvCppCross{Rodrigues}) estimated for each camera view}
1081 \cvarg{tvecsrans}{The output vector of translation vectors estimated for each camera view}
1082 \cvarg{flags}{Different flags, may be 0 or a combination of the following values:
1083 \begin{description}
1084 \cvarg{CALIB\_USE\_INTRINSIC\_GUESS}{\texttt{cameraMatrix} contains the valid initial values of \texttt{fx, fy, cx, cy} that are optimized further. Otherwise, \texttt{(cx, cy)} is initially set to the image center (computed from the input \texttt{imageSize}), and focal distances are computed in some least-squares fashion. Note, that if the focal distance initialization is currently supported only for planar calibration rigs. That is, if the calibration rig is 3D, then you must initialize \texttt{cameraMatrix} and pass \texttt{CALIB\_USE\_INTRINSIC\_GUESS} flag. Also, note that distortion coefficients are not regulated by this function; use \texttt{CALIB\_ZERO\_TANGENT\_DIST} and \texttt{CALIB\_FIX\_K?} to fix them}
1085 \cvarg{CALIB\_FIX\_PRINCIPAL\_POINT}{The principal point is not changed during the global optimization, it stays at the center or, when \texttt{CALIB\_USE\_INTRINSIC\_GUESS} is set too, at the other specified location}
1086 \cvarg{CALIB\_FIX\_ASPECT\_RATIO}{The optimization procedure considers only one of \texttt{fx} and \texttt{fy} as independent variables and keeps the aspect ratio \texttt{fx/fy} the same as it was set initially in the input \texttt{cameraMatrix}. In this case the actual initial values of \texttt{(fx, fy)} are either taken from the matrix (when \texttt{CALIB\_USE\_INTRINSIC\_GUESS} is set) or estimated.}
1087 \cvarg{CALIB\_ZERO\_TANGENT\_DIST}{Tangential distortion coefficients are set to zeros and do not change during the optimization.}
1088 \cvarg{CALIB\_FIX\_FOCAL\_LENGTH}{Both \texttt{fx} and \texttt{fy} are fixed (taken from \texttt{cameraMatrix} and do not change during the optimization.}
1089 \cvarg{CALIB\_FIX\_K1, CALIB\_FIX\_K2, CALIB\_FIX\_K3}{The particular distortion coefficients is read from the input \texttt{distCoeffs} and stays the same during optimization}
1090 \end{description}}
1091 \end{description}
1092
1093 The function estimates the intrinsic camera
1094 parameters and the extrinsic parameters for each of the views. The
1095 coordinates of 3D object points and their correspondent 2D projections
1096 in each view must be specified. You can use a calibration rig with a known geometry and easily and precisely detectable feature points, e.g. a checkerboard (see \cvCppCross{findChessboardCorners}).
1097
1098 The algorithm does the following:
1099 \begin{enumerate}
1100     \item First, it computes the initial intrinsic parameters (only for planar calibration rigs) or reads them from the input parameters. The distortion coefficients are all set to zeros initially (unless some of \texttt{CALIB\_FIX\_K?} are specified).
1101     \item The the initial camera pose is estimated as if the intrinsic parameters have been already known. This is done using \cvCppCross{solvePnP}
1102     \item After that the global Levenberg-Marquardt optimization algorithm is run to minimize the reprojection error, i.e. the total sum of squared distances between the observed feature points \texttt{imagePoints} and the projected (using the current estimates for camera parameters and the poses) object points \texttt{objectPoints}; see \cvCppCross{projectPoints}.
1103 \end{enumerate}
1104
1105 Note: if you're using a non-square (=non-NxN) grid and
1106 \cvCppCross{findChessboardCorners} for calibration, and \texttt{calibrateCamera} returns
1107 bad values (i.e. zero distortion coefficients, an image center very far from
1108 $(w/2-0.5,h/2-0.5)$, and / or large differences between $f_x$ and $f_y$ (ratios of
1109 10:1 or more)), then you've probaby used \texttt{patternSize=cvSize(rows,cols)},
1110 but should use \texttt{patternSize=cvSize(cols,rows)} in \cvCppCross{findChessboardCorners}.
1111
1112 See also: \cvCppCross{findChessboardCorners}, \cvCppCross{solvePnP}, \cvCppCross{initCameraMatrix2D}, \cvCppCross{stereoCalibrate}, \cvCppCross{undistort}
1113
1114
1115 \cvCppFunc{calibrationMatrixValues}
1116 Computes some useful camera characteristics from the camera matrix
1117
1118 \begin{lstlisting}
1119 void calibrationMatrixValues( const Mat& cameraMatrix,
1120                               Size imageSize,
1121                               double apertureWidth,
1122                               double apertureHeight,
1123                               double& fovx,
1124                               double& fovy,
1125                               double& focalLength,
1126                               Point2d& principalPoint,
1127                               double& aspectRatio );
1128 \end{lstlisting}
1129 \begin{description}
1130 \cvarg{cameraMatrix}{The input camera matrix that can be estimated by \cvCppCross{calibrateCamera} or \cvCppCross{stereoCalibrate}}
1131 \cvarg{imageSize}{The input image size in pixels}
1132 \cvarg{apertureWidth}{Physical width of the sensor}
1133 \cvarg{apertureHeight}{Physical height of the sensor}
1134 \cvarg{fovx}{The output field of view in degrees along the horizontal sensor axis}
1135 \cvarg{fovy}{The output field of view in degrees along the vertical sensor axis}
1136 \cvarg{focalLength}{The focal length of the lens in mm}
1137 \cvarg{prinicialPoint}{The principal point in pixels}
1138 \cvarg{aspectRatio}{$f_y/f_x$}
1139 \end{description}
1140
1141 The function computes various useful camera characteristics from the previously estimated camera matrix.
1142
1143 \cvCppFunc{composeRT}
1144 Combines two rotation-and-shift transformations
1145
1146 \begin{lstlisting}
1147 void composeRT( const Mat& rvec1, const Mat& tvec1,
1148                 const Mat& rvec2, const Mat& tvec2,
1149                 Mat& rvec3, Mat& tvec3 );
1150                 
1151 void composeRT( const Mat& rvec1, const Mat& tvec1,
1152                 const Mat& rvec2, const Mat& tvec2,
1153                 Mat& rvec3, Mat& tvec3,
1154                 Mat& dr3dr1, Mat& dr3dt1,
1155                 Mat& dr3dr2, Mat& dr3dt2,
1156                 Mat& dt3dr1, Mat& dt3dt1,
1157                 Mat& dt3dr2, Mat& dt3dt2 );
1158 \end{lstlisting}
1159 \begin{description}
1160 \cvarg{rvec1}{The first rotation vector}
1161 \cvarg{tvec1}{The first translation vector}
1162 \cvarg{rvec2}{The second rotation vector}
1163 \cvarg{tvec2}{The second translation vector}
1164 \cvarg{rvec3}{The output rotation vector of the superposition}
1165 \cvarg{tvec3}{The output translation vector of the superposition}
1166 \cvarg{d??d??}{The optional output derivatives of \texttt{rvec3} or \texttt{tvec3} w.r.t. \texttt{rvec?} or \texttt{tvec?}}
1167 \end{description}
1168
1169 The functions compute:
1170
1171 \[ \begin{array}{l}
1172 \texttt{rvec3} = \mathrm{rodrigues}^{-1}\left(\mathrm{rodrigues}(\texttt{rvec2}) \cdot
1173 \mathrm{rodrigues}(\texttt{rvec1})\right) \\
1174 \texttt{tvec3} = \mathrm{rodrigues}(\texttt{rvec2}) \cdot \texttt{tvec1} + \texttt{tvec2}
1175 \end{array}, \]
1176
1177 where $\mathrm{rodrigues}$ denotes a rotation vector to rotation matrix transformation, and $\mathrm{rodrigues}^{-1}$ denotes the inverse transformation, see \cvCppCross{Rodrigues}.
1178
1179 Also, the functions can compute the derivatives of the output vectors w.r.t the input vectors (see \cvCppCross{matMulDeriv}).
1180 The functions are used inside \cvCppCross{stereoCalibrate} but can also be used in your own code where Levenberg-Marquardt or another gradient-based solver is used to optimize a function that contains matrix multiplication.
1181
1182
1183 \cvCppFunc{computeCorrespondEpilines}
1184 For points in one image of a stereo pair, computes the corresponding epilines in the other image.
1185
1186 \begin{lstlisting}
1187 void computeCorrespondEpilines( const Mat& points,
1188                                 int whichImage, const Mat& F,
1189                                 vector<Vec3f>& lines );
1190 \end{lstlisting}
1191 \begin{description}
1192 \cvarg{points}{The input points. $N \times 1$ or $1 \times N$ matrix of type \texttt{CV\_32FC2} or \texttt{vector<Point2f>}}
1193 \cvarg{whichImage}{Index of the image (1 or 2) that contains the \texttt{points}}
1194 \cvarg{F}{The fundamental matrix that can be estimated using \cvCppCross{findFundamentalMat} or \texttt{stereoRectify}}
1195 \cvarg{lines}{The output vector of the corresponding to the points epipolar lines in the other image. Each line $ax + by + c=0$ is encoded as 3-element vector $(a, b, c)$}
1196 \end{description}
1197
1198 For every point in one of the two images of a stereo-pair the function
1199 \texttt{computeCorrespondEpilines} finds the equation of the
1200 corresponding epipolar line in the other image.
1201
1202 From the fundamental matrix definition (see \cvCppCross{findFundamentalMatrix}),
1203 line $l^{(2)}_i$ in the second image for the point $p^{(1)}_i$ in the first image (i.e. when \texttt{whichImage=1}) is computed as:
1204
1205 \[ l^{(2)}_i = F p^{(1)}_i \]
1206
1207 and, vice versa, when \texttt{whichImage=2}, $l^{(1)}_i$ is computed from $p^{(2)}_i$ as:
1208
1209 \[ l^{(1)}_i = F^T p^{(2)}_i \]
1210
1211 Line coefficients are defined up to a scale. They are normalized, such that $a_i^2+b_i^2=1$.
1212
1213 \cvCppFunc{convertPointHomogeneous}
1214 Converts 2D points to/from homogeneous coordinates.
1215
1216 \begin{lstlisting}
1217 void convertPointsHomogeneous( const Mat& src, vector<Point3f>& dst );
1218 void convertPointsHomogeneous( const Mat& src, vector<Point2f>& dst );
1219 \end{lstlisting}
1220 \cvarg{src}{The input array or vector of 2D or 3D points}
1221 \cvarg{dst}{The output vector of 3D or 2D points, respectively}
1222 \end{description}
1223
1224 The first of the functions converts 2D points to the homogeneous coordinates by adding extra \texttt{1} component to each point. When the input vector already contains 3D points, it is simply copied to \texttt{dst}. The second function converts 3D points to 2D points by dividing 1st and 2nd components by the 3rd one. If the input vector already contains 2D points, it is simply copied to \texttt{dst}.
1225
1226 \cvCppFunc{decomposeProjectionMatrix}
1227 Decomposes the projection matrix into a rotation matrix and a camera matrix.
1228
1229 \begin{lstlisting}
1230 void decomposeProjectionMatrix( const Mat& projMatrix, Mat& cameraMatrix,
1231                                 Mat& rotMatrix, Mat& transVect );
1232 void decomposeProjectionMatrix( const Mat& projMatrix, Mat& cameraMatrix,
1233                                 Mat& rotMatrix, Mat& transVect,
1234                                 Mat& rotMatrixX, Mat& rotMatrixY,
1235                                 Mat& rotMatrixZ, Vec3d& eulerAngles );
1236 \end{lstlisting}
1237 \begin{description}
1238 \cvarg{projMatrix}{The input $3 \times 4$ projection matrix}
1239 \cvarg{cameraMatrix}{The output $3 \times 3$ camera matrix}
1240 \cvarg{rotMatrix}{The output $3 \times 3$ rotation matrix}
1241 \cvarg{transVect}{The output $3 \times 1$ translation vector}
1242 \cvarg{rotMatrixX}{The optional output rotation matrix around x-axis}
1243 \cvarg{rotMatrixY}{The optional output rotation matrix around y-axis}
1244 \cvarg{rotMatrixZ}{The optional output rotation matrix around z-axis}
1245 \cvarg{eulerAngles}{The optional output 3-vector of the Euler rotation angles}
1246 \end{description}
1247
1248 The function computes a decomposition of a projection matrix into a calibration and a rotation matrix and the position of the camera.
1249
1250 It optionally returns three rotation matrices, one for each axis, and the three Euler angles that could be used in OpenGL.
1251
1252 The function is based on \cvCppCross{RQDecomp3x3}.
1253
1254 \cvCppFunc{drawChessboardCorners}
1255 Draws the detected chessboard corners.
1256
1257 \begin{lstlisting}
1258 void drawChessboardCorners( Mat& image, Size patternSize,
1259                             const Mat& corners,
1260                             bool patternWasFound );
1261 \end{lstlisting}
1262 \begin{description}
1263 \cvarg{image}{The destination image; it must be an 8-bit color image}
1264 \cvarg{patternSize}{The number of inner corners per chessboard row and column, i.e. \texttt{Size(<corners per row>, <corners per column>)}}
1265 \cvarg{corners}{The array of detected corners; \texttt{vector<Point2f>} can be passed here as well}
1266 \cvarg{patternWasFound}{Indicates whether the complete board was found. Just pass the return value of \cvCppCross{findChessboardCorners} here}
1267 \end{description}
1268
1269 The function draws the detected chessboard corners. If no complete board was found, the detected corners will be marked with small red circles. Otherwise, a colored board (each board row with a different color) will be drawn.
1270
1271 \cvCppFunc{findFundamentalMat}
1272 Calculates the fundamental matrix from the corresponding points in two images.
1273
1274 \begin{lstlisting}
1275 Mat findFundamentalMat( const Mat& points1, const Mat& points2,
1276                         vector<uchar>& mask, int method=FM_RANSAC,
1277                         double param1=3., double param2=0.99 );
1278
1279 Mat findFundamentalMat( const Mat& points1, const Mat& points2,
1280                         int method=FM_RANSAC,
1281                         double param1=3., double param2=0.99 );
1282                         
1283 enum
1284
1285     FM_7POINT = CV_FM_7POINT,
1286     FM_8POINT = CV_FM_8POINT,
1287     FM_LMEDS = CV_FM_LMEDS,
1288     FM_RANSAC = CV_FM_RANSAC
1289 };
1290 \end{lstlisting}
1291 \begin{description}
1292 \cvarg{points1}{Array of $N$ points in the first image, a matrix of \texttt{CV\_32FC2} type or \texttt{vector<Point2f>}. The points in homogeneous coordinates can also be passed.}
1293 \cvarg{points2}{Array of the corresponding points in the second image of the same size and the same type as \texttt{points1}}
1294 \cvarg{method}{Method for computing the fundamental matrix
1295 \begin{description}
1296   \cvarg{FM\_7POINT}{for a 7-point algorithm. $N = 7$}
1297   \cvarg{FM\_8POINT}{for an 8-point algorithm. $N \ge 8$}
1298   \cvarg{FM\_RANSAC}{for the RANSAC algorithm. $N \ge 8$}
1299   \cvarg{FM\_LMEDS}{for the LMedS algorithm. $N \ge 8$}
1300 \end{description}}
1301 \cvarg{param1}{The parameter is used for RANSAC only. It is the maximum distance in pixels from point to epipolar line in pixels, beyond which the point is considered an outlier and is not used for computing the final fundamental matrix. It can be set to something like 1-3, depending on the accuracy of the point localization, image resolution and the image noise}
1302 \cvarg{param2}{The parameter is used for RANSAC or LMedS methods only. It denotes the desirable level of confidence (between 0 and 1) that the estimated matrix is correct}
1303 \cvarg{mask}{The optional output array of $N$ elements, every element of which is set to 0 for outliers and to 1 for the other points. The array is computed only in RANSAC and LMedS methods. Other methods set every element to 1}
1304 \end{description}
1305
1306 The epipolar geometry is described by the following equation:
1307
1308 \[ [p_2; 1]^T F [p_1; 1] = 0 \]
1309
1310 where $F$ is fundamental matrix, $p_1$ and $p_2$ are corresponding points in the first and the second images, respectively.
1311
1312 The function calculates the fundamental
1313 matrix using one of four methods listed above and returns the found fundamental matrix. In the case of \texttt{FM\_7POINT} the function may return a $9 \times 3$ matrix. It means that the 3 fundamental matrices are possible and they are all found and stored sequentially.
1314
1315 The calculated fundamental matrix may be passed further to
1316 \texttt{computeCorrespondEpilines} that finds the epipolar lines
1317 corresponding to the specified points. It can also be passed to \cvCppCross{stereoRectifyUncalibrated} to compute the rectification transformation.
1318
1319 \begin{lstlisting}
1320 // Example. Estimation of fundamental matrix using RANSAC algorithm
1321 int point_count = 100;
1322 vector<Point2f> points1(point_count);
1323 vector<Point2f> points2(point_count);
1324
1325 // initialize the points here ... */
1326 for( int i = 0; i < point_count; i++ )
1327 {
1328     points1[i] = ...;
1329     points2[i] = ...;
1330 }
1331
1332 Mat fundamental_matrix =
1333  findFundamentalMat(points1, points2, FM_RANSAC, 3, 0.99);
1334 \end{lstlisting}
1335
1336
1337 \cvCppFunc{findChessboardCorners}
1338 Finds the positions of the internal corners of the chessboard.
1339
1340 \begin{lstlisting}
1341 bool findChessboardCorners( const Mat& image, Size patternSize,
1342                             vector<Point2f>& corners,
1343                             int flags=CV_CALIB_CB_ADAPTIVE_THRESH+
1344                                  CV_CALIB_CB_NORMALIZE_IMAGE );
1345 enum { CALIB_CB_ADAPTIVE_THRESH = CV_CALIB_CB_ADAPTIVE_THRESH,
1346     CALIB_CB_NORMALIZE_IMAGE = CV_CALIB_CB_NORMALIZE_IMAGE,
1347     CALIB_CB_FILTER_QUADS = CV_CALIB_CB_FILTER_QUADS };
1348 \end{lstlisting}
1349 \begin{description}
1350 \cvarg{image}{The input chessboard (a.k.a. checkerboard) view; it must be an 8-bit grayscale or color image}
1351 \cvarg{patternSize}{The number of inner corners per chessboard row and column, i.e.
1352 \texttt{patternSize = cvSize(<points per row>, <points per column>)}}
1353 \cvarg{corners}{The output vector of the corners detected. If the board is found (the function returned true), the corners should be properly ordered.}
1354 \cvarg{flags}{Various operation flags, can be 0 or a combination of the following values:
1355 \begin{description}
1356  \cvarg{CALIB\_CB\_ADAPTIVE\_THRESH}{use adaptive thresholding, instead of a fixed-level threshold, to convert the image to black and white rather than a fixed threshold level}
1357  \cvarg{CALIB\_CB\_NORMALIZE\_IMAGE}{normalize the image brightness and contrast using \cvCppCross{equalizeHist} before applying fixed or adaptive thresholding}
1358  \cvarg{CALIB\_CB\_FILTER\_QUADS}{use some additional criteria (like contour area, perimeter, square-like shape) to filter out false quads that are extracted at the contour retrieval stage. Since the current corner grouping engine is smart enough, usually this parameter is omitted.}
1359 \end{description}}
1360 \end{description}
1361
1362 The function attempts to determine
1363 whether the input image is a view of the chessboard pattern and, if yes,
1364 locate the internal chessboard corners. The function returns true if all
1365 of the chessboard corners have been found and they have been placed
1366 in a certain order (row by row, left to right in every row),
1367 otherwise, if the function fails to find all the corners or reorder
1368 them, it returns 0. For example, a regular chessboard has 8 x 8
1369 squares and 7 x 7 internal corners, that is, points, where the black
1370 squares touch each other. The coordinates detected are approximate,
1371 and to determine their position more accurately, the user may use
1372 the function \cvCppCross{cornerSubPix} or other subpixel adjustment technique.
1373
1374 Sometimes the function fails to find the board because the image is too large or too small. If so, try to resize it and then scale the found corners coordinates back (or even scale the computed \texttt{cameraMatrix} back).
1375
1376
1377 \cvCppFunc{getDefaultNewCameraMatrix}
1378 Returns the default new camera matrix
1379
1380 \begin{lstlisting}
1381 Mat getDefaultNewCameraMatrix( const Mat& cameraMatrix, Size imgSize=Size(),
1382                                bool centerPrincipalPoint=false );
1383 \end{lstlisting}
1384 \begin{description}
1385 \cvarg{cameraMatrix}{The input camera matrix}
1386 \cvarg{imageSize}{The camera view image size in pixels}
1387 \cvarg{centerPrincipalPoint}{Indicates whether in the new camera matrix the principal point should be at the image center or not}
1388 \end{description}
1389
1390 The function returns the camera matrix that is either an exact copy of the input \texttt{cameraMatrix} (when \texttt{centerPrinicipalPoint=false}), or the modified one (when \texttt{centerPrincipalPoint}=true).
1391
1392 In the latter case the new camera matrix will be:
1393
1394 \[\begin{bmatrix}
1395 f_x && 0 && (\texttt{imgSize.width}-1)*0.5 \\
1396 0 && f_y && (\texttt{imgSize.height}-1)*0.5 \\
1397 0 && 0 && 1
1398 \end{bmatrix},\]
1399
1400 where $f_x$ and $f_y$ are $(0,0)$ and $(1,1)$ elements of \texttt{cameraMatrix}, respectively.
1401
1402 By default, the undistortion functions in OpenCV (see \texttt{initUndistortRectifyMap}, \texttt{undistort}) do not move the principal point. However, when you work with stereo, it's important to move the principal points in both views to the same y-coordinate (which is required by most of stereo correspondence algorithms), and maybe to the same x-coordinate too. So you can form the new camera matrix for each view, where the principal points will be at the center. 
1403
1404 \cvCppFunc{initCameraMatrix2D}
1405 Finds the initial camera matrix from the 3D-2D point correspondences
1406
1407 \begin{lstlisting}
1408 Mat initCameraMatrix2D( const vector<vector<Point3f> >& objectPoints,
1409                         const vector<vector<Point2f> >& imagePoints,
1410                         Size imageSize, double aspectRatio=1. );
1411 \end{lstlisting}
1412 \begin{description}
1413 \cvarg{objectPoints}{The vector of vectors of the object points. See \cvCppCross{calibrateCamera}}
1414 \cvarg{imagePoints}{The vector of vectors of the corresponding image points. See \cvCppCross{calibrateCamera}}
1415 \cvarg{imageSize}{The image size in pixels; used to initialize the principal point}
1416 \cvarg{aspectRatio}{If it is zero or negative, both $f_x$ and $f_y$ are estimated independently. Otherwise $f_x = f_y * \texttt{aspectRatio}$}
1417 \end{description}
1418
1419 The function estimates and returns the initial camera matrix for camera calibration process.
1420 Currently, the function only supports planar calibration rigs, i.e. the rig for which the $3 \times 3$ covariance matrix of object points is singular.
1421
1422
1423 \cvCppFunc{Rodrigues}
1424 Converts a rotation matrix to a rotation vector or vice versa.
1425
1426 \begin{lstlisting}
1427 void Rodrigues(const Mat& src, Mat& dst);
1428 void Rodrigues(const Mat& src, Mat& dst, Mat& jacobian);
1429 \end{lstlisting}
1430
1431 \begin{description}
1432 \cvarg{src}{The input rotation vector (3x1 or 1x3) or a rotation matrix (3x3)}
1433 \cvarg{dst}{The output rotation matrix (3x3) or a rotation vector (3x1 or 1x3), respectively}
1434 \cvarg{jacobian}{The optional output Jacobian matrix, 3x9 or 9x3 - partial derivatives of the output array components with respect to the input array components}
1435 \end{description}
1436
1437 The functions convert a rotation vector to a rotation matrix or vice versa. A rotation vector is a compact representation of rotation matrix. Direction of the rotation vector is the rotation axis and the length of the vector is the rotation angle around the axis. The rotation matrix $R$, corresponding to the rotation vector $r$, is computed as following:
1438
1439 \[
1440 \begin{array}{l}
1441 \theta \leftarrow norm(r)\\
1442 r \leftarrow r/\theta\\
1443 R = \cos{\theta} I + (1-\cos{\theta}) r r^T + \sin{\theta}
1444 \vecthreethree
1445 {0}{-r_z}{r_y}
1446 {r_z}{0}{-r_x}
1447 {-r_y}{r_x}{0}
1448 \end{array}
1449 \]
1450
1451 Inverse transformation can also be done easily, since
1452
1453 \[
1454 \sin(\theta)
1455 \vecthreethree
1456 {0}{-r_z}{r_y}
1457 {r_z}{0}{-r_x}
1458 {-r_y}{r_x}{0}
1459 =
1460 \frac{R - R^T}{2}
1461 \]
1462
1463 A rotation vector is a convenient and most-compact representation of a rotation matrix
1464 (since any rotation matrix has just 3 degrees of freedom). The representation is
1465 used in the global 3D geometry optimization procedures like \cvCppCross{calibrateCamera}, \cvCppCross{stereoCalibrate} or \cvCppCross{solvePnP}.
1466
1467
1468 \cvCppFunc{RQDecomp3x3}
1469 Computes the 'RQ' decomposition of 3x3 matrices.
1470
1471 \begin{lstlisting}
1472 /* Computes RQ decomposition for 3x3 matrices */
1473 void RQDecomp3x3( const Mat& M, Mat& R, Mat& Q );
1474 Vec3d RQDecomp3x3( const Mat& M, Mat& R, Mat& Q,
1475                    Mat& Qx, Mat& Qy, Mat& Qz );
1476 \end{lstlisting}
1477 \begin{description}
1478 \cvarg{M}{The input $3 \times 3$ floating-point matrix}
1479 \cvarg{R}{The output $3 \times 3$ upper-triangular matrix}
1480 \cvarg{Q}{The output $3 \times 3$ orthogonal matrix}
1481 \cvarg{Qx, Qy, Qz}{The optional output matrices that decompose the rotation matrix Q into separate rotation matrices for each coordinate axis}
1482 \end{description}
1483
1484 The function implements RQ decomposition of a $3 \times 3$ matrix. The function is by \cvCppCross{decomposeProjectionMatrix}.
1485
1486 \cvCppFunc{matMulDeriv}
1487 Computes partial derivatives of the matrix product w.r.t each multiplied matrix
1488
1489 \begin{lstlisting}
1490 void matMulDeriv( const Mat& A, const Mat& B, Mat& dABdA, Mat& dABdB );
1491 \end{lstlisting}
1492 \begin{description}
1493 \cvarg{A}{The first multiplied matrix}
1494 \cvarg{B}{The second multiplied matrix}
1495 \cvarg{dABdA}{The first output derivative matrix \texttt{d(A*B)/dA} of size $\texttt{A.rows*B.cols} \times {A.rows*A.cols}$}
1496 \cvarg{dABdA}{The second output derivative matrix \texttt{d(A*B)/dB} of size $\texttt{A.rows*B.cols} \times {B.rows*B.cols}$}
1497 \end{description}
1498
1499 The function computes the partial derivatives of the elements of the matrix product $A*B$ w.r.t. the elements of each of the two input matrices. The function is used to compute Jacobian matrices in \cvCppCross{stereoCalibrate}, but can also be used in any other similar optimization function.
1500
1501 \cvCppFunc{projectPoints}
1502 Projects 3D points on to an image plane.
1503
1504 \begin{lstlisting}
1505 void projectPoints( const Mat& objectPoints,
1506                     const Mat& rvec, const Mat& tvec,
1507                     const Mat& cameraMatrix,
1508                     const Mat& distCoeffs,
1509                     vector<Point2f>& imagePoints );
1510
1511 void projectPoints( const Mat& objectPoints,
1512                     const Mat& rvec, const Mat& tvec,
1513                     const Mat& cameraMatrix,
1514                     const Mat& distCoeffs,
1515                     vector<Point2f>& imagePoints,
1516                     Mat& dpdrot, Mat& dpdt, Mat& dpdf,
1517                     Mat& dpdc, Mat& dpddist,
1518                     double aspectRatio=0 );
1519 \end{lstlisting}
1520 \begin{description}
1521 \cvarg{objectPoints}{The input array of 3D object points, a matrix of type \texttt{CV\_32FC3} or \texttt{vector<Point3f>}}
1522 \cvarg{imagePoints}{The output array of 2D image points}
1523 \cvarg{rvec}{The rotation vector, 1x3 or 3x1}
1524 \cvarg{tvec}{The translation vector, 1x3 or 3x1}
1525 \cvarg{cameraMatrix}{The camera matrix $\vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}$}
1526 \cvarg{distCoeffs}{The array of distortion coefficients, 4x1, 5x1, 1x4 or 1x5 $k_1, k_2, p_1, p_2[, k_3]$. If the matrix is empty, the function uses zero distortion coefficients}
1527 \cvarg{dpdrot, dpdt, dpdf, dpdc, dpdist}{The optional matrices of the partial derivatives of the computed point projections w.r.t the rotation vector, the translation vector, $f_x$ and $f_y$, $c_x$ and $c_y$ and the distortion coefficients respectively. Each matrix has $2*N$ rows (where $N$ is the number of points) - even rows (0th, 2nd ...) are the derivatives of the x-coordinates w.r.t. the camera parameters and odd rows (1st, 3rd ...) are the derivatives of the y-coordinates.}
1528 \cvarg{aspectRatio}{If zero or negative, $f_x$ and $f_y$ are treated as independent variables, otherwise they $f_x = f_y*\texttt{aspectRatio}$, so the derivatives are adjusted appropriately}
1529 \end{description}
1530
1531 The function computes projections of 3D
1532 points to the image plane given intrinsic and extrinsic camera
1533 parameters. Optionally, the function computes jacobians - matrices
1534 of partial derivatives of image points as functions of all the
1535 input parameters with respect to the particular camera parameters, intrinsic and/or
1536 extrinsic. The computed jacobians are used during the global optimization
1537 in \cvCppCross{calibrateCamera}, \cvCppCross{stereoCalibrate} and \cvCppCross{solvePnP}.
1538
1539 Note, that by setting \texttt{rvec=tvec=(0,0,0)} or by setting \texttt{cameraMatrix=Mat::eye(3,3,CV\_64F)} or by setting \texttt{distCoeffs=Mat()} you can get various useful partial cases of the function, i.e. you can computed the distorted coordinates for a sparse set of points, or apply a perspective transformation (and also compute the derivatives) in the ideal zero-distortion setup etc.
1540
1541 \cvCppFunc{reprojectImageTo3D}
1542 Reprojects disparity image to 3D space.
1543
1544 \begin{lstlisting}
1545 void reprojectImageTo3D( const Mat& disparity,
1546                          Mat& _3dImage, const Mat& Q,
1547                          bool handleMissingValues=false );
1548 \end{lstlisting}
1549 \begin{description}
1550 \cvarg{disparity}{The input single-channel 16-bit signed or 32-bit floating-point disparity image}
1551 \cvarg{\_3dImage}{The output 3-channel floating-point image of the same size as \texttt{disparity}.
1552  Each element of \texttt{\_3dImage(x,y)} will contain the 3D coordinates of the point \texttt{(x,y)}, computed from the disparity map.}
1553 \cvarg{Q}{The $4 \times 4$ perspective transformation matrix that can be obtained with \cvCppCross{stereoRectify}}
1554 \cvarg{handleMissingValues}{If true, when the pixels with the minimal disparity (that corresponds to the ouliers; see \cvCppCross{StereoBM}) will be transformed to 3D points with some very large Z value (currently set to 10000)}
1555 \end{description}
1556  
1557 The function transforms 1-channel disparity map to 3-channel image representing a 3D surface. That is, for each pixel \texttt{(x,y)} and the corresponding disparity \texttt{d=disparity(x,y)} it computes: 
1558
1559 \[\begin{array}{l}
1560 [X\; Y\; Z\; W]^T = \texttt{Q}*[x\; y\; \texttt{disparity}(x,y)\; 1]^T \\
1561 \texttt{\_3dImage}(x,y) = (X/W,\; Y/W,\; Z/W)
1562 \end{array}\]
1563
1564 The matrix \texttt{Q} can be arbitrary $4 \times 4$ matrix, e.g. the one computed by \cvCppCross{stereoRectify}. To reproject a sparse set of points {(x,y,d),...} to 3D space, use \cvCppCross{perspectiveTransform}.  
1565
1566
1567 \cvCppFunc{solvePnP}
1568 Finds the camera pose from the 3D-2D point correspondences
1569
1570 \begin{lstlisting}
1571 void solvePnP( const Mat& objectPoints,
1572                const Mat& imagePoints,
1573                const Mat& cameraMatrix,
1574                const Mat& distCoeffs,
1575                Mat& rvec, Mat& tvec,
1576                bool useExtrinsicGuess=false );
1577 \end{lstlisting}
1578 \begin{description}
1579 \cvarg{objectPoints}{The array of object points, a matrix of type \texttt{CV\_32FC3} or \texttt{vector<Point3f>}}
1580 \cvarg{imagePoints}{The array of the corresponding image points, a matrix of type{CV\_32FC2} or \texttt{vector<Point2f>}}
1581 \cvarg{cameraMatrix}{The input camera matrix $\vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}$}
1582 \cvarg{distCoeffs}{The input 4x1, 5x1, 1x4 or 1x5 array of distortion coefficients $(k_1, k_2, p_1, p_2[, k3])$. If it is NULL, all of the distortion coefficients are set to 0}
1583 \cvarg{rvec}{The output camera view rotation vector (compact representation of a rotation matrix, \cvCppCross{Rodrigues} that (together with \texttt{tvec}) brings points from the model coordinate system to the camera coordinate system}
1584 \cvarg{tvec}{The output camera view translation vector}
1585 \end{description}
1586
1587 The function estimates the camera pose given a set of object points, their corresponding image projections, as well as the camera matrix and the distortion coefficients. This function finds such a pose that minimizes back-projection error, i.e. the sum of squared distances between the observed projections \texttt{imagePoints} and the projected with \cvCppCross{projectPoints} \texttt{objectPoints}.
1588
1589 \cvCppFunc{stereoCalibrate}
1590 Calibrates stereo camera.
1591
1592 \begin{lstlisting}
1593 void stereoCalibrate( const vector<vector<Point3f> >& objectPoints,
1594                       const vector<vector<Point2f> >& imagePoints1,
1595                       const vector<vector<Point2f> >& imagePoints2,
1596                       Mat& cameraMatrix1, Mat& distCoeffs1,
1597                       Mat& cameraMatrix2, Mat& distCoeffs2,
1598                       Size imageSize, Mat& R, Mat& T,
1599                       Mat& E, Mat& F,
1600                       TermCriteria criteria = TermCriteria(TermCriteria::COUNT+
1601                          TermCriteria::EPS, 30, 1e-6),
1602                       int flags=CALIB_FIX_INTRINSIC );
1603 \end{lstlisting}
1604 \begin{description}
1605 \cvarg{objectPoints}{The vector of vectors of points on the calibration rig in its coordinate system, one vector per a view of the rig. See \cvCppCross{calibrateCamera}}
1606 \cvarg{imagePoints1}{The vector of vectors of the object point projections to the first camera views, one vector per a view. The projections must be in the same order as the corresponding object points.}
1607 \cvarg{imagePoints2}{The vector of vectors of the object point projections to the second camera views, one vector per a view. The projections must be in the same order as the corresponding object points.}
1608 \cvarg{imageSize}{Size of the image, used only to initialize the intrinsic camera matrices}
1609 \cvarg{cameraMatrix1, cameraMatrix2}{The input/output first and second camera matrices, respectively: $ \vecthreethree{f_x^{(j)}}{0}{c_x^{(j)}}{0}{f_y^{(j)}}{c_y^{(j)}}{0}{0}{1}$, $j = 0,\, 1$. If any of \texttt{CALIB\_USE\_INTRINSIC\_GUESS}, \texttt{CALIB\_FIX\_ASPECT\_RATIO},
1610 \texttt{CALIB\_FIX\_INTRINSIC} or \texttt{CALIB\_FIX\_FOCAL\_LENGTH} are specified, some or all of the matrices' components must be initialized}
1611 \cvarg{distCoeffs1, distCoeffs2}{The input/output lens distortion coefficients for the first and the second cameras, 4x1, 5x1, 1x4 or 1x5 floating-point vectors $k_1^{(j)}, k_2^{(j)}, p_1^{(j)}, p_2^{(j)}[, k_3^{(j)}]$, $j = 0,\, 1$. If any of \texttt{CALIB\_FIX\_K1}, \texttt{CALIB\_FIX\_K2} or \texttt{CALIB\_FIX\_K3} is specified, then the corresponding elements of the distortion coefficients must be initialized.}
1612 \cvarg{R}{The output rotation matrix between the 1st and the 2nd cameras' coordinate systems.}
1613 \cvarg{T}{The output translation vector between the cameras' coordinate systems.}
1614 \cvarg{E}{The output essential matrix.}
1615 \cvarg{F}{The output fundamental matrix.}
1616 \cvarg{criteria}{The termination criteria for the iterative optimiziation algorithm.}
1617 \cvarg{flags}{Different flags, may be 0 or combination of the following values:
1618 \begin{description}
1619 \cvarg{CALIB\_FIX\_INTRINSIC}{If it is set, \texttt{cameraMatrix?}, as well as \texttt{distCoeffs?} are fixed, so that only \texttt{R, T, E} and \texttt{F} are estimated.}
1620 \cvarg{CALIB\_USE\_INTRINSIC\_GUESS}{The flag allows the function to optimize some or all of the intrinsic parameters, depending on the other flags, but the initial values are provided by the user.}
1621 \cvarg{CALIB\_FIX\_PRINCIPAL\_POINT}{The principal points are fixed during the optimization.}
1622 \cvarg{CALIB\_FIX\_FOCAL\_LENGTH}{$f^{(j)}_x$ and $f^{(j)}_y$ are fixed.}
1623 \cvarg{CALIB\_FIX\_ASPECT\_RATIO}{$f^{(j)}_y$ is optimized, but the ratio $f^{(j)}_x/f^{(j)}_y$ is fixed.}
1624 \cvarg{CALIB\_SAME\_FOCAL\_LENGTH}{Enforces $f^{(0)}_x=f^{(1)}_x$ and $f^{(0)}_y=f^{(1)}_y$} \cvarg{CALIB\_ZERO\_TANGENT\_DIST}{Tangential distortion coefficients for each camera are set to zeros and fixed there.}
1625 \cvarg{CALIB\_FIX\_K1, CALIB\_FIX\_K2, CALIB\_FIX\_K3}{Fixes the corresponding radial distortion coefficient (the coefficient must be passed to the function)}
1626 \end{description}}
1627 \end{description}
1628
1629 The function estimates transformation between the 2 cameras - heads of a stereo pair. If we have a stereo camera, where the relative position and orientatation of the 2 cameras is fixed, and if we computed poses of an object relative to the fist camera and to the second camera, $(R^{(1)}, T^{(1)})$ and $(R^{(2)}, T^{(2)})$, respectively (that can be done with \cvCppCross{solvePnP}), then, obviously, those poses will relate to each other, by knowing only one of $(R^{(j)}, T^{(j)})$ we can compute the other one:
1630
1631 \[\begin{array}{l}
1632 R^{(2)}=R*R^{(1)} \\
1633 T^{(2)}=R*T^{(1)} + T,
1634 \end{array}
1635 \]
1636
1637 And, vice versa, if we computed both $(R^{(1)}, T^{(1)})$ and $(R^{(2)}, T^{(2)})$, we can compute the relative position and orientation of the 2 cameras as following:
1638
1639 \[\begin{array}{l}
1640 R=R^{(2)} {R^{(1)}}^{-1} \\
1641 T=T^{(2)} - R^{(2)} {R^{(1)}}^{-1}*T^{(1)}
1642 \end{array}
1643 \]
1644
1645 The function uses this idea, but the actual algorithm is more complex to take all the available pairs of the camera views into account.
1646
1647 Also, the function computes the essential matrix \texttt{E}:
1648
1649 \[
1650 E=
1651 \vecthreethree
1652 {0}{-T_2}{T_1}
1653 {T_2}{0}{-T_0}
1654 {-T_1}{T_0}{0}
1655 *R,
1656 \]
1657
1658 where $T_i$ are components of the translation vector $T:\,T=[T_0, T_1, T_2]^T$,
1659 and the fundamental matrix \texttt{F}:
1660
1661 \[F = cameraMatrix2^{-T} \cdot E \cdot cameraMatrix1^{-1}\]
1662
1663 Besides the stereo-related information, the function can also perform full calibration of each of the 2 cameras. However, because of the high dimensionality of the parameter space and noise in the input data the function can diverge from the correct solution. Thus, if the intrinsic parameters can be estimated with high accuracy for each of the cameras individually (e.g. using \cvCppCross{calibrateCamera}), it is recommended to do so and then pass \texttt{CALIB\_FIX\_INTRINSIC} flag to the function along with the computed intrinsic parameters. Otherwise, if all the parameters are needed to be estimated at once, it makes sense to restrict some parameters, e.g. pass \texttt{CALIB\_SAME\_FOCAL\_LENGTH} and \texttt{CALIB\_ZERO\_TANGENT\_DIST} flags, which are usually reasonable assumptions. 
1664
1665
1666 \cvCppFunc{stereoRectify}
1667 Computes rectification transforms for each head of a calibrated stereo camera.
1668
1669 \begin{lstlisting}
1670 void stereoRectify( const Mat& cameraMatrix1, const Mat& distCoeffs1,
1671                     const Mat& cameraMatrix2, const Mat& distCoeffs2,
1672                     Size imageSize, const Mat& R, const Mat& T,
1673                     Mat& R1, Mat& R2, Mat& P1, Mat& P2, Mat& Q,
1674                     int flags=CALIB_ZERO_DISPARITY );
1675 \end{lstlisting}
1676 \begin{description}
1677 \cvarg{cameraMatrix1, cameraMatrix2}{The camera matrices $\vecthreethree{f_x^{(j)}}{0}{c_x^{(j)}}{0}{f_y^{(j)}}{c_y^{(j)}}{0}{0}{1}$}
1678 \cvarg{distCoeffs1, distCoeffs2}{The vectors of distortion coefficients for each camera, \cvCppCross{4x1, 1x4, 5x1 or 1x5}}
1679 \cvarg{imageSize}{Size of the image used for stereo calibration.}
1680 \cvarg{R}{The input rotation matrix between the 1st and the 2nd cameras' coordinate systems; can be computed with \cvCppCross{stereoCalibrate}.}
1681 \cvarg{T}{The translation vector between the cameras' coordinate systems; can be computed with \cvCppCross{stereoCalibrate}.}
1682 \cvarg{R1, R2}{The output $3 \times 3$ rectification transforms (rotation matrices) for the first and the second cameras, respectively.}
1683 \cvarg{P1, P2}{The output $3 \times 4$ projection matrices in the new (rectified) coordinate systems.}
1684 \cvarg{Q}{The output $4 \times 4$ disparity-to-depth mapping matrix, see \cvCppCross{reprojectImageTo3D}.}
1685 \cvarg{flags}{The operation flags; may be 0 or \texttt{CALIB\_ZERO\_DISPARITY}. If the flag is set, the function makes the principal points of each camera have the same pixel coordinates in the rectified views. And if the flag is not set, the function may still shift the images in horizontal or vertical direction (depending on the orientation of epipolar lines) in order to maximize the useful image area.}
1686 \end{description}
1687
1688 The function computes the rotation matrices for each camera that (virtually) make both camera image planes the same plane. Consequently, that makes all the epipolar lines parallel and thus simplifies the dense stereo correspondence problem. On input the function takes the matrices computed by \cvCppCross{stereoCalibrate} and on output it gives 2 rotation matrices and also 2 projection matrices in the new coordinates. The 2 cases are distinguished by the function are: 
1689
1690 \begin{enumerate}
1691 \item{Horizontal stereo, when 1st and 2nd camera views are shifted relative to each other mainly along the x axis (with possible small vertical shift). Then in the rectified images the corresponding epipolar lines in left and right cameras will be horizontal and have the same y-coordinate. P1 and P2 will look as: 
1692
1693 \[
1694 \texttt{P1}=
1695 \begin{bmatrix}
1696 f & 0 & cx_1 & 0\\
1697 0 & f & cy & 0\\
1698 0 & 0 & 1 & 0
1699 \end{bmatrix}
1700 \]
1701 \[
1702 \texttt{P2}=
1703 \begin{bmatrix}
1704 f & 0 & cx_2 & T_x*f\\
1705 0 & f & cy & 0\\
1706 0 & 0 & 1 & 0
1707 \end{bmatrix}
1708 ,
1709 \]
1710
1711 where $T_x$ is horizontal shift between the cameras and $cx_1=cx_2$ if \texttt{CALIB\_ZERO\_DISPARITY} is set.}
1712 \item{Vertical stereo, when 1st and 2nd camera views are shifted relative to each other mainly in vertical direction (and probably a bit in the horizontal direction too). Then the epipolar lines in the rectified images will be vertical and have the same x coordinate. P2 and P2 will look as:
1713
1714 \[
1715 P1=
1716 \begin{bmatrix}
1717 f & 0 & cx & 0\\
1718 0 & f & cy_1 & 0\\
1719 0 & 0 & 1 & 0
1720 \end{bmatrix}
1721 \]
1722 \[
1723 P2=
1724 \begin{bmatrix}
1725 f & 0 & cx & 0\\
1726 0 & f & cy_2 & T_y*f\\
1727 0 & 0 & 1 & 0
1728 \end{bmatrix}
1729 ,
1730 \]
1731
1732 where $T_y$ is vertical shift between the cameras and $cy_1=cy_2$ if \texttt{CALIB\_ZERO\_DISPARITY} is set.}
1733 \end{enumerate} 
1734
1735 As you can see, the first 3 columns of \texttt{P1} and \texttt{P2} will effectively be the new "rectified" camera matrices. 
1736 The matrices, together with \texttt{R1} and \texttt{R2}, can then be passed to \cvCppCross{initUndistortRectifyMap} to initialize the rectification map for each camera.  
1737
1738 \cvCppFunc{stereoRectifyUncalibrated}
1739 Computes rectification transforms for each head of an uncalibrated stereo camera.
1740
1741 \begin{lstlisting}
1742 bool stereoRectifyUncalibrated( const Mat& points1,
1743                                 const Mat& points2,
1744                                 const Mat& F, Size imgSize,
1745                                 Mat& H1, Mat& H2,
1746                                 double threshold=5 );
1747 \end{lstlisting}
1748 \begin{description}
1749 \cvarg{points1, points2}{The two arrays of corresponding 2D points.}
1750 \cvarg{F}{Fundamental matrix. It can be computed using the same set of point pairs \texttt{points1} and \texttt{points2}  using \cvCppCross{findFundamentalMat}.}
1751 \cvarg{imageSize}{Size of the image.}
1752 \cvarg{H1, H2}{The output rectification homography matrices for the first and for the second images.}
1753 \cvarg{threshold}{Optional threshold used to filter out the outliers. If the parameter is greater than zero, then all the point pairs that do not comply the epipolar geometry well enough (that is, the points for which $|\texttt{points2[i]}^T*\texttt{F}*\texttt{points1[i]}|>\texttt{threshold}$) are rejected prior to computing the homographies.}
1754 \end{description}
1755
1756 The function computes the rectification transformations without knowing intrinsic parameters of the cameras and their relative position in space, hence the suffix "Uncalibrated". Another related difference from \cvCppCross{stereoRectify} is that the function outputs not the rectification transformations in the object (3D) space, but the planar perspective transformations, encoded by the homography matrices \texttt{H1} and \texttt{H2}. The function implements the algorithm \cite{Hartley99}. 
1757
1758 Note that while the algorithm does not need to know the intrinsic parameters of the cameras, it heavily depends on the epipolar geometry. Therefore, if the camera lenses have significant distortion, it would better be corrected before computing the fundamental matrix and calling this function. For example, distortion coefficients can be estimated for each head of stereo camera separately by using \cvCppCross{calibrateCamera} and then the images can be corrected using \cvCppCross{undistort}, or just the point coordinates can be corrected with \cvCppCross{undistortPoints}. 
1759
1760 \cvCppFunc{StereoBM}
1761 The class for computing stereo correspondence using block matching algorithm.
1762
1763 \begin{lstlisting}
1764 // Block matching stereo correspondence algorithm
1765 class StereoBM
1766 {
1767     enum { NORMALIZED_RESPONSE = CV_STEREO_BM_NORMALIZED_RESPONSE,
1768         BASIC_PRESET=CV_STEREO_BM_BASIC,
1769         FISH_EYE_PRESET=CV_STEREO_BM_FISH_EYE,
1770         NARROW_PRESET=CV_STEREO_BM_NARROW };
1771     
1772     StereoBM();
1773     // the preset is one of ..._PRESET above.
1774     // ndisparities is the size of disparity range,
1775     // in which the optimal disparity at each pixel is searched for.
1776     // SADWindowSize is the size of averaging window used to match pixel blocks
1777     //    (larger values mean better robustness to noise, but yield blurry disparity maps)
1778     StereoBM(int preset, int ndisparities=0, int SADWindowSize=21);
1779     // separate initialization function
1780     void init(int preset, int ndisparities=0, int SADWindowSize=21);
1781     // computes the disparity for the two rectified 8-bit single-channel images.
1782     // the disparity will be 16-bit singed image of the same size as left.
1783     void operator()( const Mat& left, const Mat& right, Mat& disparity );
1784
1785     Ptr<CvStereoBMState> state;
1786 };
1787 \end{lstlisting}
1788
1789 \cvCppFunc{undistortPoints}
1790 Computes the ideal point coordinates from the observed point coordinates.
1791
1792 \begin{lstlisting}
1793 void undistortPoints( const Mat& src, vector<Point2f>& dst,
1794                       const Mat& cameraMatrix, const Mat& distCoeffs,
1795                       const Mat& R=Mat(), const Mat& P=Mat());
1796 void undistortPoints( const Mat& src, Mat& dst,
1797                       const Mat& cameraMatrix, const Mat& distCoeffs,
1798                       const Mat& R=Mat(), const Mat& P=Mat());
1799 \end{lstlisting}
1800 \begin{description}
1801 \cvarg{src}{The observed point coordinates, a matrix or vector of 2D points.}
1802 \cvarg{dst}{The ideal point coordinates, after undistortion and reverse perspective transformation}
1803 \cvarg{cameraMatrix}{The camera matrix $\vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}$}
1804 \cvarg{distCoeffs}{he vector of distortion coefficients, \cvCppCross{4x1, 1x4, 5x1 or 1x5}}
1805 \cvarg{R}{The rectification transformation in object space (3x3 matrix). \texttt{R1} or \texttt{R2}, computed by \cvCppCross{StereoRectify} can be passed here. If the matrix is empty, the identity transformation is used}
1806 \cvarg{P}{The new camera matrix (3x3) or the new projection matrix (3x4). \texttt{P1} or \texttt{P2}, computed by \cvCppCross{StereoRectify} can be passed here. If the matrix is empty, the identity new camera matrix is used}
1807 \end{description}
1808
1809 The function is similar to \cvCppCross{undistort} and \cvCppCross{initUndistortRectifyMap}, but it operates on a sparse set of points instead of a raster image. Also the function does some kind of reverse transformation to \cvCppCross{projectPoints} (in the case of 3D object it will not reconstruct its 3D coordinates, of course; but for a planar object it will, up to a translation vector, if the proper \texttt{R} is specified).
1810
1811 \fi