1 /*M/////////////////////////////////////////////////////////////////////////////////////// 2 // 3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 4 // 5 // By downloading, copying, installing or using the software you agree to this license. 6 // If you do not agree to this license, do not download, install, 7 // copy or use the software. 8 // 9 // 10 // License Agreement 11 // For Open Source Computer Vision Library 12 // 13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. 14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved. 15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved. 16 // Third party copyrights are property of their respective owners. 17 // 18 // Redistribution and use in source and binary forms, with or without modification, 19 // are permitted provided that the following conditions are met: 20 // 21 // * Redistribution's of source code must retain the above copyright notice, 22 // this list of conditions and the following disclaimer. 23 // 24 // * Redistribution's in binary form must reproduce the above copyright notice, 25 // this list of conditions and the following disclaimer in the documentation 26 // and/or other materials provided with the distribution. 27 // 28 // * The name of the copyright holders may not be used to endorse or promote products 29 // derived from this software without specific prior written permission. 30 // 31 // This software is provided by the copyright holders and contributors "as is" and 32 // any express or implied warranties, including, but not limited to, the implied 33 // warranties of merchantability and fitness for a particular purpose are disclaimed. 34 // In no event shall the Intel Corporation or contributors be liable for any direct, 35 // indirect, incidental, special, exemplary, or consequential damages 36 // (including, but not limited to, procurement of substitute goods or services; 37 // loss of use, data, or profits; or business interruption) however caused 38 // and on any theory of liability, whether in contract, strict liability, 39 // or tort (including negligence or otherwise) arising in any way out of 40 // the use of this software, even if advised of the possibility of such damage. 41 // 42 //M*/ 43 44 #ifndef OPENCV_CALIB3D_HPP 45 #define OPENCV_CALIB3D_HPP 46 47 #include "opencv2/core.hpp" 48 #include "opencv2/features2d.hpp" 49 #include "opencv2/core/affine.hpp" 50 51 /** 52 @defgroup calib3d Camera Calibration and 3D Reconstruction 53 54 The functions in this section use a so-called pinhole camera model. The view of a scene 55 is obtained by projecting a scene's 3D point \f$P_w\f$ into the image plane using a perspective 56 transformation which forms the corresponding pixel \f$p\f$. Both \f$P_w\f$ and \f$p\f$ are 57 represented in homogeneous coordinates, i.e. as 3D and 2D homogeneous vector respectively. You will 58 find a brief introduction to projective geometry, homogeneous vectors and homogeneous 59 transformations at the end of this section's introduction. For more succinct notation, we often drop 60 the 'homogeneous' and say vector instead of homogeneous vector. 61 62 The distortion-free projective transformation given by a pinhole camera model is shown below. 63 64 \f[s \; p = A \begin{bmatrix} R|t \end{bmatrix} P_w,\f] 65 66 where \f$P_w\f$ is a 3D point expressed with respect to the world coordinate system, 67 \f$p\f$ is a 2D pixel in the image plane, \f$A\f$ is the intrinsic camera matrix, 68 \f$R\f$ and \f$t\f$ are the rotation and translation that describe the change of coordinates from 69 world to camera coordinate systems (or camera frame) and \f$s\f$ is the projective transformation's 70 arbitrary scaling and not part of the camera model. 71 72 The intrinsic camera matrix \f$A\f$ (notation used as in @cite Zhang2000 and also generally notated 73 as \f$K\f$) projects 3D points given in the camera coordinate system to 2D pixel coordinates, i.e. 74 75 \f[p = A P_c.\f] 76 77 The camera matrix \f$A\f$ is composed of the focal lengths \f$f_x\f$ and \f$f_y\f$, which are 78 expressed in pixel units, and the principal point \f$(c_x, c_y)\f$, that is usually close to the 79 image center: 80 81 \f[A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1},\f] 82 83 and thus 84 85 \f[s \vecthree{u}{v}{1} = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1} \vecthree{X_c}{Y_c}{Z_c}.\f] 86 87 The matrix of intrinsic parameters does not depend on the scene viewed. So, once estimated, it can 88 be re-used as long as the focal length is fixed (in case of a zoom lens). Thus, if an image from the 89 camera is scaled by a factor, all of these parameters need to be scaled (multiplied/divided, 90 respectively) by the same factor. 91 92 The joint rotation-translation matrix \f$[R|t]\f$ is the matrix product of a projective 93 transformation and a homogeneous transformation. The 3-by-4 projective transformation maps 3D points 94 represented in camera coordinates to 2D poins in the image plane and represented in normalized 95 camera coordinates \f$x' = X_c / Z_c\f$ and \f$y' = Y_c / Z_c\f$: 96 97 \f[Z_c \begin{bmatrix} 98 x' \\ 99 y' \\ 100 1 101 \end{bmatrix} = \begin{bmatrix} 102 1 & 0 & 0 & 0 \\ 103 0 & 1 & 0 & 0 \\ 104 0 & 0 & 1 & 0 105 \end{bmatrix} 106 \begin{bmatrix} 107 X_c \\ 108 Y_c \\ 109 Z_c \\ 110 1 111 \end{bmatrix}.\f] 112 113 The homogeneous transformation is encoded by the extrinsic parameters \f$R\f$ and \f$t\f$ and 114 represents the change of basis from world coordinate system \f$w\f$ to the camera coordinate sytem 115 \f$c\f$. Thus, given the representation of the point \f$P\f$ in world coordinates, \f$P_w\f$, we 116 obtain \f$P\f$'s representation in the camera coordinate system, \f$P_c\f$, by 117 118 \f[P_c = \begin{bmatrix} 119 R & t \\ 120 0 & 1 121 \end{bmatrix} P_w,\f] 122 123 This homogeneous transformation is composed out of \f$R\f$, a 3-by-3 rotation matrix, and \f$t\f$, a 124 3-by-1 translation vector: 125 126 \f[\begin{bmatrix} 127 R & t \\ 128 0 & 1 129 \end{bmatrix} = \begin{bmatrix} 130 r_{11} & r_{12} & r_{13} & t_x \\ 131 r_{21} & r_{22} & r_{23} & t_y \\ 132 r_{31} & r_{32} & r_{33} & t_z \\ 133 0 & 0 & 0 & 1 134 \end{bmatrix}, 135 \f] 136 137 and therefore 138 139 \f[\begin{bmatrix} 140 X_c \\ 141 Y_c \\ 142 Z_c \\ 143 1 144 \end{bmatrix} = \begin{bmatrix} 145 r_{11} & r_{12} & r_{13} & t_x \\ 146 r_{21} & r_{22} & r_{23} & t_y \\ 147 r_{31} & r_{32} & r_{33} & t_z \\ 148 0 & 0 & 0 & 1 149 \end{bmatrix} 150 \begin{bmatrix} 151 X_w \\ 152 Y_w \\ 153 Z_w \\ 154 1 155 \end{bmatrix}.\f] 156 157 Combining the projective transformation and the homogeneous transformation, we obtain the projective 158 transformation that maps 3D points in world coordinates into 2D points in the image plane and in 159 normalized camera coordinates: 160 161 \f[Z_c \begin{bmatrix} 162 x' \\ 163 y' \\ 164 1 165 \end{bmatrix} = \begin{bmatrix} R|t \end{bmatrix} \begin{bmatrix} 166 X_w \\ 167 Y_w \\ 168 Z_w \\ 169 1 170 \end{bmatrix} = \begin{bmatrix} 171 r_{11} & r_{12} & r_{13} & t_x \\ 172 r_{21} & r_{22} & r_{23} & t_y \\ 173 r_{31} & r_{32} & r_{33} & t_z 174 \end{bmatrix} 175 \begin{bmatrix} 176 X_w \\ 177 Y_w \\ 178 Z_w \\ 179 1 180 \end{bmatrix},\f] 181 182 with \f$x' = X_c / Z_c\f$ and \f$y' = Y_c / Z_c\f$. Putting the equations for instrincs and extrinsics together, we can write out 183 \f$s \; p = A \begin{bmatrix} R|t \end{bmatrix} P_w\f$ as 184 185 \f[s \vecthree{u}{v}{1} = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1} 186 \begin{bmatrix} 187 r_{11} & r_{12} & r_{13} & t_x \\ 188 r_{21} & r_{22} & r_{23} & t_y \\ 189 r_{31} & r_{32} & r_{33} & t_z 190 \end{bmatrix} 191 \begin{bmatrix} 192 X_w \\ 193 Y_w \\ 194 Z_w \\ 195 1 196 \end{bmatrix}.\f] 197 198 If \f$Z_c \ne 0\f$, the transformation above is equivalent to the following, 199 200 \f[\begin{bmatrix} 201 u \\ 202 v 203 \end{bmatrix} = \begin{bmatrix} 204 f_x X_c/Z_c + c_x \\ 205 f_y Y_c/Z_c + c_y 206 \end{bmatrix}\f] 207 208 with 209 210 \f[\vecthree{X_c}{Y_c}{Z_c} = \begin{bmatrix} 211 R|t 212 \end{bmatrix} \begin{bmatrix} 213 X_w \\ 214 Y_w \\ 215 Z_w \\ 216 1 217 \end{bmatrix}.\f] 218 219 The following figure illustrates the pinhole camera model. 220 221  222 223 Real lenses usually have some distortion, mostly radial distortion, and slight tangential distortion. 224 So, the above model is extended as: 225 226 \f[\begin{bmatrix} 227 u \\ 228 v 229 \end{bmatrix} = \begin{bmatrix} 230 f_x x'' + c_x \\ 231 f_y y'' + c_y 232 \end{bmatrix}\f] 233 234 where 235 236 \f[\begin{bmatrix} 237 x'' \\ 238 y'' 239 \end{bmatrix} = \begin{bmatrix} 240 x' \frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} + 2 p_1 x' y' + p_2(r^2 + 2 x'^2) + s_1 r^2 + s_2 r^4 \\ 241 y' \frac{1 + k_1 r^2 + k_2 r^4 + k_3 r^6}{1 + k_4 r^2 + k_5 r^4 + k_6 r^6} + p_1 (r^2 + 2 y'^2) + 2 p_2 x' y' + s_3 r^2 + s_4 r^4 \\ 242 \end{bmatrix}\f] 243 244 with 245 246 \f[r^2 = x'^2 + y'^2\f] 247 248 and 249 250 \f[\begin{bmatrix} 251 x'\\ 252 y' 253 \end{bmatrix} = \begin{bmatrix} 254 X_c/Z_c \\ 255 Y_c/Z_c 256 \end{bmatrix},\f] 257 258 if \f$Z_c \ne 0\f$. 259 260 The distortion parameters are the radial coefficients \f$k_1\f$, \f$k_2\f$, \f$k_3\f$, \f$k_4\f$, \f$k_5\f$, and \f$k_6\f$ 261 ,\f$p_1\f$ and \f$p_2\f$ are the tangential distortion coefficients, and \f$s_1\f$, \f$s_2\f$, \f$s_3\f$, and \f$s_4\f$, 262 are the thin prism distortion coefficients. Higher-order coefficients are not considered in OpenCV. 263 264 The next figures show two common types of radial distortion: barrel distortion 265 (\f$ 1 + k_1 r^2 + k_2 r^4 + k_3 r^6 \f$ monotonically decreasing) 266 and pincushion distortion (\f$ 1 + k_1 r^2 + k_2 r^4 + k_3 r^6 \f$ monotonically increasing). 267 Radial distortion is always monotonic for real lenses, 268 and if the estimator produces a non-monotonic result, 269 this should be considered a calibration failure. 270 More generally, radial distortion must be monotonic and the distortion function must be bijective. 271 A failed estimation result may look deceptively good near the image center 272 but will work poorly in e.g. AR/SFM applications. 273 The optimization method used in OpenCV camera calibration does not include these constraints as 274 the framework does not support the required integer programming and polynomial inequalities. 275 See [issue #15992](https://github.com/opencv/opencv/issues/15992) for additional information. 276 277  278  279 280 In some cases, the image sensor may be tilted in order to focus an oblique plane in front of the 281 camera (Scheimpflug principle). This can be useful for particle image velocimetry (PIV) or 282 triangulation with a laser fan. The tilt causes a perspective distortion of \f$x''\f$ and 283 \f$y''\f$. This distortion can be modeled in the following way, see e.g. @cite Louhichi07. 284 285 \f[\begin{bmatrix} 286 u \\ 287 v 288 \end{bmatrix} = \begin{bmatrix} 289 f_x x''' + c_x \\ 290 f_y y''' + c_y 291 \end{bmatrix},\f] 292 293 where 294 295 \f[s\vecthree{x'''}{y'''}{1} = 296 \vecthreethree{R_{33}(\tau_x, \tau_y)}{0}{-R_{13}(\tau_x, \tau_y)} 297 {0}{R_{33}(\tau_x, \tau_y)}{-R_{23}(\tau_x, \tau_y)} 298 {0}{0}{1} R(\tau_x, \tau_y) \vecthree{x''}{y''}{1}\f] 299 300 and the matrix \f$R(\tau_x, \tau_y)\f$ is defined by two rotations with angular parameter 301 \f$\tau_x\f$ and \f$\tau_y\f$, respectively, 302 303 \f[ 304 R(\tau_x, \tau_y) = 305 \vecthreethree{\cos(\tau_y)}{0}{-\sin(\tau_y)}{0}{1}{0}{\sin(\tau_y)}{0}{\cos(\tau_y)} 306 \vecthreethree{1}{0}{0}{0}{\cos(\tau_x)}{\sin(\tau_x)}{0}{-\sin(\tau_x)}{\cos(\tau_x)} = 307 \vecthreethree{\cos(\tau_y)}{\sin(\tau_y)\sin(\tau_x)}{-\sin(\tau_y)\cos(\tau_x)} 308 {0}{\cos(\tau_x)}{\sin(\tau_x)} 309 {\sin(\tau_y)}{-\cos(\tau_y)\sin(\tau_x)}{\cos(\tau_y)\cos(\tau_x)}. 310 \f] 311 312 In the functions below the coefficients are passed or returned as 313 314 \f[(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f] 315 316 vector. That is, if the vector contains four elements, it means that \f$k_3=0\f$ . The distortion 317 coefficients do not depend on the scene viewed. Thus, they also belong to the intrinsic camera 318 parameters. And they remain the same regardless of the captured image resolution. If, for example, a 319 camera has been calibrated on images of 320 x 240 resolution, absolutely the same distortion 320 coefficients can be used for 640 x 480 images from the same camera while \f$f_x\f$, \f$f_y\f$, 321 \f$c_x\f$, and \f$c_y\f$ need to be scaled appropriately. 322 323 The functions below use the above model to do the following: 324 325 - Project 3D points to the image plane given intrinsic and extrinsic parameters. 326 - Compute extrinsic parameters given intrinsic parameters, a few 3D points, and their 327 projections. 328 - Estimate intrinsic and extrinsic camera parameters from several views of a known calibration 329 pattern (every view is described by several 3D-2D point correspondences). 330 - Estimate the relative position and orientation of the stereo camera "heads" and compute the 331 *rectification* transformation that makes the camera optical axes parallel. 332 333 <B> Homogeneous Coordinates </B><br> 334 Homogeneous Coordinates are a system of coordinates that are used in projective geometry. Their use 335 allows to represent points at infinity by finite coordinates and simplifies formulas when compared 336 to the cartesian counterparts, e.g. they have the advantage that affine transformations can be 337 expressed as linear homogeneous transformation. 338 339 One obtains the homogeneous vector \f$P_h\f$ by appending a 1 along an n-dimensional cartesian 340 vector \f$P\f$ e.g. for a 3D cartesian vector the mapping \f$P \rightarrow P_h\f$ is: 341 342 \f[\begin{bmatrix} 343 X \\ 344 Y \\ 345 Z 346 \end{bmatrix} \rightarrow \begin{bmatrix} 347 X \\ 348 Y \\ 349 Z \\ 350 1 351 \end{bmatrix}.\f] 352 353 For the inverse mapping \f$P_h \rightarrow P\f$, one divides all elements of the homogeneous vector 354 by its last element, e.g. for a 3D homogeneous vector one gets its 2D cartesian counterpart by: 355 356 \f[\begin{bmatrix} 357 X \\ 358 Y \\ 359 W 360 \end{bmatrix} \rightarrow \begin{bmatrix} 361 X / W \\ 362 Y / W 363 \end{bmatrix},\f] 364 365 if \f$W \ne 0\f$. 366 367 Due to this mapping, all multiples \f$k P_h\f$, for \f$k \ne 0\f$, of a homogeneous point represent 368 the same point \f$P_h\f$. An intuitive understanding of this property is that under a projective 369 transformation, all multiples of \f$P_h\f$ are mapped to the same point. This is the physical 370 observation one does for pinhole cameras, as all points along a ray through the camera's pinhole are 371 projected to the same image point, e.g. all points along the red ray in the image of the pinhole 372 camera model above would be mapped to the same image coordinate. This property is also the source 373 for the scale ambiguity s in the equation of the pinhole camera model. 374 375 As mentioned, by using homogeneous coordinates we can express any change of basis parameterized by 376 \f$R\f$ and \f$t\f$ as a linear transformation, e.g. for the change of basis from coordinate system 377 0 to coordinate system 1 becomes: 378 379 \f[P_1 = R P_0 + t \rightarrow P_{h_1} = \begin{bmatrix} 380 R & t \\ 381 0 & 1 382 \end{bmatrix} P_{h_0}.\f] 383 384 @note 385 - Many functions in this module take a camera matrix as an input parameter. Although all 386 functions assume the same structure of this parameter, they may name it differently. The 387 parameter's description, however, will be clear in that a camera matrix with the structure 388 shown above is required. 389 - A calibration sample for 3 cameras in a horizontal position can be found at 390 opencv_source_code/samples/cpp/3calibration.cpp 391 - A calibration sample based on a sequence of images can be found at 392 opencv_source_code/samples/cpp/calibration.cpp 393 - A calibration sample in order to do 3D reconstruction can be found at 394 opencv_source_code/samples/cpp/build3dmodel.cpp 395 - A calibration example on stereo calibration can be found at 396 opencv_source_code/samples/cpp/stereo_calib.cpp 397 - A calibration example on stereo matching can be found at 398 opencv_source_code/samples/cpp/stereo_match.cpp 399 - (Python) A camera calibration sample can be found at 400 opencv_source_code/samples/python/calibrate.py 401 402 @{ 403 @defgroup calib3d_fisheye Fisheye camera model 404 405 Definitions: Let P be a point in 3D of coordinates X in the world reference frame (stored in the 406 matrix X) The coordinate vector of P in the camera reference frame is: 407 408 \f[Xc = R X + T\f] 409 410 where R is the rotation matrix corresponding to the rotation vector om: R = rodrigues(om); call x, y 411 and z the 3 coordinates of Xc: 412 413 \f[x = Xc_1 \\ y = Xc_2 \\ z = Xc_3\f] 414 415 The pinhole projection coordinates of P is [a; b] where 416 417 \f[a = x / z \ and \ b = y / z \\ r^2 = a^2 + b^2 \\ \theta = atan(r)\f] 418 419 Fisheye distortion: 420 421 \f[\theta_d = \theta (1 + k_1 \theta^2 + k_2 \theta^4 + k_3 \theta^6 + k_4 \theta^8)\f] 422 423 The distorted point coordinates are [x'; y'] where 424 425 \f[x' = (\theta_d / r) a \\ y' = (\theta_d / r) b \f] 426 427 Finally, conversion into pixel coordinates: The final pixel coordinates vector [u; v] where: 428 429 \f[u = f_x (x' + \alpha y') + c_x \\ 430 v = f_y y' + c_y\f] 431 432 @defgroup calib3d_c C API 433 434 @} 435 */ 436 437 namespace cv 438 { 439 440 //! @addtogroup calib3d 441 //! @{ 442 443 //! type of the robust estimation algorithm 444 enum { LMEDS = 4, //!< least-median of squares algorithm 445 RANSAC = 8, //!< RANSAC algorithm 446 RHO = 16 //!< RHO algorithm 447 }; 448 449 enum SolvePnPMethod { 450 SOLVEPNP_ITERATIVE = 0, 451 SOLVEPNP_EPNP = 1, //!< EPnP: Efficient Perspective-n-Point Camera Pose Estimation @cite lepetit2009epnp 452 SOLVEPNP_P3P = 2, //!< Complete Solution Classification for the Perspective-Three-Point Problem @cite gao2003complete 453 SOLVEPNP_DLS = 3, //!< A Direct Least-Squares (DLS) Method for PnP @cite hesch2011direct 454 SOLVEPNP_UPNP = 4, //!< Exhaustive Linearization for Robust Camera Pose and Focal Length Estimation @cite penate2013exhaustive 455 SOLVEPNP_AP3P = 5, //!< An Efficient Algebraic Solution to the Perspective-Three-Point Problem @cite Ke17 456 SOLVEPNP_IPPE = 6, //!< Infinitesimal Plane-Based Pose Estimation @cite Collins14 \n 457 //!< Object points must be coplanar. 458 SOLVEPNP_IPPE_SQUARE = 7, //!< Infinitesimal Plane-Based Pose Estimation @cite Collins14 \n 459 //!< This is a special case suitable for marker pose estimation.\n 460 //!< 4 coplanar object points must be defined in the following order: 461 //!< - point 0: [-squareLength / 2, squareLength / 2, 0] 462 //!< - point 1: [ squareLength / 2, squareLength / 2, 0] 463 //!< - point 2: [ squareLength / 2, -squareLength / 2, 0] 464 //!< - point 3: [-squareLength / 2, -squareLength / 2, 0] 465 #ifndef CV_DOXYGEN 466 SOLVEPNP_MAX_COUNT //!< Used for count 467 #endif 468 }; 469 470 enum { CALIB_CB_ADAPTIVE_THRESH = 1, 471 CALIB_CB_NORMALIZE_IMAGE = 2, 472 CALIB_CB_FILTER_QUADS = 4, 473 CALIB_CB_FAST_CHECK = 8 474 }; 475 476 enum { CALIB_CB_SYMMETRIC_GRID = 1, 477 CALIB_CB_ASYMMETRIC_GRID = 2, 478 CALIB_CB_CLUSTERING = 4 479 }; 480 481 enum { CALIB_USE_INTRINSIC_GUESS = 0x00001, 482 CALIB_FIX_ASPECT_RATIO = 0x00002, 483 CALIB_FIX_PRINCIPAL_POINT = 0x00004, 484 CALIB_ZERO_TANGENT_DIST = 0x00008, 485 CALIB_FIX_FOCAL_LENGTH = 0x00010, 486 CALIB_FIX_K1 = 0x00020, 487 CALIB_FIX_K2 = 0x00040, 488 CALIB_FIX_K3 = 0x00080, 489 CALIB_FIX_K4 = 0x00800, 490 CALIB_FIX_K5 = 0x01000, 491 CALIB_FIX_K6 = 0x02000, 492 CALIB_RATIONAL_MODEL = 0x04000, 493 CALIB_THIN_PRISM_MODEL = 0x08000, 494 CALIB_FIX_S1_S2_S3_S4 = 0x10000, 495 CALIB_TILTED_MODEL = 0x40000, 496 CALIB_FIX_TAUX_TAUY = 0x80000, 497 CALIB_USE_QR = 0x100000, //!< use QR instead of SVD decomposition for solving. Faster but potentially less precise 498 CALIB_FIX_TANGENT_DIST = 0x200000, 499 // only for stereo 500 CALIB_FIX_INTRINSIC = 0x00100, 501 CALIB_SAME_FOCAL_LENGTH = 0x00200, 502 // for stereo rectification 503 CALIB_ZERO_DISPARITY = 0x00400, 504 CALIB_USE_LU = (1 << 17), //!< use LU instead of SVD decomposition for solving. much faster but potentially less precise 505 CALIB_USE_EXTRINSIC_GUESS = (1 << 22) //!< for stereoCalibrate 506 }; 507 508 //! the algorithm for finding fundamental matrix 509 enum { FM_7POINT = 1, //!< 7-point algorithm 510 FM_8POINT = 2, //!< 8-point algorithm 511 FM_LMEDS = 4, //!< least-median algorithm. 7-point algorithm is used. 512 FM_RANSAC = 8 //!< RANSAC algorithm. It needs at least 15 points. 7-point algorithm is used. 513 }; 514 515 enum HandEyeCalibrationMethod 516 { 517 CALIB_HAND_EYE_TSAI = 0, //!< A New Technique for Fully Autonomous and Efficient 3D Robotics Hand/Eye Calibration @cite Tsai89 518 CALIB_HAND_EYE_PARK = 1, //!< Robot Sensor Calibration: Solving AX = XB on the Euclidean Group @cite Park94 519 CALIB_HAND_EYE_HORAUD = 2, //!< Hand-eye Calibration @cite Horaud95 520 CALIB_HAND_EYE_ANDREFF = 3, //!< On-line Hand-Eye Calibration @cite Andreff99 521 CALIB_HAND_EYE_DANIILIDIS = 4 //!< Hand-Eye Calibration Using Dual Quaternions @cite Daniilidis98 522 }; 523 524 525 /** @brief Converts a rotation matrix to a rotation vector or vice versa. 526 527 @param src Input rotation vector (3x1 or 1x3) or rotation matrix (3x3). 528 @param dst Output rotation matrix (3x3) or rotation vector (3x1 or 1x3), respectively. 529 @param jacobian Optional output Jacobian matrix, 3x9 or 9x3, which is a matrix of partial 530 derivatives of the output array components with respect to the input array components. 531 532 \f[\begin{array}{l} \theta \leftarrow norm(r) \\ r \leftarrow r/ \theta \\ R = \cos(\theta) I + (1- \cos{\theta} ) r r^T + \sin(\theta) \vecthreethree{0}{-r_z}{r_y}{r_z}{0}{-r_x}{-r_y}{r_x}{0} \end{array}\f] 533 534 Inverse transformation can be also done easily, since 535 536 \f[\sin ( \theta ) \vecthreethree{0}{-r_z}{r_y}{r_z}{0}{-r_x}{-r_y}{r_x}{0} = \frac{R - R^T}{2}\f] 537 538 A rotation vector is a convenient and most compact representation of a rotation matrix (since any 539 rotation matrix has just 3 degrees of freedom). The representation is used in the global 3D geometry 540 optimization procedures like @ref calibrateCamera, @ref stereoCalibrate, or @ref solvePnP . 541 542 @note More information about the computation of the derivative of a 3D rotation matrix with respect to its exponential coordinate 543 can be found in: 544 - A Compact Formula for the Derivative of a 3-D Rotation in Exponential Coordinates, Guillermo Gallego, Anthony J. Yezzi @cite Gallego2014ACF 545 546 @note Useful information on SE(3) and Lie Groups can be found in: 547 - A tutorial on SE(3) transformation parameterizations and on-manifold optimization, Jose-Luis Blanco @cite blanco2010tutorial 548 - Lie Groups for 2D and 3D Transformation, Ethan Eade @cite Eade17 549 - A micro Lie theory for state estimation in robotics, Joan Solà, Jérémie Deray, Dinesh Atchuthan @cite Sol2018AML 550 */ 551 CV_EXPORTS_W void Rodrigues( InputArray src, OutputArray dst, OutputArray jacobian = noArray() ); 552 553 /** @example samples/cpp/tutorial_code/features2D/Homography/pose_from_homography.cpp 554 An example program about pose estimation from coplanar points 555 556 Check @ref tutorial_homography "the corresponding tutorial" for more details 557 */ 558 559 /** @brief Finds a perspective transformation between two planes. 560 561 @param srcPoints Coordinates of the points in the original plane, a matrix of the type CV_32FC2 562 or vector\<Point2f\> . 563 @param dstPoints Coordinates of the points in the target plane, a matrix of the type CV_32FC2 or 564 a vector\<Point2f\> . 565 @param method Method used to compute a homography matrix. The following methods are possible: 566 - **0** - a regular method using all the points, i.e., the least squares method 567 - **RANSAC** - RANSAC-based robust method 568 - **LMEDS** - Least-Median robust method 569 - **RHO** - PROSAC-based robust method 570 @param ransacReprojThreshold Maximum allowed reprojection error to treat a point pair as an inlier 571 (used in the RANSAC and RHO methods only). That is, if 572 \f[\| \texttt{dstPoints} _i - \texttt{convertPointsHomogeneous} ( \texttt{H} * \texttt{srcPoints} _i) \|_2 > \texttt{ransacReprojThreshold}\f] 573 then the point \f$i\f$ is considered as an outlier. If srcPoints and dstPoints are measured in pixels, 574 it usually makes sense to set this parameter somewhere in the range of 1 to 10. 575 @param mask Optional output mask set by a robust method ( RANSAC or LMEDS ). Note that the input 576 mask values are ignored. 577 @param maxIters The maximum number of RANSAC iterations. 578 @param confidence Confidence level, between 0 and 1. 579 580 The function finds and returns the perspective transformation \f$H\f$ between the source and the 581 destination planes: 582 583 \f[s_i \vecthree{x'_i}{y'_i}{1} \sim H \vecthree{x_i}{y_i}{1}\f] 584 585 so that the back-projection error 586 587 \f[\sum _i \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+ \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\f] 588 589 is minimized. If the parameter method is set to the default value 0, the function uses all the point 590 pairs to compute an initial homography estimate with a simple least-squares scheme. 591 592 However, if not all of the point pairs ( \f$srcPoints_i\f$, \f$dstPoints_i\f$ ) fit the rigid perspective 593 transformation (that is, there are some outliers), this initial estimate will be poor. In this case, 594 you can use one of the three robust methods. The methods RANSAC, LMeDS and RHO try many different 595 random subsets of the corresponding point pairs (of four pairs each, collinear pairs are discarded), estimate the homography matrix 596 using this subset and a simple least-squares algorithm, and then compute the quality/goodness of the 597 computed homography (which is the number of inliers for RANSAC or the least median re-projection error for 598 LMeDS). The best subset is then used to produce the initial estimate of the homography matrix and 599 the mask of inliers/outliers. 600 601 Regardless of the method, robust or not, the computed homography matrix is refined further (using 602 inliers only in case of a robust method) with the Levenberg-Marquardt method to reduce the 603 re-projection error even more. 604 605 The methods RANSAC and RHO can handle practically any ratio of outliers but need a threshold to 606 distinguish inliers from outliers. The method LMeDS does not need any threshold but it works 607 correctly only when there are more than 50% of inliers. Finally, if there are no outliers and the 608 noise is rather small, use the default method (method=0). 609 610 The function is used to find initial intrinsic and extrinsic matrices. Homography matrix is 611 determined up to a scale. Thus, it is normalized so that \f$h_{33}=1\f$. Note that whenever an \f$H\f$ matrix 612 cannot be estimated, an empty one will be returned. 613 614 @sa 615 getAffineTransform, estimateAffine2D, estimateAffinePartial2D, getPerspectiveTransform, warpPerspective, 616 perspectiveTransform 617 */ 618 CV_EXPORTS_W Mat findHomography( InputArray srcPoints, InputArray dstPoints, 619 int method = 0, double ransacReprojThreshold = 3, 620 OutputArray mask=noArray(), const int maxIters = 2000, 621 const double confidence = 0.995); 622 623 /** @overload */ 624 CV_EXPORTS Mat findHomography( InputArray srcPoints, InputArray dstPoints, 625 OutputArray mask, int method = 0, double ransacReprojThreshold = 3 ); 626 627 /** @brief Computes an RQ decomposition of 3x3 matrices. 628 629 @param src 3x3 input matrix. 630 @param mtxR Output 3x3 upper-triangular matrix. 631 @param mtxQ Output 3x3 orthogonal matrix. 632 @param Qx Optional output 3x3 rotation matrix around x-axis. 633 @param Qy Optional output 3x3 rotation matrix around y-axis. 634 @param Qz Optional output 3x3 rotation matrix around z-axis. 635 636 The function computes a RQ decomposition using the given rotations. This function is used in 637 decomposeProjectionMatrix to decompose the left 3x3 submatrix of a projection matrix into a camera 638 and a rotation matrix. 639 640 It optionally returns three rotation matrices, one for each axis, and the three Euler angles in 641 degrees (as the return value) that could be used in OpenGL. Note, there is always more than one 642 sequence of rotations about the three principal axes that results in the same orientation of an 643 object, e.g. see @cite Slabaugh . Returned tree rotation matrices and corresponding three Euler angles 644 are only one of the possible solutions. 645 */ 646 CV_EXPORTS_W Vec3d RQDecomp3x3( InputArray src, OutputArray mtxR, OutputArray mtxQ, 647 OutputArray Qx = noArray(), 648 OutputArray Qy = noArray(), 649 OutputArray Qz = noArray()); 650 651 /** @brief Decomposes a projection matrix into a rotation matrix and a camera matrix. 652 653 @param projMatrix 3x4 input projection matrix P. 654 @param cameraMatrix Output 3x3 camera matrix K. 655 @param rotMatrix Output 3x3 external rotation matrix R. 656 @param transVect Output 4x1 translation vector T. 657 @param rotMatrixX Optional 3x3 rotation matrix around x-axis. 658 @param rotMatrixY Optional 3x3 rotation matrix around y-axis. 659 @param rotMatrixZ Optional 3x3 rotation matrix around z-axis. 660 @param eulerAngles Optional three-element vector containing three Euler angles of rotation in 661 degrees. 662 663 The function computes a decomposition of a projection matrix into a calibration and a rotation 664 matrix and the position of a camera. 665 666 It optionally returns three rotation matrices, one for each axis, and three Euler angles that could 667 be used in OpenGL. Note, there is always more than one sequence of rotations about the three 668 principal axes that results in the same orientation of an object, e.g. see @cite Slabaugh . Returned 669 tree rotation matrices and corresponding three Euler angles are only one of the possible solutions. 670 671 The function is based on RQDecomp3x3 . 672 */ 673 CV_EXPORTS_W void decomposeProjectionMatrix( InputArray projMatrix, OutputArray cameraMatrix, 674 OutputArray rotMatrix, OutputArray transVect, 675 OutputArray rotMatrixX = noArray(), 676 OutputArray rotMatrixY = noArray(), 677 OutputArray rotMatrixZ = noArray(), 678 OutputArray eulerAngles =noArray() ); 679 680 /** @brief Computes partial derivatives of the matrix product for each multiplied matrix. 681 682 @param A First multiplied matrix. 683 @param B Second multiplied matrix. 684 @param dABdA First output derivative matrix d(A\*B)/dA of size 685 \f$\texttt{A.rows*B.cols} \times {A.rows*A.cols}\f$ . 686 @param dABdB Second output derivative matrix d(A\*B)/dB of size 687 \f$\texttt{A.rows*B.cols} \times {B.rows*B.cols}\f$ . 688 689 The function computes partial derivatives of the elements of the matrix product \f$A*B\f$ with regard to 690 the elements of each of the two input matrices. The function is used to compute the Jacobian 691 matrices in stereoCalibrate but can also be used in any other similar optimization function. 692 */ 693 CV_EXPORTS_W void matMulDeriv( InputArray A, InputArray B, OutputArray dABdA, OutputArray dABdB ); 694 695 /** @brief Combines two rotation-and-shift transformations. 696 697 @param rvec1 First rotation vector. 698 @param tvec1 First translation vector. 699 @param rvec2 Second rotation vector. 700 @param tvec2 Second translation vector. 701 @param rvec3 Output rotation vector of the superposition. 702 @param tvec3 Output translation vector of the superposition. 703 @param dr3dr1 Optional output derivative of rvec3 with regard to rvec1 704 @param dr3dt1 Optional output derivative of rvec3 with regard to tvec1 705 @param dr3dr2 Optional output derivative of rvec3 with regard to rvec2 706 @param dr3dt2 Optional output derivative of rvec3 with regard to tvec2 707 @param dt3dr1 Optional output derivative of tvec3 with regard to rvec1 708 @param dt3dt1 Optional output derivative of tvec3 with regard to tvec1 709 @param dt3dr2 Optional output derivative of tvec3 with regard to rvec2 710 @param dt3dt2 Optional output derivative of tvec3 with regard to tvec2 711 712 The functions compute: 713 714 \f[\begin{array}{l} \texttt{rvec3} = \mathrm{rodrigues} ^{-1} \left ( \mathrm{rodrigues} ( \texttt{rvec2} ) \cdot \mathrm{rodrigues} ( \texttt{rvec1} ) \right ) \\ \texttt{tvec3} = \mathrm{rodrigues} ( \texttt{rvec2} ) \cdot \texttt{tvec1} + \texttt{tvec2} \end{array} ,\f] 715 716 where \f$\mathrm{rodrigues}\f$ denotes a rotation vector to a rotation matrix transformation, and 717 \f$\mathrm{rodrigues}^{-1}\f$ denotes the inverse transformation. See Rodrigues for details. 718 719 Also, the functions can compute the derivatives of the output vectors with regards to the input 720 vectors (see matMulDeriv ). The functions are used inside stereoCalibrate but can also be used in 721 your own code where Levenberg-Marquardt or another gradient-based solver is used to optimize a 722 function that contains a matrix multiplication. 723 */ 724 CV_EXPORTS_W void composeRT( InputArray rvec1, InputArray tvec1, 725 InputArray rvec2, InputArray tvec2, 726 OutputArray rvec3, OutputArray tvec3, 727 OutputArray dr3dr1 = noArray(), OutputArray dr3dt1 = noArray(), 728 OutputArray dr3dr2 = noArray(), OutputArray dr3dt2 = noArray(), 729 OutputArray dt3dr1 = noArray(), OutputArray dt3dt1 = noArray(), 730 OutputArray dt3dr2 = noArray(), OutputArray dt3dt2 = noArray() ); 731 732 /** @brief Projects 3D points to an image plane. 733 734 @param objectPoints Array of object points expressed wrt. the world coordinate frame. A 3xN/Nx3 735 1-channel or 1xN/Nx1 3-channel (or vector\<Point3f\> ), where N is the number of points in the view. 736 @param rvec The rotation vector (@ref Rodrigues) that, together with tvec, performs a change of 737 basis from world to camera coordinate system, see @ref calibrateCamera for details. 738 @param tvec The translation vector, see parameter description above. 739 @param cameraMatrix Camera matrix \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$ . 740 @param distCoeffs Input vector of distortion coefficients 741 \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of 742 4, 5, 8, 12 or 14 elements. If the vector is empty, the zero distortion coefficients are assumed. 743 @param imagePoints Output array of image points, 1xN/Nx1 2-channel, or 744 vector\<Point2f\> . 745 @param jacobian Optional output 2Nx(10+\<numDistCoeffs\>) jacobian matrix of derivatives of image 746 points with respect to components of the rotation vector, translation vector, focal lengths, 747 coordinates of the principal point and the distortion coefficients. In the old interface different 748 components of the jacobian are returned via different output parameters. 749 @param aspectRatio Optional "fixed aspect ratio" parameter. If the parameter is not 0, the 750 function assumes that the aspect ratio (\f$f_x / f_y\f$) is fixed and correspondingly adjusts the 751 jacobian matrix. 752 753 The function computes the 2D projections of 3D points to the image plane, given intrinsic and 754 extrinsic camera parameters. Optionally, the function computes Jacobians -matrices of partial 755 derivatives of image points coordinates (as functions of all the input parameters) with respect to 756 the particular parameters, intrinsic and/or extrinsic. The Jacobians are used during the global 757 optimization in @ref calibrateCamera, @ref solvePnP, and @ref stereoCalibrate. The function itself 758 can also be used to compute a re-projection error, given the current intrinsic and extrinsic 759 parameters. 760 761 @note By setting rvec = tvec = \f$[0, 0, 0]\f$, or by setting cameraMatrix to a 3x3 identity matrix, 762 or by passing zero distortion coefficients, one can get various useful partial cases of the 763 function. This means, one can compute the distorted coordinates for a sparse set of points or apply 764 a perspective transformation (and also compute the derivatives) in the ideal zero-distortion setup. 765 */ 766 CV_EXPORTS_W void projectPoints( InputArray objectPoints, 767 InputArray rvec, InputArray tvec, 768 InputArray cameraMatrix, InputArray distCoeffs, 769 OutputArray imagePoints, 770 OutputArray jacobian = noArray(), 771 double aspectRatio = 0 ); 772 773 /** @example samples/cpp/tutorial_code/features2D/Homography/homography_from_camera_displacement.cpp 774 An example program about homography from the camera displacement 775 776 Check @ref tutorial_homography "the corresponding tutorial" for more details 777 */ 778 779 /** @brief Finds an object pose from 3D-2D point correspondences. 780 This function returns the rotation and the translation vectors that transform a 3D point expressed in the object 781 coordinate frame to the camera coordinate frame, using different methods: 782 - P3P methods (@ref SOLVEPNP_P3P, @ref SOLVEPNP_AP3P): need 4 input points to return a unique solution. 783 - @ref SOLVEPNP_IPPE Input points must be >= 4 and object points must be coplanar. 784 - @ref SOLVEPNP_IPPE_SQUARE Special case suitable for marker pose estimation. 785 Number of input points must be 4. Object points must be defined in the following order: 786 - point 0: [-squareLength / 2, squareLength / 2, 0] 787 - point 1: [ squareLength / 2, squareLength / 2, 0] 788 - point 2: [ squareLength / 2, -squareLength / 2, 0] 789 - point 3: [-squareLength / 2, -squareLength / 2, 0] 790 - for all the other flags, number of input points must be >= 4 and object points can be in any configuration. 791 792 @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or 793 1xN/Nx1 3-channel, where N is the number of points. vector\<Point3d\> can be also passed here. 794 @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel, 795 where N is the number of points. vector\<Point2d\> can be also passed here. 796 @param cameraMatrix Input camera matrix \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . 797 @param distCoeffs Input vector of distortion coefficients 798 \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of 799 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are 800 assumed. 801 @param rvec Output rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from 802 the model coordinate system to the camera coordinate system. 803 @param tvec Output translation vector. 804 @param useExtrinsicGuess Parameter used for #SOLVEPNP_ITERATIVE. If true (1), the function uses 805 the provided rvec and tvec values as initial approximations of the rotation and translation 806 vectors, respectively, and further optimizes them. 807 @param flags Method for solving a PnP problem: 808 - **SOLVEPNP_ITERATIVE** Iterative method is based on a Levenberg-Marquardt optimization. In 809 this case the function finds such a pose that minimizes reprojection error, that is the sum 810 of squared distances between the observed projections imagePoints and the projected (using 811 projectPoints ) objectPoints . 812 - **SOLVEPNP_P3P** Method is based on the paper of X.S. Gao, X.-R. Hou, J. Tang, H.-F. Chang 813 "Complete Solution Classification for the Perspective-Three-Point Problem" (@cite gao2003complete). 814 In this case the function requires exactly four object and image points. 815 - **SOLVEPNP_AP3P** Method is based on the paper of T. Ke, S. Roumeliotis 816 "An Efficient Algebraic Solution to the Perspective-Three-Point Problem" (@cite Ke17). 817 In this case the function requires exactly four object and image points. 818 - **SOLVEPNP_EPNP** Method has been introduced by F. Moreno-Noguer, V. Lepetit and P. Fua in the 819 paper "EPnP: Efficient Perspective-n-Point Camera Pose Estimation" (@cite lepetit2009epnp). 820 - **SOLVEPNP_DLS** Method is based on the paper of J. Hesch and S. Roumeliotis. 821 "A Direct Least-Squares (DLS) Method for PnP" (@cite hesch2011direct). 822 - **SOLVEPNP_UPNP** Method is based on the paper of A. Penate-Sanchez, J. Andrade-Cetto, 823 F. Moreno-Noguer. "Exhaustive Linearization for Robust Camera Pose and Focal Length 824 Estimation" (@cite penate2013exhaustive). In this case the function also estimates the parameters \f$f_x\f$ and \f$f_y\f$ 825 assuming that both have the same value. Then the cameraMatrix is updated with the estimated 826 focal length. 827 - **SOLVEPNP_IPPE** Method is based on the paper of T. Collins and A. Bartoli. 828 "Infinitesimal Plane-Based Pose Estimation" (@cite Collins14). This method requires coplanar object points. 829 - **SOLVEPNP_IPPE_SQUARE** Method is based on the paper of Toby Collins and Adrien Bartoli. 830 "Infinitesimal Plane-Based Pose Estimation" (@cite Collins14). This method is suitable for marker pose estimation. 831 It requires 4 coplanar object points defined in the following order: 832 - point 0: [-squareLength / 2, squareLength / 2, 0] 833 - point 1: [ squareLength / 2, squareLength / 2, 0] 834 - point 2: [ squareLength / 2, -squareLength / 2, 0] 835 - point 3: [-squareLength / 2, -squareLength / 2, 0] 836 837 The function estimates the object pose given a set of object points, their corresponding image 838 projections, as well as the camera matrix and the distortion coefficients, see the figure below 839 (more precisely, the X-axis of the camera frame is pointing to the right, the Y-axis downward 840 and the Z-axis forward). 841 842  843 844 Points expressed in the world frame \f$ \bf{X}_w \f$ are projected into the image plane \f$ \left[ u, v \right] \f$ 845 using the perspective projection model \f$ \Pi \f$ and the camera intrinsic parameters matrix \f$ \bf{A} \f$: 846 847 \f[ 848 \begin{align*} 849 \begin{bmatrix} 850 u \\ 851 v \\ 852 1 853 \end{bmatrix} &= 854 \bf{A} \hspace{0.1em} \Pi \hspace{0.2em} ^{c}\bf{T}_w 855 \begin{bmatrix} 856 X_{w} \\ 857 Y_{w} \\ 858 Z_{w} \\ 859 1 860 \end{bmatrix} \\ 861 \begin{bmatrix} 862 u \\ 863 v \\ 864 1 865 \end{bmatrix} &= 866 \begin{bmatrix} 867 f_x & 0 & c_x \\ 868 0 & f_y & c_y \\ 869 0 & 0 & 1 870 \end{bmatrix} 871 \begin{bmatrix} 872 1 & 0 & 0 & 0 \\ 873 0 & 1 & 0 & 0 \\ 874 0 & 0 & 1 & 0 875 \end{bmatrix} 876 \begin{bmatrix} 877 r_{11} & r_{12} & r_{13} & t_x \\ 878 r_{21} & r_{22} & r_{23} & t_y \\ 879 r_{31} & r_{32} & r_{33} & t_z \\ 880 0 & 0 & 0 & 1 881 \end{bmatrix} 882 \begin{bmatrix} 883 X_{w} \\ 884 Y_{w} \\ 885 Z_{w} \\ 886 1 887 \end{bmatrix} 888 \end{align*} 889 \f] 890 891 The estimated pose is thus the rotation (`rvec`) and the translation (`tvec`) vectors that allow transforming 892 a 3D point expressed in the world frame into the camera frame: 893 894 \f[ 895 \begin{align*} 896 \begin{bmatrix} 897 X_c \\ 898 Y_c \\ 899 Z_c \\ 900 1 901 \end{bmatrix} &= 902 \hspace{0.2em} ^{c}\bf{T}_w 903 \begin{bmatrix} 904 X_{w} \\ 905 Y_{w} \\ 906 Z_{w} \\ 907 1 908 \end{bmatrix} \\ 909 \begin{bmatrix} 910 X_c \\ 911 Y_c \\ 912 Z_c \\ 913 1 914 \end{bmatrix} &= 915 \begin{bmatrix} 916 r_{11} & r_{12} & r_{13} & t_x \\ 917 r_{21} & r_{22} & r_{23} & t_y \\ 918 r_{31} & r_{32} & r_{33} & t_z \\ 919 0 & 0 & 0 & 1 920 \end{bmatrix} 921 \begin{bmatrix} 922 X_{w} \\ 923 Y_{w} \\ 924 Z_{w} \\ 925 1 926 \end{bmatrix} 927 \end{align*} 928 \f] 929 930 @note 931 - An example of how to use solvePnP for planar augmented reality can be found at 932 opencv_source_code/samples/python/plane_ar.py 933 - If you are using Python: 934 - Numpy array slices won't work as input because solvePnP requires contiguous 935 arrays (enforced by the assertion using cv::Mat::checkVector() around line 55 of 936 modules/calib3d/src/solvepnp.cpp version 2.4.9) 937 - The P3P algorithm requires image points to be in an array of shape (N,1,2) due 938 to its calling of cv::undistortPoints (around line 75 of modules/calib3d/src/solvepnp.cpp version 2.4.9) 939 which requires 2-channel information. 940 - Thus, given some data D = np.array(...) where D.shape = (N,M), in order to use a subset of 941 it as, e.g., imagePoints, one must effectively copy it into a new array: imagePoints = 942 np.ascontiguousarray(D[:,:2]).reshape((N,1,2)) 943 - The methods **SOLVEPNP_DLS** and **SOLVEPNP_UPNP** cannot be used as the current implementations are 944 unstable and sometimes give completely wrong results. If you pass one of these two 945 flags, **SOLVEPNP_EPNP** method will be used instead. 946 - The minimum number of points is 4 in the general case. In the case of **SOLVEPNP_P3P** and **SOLVEPNP_AP3P** 947 methods, it is required to use exactly 4 points (the first 3 points are used to estimate all the solutions 948 of the P3P problem, the last one is used to retain the best solution that minimizes the reprojection error). 949 - With **SOLVEPNP_ITERATIVE** method and `useExtrinsicGuess=true`, the minimum number of points is 3 (3 points 950 are sufficient to compute a pose but there are up to 4 solutions). The initial solution should be close to the 951 global solution to converge. 952 - With **SOLVEPNP_IPPE** input points must be >= 4 and object points must be coplanar. 953 - With **SOLVEPNP_IPPE_SQUARE** this is a special case suitable for marker pose estimation. 954 Number of input points must be 4. Object points must be defined in the following order: 955 - point 0: [-squareLength / 2, squareLength / 2, 0] 956 - point 1: [ squareLength / 2, squareLength / 2, 0] 957 - point 2: [ squareLength / 2, -squareLength / 2, 0] 958 - point 3: [-squareLength / 2, -squareLength / 2, 0] 959 */ 960 CV_EXPORTS_W bool solvePnP( InputArray objectPoints, InputArray imagePoints, 961 InputArray cameraMatrix, InputArray distCoeffs, 962 OutputArray rvec, OutputArray tvec, 963 bool useExtrinsicGuess = false, int flags = SOLVEPNP_ITERATIVE ); 964 965 /** @brief Finds an object pose from 3D-2D point correspondences using the RANSAC scheme. 966 967 @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or 968 1xN/Nx1 3-channel, where N is the number of points. vector\<Point3d\> can be also passed here. 969 @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel, 970 where N is the number of points. vector\<Point2d\> can be also passed here. 971 @param cameraMatrix Input camera matrix \f$A = \vecthreethree{fx}{0}{cx}{0}{fy}{cy}{0}{0}{1}\f$ . 972 @param distCoeffs Input vector of distortion coefficients 973 \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of 974 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are 975 assumed. 976 @param rvec Output rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from 977 the model coordinate system to the camera coordinate system. 978 @param tvec Output translation vector. 979 @param useExtrinsicGuess Parameter used for @ref SOLVEPNP_ITERATIVE. If true (1), the function uses 980 the provided rvec and tvec values as initial approximations of the rotation and translation 981 vectors, respectively, and further optimizes them. 982 @param iterationsCount Number of iterations. 983 @param reprojectionError Inlier threshold value used by the RANSAC procedure. The parameter value 984 is the maximum allowed distance between the observed and computed point projections to consider it 985 an inlier. 986 @param confidence The probability that the algorithm produces a useful result. 987 @param inliers Output vector that contains indices of inliers in objectPoints and imagePoints . 988 @param flags Method for solving a PnP problem (see @ref solvePnP ). 989 990 The function estimates an object pose given a set of object points, their corresponding image 991 projections, as well as the camera matrix and the distortion coefficients. This function finds such 992 a pose that minimizes reprojection error, that is, the sum of squared distances between the observed 993 projections imagePoints and the projected (using @ref projectPoints ) objectPoints. The use of RANSAC 994 makes the function resistant to outliers. 995 996 @note 997 - An example of how to use solvePNPRansac for object detection can be found at 998 opencv_source_code/samples/cpp/tutorial_code/calib3d/real_time_pose_estimation/ 999 - The default method used to estimate the camera pose for the Minimal Sample Sets step 1000 is #SOLVEPNP_EPNP. Exceptions are: 1001 - if you choose #SOLVEPNP_P3P or #SOLVEPNP_AP3P, these methods will be used. 1002 - if the number of input points is equal to 4, #SOLVEPNP_P3P is used. 1003 - The method used to estimate the camera pose using all the inliers is defined by the 1004 flags parameters unless it is equal to #SOLVEPNP_P3P or #SOLVEPNP_AP3P. In this case, 1005 the method #SOLVEPNP_EPNP will be used instead. 1006 */ 1007 CV_EXPORTS_W bool solvePnPRansac( InputArray objectPoints, InputArray imagePoints, 1008 InputArray cameraMatrix, InputArray distCoeffs, 1009 OutputArray rvec, OutputArray tvec, 1010 bool useExtrinsicGuess = false, int iterationsCount = 100, 1011 float reprojectionError = 8.0, double confidence = 0.99, 1012 OutputArray inliers = noArray(), int flags = SOLVEPNP_ITERATIVE ); 1013 1014 /** @brief Finds an object pose from 3 3D-2D point correspondences. 1015 1016 @param objectPoints Array of object points in the object coordinate space, 3x3 1-channel or 1017 1x3/3x1 3-channel. vector\<Point3f\> can be also passed here. 1018 @param imagePoints Array of corresponding image points, 3x2 1-channel or 1x3/3x1 2-channel. 1019 vector\<Point2f\> can be also passed here. 1020 @param cameraMatrix Input camera matrix \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . 1021 @param distCoeffs Input vector of distortion coefficients 1022 \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of 1023 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are 1024 assumed. 1025 @param rvecs Output rotation vectors (see @ref Rodrigues ) that, together with tvecs, brings points from 1026 the model coordinate system to the camera coordinate system. A P3P problem has up to 4 solutions. 1027 @param tvecs Output translation vectors. 1028 @param flags Method for solving a P3P problem: 1029 - **SOLVEPNP_P3P** Method is based on the paper of X.S. Gao, X.-R. Hou, J. Tang, H.-F. Chang 1030 "Complete Solution Classification for the Perspective-Three-Point Problem" (@cite gao2003complete). 1031 - **SOLVEPNP_AP3P** Method is based on the paper of T. Ke and S. Roumeliotis. 1032 "An Efficient Algebraic Solution to the Perspective-Three-Point Problem" (@cite Ke17). 1033 1034 The function estimates the object pose given 3 object points, their corresponding image 1035 projections, as well as the camera matrix and the distortion coefficients. 1036 1037 @note 1038 The solutions are sorted by reprojection errors (lowest to highest). 1039 */ 1040 CV_EXPORTS_W int solveP3P( InputArray objectPoints, InputArray imagePoints, 1041 InputArray cameraMatrix, InputArray distCoeffs, 1042 OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, 1043 int flags ); 1044 1045 /** @brief Refine a pose (the translation and the rotation that transform a 3D point expressed in the object coordinate frame 1046 to the camera coordinate frame) from a 3D-2D point correspondences and starting from an initial solution. 1047 1048 @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or 1xN/Nx1 3-channel, 1049 where N is the number of points. vector\<Point3d\> can also be passed here. 1050 @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel, 1051 where N is the number of points. vector\<Point2d\> can also be passed here. 1052 @param cameraMatrix Input camera matrix \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . 1053 @param distCoeffs Input vector of distortion coefficients 1054 \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of 1055 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are 1056 assumed. 1057 @param rvec Input/Output rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from 1058 the model coordinate system to the camera coordinate system. Input values are used as an initial solution. 1059 @param tvec Input/Output translation vector. Input values are used as an initial solution. 1060 @param criteria Criteria when to stop the Levenberg-Marquard iterative algorithm. 1061 1062 The function refines the object pose given at least 3 object points, their corresponding image 1063 projections, an initial solution for the rotation and translation vector, 1064 as well as the camera matrix and the distortion coefficients. 1065 The function minimizes the projection error with respect to the rotation and the translation vectors, according 1066 to a Levenberg-Marquardt iterative minimization @cite Madsen04 @cite Eade13 process. 1067 */ 1068 CV_EXPORTS_W void solvePnPRefineLM( InputArray objectPoints, InputArray imagePoints, 1069 InputArray cameraMatrix, InputArray distCoeffs, 1070 InputOutputArray rvec, InputOutputArray tvec, 1071 TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 20, FLT_EPSILON)); 1072 1073 /** @brief Refine a pose (the translation and the rotation that transform a 3D point expressed in the object coordinate frame 1074 to the camera coordinate frame) from a 3D-2D point correspondences and starting from an initial solution. 1075 1076 @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or 1xN/Nx1 3-channel, 1077 where N is the number of points. vector\<Point3d\> can also be passed here. 1078 @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel, 1079 where N is the number of points. vector\<Point2d\> can also be passed here. 1080 @param cameraMatrix Input camera matrix \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . 1081 @param distCoeffs Input vector of distortion coefficients 1082 \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of 1083 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are 1084 assumed. 1085 @param rvec Input/Output rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from 1086 the model coordinate system to the camera coordinate system. Input values are used as an initial solution. 1087 @param tvec Input/Output translation vector. Input values are used as an initial solution. 1088 @param criteria Criteria when to stop the Levenberg-Marquard iterative algorithm. 1089 @param VVSlambda Gain for the virtual visual servoing control law, equivalent to the \f$\alpha\f$ 1090 gain in the Damped Gauss-Newton formulation. 1091 1092 The function refines the object pose given at least 3 object points, their corresponding image 1093 projections, an initial solution for the rotation and translation vector, 1094 as well as the camera matrix and the distortion coefficients. 1095 The function minimizes the projection error with respect to the rotation and the translation vectors, using a 1096 virtual visual servoing (VVS) @cite Chaumette06 @cite Marchand16 scheme. 1097 */ 1098 CV_EXPORTS_W void solvePnPRefineVVS( InputArray objectPoints, InputArray imagePoints, 1099 InputArray cameraMatrix, InputArray distCoeffs, 1100 InputOutputArray rvec, InputOutputArray tvec, 1101 TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 20, FLT_EPSILON), 1102 double VVSlambda = 1); 1103 1104 /** @brief Finds an object pose from 3D-2D point correspondences. 1105 This function returns a list of all the possible solutions (a solution is a <rotation vector, translation vector> 1106 couple), depending on the number of input points and the chosen method: 1107 - P3P methods (@ref SOLVEPNP_P3P, @ref SOLVEPNP_AP3P): 3 or 4 input points. Number of returned solutions can be between 0 and 4 with 3 input points. 1108 - @ref SOLVEPNP_IPPE Input points must be >= 4 and object points must be coplanar. Returns 2 solutions. 1109 - @ref SOLVEPNP_IPPE_SQUARE Special case suitable for marker pose estimation. 1110 Number of input points must be 4 and 2 solutions are returned. Object points must be defined in the following order: 1111 - point 0: [-squareLength / 2, squareLength / 2, 0] 1112 - point 1: [ squareLength / 2, squareLength / 2, 0] 1113 - point 2: [ squareLength / 2, -squareLength / 2, 0] 1114 - point 3: [-squareLength / 2, -squareLength / 2, 0] 1115 - for all the other flags, number of input points must be >= 4 and object points can be in any configuration. 1116 Only 1 solution is returned. 1117 1118 @param objectPoints Array of object points in the object coordinate space, Nx3 1-channel or 1119 1xN/Nx1 3-channel, where N is the number of points. vector\<Point3d\> can be also passed here. 1120 @param imagePoints Array of corresponding image points, Nx2 1-channel or 1xN/Nx1 2-channel, 1121 where N is the number of points. vector\<Point2d\> can be also passed here. 1122 @param cameraMatrix Input camera matrix \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . 1123 @param distCoeffs Input vector of distortion coefficients 1124 \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of 1125 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are 1126 assumed. 1127 @param rvecs Vector of output rotation vectors (see @ref Rodrigues ) that, together with tvecs, brings points from 1128 the model coordinate system to the camera coordinate system. 1129 @param tvecs Vector of output translation vectors. 1130 @param useExtrinsicGuess Parameter used for #SOLVEPNP_ITERATIVE. If true (1), the function uses 1131 the provided rvec and tvec values as initial approximations of the rotation and translation 1132 vectors, respectively, and further optimizes them. 1133 @param flags Method for solving a PnP problem: 1134 - **SOLVEPNP_ITERATIVE** Iterative method is based on a Levenberg-Marquardt optimization. In 1135 this case the function finds such a pose that minimizes reprojection error, that is the sum 1136 of squared distances between the observed projections imagePoints and the projected (using 1137 projectPoints ) objectPoints . 1138 - **SOLVEPNP_P3P** Method is based on the paper of X.S. Gao, X.-R. Hou, J. Tang, H.-F. Chang 1139 "Complete Solution Classification for the Perspective-Three-Point Problem" (@cite gao2003complete). 1140 In this case the function requires exactly four object and image points. 1141 - **SOLVEPNP_AP3P** Method is based on the paper of T. Ke, S. Roumeliotis 1142 "An Efficient Algebraic Solution to the Perspective-Three-Point Problem" (@cite Ke17). 1143 In this case the function requires exactly four object and image points. 1144 - **SOLVEPNP_EPNP** Method has been introduced by F.Moreno-Noguer, V.Lepetit and P.Fua in the 1145 paper "EPnP: Efficient Perspective-n-Point Camera Pose Estimation" (@cite lepetit2009epnp). 1146 - **SOLVEPNP_DLS** Method is based on the paper of Joel A. Hesch and Stergios I. Roumeliotis. 1147 "A Direct Least-Squares (DLS) Method for PnP" (@cite hesch2011direct). 1148 - **SOLVEPNP_UPNP** Method is based on the paper of A.Penate-Sanchez, J.Andrade-Cetto, 1149 F.Moreno-Noguer. "Exhaustive Linearization for Robust Camera Pose and Focal Length 1150 Estimation" (@cite penate2013exhaustive). In this case the function also estimates the parameters \f$f_x\f$ and \f$f_y\f$ 1151 assuming that both have the same value. Then the cameraMatrix is updated with the estimated 1152 focal length. 1153 - **SOLVEPNP_IPPE** Method is based on the paper of T. Collins and A. Bartoli. 1154 "Infinitesimal Plane-Based Pose Estimation" (@cite Collins14). This method requires coplanar object points. 1155 - **SOLVEPNP_IPPE_SQUARE** Method is based on the paper of Toby Collins and Adrien Bartoli. 1156 "Infinitesimal Plane-Based Pose Estimation" (@cite Collins14). This method is suitable for marker pose estimation. 1157 It requires 4 coplanar object points defined in the following order: 1158 - point 0: [-squareLength / 2, squareLength / 2, 0] 1159 - point 1: [ squareLength / 2, squareLength / 2, 0] 1160 - point 2: [ squareLength / 2, -squareLength / 2, 0] 1161 - point 3: [-squareLength / 2, -squareLength / 2, 0] 1162 @param rvec Rotation vector used to initialize an iterative PnP refinement algorithm, when flag is SOLVEPNP_ITERATIVE 1163 and useExtrinsicGuess is set to true. 1164 @param tvec Translation vector used to initialize an iterative PnP refinement algorithm, when flag is SOLVEPNP_ITERATIVE 1165 and useExtrinsicGuess is set to true. 1166 @param reprojectionError Optional vector of reprojection error, that is the RMS error 1167 (\f$ \text{RMSE} = \sqrt{\frac{\sum_{i}^{N} \left ( \hat{y_i} - y_i \right )^2}{N}} \f$) between the input image points 1168 and the 3D object points projected with the estimated pose. 1169 1170 The function estimates the object pose given a set of object points, their corresponding image 1171 projections, as well as the camera matrix and the distortion coefficients, see the figure below 1172 (more precisely, the X-axis of the camera frame is pointing to the right, the Y-axis downward 1173 and the Z-axis forward). 1174 1175  1176 1177 Points expressed in the world frame \f$ \bf{X}_w \f$ are projected into the image plane \f$ \left[ u, v \right] \f$ 1178 using the perspective projection model \f$ \Pi \f$ and the camera intrinsic parameters matrix \f$ \bf{A} \f$: 1179 1180 \f[ 1181 \begin{align*} 1182 \begin{bmatrix} 1183 u \\ 1184 v \\ 1185 1 1186 \end{bmatrix} &= 1187 \bf{A} \hspace{0.1em} \Pi \hspace{0.2em} ^{c}\bf{T}_w 1188 \begin{bmatrix} 1189 X_{w} \\ 1190 Y_{w} \\ 1191 Z_{w} \\ 1192 1 1193 \end{bmatrix} \\ 1194 \begin{bmatrix} 1195 u \\ 1196 v \\ 1197 1 1198 \end{bmatrix} &= 1199 \begin{bmatrix} 1200 f_x & 0 & c_x \\ 1201 0 & f_y & c_y \\ 1202 0 & 0 & 1 1203 \end{bmatrix} 1204 \begin{bmatrix} 1205 1 & 0 & 0 & 0 \\ 1206 0 & 1 & 0 & 0 \\ 1207 0 & 0 & 1 & 0 1208 \end{bmatrix} 1209 \begin{bmatrix} 1210 r_{11} & r_{12} & r_{13} & t_x \\ 1211 r_{21} & r_{22} & r_{23} & t_y \\ 1212 r_{31} & r_{32} & r_{33} & t_z \\ 1213 0 & 0 & 0 & 1 1214 \end{bmatrix} 1215 \begin{bmatrix} 1216 X_{w} \\ 1217 Y_{w} \\ 1218 Z_{w} \\ 1219 1 1220 \end{bmatrix} 1221 \end{align*} 1222 \f] 1223 1224 The estimated pose is thus the rotation (`rvec`) and the translation (`tvec`) vectors that allow transforming 1225 a 3D point expressed in the world frame into the camera frame: 1226 1227 \f[ 1228 \begin{align*} 1229 \begin{bmatrix} 1230 X_c \\ 1231 Y_c \\ 1232 Z_c \\ 1233 1 1234 \end{bmatrix} &= 1235 \hspace{0.2em} ^{c}\bf{T}_w 1236 \begin{bmatrix} 1237 X_{w} \\ 1238 Y_{w} \\ 1239 Z_{w} \\ 1240 1 1241 \end{bmatrix} \\ 1242 \begin{bmatrix} 1243 X_c \\ 1244 Y_c \\ 1245 Z_c \\ 1246 1 1247 \end{bmatrix} &= 1248 \begin{bmatrix} 1249 r_{11} & r_{12} & r_{13} & t_x \\ 1250 r_{21} & r_{22} & r_{23} & t_y \\ 1251 r_{31} & r_{32} & r_{33} & t_z \\ 1252 0 & 0 & 0 & 1 1253 \end{bmatrix} 1254 \begin{bmatrix} 1255 X_{w} \\ 1256 Y_{w} \\ 1257 Z_{w} \\ 1258 1 1259 \end{bmatrix} 1260 \end{align*} 1261 \f] 1262 1263 @note 1264 - An example of how to use solvePnP for planar augmented reality can be found at 1265 opencv_source_code/samples/python/plane_ar.py 1266 - If you are using Python: 1267 - Numpy array slices won't work as input because solvePnP requires contiguous 1268 arrays (enforced by the assertion using cv::Mat::checkVector() around line 55 of 1269 modules/calib3d/src/solvepnp.cpp version 2.4.9) 1270 - The P3P algorithm requires image points to be in an array of shape (N,1,2) due 1271 to its calling of cv::undistortPoints (around line 75 of modules/calib3d/src/solvepnp.cpp version 2.4.9) 1272 which requires 2-channel information. 1273 - Thus, given some data D = np.array(...) where D.shape = (N,M), in order to use a subset of 1274 it as, e.g., imagePoints, one must effectively copy it into a new array: imagePoints = 1275 np.ascontiguousarray(D[:,:2]).reshape((N,1,2)) 1276 - The methods **SOLVEPNP_DLS** and **SOLVEPNP_UPNP** cannot be used as the current implementations are 1277 unstable and sometimes give completely wrong results. If you pass one of these two 1278 flags, **SOLVEPNP_EPNP** method will be used instead. 1279 - The minimum number of points is 4 in the general case. In the case of **SOLVEPNP_P3P** and **SOLVEPNP_AP3P** 1280 methods, it is required to use exactly 4 points (the first 3 points are used to estimate all the solutions 1281 of the P3P problem, the last one is used to retain the best solution that minimizes the reprojection error). 1282 - With **SOLVEPNP_ITERATIVE** method and `useExtrinsicGuess=true`, the minimum number of points is 3 (3 points 1283 are sufficient to compute a pose but there are up to 4 solutions). The initial solution should be close to the 1284 global solution to converge. 1285 - With **SOLVEPNP_IPPE** input points must be >= 4 and object points must be coplanar. 1286 - With **SOLVEPNP_IPPE_SQUARE** this is a special case suitable for marker pose estimation. 1287 Number of input points must be 4. Object points must be defined in the following order: 1288 - point 0: [-squareLength / 2, squareLength / 2, 0] 1289 - point 1: [ squareLength / 2, squareLength / 2, 0] 1290 - point 2: [ squareLength / 2, -squareLength / 2, 0] 1291 - point 3: [-squareLength / 2, -squareLength / 2, 0] 1292 */ 1293 CV_EXPORTS_W int solvePnPGeneric( InputArray objectPoints, InputArray imagePoints, 1294 InputArray cameraMatrix, InputArray distCoeffs, 1295 OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, 1296 bool useExtrinsicGuess = false, SolvePnPMethod flags = SOLVEPNP_ITERATIVE, 1297 InputArray rvec = noArray(), InputArray tvec = noArray(), 1298 OutputArray reprojectionError = noArray() ); 1299 1300 /** @brief Finds an initial camera matrix from 3D-2D point correspondences. 1301 1302 @param objectPoints Vector of vectors of the calibration pattern points in the calibration pattern 1303 coordinate space. In the old interface all the per-view vectors are concatenated. See 1304 calibrateCamera for details. 1305 @param imagePoints Vector of vectors of the projections of the calibration pattern points. In the 1306 old interface all the per-view vectors are concatenated. 1307 @param imageSize Image size in pixels used to initialize the principal point. 1308 @param aspectRatio If it is zero or negative, both \f$f_x\f$ and \f$f_y\f$ are estimated independently. 1309 Otherwise, \f$f_x = f_y * \texttt{aspectRatio}\f$ . 1310 1311 The function estimates and returns an initial camera matrix for the camera calibration process. 1312 Currently, the function only supports planar calibration patterns, which are patterns where each 1313 object point has z-coordinate =0. 1314 */ 1315 CV_EXPORTS_W Mat initCameraMatrix2D( InputArrayOfArrays objectPoints, 1316 InputArrayOfArrays imagePoints, 1317 Size imageSize, double aspectRatio = 1.0 ); 1318 1319 /** @brief Finds the positions of internal corners of the chessboard. 1320 1321 @param image Source chessboard view. It must be an 8-bit grayscale or color image. 1322 @param patternSize Number of inner corners per a chessboard row and column 1323 ( patternSize = cvSize(points_per_row,points_per_colum) = cvSize(columns,rows) ). 1324 @param corners Output array of detected corners. 1325 @param flags Various operation flags that can be zero or a combination of the following values: 1326 - **CALIB_CB_ADAPTIVE_THRESH** Use adaptive thresholding to convert the image to black 1327 and white, rather than a fixed threshold level (computed from the average image brightness). 1328 - **CALIB_CB_NORMALIZE_IMAGE** Normalize the image gamma with equalizeHist before 1329 applying fixed or adaptive thresholding. 1330 - **CALIB_CB_FILTER_QUADS** Use additional criteria (like contour area, perimeter, 1331 square-like shape) to filter out false quads extracted at the contour retrieval stage. 1332 - **CALIB_CB_FAST_CHECK** Run a fast check on the image that looks for chessboard corners, 1333 and shortcut the call if none is found. This can drastically speed up the call in the 1334 degenerate condition when no chessboard is observed. 1335 1336 The function attempts to determine whether the input image is a view of the chessboard pattern and 1337 locate the internal chessboard corners. The function returns a non-zero value if all of the corners 1338 are found and they are placed in a certain order (row by row, left to right in every row). 1339 Otherwise, if the function fails to find all the corners or reorder them, it returns 0. For example, 1340 a regular chessboard has 8 x 8 squares and 7 x 7 internal corners, that is, points where the black 1341 squares touch each other. The detected coordinates are approximate, and to determine their positions 1342 more accurately, the function calls cornerSubPix. You also may use the function cornerSubPix with 1343 different parameters if returned coordinates are not accurate enough. 1344 1345 Sample usage of detecting and drawing chessboard corners: : 1346 @code 1347 Size patternsize(8,6); //interior number of corners 1348 Mat gray = ....; //source image 1349 vector<Point2f> corners; //this will be filled by the detected corners 1350 1351 //CALIB_CB_FAST_CHECK saves a lot of time on images 1352 //that do not contain any chessboard corners 1353 bool patternfound = findChessboardCorners(gray, patternsize, corners, 1354 CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE 1355 + CALIB_CB_FAST_CHECK); 1356 1357 if(patternfound) 1358 cornerSubPix(gray, corners, Size(11, 11), Size(-1, -1), 1359 TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1)); 1360 1361 drawChessboardCorners(img, patternsize, Mat(corners), patternfound); 1362 @endcode 1363 @note The function requires white space (like a square-thick border, the wider the better) around 1364 the board to make the detection more robust in various environments. Otherwise, if there is no 1365 border and the background is dark, the outer black squares cannot be segmented properly and so the 1366 square grouping and ordering algorithm fails. 1367 */ 1368 CV_EXPORTS_W bool findChessboardCorners( InputArray image, Size patternSize, OutputArray corners, 1369 int flags = CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE ); 1370 1371 //! finds subpixel-accurate positions of the chessboard corners 1372 CV_EXPORTS_W bool find4QuadCornerSubpix( InputArray img, InputOutputArray corners, Size region_size ); 1373 1374 /** @brief Renders the detected chessboard corners. 1375 1376 @param image Destination image. It must be an 8-bit color image. 1377 @param patternSize Number of inner corners per a chessboard row and column 1378 (patternSize = cv::Size(points_per_row,points_per_column)). 1379 @param corners Array of detected corners, the output of findChessboardCorners. 1380 @param patternWasFound Parameter indicating whether the complete board was found or not. The 1381 return value of findChessboardCorners should be passed here. 1382 1383 The function draws individual chessboard corners detected either as red circles if the board was not 1384 found, or as colored corners connected with lines if the board was found. 1385 */ 1386 CV_EXPORTS_W void drawChessboardCorners( InputOutputArray image, Size patternSize, 1387 InputArray corners, bool patternWasFound ); 1388 1389 /** @brief Draw axes of the world/object coordinate system from pose estimation. @sa solvePnP 1390 1391 @param image Input/output image. It must have 1 or 3 channels. The number of channels is not altered. 1392 @param cameraMatrix Input 3x3 floating-point matrix of camera intrinsic parameters. 1393 \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ 1394 @param distCoeffs Input vector of distortion coefficients 1395 \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of 1396 4, 5, 8, 12 or 14 elements. If the vector is empty, the zero distortion coefficients are assumed. 1397 @param rvec Rotation vector (see @ref Rodrigues ) that, together with tvec, brings points from 1398 the model coordinate system to the camera coordinate system. 1399 @param tvec Translation vector. 1400 @param length Length of the painted axes in the same unit than tvec (usually in meters). 1401 @param thickness Line thickness of the painted axes. 1402 1403 This function draws the axes of the world/object coordinate system w.r.t. to the camera frame. 1404 OX is drawn in red, OY in green and OZ in blue. 1405 */ 1406 CV_EXPORTS_W void drawFrameAxes(InputOutputArray image, InputArray cameraMatrix, InputArray distCoeffs, 1407 InputArray rvec, InputArray tvec, float length, int thickness=3); 1408 1409 struct CV_EXPORTS_W_SIMPLE CirclesGridFinderParameters 1410 { 1411 CV_WRAP CirclesGridFinderParameters(); 1412 CV_PROP_RW cv::Size2f densityNeighborhoodSize; 1413 CV_PROP_RW float minDensity; 1414 CV_PROP_RW int kmeansAttempts; 1415 CV_PROP_RW int minDistanceToAddKeypoint; 1416 CV_PROP_RW int keypointScale; 1417 CV_PROP_RW float minGraphConfidence; 1418 CV_PROP_RW float vertexGain; 1419 CV_PROP_RW float vertexPenalty; 1420 CV_PROP_RW float existingVertexGain; 1421 CV_PROP_RW float edgeGain; 1422 CV_PROP_RW float edgePenalty; 1423 CV_PROP_RW float convexHullFactor; 1424 CV_PROP_RW float minRNGEdgeSwitchDist; 1425 1426 enum GridType 1427 { 1428 SYMMETRIC_GRID, ASYMMETRIC_GRID 1429 }; 1430 GridType gridType; 1431 }; 1432 1433 struct CV_EXPORTS_W_SIMPLE CirclesGridFinderParameters2 : public CirclesGridFinderParameters 1434 { 1435 CV_WRAP CirclesGridFinderParameters2(); 1436 1437 CV_PROP_RW float squareSize; //!< Distance between two adjacent points. Used by CALIB_CB_CLUSTERING. 1438 CV_PROP_RW float maxRectifiedDistance; //!< Max deviation from prediction. Used by CALIB_CB_CLUSTERING. 1439 }; 1440 1441 /** @brief Finds centers in the grid of circles. 1442 1443 @param image grid view of input circles; it must be an 8-bit grayscale or color image. 1444 @param patternSize number of circles per row and column 1445 ( patternSize = Size(points_per_row, points_per_colum) ). 1446 @param centers output array of detected centers. 1447 @param flags various operation flags that can be one of the following values: 1448 - **CALIB_CB_SYMMETRIC_GRID** uses symmetric pattern of circles. 1449 - **CALIB_CB_ASYMMETRIC_GRID** uses asymmetric pattern of circles. 1450 - **CALIB_CB_CLUSTERING** uses a special algorithm for grid detection. It is more robust to 1451 perspective distortions but much more sensitive to background clutter. 1452 @param blobDetector feature detector that finds blobs like dark circles on light background. 1453 @param parameters struct for finding circles in a grid pattern. 1454 1455 The function attempts to determine whether the input image contains a grid of circles. If it is, the 1456 function locates centers of the circles. The function returns a non-zero value if all of the centers 1457 have been found and they have been placed in a certain order (row by row, left to right in every 1458 row). Otherwise, if the function fails to find all the corners or reorder them, it returns 0. 1459 1460 Sample usage of detecting and drawing the centers of circles: : 1461 @code 1462 Size patternsize(7,7); //number of centers 1463 Mat gray = ....; //source image 1464 vector<Point2f> centers; //this will be filled by the detected centers 1465 1466 bool patternfound = findCirclesGrid(gray, patternsize, centers); 1467 1468 drawChessboardCorners(img, patternsize, Mat(centers), patternfound); 1469 @endcode 1470 @note The function requires white space (like a square-thick border, the wider the better) around 1471 the board to make the detection more robust in various environments. 1472 */ 1473 CV_EXPORTS_W bool findCirclesGrid( InputArray image, Size patternSize, 1474 OutputArray centers, int flags, 1475 const Ptr<FeatureDetector> &blobDetector, 1476 CirclesGridFinderParameters parameters); 1477 1478 /** @overload */ 1479 CV_EXPORTS_W bool findCirclesGrid2( InputArray image, Size patternSize, 1480 OutputArray centers, int flags, 1481 const Ptr<FeatureDetector> &blobDetector, 1482 CirclesGridFinderParameters2 parameters); 1483 1484 /** @overload */ 1485 CV_EXPORTS_W bool findCirclesGrid( InputArray image, Size patternSize, 1486 OutputArray centers, int flags = CALIB_CB_SYMMETRIC_GRID, 1487 const Ptr<FeatureDetector> &blobDetector = SimpleBlobDetector::create()); 1488 1489 /** @brief Finds the camera intrinsic and extrinsic parameters from several views of a calibration 1490 pattern. 1491 1492 @param objectPoints In the new interface it is a vector of vectors of calibration pattern points in 1493 the calibration pattern coordinate space (e.g. std::vector<std::vector<cv::Vec3f>>). The outer 1494 vector contains as many elements as the number of pattern views. If the same calibration pattern 1495 is shown in each view and it is fully visible, all the vectors will be the same. Although, it is 1496 possible to use partially occluded patterns or even different patterns in different views. Then, 1497 the vectors will be different. Although the points are 3D, they all lie in the calibration pattern's 1498 XY coordinate plane (thus 0 in the Z-coordinate), if the used calibration pattern is a planar rig. 1499 In the old interface all the vectors of object points from different views are concatenated 1500 together. 1501 @param imagePoints In the new interface it is a vector of vectors of the projections of calibration 1502 pattern points (e.g. std::vector<std::vector<cv::Vec2f>>). imagePoints.size() and 1503 objectPoints.size(), and imagePoints[i].size() and objectPoints[i].size() for each i, must be equal, 1504 respectively. In the old interface all the vectors of object points from different views are 1505 concatenated together. 1506 @param imageSize Size of the image used only to initialize the intrinsic camera matrix. 1507 @param cameraMatrix Input/output 3x3 floating-point camera matrix 1508 \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . If CV\_CALIB\_USE\_INTRINSIC\_GUESS 1509 and/or CALIB_FIX_ASPECT_RATIO are specified, some or all of fx, fy, cx, cy must be 1510 initialized before calling the function. 1511 @param distCoeffs Input/output vector of distortion coefficients 1512 \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of 1513 4, 5, 8, 12 or 14 elements. 1514 @param rvecs Output vector of rotation vectors (@ref Rodrigues ) estimated for each pattern view 1515 (e.g. std::vector<cv::Mat>>). That is, each i-th rotation vector together with the corresponding 1516 i-th translation vector (see the next output parameter description) brings the calibration pattern 1517 from the object coordinate space (in which object points are specified) to the camera coordinate 1518 space. In more technical terms, the tuple of the i-th rotation and translation vector performs 1519 a change of basis from object coordinate space to camera coordinate space. Due to its duality, this 1520 tuple is equivalent to the position of the calibration pattern with respect to the camera coordinate 1521 space. 1522 @param tvecs Output vector of translation vectors estimated for each pattern view, see parameter 1523 describtion above. 1524 @param stdDeviationsIntrinsics Output vector of standard deviations estimated for intrinsic 1525 parameters. Order of deviations values: 1526 \f$(f_x, f_y, c_x, c_y, k_1, k_2, p_1, p_2, k_3, k_4, k_5, k_6 , s_1, s_2, s_3, 1527 s_4, \tau_x, \tau_y)\f$ If one of parameters is not estimated, it's deviation is equals to zero. 1528 @param stdDeviationsExtrinsics Output vector of standard deviations estimated for extrinsic 1529 parameters. Order of deviations values: \f$(R_0, T_0, \dotsc , R_{M - 1}, T_{M - 1})\f$ where M is 1530 the number of pattern views. \f$R_i, T_i\f$ are concatenated 1x3 vectors. 1531 @param perViewErrors Output vector of the RMS re-projection error estimated for each pattern view. 1532 @param flags Different flags that may be zero or a combination of the following values: 1533 - **CALIB_USE_INTRINSIC_GUESS** cameraMatrix contains valid initial values of 1534 fx, fy, cx, cy that are optimized further. Otherwise, (cx, cy) is initially set to the image 1535 center ( imageSize is used), and focal distances are computed in a least-squares fashion. 1536 Note, that if intrinsic parameters are known, there is no need to use this function just to 1537 estimate extrinsic parameters. Use solvePnP instead. 1538 - **CALIB_FIX_PRINCIPAL_POINT** The principal point is not changed during the global 1539 optimization. It stays at the center or at a different location specified when 1540 CALIB_USE_INTRINSIC_GUESS is set too. 1541 - **CALIB_FIX_ASPECT_RATIO** The functions consider only fy as a free parameter. The 1542 ratio fx/fy stays the same as in the input cameraMatrix . When 1543 CALIB_USE_INTRINSIC_GUESS is not set, the actual input values of fx and fy are 1544 ignored, only their ratio is computed and used further. 1545 - **CALIB_ZERO_TANGENT_DIST** Tangential distortion coefficients \f$(p_1, p_2)\f$ are set 1546 to zeros and stay zero. 1547 - **CALIB_FIX_K1,...,CALIB_FIX_K6** The corresponding radial distortion 1548 coefficient is not changed during the optimization. If CALIB_USE_INTRINSIC_GUESS is 1549 set, the coefficient from the supplied distCoeffs matrix is used. Otherwise, it is set to 0. 1550 - **CALIB_RATIONAL_MODEL** Coefficients k4, k5, and k6 are enabled. To provide the 1551 backward compatibility, this extra flag should be explicitly specified to make the 1552 calibration function use the rational model and return 8 coefficients. If the flag is not 1553 set, the function computes and returns only 5 distortion coefficients. 1554 - **CALIB_THIN_PRISM_MODEL** Coefficients s1, s2, s3 and s4 are enabled. To provide the 1555 backward compatibility, this extra flag should be explicitly specified to make the 1556 calibration function use the thin prism model and return 12 coefficients. If the flag is not 1557 set, the function computes and returns only 5 distortion coefficients. 1558 - **CALIB_FIX_S1_S2_S3_S4** The thin prism distortion coefficients are not changed during 1559 the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the 1560 supplied distCoeffs matrix is used. Otherwise, it is set to 0. 1561 - **CALIB_TILTED_MODEL** Coefficients tauX and tauY are enabled. To provide the 1562 backward compatibility, this extra flag should be explicitly specified to make the 1563 calibration function use the tilted sensor model and return 14 coefficients. If the flag is not 1564 set, the function computes and returns only 5 distortion coefficients. 1565 - **CALIB_FIX_TAUX_TAUY** The coefficients of the tilted sensor model are not changed during 1566 the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the 1567 supplied distCoeffs matrix is used. Otherwise, it is set to 0. 1568 @param criteria Termination criteria for the iterative optimization algorithm. 1569 1570 @return the overall RMS re-projection error. 1571 1572 The function estimates the intrinsic camera parameters and extrinsic parameters for each of the 1573 views. The algorithm is based on @cite Zhang2000 and @cite BouguetMCT . The coordinates of 3D object 1574 points and their corresponding 2D projections in each view must be specified. That may be achieved 1575 by using an object with known geometry and easily detectable feature points. Such an object is 1576 called a calibration rig or calibration pattern, and OpenCV has built-in support for a chessboard as 1577 a calibration rig (see @ref findChessboardCorners). Currently, initialization of intrinsic 1578 parameters (when CALIB_USE_INTRINSIC_GUESS is not set) is only implemented for planar calibration 1579 patterns (where Z-coordinates of the object points must be all zeros). 3D calibration rigs can also 1580 be used as long as initial cameraMatrix is provided. 1581 1582 The algorithm performs the following steps: 1583 1584 - Compute the initial intrinsic parameters (the option only available for planar calibration 1585 patterns) or read them from the input parameters. The distortion coefficients are all set to 1586 zeros initially unless some of CALIB_FIX_K? are specified. 1587 1588 - Estimate the initial camera pose as if the intrinsic parameters have been already known. This is 1589 done using solvePnP . 1590 1591 - Run the global Levenberg-Marquardt optimization algorithm to minimize the reprojection error, 1592 that is, the total sum of squared distances between the observed feature points imagePoints and 1593 the projected (using the current estimates for camera parameters and the poses) object points 1594 objectPoints. See projectPoints for details. 1595 1596 @note 1597 If you use a non-square (i.e. non-N-by-N) grid and @ref findChessboardCorners for calibration, 1598 and @ref calibrateCamera returns bad values (zero distortion coefficients, \f$c_x\f$ and 1599 \f$c_y\f$ very far from the image center, and/or large differences between \f$f_x\f$ and 1600 \f$f_y\f$ (ratios of 10:1 or more)), then you are probably using patternSize=cvSize(rows,cols) 1601 instead of using patternSize=cvSize(cols,rows) in @ref findChessboardCorners. 1602 1603 @sa 1604 findChessboardCorners, solvePnP, initCameraMatrix2D, stereoCalibrate, undistort 1605 */ 1606 CV_EXPORTS_AS(calibrateCameraExtended) double calibrateCamera( InputArrayOfArrays objectPoints, 1607 InputArrayOfArrays imagePoints, Size imageSize, 1608 InputOutputArray cameraMatrix, InputOutputArray distCoeffs, 1609 OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, 1610 OutputArray stdDeviationsIntrinsics, 1611 OutputArray stdDeviationsExtrinsics, 1612 OutputArray perViewErrors, 1613 int flags = 0, TermCriteria criteria = TermCriteria( 1614 TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) ); 1615 1616 /** @overload double calibrateCamera( InputArrayOfArrays objectPoints, 1617 InputArrayOfArrays imagePoints, Size imageSize, 1618 InputOutputArray cameraMatrix, InputOutputArray distCoeffs, 1619 OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, 1620 OutputArray stdDeviations, OutputArray perViewErrors, 1621 int flags = 0, TermCriteria criteria = TermCriteria( 1622 TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) ) 1623 */ 1624 CV_EXPORTS_W double calibrateCamera( InputArrayOfArrays objectPoints, 1625 InputArrayOfArrays imagePoints, Size imageSize, 1626 InputOutputArray cameraMatrix, InputOutputArray distCoeffs, 1627 OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, 1628 int flags = 0, TermCriteria criteria = TermCriteria( 1629 TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) ); 1630 1631 /** @brief Computes useful camera characteristics from the camera matrix. 1632 1633 @param cameraMatrix Input camera matrix that can be estimated by calibrateCamera or 1634 stereoCalibrate . 1635 @param imageSize Input image size in pixels. 1636 @param apertureWidth Physical width in mm of the sensor. 1637 @param apertureHeight Physical height in mm of the sensor. 1638 @param fovx Output field of view in degrees along the horizontal sensor axis. 1639 @param fovy Output field of view in degrees along the vertical sensor axis. 1640 @param focalLength Focal length of the lens in mm. 1641 @param principalPoint Principal point in mm. 1642 @param aspectRatio \f$f_y/f_x\f$ 1643 1644 The function computes various useful camera characteristics from the previously estimated camera 1645 matrix. 1646 1647 @note 1648 Do keep in mind that the unity measure 'mm' stands for whatever unit of measure one chooses for 1649 the chessboard pitch (it can thus be any value). 1650 */ 1651 CV_EXPORTS_W void calibrationMatrixValues( InputArray cameraMatrix, Size imageSize, 1652 double apertureWidth, double apertureHeight, 1653 CV_OUT double& fovx, CV_OUT double& fovy, 1654 CV_OUT double& focalLength, CV_OUT Point2d& principalPoint, 1655 CV_OUT double& aspectRatio ); 1656 1657 /** @brief Calibrates a stereo camera set up. This function finds the intrinsic parameters 1658 for each of the two cameras and the extrinsic parameters between the two cameras. 1659 1660 @param objectPoints Vector of vectors of the calibration pattern points. The same structure as 1661 in @ref calibrateCamera. For each pattern view, both cameras need to see the same object 1662 points. Therefore, objectPoints.size(), imagePoints1.size(), and imagePoints2.size() need to be 1663 equal as well as objectPoints[i].size(), imagePoints1[i].size(), and imagePoints2[i].size() need to 1664 be equal for each i. 1665 @param imagePoints1 Vector of vectors of the projections of the calibration pattern points, 1666 observed by the first camera. The same structure as in @ref calibrateCamera. 1667 @param imagePoints2 Vector of vectors of the projections of the calibration pattern points, 1668 observed by the second camera. The same structure as in @ref calibrateCamera. 1669 @param cameraMatrix1 Input/output camera matrix for the first camera, the same as in 1670 @ref calibrateCamera. Furthermore, for the stereo case, additional flags may be used, see below. 1671 @param distCoeffs1 Input/output vector of distortion coefficients, the same as in 1672 @ref calibrateCamera. 1673 @param cameraMatrix2 Input/output second camera matrix for the second camera. See description for 1674 cameraMatrix1. 1675 @param distCoeffs2 Input/output lens distortion coefficients for the second camera. See 1676 description for distCoeffs1. 1677 @param imageSize Size of the image used only to initialize the intrinsic camera matrices. 1678 @param R Output rotation matrix. Together with the translation vector T, this matrix brings 1679 points given in the first camera's coordinate system to points in the second camera's 1680 coordinate system. In more technical terms, the tuple of R and T performs a change of basis 1681 from the first camera's coordinate system to the second camera's coordinate system. Due to its 1682 duality, this tuple is equivalent to the position of the first camera with respect to the 1683 second camera coordinate system. 1684 @param T Output translation vector, see description above. 1685 @param E Output essential matrix. 1686 @param F Output fundamental matrix. 1687 @param perViewErrors Output vector of the RMS re-projection error estimated for each pattern view. 1688 @param flags Different flags that may be zero or a combination of the following values: 1689 - **CALIB_FIX_INTRINSIC** Fix cameraMatrix? and distCoeffs? so that only R, T, E, and F 1690 matrices are estimated. 1691 - **CALIB_USE_INTRINSIC_GUESS** Optimize some or all of the intrinsic parameters 1692 according to the specified flags. Initial values are provided by the user. 1693 - **CALIB_USE_EXTRINSIC_GUESS** R and T contain valid initial values that are optimized further. 1694 Otherwise R and T are initialized to the median value of the pattern views (each dimension separately). 1695 - **CALIB_FIX_PRINCIPAL_POINT** Fix the principal points during the optimization. 1696 - **CALIB_FIX_FOCAL_LENGTH** Fix \f$f^{(j)}_x\f$ and \f$f^{(j)}_y\f$ . 1697 - **CALIB_FIX_ASPECT_RATIO** Optimize \f$f^{(j)}_y\f$ . Fix the ratio \f$f^{(j)}_x/f^{(j)}_y\f$ 1698 . 1699 - **CALIB_SAME_FOCAL_LENGTH** Enforce \f$f^{(0)}_x=f^{(1)}_x\f$ and \f$f^{(0)}_y=f^{(1)}_y\f$ . 1700 - **CALIB_ZERO_TANGENT_DIST** Set tangential distortion coefficients for each camera to 1701 zeros and fix there. 1702 - **CALIB_FIX_K1,...,CALIB_FIX_K6** Do not change the corresponding radial 1703 distortion coefficient during the optimization. If CALIB_USE_INTRINSIC_GUESS is set, 1704 the coefficient from the supplied distCoeffs matrix is used. Otherwise, it is set to 0. 1705 - **CALIB_RATIONAL_MODEL** Enable coefficients k4, k5, and k6. To provide the backward 1706 compatibility, this extra flag should be explicitly specified to make the calibration 1707 function use the rational model and return 8 coefficients. If the flag is not set, the 1708 function computes and returns only 5 distortion coefficients. 1709 - **CALIB_THIN_PRISM_MODEL** Coefficients s1, s2, s3 and s4 are enabled. To provide the 1710 backward compatibility, this extra flag should be explicitly specified to make the 1711 calibration function use the thin prism model and return 12 coefficients. If the flag is not 1712 set, the function computes and returns only 5 distortion coefficients. 1713 - **CALIB_FIX_S1_S2_S3_S4** The thin prism distortion coefficients are not changed during 1714 the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the 1715 supplied distCoeffs matrix is used. Otherwise, it is set to 0. 1716 - **CALIB_TILTED_MODEL** Coefficients tauX and tauY are enabled. To provide the 1717 backward compatibility, this extra flag should be explicitly specified to make the 1718 calibration function use the tilted sensor model and return 14 coefficients. If the flag is not 1719 set, the function computes and returns only 5 distortion coefficients. 1720 - **CALIB_FIX_TAUX_TAUY** The coefficients of the tilted sensor model are not changed during 1721 the optimization. If CALIB_USE_INTRINSIC_GUESS is set, the coefficient from the 1722 supplied distCoeffs matrix is used. Otherwise, it is set to 0. 1723 @param criteria Termination criteria for the iterative optimization algorithm. 1724 1725 The function estimates the transformation between two cameras making a stereo pair. If one computes 1726 the poses of an object relative to the first camera and to the second camera, 1727 ( \f$R_1\f$,\f$T_1\f$ ) and (\f$R_2\f$,\f$T_2\f$), respectively, for a stereo camera where the 1728 relative position and orientation between the two cameras are fixed, then those poses definitely 1729 relate to each other. This means, if the relative position and orientation (\f$R\f$,\f$T\f$) of the 1730 two cameras is known, it is possible to compute (\f$R_2\f$,\f$T_2\f$) when (\f$R_1\f$,\f$T_1\f$) is 1731 given. This is what the described function does. It computes (\f$R\f$,\f$T\f$) such that: 1732 1733 \f[R_2=R R_1\f] 1734 \f[T_2=R T_1 + T.\f] 1735 1736 Therefore, one can compute the coordinate representation of a 3D point for the second camera's 1737 coordinate system when given the point's coordinate representation in the first camera's coordinate 1738 system: 1739 1740 \f[\begin{bmatrix} 1741 X_2 \\ 1742 Y_2 \\ 1743 Z_2 \\ 1744 1 1745 \end{bmatrix} = \begin{bmatrix} 1746 R & T \\ 1747 0 & 1 1748 \end{bmatrix} \begin{bmatrix} 1749 X_1 \\ 1750 Y_1 \\ 1751 Z_1 \\ 1752 1 1753 \end{bmatrix}.\f] 1754 1755 1756 Optionally, it computes the essential matrix E: 1757 1758 \f[E= \vecthreethree{0}{-T_2}{T_1}{T_2}{0}{-T_0}{-T_1}{T_0}{0} R\f] 1759 1760 where \f$T_i\f$ are components of the translation vector \f$T\f$ : \f$T=[T_0, T_1, T_2]^T\f$ . 1761 And the function can also compute the fundamental matrix F: 1762 1763 \f[F = cameraMatrix2^{-T} E cameraMatrix1^{-1}\f] 1764 1765 Besides the stereo-related information, the function can also perform a full calibration of each of 1766 the two cameras. However, due to the high dimensionality of the parameter space and noise in the 1767 input data, the function can diverge from the correct solution. If the intrinsic parameters can be 1768 estimated with high accuracy for each of the cameras individually (for example, using 1769 calibrateCamera ), you are recommended to do so and then pass CALIB_FIX_INTRINSIC flag to the 1770 function along with the computed intrinsic parameters. Otherwise, if all the parameters are 1771 estimated at once, it makes sense to restrict some parameters, for example, pass 1772 CALIB_SAME_FOCAL_LENGTH and CALIB_ZERO_TANGENT_DIST flags, which is usually a 1773 reasonable assumption. 1774 1775 Similarly to calibrateCamera, the function minimizes the total re-projection error for all the 1776 points in all the available views from both cameras. The function returns the final value of the 1777 re-projection error. 1778 */ 1779 CV_EXPORTS_AS(stereoCalibrateExtended) double stereoCalibrate( InputArrayOfArrays objectPoints, 1780 InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2, 1781 InputOutputArray cameraMatrix1, InputOutputArray distCoeffs1, 1782 InputOutputArray cameraMatrix2, InputOutputArray distCoeffs2, 1783 Size imageSize, InputOutputArray R,InputOutputArray T, OutputArray E, OutputArray F, 1784 OutputArray perViewErrors, int flags = CALIB_FIX_INTRINSIC, 1785 TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 1e-6) ); 1786 1787 /// @overload 1788 CV_EXPORTS_W double stereoCalibrate( InputArrayOfArrays objectPoints, 1789 InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2, 1790 InputOutputArray cameraMatrix1, InputOutputArray distCoeffs1, 1791 InputOutputArray cameraMatrix2, InputOutputArray distCoeffs2, 1792 Size imageSize, OutputArray R,OutputArray T, OutputArray E, OutputArray F, 1793 int flags = CALIB_FIX_INTRINSIC, 1794 TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 1e-6) ); 1795 1796 /** @brief Computes rectification transforms for each head of a calibrated stereo camera. 1797 1798 @param cameraMatrix1 First camera matrix. 1799 @param distCoeffs1 First camera distortion parameters. 1800 @param cameraMatrix2 Second camera matrix. 1801 @param distCoeffs2 Second camera distortion parameters. 1802 @param imageSize Size of the image used for stereo calibration. 1803 @param R Rotation matrix from the coordinate system of the first camera to the second camera, 1804 see @ref stereoCalibrate. 1805 @param T Translation vector from the coordinate system of the first camera to the second camera, 1806 see @ref stereoCalibrate. 1807 @param R1 Output 3x3 rectification transform (rotation matrix) for the first camera. This matrix 1808 brings points given in the unrectified first camera's coordinate system to points in the rectified 1809 first camera's coordinate system. In more technical terms, it performs a change of basis from the 1810 unrectified first camera's coordinate system to the rectified first camera's coordinate system. 1811 @param R2 Output 3x3 rectification transform (rotation matrix) for the second camera. This matrix 1812 brings points given in the unrectified second camera's coordinate system to points in the rectified 1813 second camera's coordinate system. In more technical terms, it performs a change of basis from the 1814 unrectified second camera's coordinate system to the rectified second camera's coordinate system. 1815 @param P1 Output 3x4 projection matrix in the new (rectified) coordinate systems for the first 1816 camera, i.e. it projects points given in the rectified first camera coordinate system into the 1817 rectified first camera's image. 1818 @param P2 Output 3x4 projection matrix in the new (rectified) coordinate systems for the second 1819 camera, i.e. it projects points given in the rectified first camera coordinate system into the 1820 rectified second camera's image. 1821 @param Q Output \f$4 \times 4\f$ disparity-to-depth mapping matrix (see @ref reprojectImageTo3D). 1822 @param flags Operation flags that may be zero or CALIB_ZERO_DISPARITY . If the flag is set, 1823 the function makes the principal points of each camera have the same pixel coordinates in the 1824 rectified views. And if the flag is not set, the function may still shift the images in the 1825 horizontal or vertical direction (depending on the orientation of epipolar lines) to maximize the 1826 useful image area. 1827 @param alpha Free scaling parameter. If it is -1 or absent, the function performs the default 1828 scaling. Otherwise, the parameter should be between 0 and 1. alpha=0 means that the rectified 1829 images are zoomed and shifted so that only valid pixels are visible (no black areas after 1830 rectification). alpha=1 means that the rectified image is decimated and shifted so that all the 1831 pixels from the original images from the cameras are retained in the rectified images (no source 1832 image pixels are lost). Any intermediate value yields an intermediate result between 1833 those two extreme cases. 1834 @param newImageSize New image resolution after rectification. The same size should be passed to 1835 initUndistortRectifyMap (see the stereo_calib.cpp sample in OpenCV samples directory). When (0,0) 1836 is passed (default), it is set to the original imageSize . Setting it to a larger value can help you 1837 preserve details in the original image, especially when there is a big radial distortion. 1838 @param validPixROI1 Optional output rectangles inside the rectified images where all the pixels 1839 are valid. If alpha=0 , the ROIs cover the whole images. Otherwise, they are likely to be smaller 1840 (see the picture below). 1841 @param validPixROI2 Optional output rectangles inside the rectified images where all the pixels 1842 are valid. If alpha=0 , the ROIs cover the whole images. Otherwise, they are likely to be smaller 1843 (see the picture below). 1844 1845 The function computes the rotation matrices for each camera that (virtually) make both camera image 1846 planes the same plane. Consequently, this makes all the epipolar lines parallel and thus simplifies 1847 the dense stereo correspondence problem. The function takes the matrices computed by stereoCalibrate 1848 as input. As output, it provides two rotation matrices and also two projection matrices in the new 1849 coordinates. The function distinguishes the following two cases: 1850 1851 - **Horizontal stereo**: the first and the second camera views are shifted relative to each other 1852 mainly along the x-axis (with possible small vertical shift). In the rectified images, the 1853 corresponding epipolar lines in the left and right cameras are horizontal and have the same 1854 y-coordinate. P1 and P2 look like: 1855 1856 \f[\texttt{P1} = \begin{bmatrix} 1857 f & 0 & cx_1 & 0 \\ 1858 0 & f & cy & 0 \\ 1859 0 & 0 & 1 & 0 1860 \end{bmatrix}\f] 1861 1862 \f[\texttt{P2} = \begin{bmatrix} 1863 f & 0 & cx_2 & T_x*f \\ 1864 0 & f & cy & 0 \\ 1865 0 & 0 & 1 & 0 1866 \end{bmatrix} ,\f] 1867 1868 where \f$T_x\f$ is a horizontal shift between the cameras and \f$cx_1=cx_2\f$ if 1869 CALIB_ZERO_DISPARITY is set. 1870 1871 - **Vertical stereo**: the first and the second camera views are shifted relative to each other 1872 mainly in the vertical direction (and probably a bit in the horizontal direction too). The epipolar 1873 lines in the rectified images are vertical and have the same x-coordinate. P1 and P2 look like: 1874 1875 \f[\texttt{P1} = \begin{bmatrix} 1876 f & 0 & cx & 0 \\ 1877 0 & f & cy_1 & 0 \\ 1878 0 & 0 & 1 & 0 1879 \end{bmatrix}\f] 1880 1881 \f[\texttt{P2} = \begin{bmatrix} 1882 f & 0 & cx & 0 \\ 1883 0 & f & cy_2 & T_y*f \\ 1884 0 & 0 & 1 & 0 1885 \end{bmatrix},\f] 1886 1887 where \f$T_y\f$ is a vertical shift between the cameras and \f$cy_1=cy_2\f$ if 1888 CALIB_ZERO_DISPARITY is set. 1889 1890 As you can see, the first three columns of P1 and P2 will effectively be the new "rectified" camera 1891 matrices. The matrices, together with R1 and R2 , can then be passed to initUndistortRectifyMap to 1892 initialize the rectification map for each camera. 1893 1894 See below the screenshot from the stereo_calib.cpp sample. Some red horizontal lines pass through 1895 the corresponding image regions. This means that the images are well rectified, which is what most 1896 stereo correspondence algorithms rely on. The green rectangles are roi1 and roi2 . You see that 1897 their interiors are all valid pixels. 1898 1899  1900 */ 1901 CV_EXPORTS_W void stereoRectify( InputArray cameraMatrix1, InputArray distCoeffs1, 1902 InputArray cameraMatrix2, InputArray distCoeffs2, 1903 Size imageSize, InputArray R, InputArray T, 1904 OutputArray R1, OutputArray R2, 1905 OutputArray P1, OutputArray P2, 1906 OutputArray Q, int flags = CALIB_ZERO_DISPARITY, 1907 double alpha = -1, Size newImageSize = Size(), 1908 CV_OUT Rect* validPixROI1 = 0, CV_OUT Rect* validPixROI2 = 0 ); 1909 1910 /** @brief Computes a rectification transform for an uncalibrated stereo camera. 1911 1912 @param points1 Array of feature points in the first image. 1913 @param points2 The corresponding points in the second image. The same formats as in 1914 findFundamentalMat are supported. 1915 @param F Input fundamental matrix. It can be computed from the same set of point pairs using 1916 findFundamentalMat . 1917 @param imgSize Size of the image. 1918 @param H1 Output rectification homography matrix for the first image. 1919 @param H2 Output rectification homography matrix for the second image. 1920 @param threshold Optional threshold used to filter out the outliers. If the parameter is greater 1921 than zero, all the point pairs that do not comply with the epipolar geometry (that is, the points 1922 for which \f$|\texttt{points2[i]}^T*\texttt{F}*\texttt{points1[i]}|>\texttt{threshold}\f$ ) are 1923 rejected prior to computing the homographies. Otherwise, all the points are considered inliers. 1924 1925 The function computes the rectification transformations without knowing intrinsic parameters of the 1926 cameras and their relative position in the space, which explains the suffix "uncalibrated". Another 1927 related difference from stereoRectify is that the function outputs not the rectification 1928 transformations in the object (3D) space, but the planar perspective transformations encoded by the 1929 homography matrices H1 and H2 . The function implements the algorithm @cite Hartley99 . 1930 1931 @note 1932 While the algorithm does not need to know the intrinsic parameters of the cameras, it heavily 1933 depends on the epipolar geometry. Therefore, if the camera lenses have a significant distortion, 1934 it would be better to correct it before computing the fundamental matrix and calling this 1935 function. For example, distortion coefficients can be estimated for each head of stereo camera 1936 separately by using calibrateCamera . Then, the images can be corrected using undistort , or 1937 just the point coordinates can be corrected with undistortPoints . 1938 */ 1939 CV_EXPORTS_W bool stereoRectifyUncalibrated( InputArray points1, InputArray points2, 1940 InputArray F, Size imgSize, 1941 OutputArray H1, OutputArray H2, 1942 double threshold = 5 ); 1943 1944 //! computes the rectification transformations for 3-head camera, where all the heads are on the same line. 1945 CV_EXPORTS_W float rectify3Collinear( InputArray cameraMatrix1, InputArray distCoeffs1, 1946 InputArray cameraMatrix2, InputArray distCoeffs2, 1947 InputArray cameraMatrix3, InputArray distCoeffs3, 1948 InputArrayOfArrays imgpt1, InputArrayOfArrays imgpt3, 1949 Size imageSize, InputArray R12, InputArray T12, 1950 InputArray R13, InputArray T13, 1951 OutputArray R1, OutputArray R2, OutputArray R3, 1952 OutputArray P1, OutputArray P2, OutputArray P3, 1953 OutputArray Q, double alpha, Size newImgSize, 1954 CV_OUT Rect* roi1, CV_OUT Rect* roi2, int flags ); 1955 1956 /** @brief Returns the new camera matrix based on the free scaling parameter. 1957 1958 @param cameraMatrix Input camera matrix. 1959 @param distCoeffs Input vector of distortion coefficients 1960 \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \tau_x, \tau_y]]]])\f$ of 1961 4, 5, 8, 12 or 14 elements. If the vector is NULL/empty, the zero distortion coefficients are 1962 assumed. 1963 @param imageSize Original image size. 1964 @param alpha Free scaling parameter between 0 (when all the pixels in the undistorted image are 1965 valid) and 1 (when all the source image pixels are retained in the undistorted image). See 1966 stereoRectify for details. 1967 @param newImgSize Image size after rectification. By default, it is set to imageSize . 1968 @param validPixROI Optional output rectangle that outlines all-good-pixels region in the 1969 undistorted image. See roi1, roi2 description in stereoRectify . 1970 @param centerPrincipalPoint Optional flag that indicates whether in the new camera matrix the 1971 principal point should be at the image center or not. By default, the principal point is chosen to 1972 best fit a subset of the source image (determined by alpha) to the corrected image. 1973 @return new_camera_matrix Output new camera matrix. 1974 1975 The function computes and returns the optimal new camera matrix based on the free scaling parameter. 1976 By varying this parameter, you may retrieve only sensible pixels alpha=0 , keep all the original 1977 image pixels if there is valuable information in the corners alpha=1 , or get something in between. 1978 When alpha\>0 , the undistorted result is likely to have some black pixels corresponding to 1979 "virtual" pixels outside of the captured distorted image. The original camera matrix, distortion 1980 coefficients, the computed new camera matrix, and newImageSize should be passed to 1981 initUndistortRectifyMap to produce the maps for remap . 1982 */ 1983 CV_EXPORTS_W Mat getOptimalNewCameraMatrix( InputArray cameraMatrix, InputArray distCoeffs, 1984 Size imageSize, double alpha, Size newImgSize = Size(), 1985 CV_OUT Rect* validPixROI = 0, 1986 bool centerPrincipalPoint = false); 1987 1988 /** @brief Computes Hand-Eye calibration: \f$_{}^{g}\textrm{T}_c\f$ 1989 1990 @param[in] R_gripper2base Rotation part extracted from the homogeneous matrix that transforms a point 1991 expressed in the gripper frame to the robot base frame (\f$_{}^{b}\textrm{T}_g\f$). 1992 This is a vector (`vector<Mat>`) that contains the rotation matrices for all the transformations 1993 from gripper frame to robot base frame. 1994 @param[in] t_gripper2base Translation part extracted from the homogeneous matrix that transforms a point 1995 expressed in the gripper frame to the robot base frame (\f$_{}^{b}\textrm{T}_g\f$). 1996 This is a vector (`vector<Mat>`) that contains the translation vectors for all the transformations 1997 from gripper frame to robot base frame. 1998 @param[in] R_target2cam Rotation part extracted from the homogeneous matrix that transforms a point 1999 expressed in the target frame to the camera frame (\f$_{}^{c}\textrm{T}_t\f$). 2000 This is a vector (`vector<Mat>`) that contains the rotation matrices for all the transformations 2001 from calibration target frame to camera frame. 2002 @param[in] t_target2cam Rotation part extracted from the homogeneous matrix that transforms a point 2003 expressed in the target frame to the camera frame (\f$_{}^{c}\textrm{T}_t\f$). 2004 This is a vector (`vector<Mat>`) that contains the translation vectors for all the transformations 2005 from calibration target frame to camera frame. 2006 @param[out] R_cam2gripper Estimated rotation part extracted from the homogeneous matrix that transforms a point 2007 expressed in the camera frame to the gripper frame (\f$_{}^{g}\textrm{T}_c\f$). 2008 @param[out] t_cam2gripper Estimated translation part extracted from the homogeneous matrix that transforms a point 2009 expressed in the camera frame to the gripper frame (\f$_{}^{g}\textrm{T}_c\f$). 2010 @param[in] method One of the implemented Hand-Eye calibration method, see cv::HandEyeCalibrationMethod 2011 2012 The function performs the Hand-Eye calibration using various methods. One approach consists in estimating the 2013 rotation then the translation (separable solutions) and the following methods are implemented: 2014 - R. Tsai, R. Lenz A New Technique for Fully Autonomous and Efficient 3D Robotics Hand/EyeCalibration \cite Tsai89 2015 - F. Park, B. Martin Robot Sensor Calibration: Solving AX = XB on the Euclidean Group \cite Park94 2016 - R. Horaud, F. Dornaika Hand-Eye Calibration \cite Horaud95 2017 2018 Another approach consists in estimating simultaneously the rotation and the translation (simultaneous solutions), 2019 with the following implemented method: 2020 - N. Andreff, R. Horaud, B. Espiau On-line Hand-Eye Calibration \cite Andreff99 2021 - K. Daniilidis Hand-Eye Calibration Using Dual Quaternions \cite Daniilidis98 2022 2023 The following picture describes the Hand-Eye calibration problem where the transformation between a camera ("eye") 2024 mounted on a robot gripper ("hand") has to be estimated. 2025 2026  2027 2028 The calibration procedure is the following: 2029 - a static calibration pattern is used to estimate the transformation between the target frame 2030 and the camera frame 2031 - the robot gripper is moved in order to acquire several poses 2032 - for each pose, the homogeneous transformation between the gripper frame and the robot base frame is recorded using for 2033 instance the robot kinematics 2034 \f[ 2035 \begin{bmatrix} 2036 X_b\\ 2037 Y_b\\ 2038 Z_b\\ 2039 1 2040 \end{bmatrix} 2041 = 2042 \begin{bmatrix} 2043 _{}^{b}\textrm{R}_g & _{}^{b}\textrm{t}_g \\ 2044 0_{1 \times 3} & 1 2045 \end{bmatrix} 2046 \begin{bmatrix} 2047 X_g\\ 2048 Y_g\\ 2049 Z_g\\ 2050 1 2051 \end{bmatrix} 2052 \f] 2053 - for each pose, the homogeneous transformation between the calibration target frame and the camera frame is recorded using 2054 for instance a pose estimation method (PnP) from 2D-3D point correspondences 2055 \f[ 2056 \begin{bmatrix} 2057 X_c\\ 2058 Y_c\\ 2059 Z_c\\ 2060 1 2061 \end{bmatrix} 2062 = 2063 \begin{bmatrix} 2064 _{}^{c}\textrm{R}_t & _{}^{c}\textrm{t}_t \\ 2065 0_{1 \times 3} & 1 2066 \end{bmatrix} 2067 \begin{bmatrix} 2068 X_t\\ 2069 Y_t\\ 2070 Z_t\\ 2071 1 2072 \end{bmatrix} 2073 \f] 2074 2075 The Hand-Eye calibration procedure returns the following homogeneous transformation 2076 \f[ 2077 \begin{bmatrix} 2078 X_g\\ 2079 Y_g\\ 2080 Z_g\\ 2081 1 2082 \end{bmatrix} 2083 = 2084 \begin{bmatrix} 2085 _{}^{g}\textrm{R}_c & _{}^{g}\textrm{t}_c \\ 2086 0_{1 \times 3} & 1 2087 \end{bmatrix} 2088 \begin{bmatrix} 2089 X_c\\ 2090 Y_c\\ 2091 Z_c\\ 2092 1 2093 \end{bmatrix} 2094 \f] 2095 2096 This problem is also known as solving the \f$\mathbf{A}\mathbf{X}=\mathbf{X}\mathbf{B}\f$ equation: 2097 \f[ 2098 \begin{align*} 2099 ^{b}{\textrm{T}_g}^{(1)} \hspace{0.2em} ^{g}\textrm{T}_c \hspace{0.2em} ^{c}{\textrm{T}_t}^{(1)} &= 2100 \hspace{0.1em} ^{b}{\textrm{T}_g}^{(2)} \hspace{0.2em} ^{g}\textrm{T}_c \hspace{0.2em} ^{c}{\textrm{T}_t}^{(2)} \\ 2101 2102 (^{b}{\textrm{T}_g}^{(2)})^{-1} \hspace{0.2em} ^{b}{\textrm{T}_g}^{(1)} \hspace{0.2em} ^{g}\textrm{T}_c &= 2103 \hspace{0.1em} ^{g}\textrm{T}_c \hspace{0.2em} ^{c}{\textrm{T}_t}^{(2)} (^{c}{\textrm{T}_t}^{(1)})^{-1} \\ 2104 2105 \textrm{A}_i \textrm{X} &= \textrm{X} \textrm{B}_i \\ 2106 \end{align*} 2107 \f] 2108 2109 \note 2110 Additional information can be found on this [website](http://campar.in.tum.de/Chair/HandEyeCalibration). 2111 \note 2112 A minimum of 2 motions with non parallel rotation axes are necessary to determine the hand-eye transformation. 2113 So at least 3 different poses are required, but it is strongly recommended to use many more poses. 2114 2115 */ 2116 CV_EXPORTS_W void calibrateHandEye( InputArrayOfArrays R_gripper2base, InputArrayOfArrays t_gripper2base, 2117 InputArrayOfArrays R_target2cam, InputArrayOfArrays t_target2cam, 2118 OutputArray R_cam2gripper, OutputArray t_cam2gripper, 2119 HandEyeCalibrationMethod method=CALIB_HAND_EYE_TSAI ); 2120 2121 /** @brief Converts points from Euclidean to homogeneous space. 2122 2123 @param src Input vector of N-dimensional points. 2124 @param dst Output vector of N+1-dimensional points. 2125 2126 The function converts points from Euclidean to homogeneous space by appending 1's to the tuple of 2127 point coordinates. That is, each point (x1, x2, ..., xn) is converted to (x1, x2, ..., xn, 1). 2128 */ 2129 CV_EXPORTS_W void convertPointsToHomogeneous( InputArray src, OutputArray dst ); 2130 2131 /** @brief Converts points from homogeneous to Euclidean space. 2132 2133 @param src Input vector of N-dimensional points. 2134 @param dst Output vector of N-1-dimensional points. 2135 2136 The function converts points homogeneous to Euclidean space using perspective projection. That is, 2137 each point (x1, x2, ... x(n-1), xn) is converted to (x1/xn, x2/xn, ..., x(n-1)/xn). When xn=0, the 2138 output point coordinates will be (0,0,0,...). 2139 */ 2140 CV_EXPORTS_W void convertPointsFromHomogeneous( InputArray src, OutputArray dst ); 2141 2142 /** @brief Converts points to/from homogeneous coordinates. 2143 2144 @param src Input array or vector of 2D, 3D, or 4D points. 2145 @param dst Output vector of 2D, 3D, or 4D points. 2146 2147 The function converts 2D or 3D points from/to homogeneous coordinates by calling either 2148 convertPointsToHomogeneous or convertPointsFromHomogeneous. 2149 2150 @note The function is obsolete. Use one of the previous two functions instead. 2151 */ 2152 CV_EXPORTS void convertPointsHomogeneous( InputArray src, OutputArray dst ); 2153 2154 /** @brief Calculates a fundamental matrix from the corresponding points in two images. 2155 2156 @param points1 Array of N points from the first image. The point coordinates should be 2157 floating-point (single or double precision). 2158 @param points2 Array of the second image points of the same size and format as points1 . 2159 @param method Method for computing a fundamental matrix. 2160 - **CV_FM_7POINT** for a 7-point algorithm. \f$N = 7\f$ 2161 - **CV_FM_8POINT** for an 8-point algorithm. \f$N \ge 8\f$ 2162 - **CV_FM_RANSAC** for the RANSAC algorithm. \f$N \ge 8\f$ 2163 - **CV_FM_LMEDS** for the LMedS algorithm. \f$N \ge 8\f$ 2164 @param ransacReprojThreshold Parameter used only for RANSAC. It is the maximum distance from a point to an epipolar 2165 line in pixels, beyond which the point is considered an outlier and is not used for computing the 2166 final fundamental matrix. It can be set to something like 1-3, depending on the accuracy of the 2167 point localization, image resolution, and the image noise. 2168 @param confidence Parameter used for the RANSAC and LMedS methods only. It specifies a desirable level 2169 of confidence (probability) that the estimated matrix is correct. 2170 @param mask 2171 @param maxIters The maximum number of robust method iterations. 2172 2173 The epipolar geometry is described by the following equation: 2174 2175 \f[[p_2; 1]^T F [p_1; 1] = 0\f] 2176 2177 where \f$F\f$ is a fundamental matrix, \f$p_1\f$ and \f$p_2\f$ are corresponding points in the first and the 2178 second images, respectively. 2179 2180 The function calculates the fundamental matrix using one of four methods listed above and returns 2181 the found fundamental matrix. Normally just one matrix is found. But in case of the 7-point 2182 algorithm, the function may return up to 3 solutions ( \f$9 \times 3\f$ matrix that stores all 3 2183 matrices sequentially). 2184 2185 The calculated fundamental matrix may be passed further to computeCorrespondEpilines that finds the 2186 epipolar lines corresponding to the specified points. It can also be passed to 2187 stereoRectifyUncalibrated to compute the rectification transformation. : 2188 @code 2189 // Example. Estimation of fundamental matrix using the RANSAC algorithm 2190 int point_count = 100; 2191 vector<Point2f> points1(point_count); 2192 vector<Point2f> points2(point_count); 2193 2194 // initialize the points here ... 2195 for( int i = 0; i < point_count; i++ ) 2196 { 2197 points1[i] = ...; 2198 points2[i] = ...; 2199 } 2200 2201 Mat fundamental_matrix = 2202 findFundamentalMat(points1, points2, FM_RANSAC, 3, 0.99); 2203 @endcode 2204 */ 2205 CV_EXPORTS_W Mat findFundamentalMat( InputArray points1, InputArray points2, 2206 int method, double ransacReprojThreshold, double confidence, 2207 int maxIters, OutputArray mask = noArray() ); 2208 2209 /** @overload */ 2210 CV_EXPORTS_W Mat findFundamentalMat( InputArray points1, InputArray points2, 2211 int method = FM_RANSAC, 2212 double ransacReprojThreshold = 3., double confidence = 0.99, 2213 OutputArray mask = noArray() ); 2214 2215 /** @overload */ 2216 CV_EXPORTS Mat findFundamentalMat( InputArray points1, InputArray points2, 2217 OutputArray mask, int method = FM_RANSAC, 2218 double ransacReprojThreshold = 3., double confidence = 0.99 ); 2219 2220 /** @brief Calculates an essential matrix from the corresponding points in two images. 2221 2222 @param points1 Array of N (N \>= 5) 2D points from the first image. The point coordinates should 2223 be floating-point (single or double precision). 2224 @param points2 Array of the second image points of the same size and format as points1 . 2225 @param cameraMatrix Camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . 2226 Note that this function assumes that points1 and points2 are feature points from cameras with the 2227 same camera matrix. 2228 @param method Method for computing an essential matrix. 2229 - **RANSAC** for the RANSAC algorithm. 2230 - **LMEDS** for the LMedS algorithm. 2231 @param prob Parameter used for the RANSAC or LMedS methods only. It specifies a desirable level of 2232 confidence (probability) that the estimated matrix is correct. 2233 @param threshold Parameter used for RANSAC. It is the maximum distance from a point to an epipolar 2234 line in pixels, beyond which the point is considered an outlier and is not used for computing the 2235 final fundamental matrix. It can be set to something like 1-3, depending on the accuracy of the 2236 point localization, image resolution, and the image noise. 2237 @param mask Output array of N elements, every element of which is set to 0 for outliers and to 1 2238 for the other points. The array is computed only in the RANSAC and LMedS methods. 2239 2240 This function estimates essential matrix based on the five-point algorithm solver in @cite Nister03 . 2241 @cite SteweniusCFS is also a related. The epipolar geometry is described by the following equation: 2242 2243 \f[[p_2; 1]^T K^{-T} E K^{-1} [p_1; 1] = 0\f] 2244 2245 where \f$E\f$ is an essential matrix, \f$p_1\f$ and \f$p_2\f$ are corresponding points in the first and the 2246 second images, respectively. The result of this function may be passed further to 2247 decomposeEssentialMat or recoverPose to recover the relative pose between cameras. 2248 */ 2249 CV_EXPORTS_W Mat findEssentialMat( InputArray points1, InputArray points2, 2250 InputArray cameraMatrix, int method = RANSAC, 2251 double prob = 0.999, double threshold = 1.0, 2252 OutputArray mask = noArray() ); 2253 2254 /** @overload 2255 @param points1 Array of N (N \>= 5) 2D points from the first image. The point coordinates should 2256 be floating-point (single or double precision). 2257 @param points2 Array of the second image points of the same size and format as points1 . 2258 @param focal focal length of the camera. Note that this function assumes that points1 and points2 2259 are feature points from cameras with same focal length and principal point. 2260 @param pp principal point of the camera. 2261 @param method Method for computing a fundamental matrix. 2262 - **RANSAC** for the RANSAC algorithm. 2263 - **LMEDS** for the LMedS algorithm. 2264 @param threshold Parameter used for RANSAC. It is the maximum distance from a point to an epipolar 2265 line in pixels, beyond which the point is considered an outlier and is not used for computing the 2266 final fundamental matrix. It can be set to something like 1-3, depending on the accuracy of the 2267 point localization, image resolution, and the image noise. 2268 @param prob Parameter used for the RANSAC or LMedS methods only. It specifies a desirable level of 2269 confidence (probability) that the estimated matrix is correct. 2270 @param mask Output array of N elements, every element of which is set to 0 for outliers and to 1 2271 for the other points. The array is computed only in the RANSAC and LMedS methods. 2272 2273 This function differs from the one above that it computes camera matrix from focal length and 2274 principal point: 2275 2276 \f[K = 2277 \begin{bmatrix} 2278 f & 0 & x_{pp} \\ 2279 0 & f & y_{pp} \\ 2280 0 & 0 & 1 2281 \end{bmatrix}\f] 2282 */ 2283 CV_EXPORTS_W Mat findEssentialMat( InputArray points1, InputArray points2, 2284 double focal = 1.0, Point2d pp = Point2d(0, 0), 2285 int method = RANSAC, double prob = 0.999, 2286 double threshold = 1.0, OutputArray mask = noArray() ); 2287 2288 /** @brief Decompose an essential matrix to possible rotations and translation. 2289 2290 @param E The input essential matrix. 2291 @param R1 One possible rotation matrix. 2292 @param R2 Another possible rotation matrix. 2293 @param t One possible translation. 2294 2295 This function decomposes the essential matrix E using svd decomposition @cite HartleyZ00. In 2296 general, four possible poses exist for the decomposition of E. They are \f$[R_1, t]\f$, 2297 \f$[R_1, -t]\f$, \f$[R_2, t]\f$, \f$[R_2, -t]\f$. 2298 2299 If E gives the epipolar constraint \f$[p_2; 1]^T A^{-T} E A^{-1} [p_1; 1] = 0\f$ between the image 2300 points \f$p_1\f$ in the first image and \f$p_2\f$ in second image, then any of the tuples 2301 \f$[R_1, t]\f$, \f$[R_1, -t]\f$, \f$[R_2, t]\f$, \f$[R_2, -t]\f$ is a change of basis from the first 2302 camera's coordinate system to the second camera's coordinate system. However, by decomposing E, one 2303 can only get the direction of the translation. For this reason, the translation t is returned with 2304 unit length. 2305 */ 2306 CV_EXPORTS_W void decomposeEssentialMat( InputArray E, OutputArray R1, OutputArray R2, OutputArray t ); 2307 2308 /** @brief Recovers the relative camera rotation and the translation from an estimated essential 2309 matrix and the corresponding points in two images, using cheirality check. Returns the number of 2310 inliers that pass the check. 2311 2312 @param E The input essential matrix. 2313 @param points1 Array of N 2D points from the first image. The point coordinates should be 2314 floating-point (single or double precision). 2315 @param points2 Array of the second image points of the same size and format as points1 . 2316 @param cameraMatrix Camera matrix \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . 2317 Note that this function assumes that points1 and points2 are feature points from cameras with the 2318 same camera matrix. 2319 @param R Output rotation matrix. Together with the translation vector, this matrix makes up a tuple 2320 that performs a change of basis from the first camera's coordinate system to the second camera's 2321 coordinate system. Note that, in general, t can not be used for this tuple, see the parameter 2322 described below. 2323 @param t Output translation vector. This vector is obtained by @ref decomposeEssentialMat and 2324 therefore is only known up to scale, i.e. t is the direction of the translation vector and has unit 2325 length. 2326 @param mask Input/output mask for inliers in points1 and points2. If it is not empty, then it marks 2327 inliers in points1 and points2 for then given essential matrix E. Only these inliers will be used to 2328 recover pose. In the output mask only inliers which pass the cheirality check. 2329 2330 This function decomposes an essential matrix using @ref decomposeEssentialMat and then verifies 2331 possible pose hypotheses by doing cheirality check. The cheirality check means that the 2332 triangulated 3D points should have positive depth. Some details can be found in @cite Nister03. 2333 2334 This function can be used to process the output E and mask from @ref findEssentialMat. In this 2335 scenario, points1 and points2 are the same input for findEssentialMat.: 2336 @code 2337 // Example. Estimation of fundamental matrix using the RANSAC algorithm 2338 int point_count = 100; 2339 vector<Point2f> points1(point_count); 2340 vector<Point2f> points2(point_count); 2341 2342 // initialize the points here ... 2343 for( int i = 0; i < point_count; i++ ) 2344 { 2345 points1[i] = ...; 2346 points2[i] = ...; 2347 } 2348 2349 // cametra matrix with both focal lengths = 1, and principal point = (0, 0) 2350 Mat cameraMatrix = Mat::eye(3, 3, CV_64F); 2351 2352 Mat E, R, t, mask; 2353 2354 E = findEssentialMat(points1, points2, cameraMatrix, RANSAC, 0.999, 1.0, mask); 2355 recoverPose(E, points1, points2, cameraMatrix, R, t, mask); 2356 @endcode 2357 */ 2358 CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray points2, 2359 InputArray cameraMatrix, OutputArray R, OutputArray t, 2360 InputOutputArray mask = noArray() ); 2361 2362 /** @overload 2363 @param E The input essential matrix. 2364 @param points1 Array of N 2D points from the first image. The point coordinates should be 2365 floating-point (single or double precision). 2366 @param points2 Array of the second image points of the same size and format as points1 . 2367 @param R Output rotation matrix. Together with the translation vector, this matrix makes up a tuple 2368 that performs a change of basis from the first camera's coordinate system to the second camera's 2369 coordinate system. Note that, in general, t can not be used for this tuple, see the parameter 2370 description below. 2371 @param t Output translation vector. This vector is obtained by @ref decomposeEssentialMat and 2372 therefore is only known up to scale, i.e. t is the direction of the translation vector and has unit 2373 length. 2374 @param focal Focal length of the camera. Note that this function assumes that points1 and points2 2375 are feature points from cameras with same focal length and principal point. 2376 @param pp principal point of the camera. 2377 @param mask Input/output mask for inliers in points1 and points2. If it is not empty, then it marks 2378 inliers in points1 and points2 for then given essential matrix E. Only these inliers will be used to 2379 recover pose. In the output mask only inliers which pass the cheirality check. 2380 2381 This function differs from the one above that it computes camera matrix from focal length and 2382 principal point: 2383 2384 \f[A = 2385 \begin{bmatrix} 2386 f & 0 & x_{pp} \\ 2387 0 & f & y_{pp} \\ 2388 0 & 0 & 1 2389 \end{bmatrix}\f] 2390 */ 2391 CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray points2, 2392 OutputArray R, OutputArray t, 2393 double focal = 1.0, Point2d pp = Point2d(0, 0), 2394 InputOutputArray mask = noArray() ); 2395 2396 /** @overload 2397 @param E The input essential matrix. 2398 @param points1 Array of N 2D points from the first image. The point coordinates should be 2399 floating-point (single or double precision). 2400 @param points2 Array of the second image points of the same size and format as points1. 2401 @param cameraMatrix Camera matrix \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . 2402 Note that this function assumes that points1 and points2 are feature points from cameras with the 2403 same camera matrix. 2404 @param R Output rotation matrix. Together with the translation vector, this matrix makes up a tuple 2405 that performs a change of basis from the first camera's coordinate system to the second camera's 2406 coordinate system. Note that, in general, t can not be used for this tuple, see the parameter 2407 description below. 2408 @param t Output translation vector. This vector is obtained by @ref decomposeEssentialMat and 2409 therefore is only known up to scale, i.e. t is the direction of the translation vector and has unit 2410 length. 2411 @param distanceThresh threshold distance which is used to filter out far away points (i.e. infinite 2412 points). 2413 @param mask Input/output mask for inliers in points1 and points2. If it is not empty, then it marks 2414 inliers in points1 and points2 for then given essential matrix E. Only these inliers will be used to 2415 recover pose. In the output mask only inliers which pass the cheirality check. 2416 @param triangulatedPoints 3D points which were reconstructed by triangulation. 2417 2418 This function differs from the one above that it outputs the triangulated 3D point that are used for 2419 the cheirality check. 2420 */ 2421 CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray points2, 2422 InputArray cameraMatrix, OutputArray R, OutputArray t, double distanceThresh, InputOutputArray mask = noArray(), 2423 OutputArray triangulatedPoints = noArray()); 2424 2425 /** @brief For points in an image of a stereo pair, computes the corresponding epilines in the other image. 2426 2427 @param points Input points. \f$N \times 1\f$ or \f$1 \times N\f$ matrix of type CV_32FC2 or 2428 vector\<Point2f\> . 2429 @param whichImage Index of the image (1 or 2) that contains the points . 2430 @param F Fundamental matrix that can be estimated using findFundamentalMat or stereoRectify . 2431 @param lines Output vector of the epipolar lines corresponding to the points in the other image. 2432 Each line \f$ax + by + c=0\f$ is encoded by 3 numbers \f$(a, b, c)\f$ . 2433 2434 For every point in one of the two images of a stereo pair, the function finds the equation of the 2435 corresponding epipolar line in the other image. 2436 2437 From the fundamental matrix definition (see findFundamentalMat ), line \f$l^{(2)}_i\f$ in the second 2438 image for the point \f$p^{(1)}_i\f$ in the first image (when whichImage=1 ) is computed as: 2439 2440 \f[l^{(2)}_i = F p^{(1)}_i\f] 2441 2442 And vice versa, when whichImage=2, \f$l^{(1)}_i\f$ is computed from \f$p^{(2)}_i\f$ as: 2443 2444 \f[l^{(1)}_i = F^T p^{(2)}_i\f] 2445 2446 Line coefficients are defined up to a scale. They are normalized so that \f$a_i^2+b_i^2=1\f$ . 2447 */ 2448 CV_EXPORTS_W void computeCorrespondEpilines( InputArray points, int whichImage, 2449 InputArray F, OutputArray lines ); 2450 2451 /** @brief This function reconstructs 3-dimensional points (in homogeneous coordinates) by using 2452 their observations with a stereo camera. 2453 2454 @param projMatr1 3x4 projection matrix of the first camera, i.e. this matrix projects 3D points 2455 given in the world's coordinate system into the first image. 2456 @param projMatr2 3x4 projection matrix of the second camera, i.e. this matrix projects 3D points 2457 given in the world's coordinate system into the second image. 2458 @param projPoints1 2xN array of feature points in the first image. In the case of the c++ version, 2459 it can be also a vector of feature points or two-channel matrix of size 1xN or Nx1. 2460 @param projPoints2 2xN array of corresponding points in the second image. In the case of the c++ 2461 version, it can be also a vector of feature points or two-channel matrix of size 1xN or Nx1. 2462 @param points4D 4xN array of reconstructed points in homogeneous coordinates. These points are 2463 returned in the world's coordinate system. 2464 2465 @note 2466 Keep in mind that all input data should be of float type in order for this function to work. 2467 2468 @note 2469 If the projection matrices from @ref stereoRectify are used, then the returned points are 2470 represented in the first camera's rectified coordinate system. 2471 2472 @sa 2473 reprojectImageTo3D 2474 */ 2475 CV_EXPORTS_W void triangulatePoints( InputArray projMatr1, InputArray projMatr2, 2476 InputArray projPoints1, InputArray projPoints2, 2477 OutputArray points4D ); 2478 2479 /** @brief Refines coordinates of corresponding points. 2480 2481 @param F 3x3 fundamental matrix. 2482 @param points1 1xN array containing the first set of points. 2483 @param points2 1xN array containing the second set of points. 2484 @param newPoints1 The optimized points1. 2485 @param newPoints2 The optimized points2. 2486 2487 The function implements the Optimal Triangulation Method (see Multiple View Geometry for details). 2488 For each given point correspondence points1[i] \<-\> points2[i], and a fundamental matrix F, it 2489 computes the corrected correspondences newPoints1[i] \<-\> newPoints2[i] that minimize the geometric 2490 error \f$d(points1[i], newPoints1[i])^2 + d(points2[i],newPoints2[i])^2\f$ (where \f$d(a,b)\f$ is the 2491 geometric distance between points \f$a\f$ and \f$b\f$ ) subject to the epipolar constraint 2492 \f$newPoints2^T * F * newPoints1 = 0\f$ . 2493 */ 2494 CV_EXPORTS_W void correctMatches( InputArray F, InputArray points1, InputArray points2, 2495 OutputArray newPoints1, OutputArray newPoints2 ); 2496 2497 /** @brief Filters off small noise blobs (speckles) in the disparity map 2498 2499 @param img The input 16-bit signed disparity image 2500 @param newVal The disparity value used to paint-off the speckles 2501 @param maxSpeckleSize The maximum speckle size to consider it a speckle. Larger blobs are not 2502 affected by the algorithm 2503 @param maxDiff Maximum difference between neighbor disparity pixels to put them into the same 2504 blob. Note that since StereoBM, StereoSGBM and may be other algorithms return a fixed-point 2505 disparity map, where disparity values are multiplied by 16, this scale factor should be taken into 2506 account when specifying this parameter value. 2507 @param buf The optional temporary buffer to avoid memory allocation within the function. 2508 */ 2509 CV_EXPORTS_W void filterSpeckles( InputOutputArray img, double newVal, 2510 int maxSpeckleSize, double maxDiff, 2511 InputOutputArray buf = noArray() ); 2512 2513 //! computes valid disparity ROI from the valid ROIs of the rectified images (that are returned by cv::stereoRectify()) 2514 CV_EXPORTS_W Rect getValidDisparityROI( Rect roi1, Rect roi2, 2515 int minDisparity, int numberOfDisparities, 2516 int blockSize ); 2517 2518 //! validates disparity using the left-right check. The matrix "cost" should be computed by the stereo correspondence algorithm 2519 CV_EXPORTS_W void validateDisparity( InputOutputArray disparity, InputArray cost, 2520 int minDisparity, int numberOfDisparities, 2521 int disp12MaxDisp = 1 ); 2522 2523 /** @brief Reprojects a disparity image to 3D space. 2524 2525 @param disparity Input single-channel 8-bit unsigned, 16-bit signed, 32-bit signed or 32-bit 2526 floating-point disparity image. The values of 8-bit / 16-bit signed formats are assumed to have no 2527 fractional bits. If the disparity is 16-bit signed format, as computed by @ref StereoBM or 2528 @ref StereoSGBM and maybe other algorithms, it should be divided by 16 (and scaled to float) before 2529 being used here. 2530 @param _3dImage Output 3-channel floating-point image of the same size as disparity. Each element of 2531 _3dImage(x,y) contains 3D coordinates of the point (x,y) computed from the disparity map. If one 2532 uses Q obtained by @ref stereoRectify, then the returned points are represented in the first 2533 camera's rectified coordinate system. 2534 @param Q \f$4 \times 4\f$ perspective transformation matrix that can be obtained with 2535 @ref stereoRectify. 2536 @param handleMissingValues Indicates, whether the function should handle missing values (i.e. 2537 points where the disparity was not computed). If handleMissingValues=true, then pixels with the 2538 minimal disparity that corresponds to the outliers (see StereoMatcher::compute ) are transformed 2539 to 3D points with a very large Z value (currently set to 10000). 2540 @param ddepth The optional output array depth. If it is -1, the output image will have CV_32F 2541 depth. ddepth can also be set to CV_16S, CV_32S or CV_32F. 2542 2543 The function transforms a single-channel disparity map to a 3-channel image representing a 3D 2544 surface. That is, for each pixel (x,y) and the corresponding disparity d=disparity(x,y) , it 2545 computes: 2546 2547 \f[\begin{bmatrix} 2548 X \\ 2549 Y \\ 2550 Z \\ 2551 W 2552 \end{bmatrix} = Q \begin{bmatrix} 2553 x \\ 2554 y \\ 2555 \texttt{disparity} (x,y) \\ 2556 z 2557 \end{bmatrix}.\f] 2558 2559 @sa 2560 To reproject a sparse set of points {(x,y,d),...} to 3D space, use perspectiveTransform. 2561 */ 2562 CV_EXPORTS_W void reprojectImageTo3D( InputArray disparity, 2563 OutputArray _3dImage, InputArray Q, 2564 bool handleMissingValues = false, 2565 int ddepth = -1 ); 2566 2567 /** @brief Calculates the Sampson Distance between two points. 2568 2569 The function cv::sampsonDistance calculates and returns the first order approximation of the geometric error as: 2570 \f[ 2571 sd( \texttt{pt1} , \texttt{pt2} )= 2572 \frac{(\texttt{pt2}^t \cdot \texttt{F} \cdot \texttt{pt1})^2} 2573 {((\texttt{F} \cdot \texttt{pt1})(0))^2 + 2574 ((\texttt{F} \cdot \texttt{pt1})(1))^2 + 2575 ((\texttt{F}^t \cdot \texttt{pt2})(0))^2 + 2576 ((\texttt{F}^t \cdot \texttt{pt2})(1))^2} 2577 \f] 2578 The fundamental matrix may be calculated using the cv::findFundamentalMat function. See @cite HartleyZ00 11.4.3 for details. 2579 @param pt1 first homogeneous 2d point 2580 @param pt2 second homogeneous 2d point 2581 @param F fundamental matrix 2582 @return The computed Sampson distance. 2583 */ 2584 CV_EXPORTS_W double sampsonDistance(InputArray pt1, InputArray pt2, InputArray F); 2585 2586 /** @brief Computes an optimal affine transformation between two 3D point sets. 2587 2588 It computes 2589 \f[ 2590 \begin{bmatrix} 2591 x\\ 2592 y\\ 2593 z\\ 2594 \end{bmatrix} 2595 = 2596 \begin{bmatrix} 2597 a_{11} & a_{12} & a_{13}\\ 2598 a_{21} & a_{22} & a_{23}\\ 2599 a_{31} & a_{32} & a_{33}\\ 2600 \end{bmatrix} 2601 \begin{bmatrix} 2602 X\\ 2603 Y\\ 2604 Z\\ 2605 \end{bmatrix} 2606 + 2607 \begin{bmatrix} 2608 b_1\\ 2609 b_2\\ 2610 b_3\\ 2611 \end{bmatrix} 2612 \f] 2613 2614 @param src First input 3D point set containing \f$(X,Y,Z)\f$. 2615 @param dst Second input 3D point set containing \f$(x,y,z)\f$. 2616 @param out Output 3D affine transformation matrix \f$3 \times 4\f$ of the form 2617 \f[ 2618 \begin{bmatrix} 2619 a_{11} & a_{12} & a_{13} & b_1\\ 2620 a_{21} & a_{22} & a_{23} & b_2\\ 2621 a_{31} & a_{32} & a_{33} & b_3\\ 2622 \end{bmatrix} 2623 \f] 2624 @param inliers Output vector indicating which points are inliers (1-inlier, 0-outlier). 2625 @param ransacThreshold Maximum reprojection error in the RANSAC algorithm to consider a point as 2626 an inlier. 2627 @param confidence Confidence level, between 0 and 1, for the estimated transformation. Anything 2628 between 0.95 and 0.99 is usually good enough. Values too close to 1 can slow down the estimation 2629 significantly. Values lower than 0.8-0.9 can result in an incorrectly estimated transformation. 2630 2631 The function estimates an optimal 3D affine transformation between two 3D point sets using the 2632 RANSAC algorithm. 2633 */ 2634 CV_EXPORTS_W int estimateAffine3D(InputArray src, InputArray dst, 2635 OutputArray out, OutputArray inliers, 2636 double ransacThreshold = 3, double confidence = 0.99); 2637 2638 /** @brief Computes an optimal affine transformation between two 2D point sets. 2639 2640 It computes 2641 \f[ 2642 \begin{bmatrix} 2643 x\\ 2644 y\\ 2645 \end{bmatrix} 2646 = 2647 \begin{bmatrix} 2648 a_{11} & a_{12}\\ 2649 a_{21} & a_{22}\\ 2650 \end{bmatrix} 2651 \begin{bmatrix} 2652 X\\ 2653 Y\\ 2654 \end{bmatrix} 2655 + 2656 \begin{bmatrix} 2657 b_1\\ 2658 b_2\\ 2659 \end{bmatrix} 2660 \f] 2661 2662 @param from First input 2D point set containing \f$(X,Y)\f$. 2663 @param to Second input 2D point set containing \f$(x,y)\f$. 2664 @param inliers Output vector indicating which points are inliers (1-inlier, 0-outlier). 2665 @param method Robust method used to compute transformation. The following methods are possible: 2666 - cv::RANSAC - RANSAC-based robust method 2667 - cv::LMEDS - Least-Median robust method 2668 RANSAC is the default method. 2669 @param ransacReprojThreshold Maximum reprojection error in the RANSAC algorithm to consider 2670 a point as an inlier. Applies only to RANSAC. 2671 @param maxIters The maximum number of robust method iterations. 2672 @param confidence Confidence level, between 0 and 1, for the estimated transformation. Anything 2673 between 0.95 and 0.99 is usually good enough. Values too close to 1 can slow down the estimation 2674 significantly. Values lower than 0.8-0.9 can result in an incorrectly estimated transformation. 2675 @param refineIters Maximum number of iterations of refining algorithm (Levenberg-Marquardt). 2676 Passing 0 will disable refining, so the output matrix will be output of robust method. 2677 2678 @return Output 2D affine transformation matrix \f$2 \times 3\f$ or empty matrix if transformation 2679 could not be estimated. The returned matrix has the following form: 2680 \f[ 2681 \begin{bmatrix} 2682 a_{11} & a_{12} & b_1\\ 2683 a_{21} & a_{22} & b_2\\ 2684 \end{bmatrix} 2685 \f] 2686 2687 The function estimates an optimal 2D affine transformation between two 2D point sets using the 2688 selected robust algorithm. 2689 2690 The computed transformation is then refined further (using only inliers) with the 2691 Levenberg-Marquardt method to reduce the re-projection error even more. 2692 2693 @note 2694 The RANSAC method can handle practically any ratio of outliers but needs a threshold to 2695 distinguish inliers from outliers. The method LMeDS does not need any threshold but it works 2696 correctly only when there are more than 50% of inliers. 2697 2698 @sa estimateAffinePartial2D, getAffineTransform 2699 */ 2700 CV_EXPORTS_W cv::Mat estimateAffine2D(InputArray from, InputArray to, OutputArray inliers = noArray(), 2701 int method = RANSAC, double ransacReprojThreshold = 3, 2702 size_t maxIters = 2000, double confidence = 0.99, 2703 size_t refineIters = 10); 2704 2705 /** @brief Computes an optimal limited affine transformation with 4 degrees of freedom between 2706 two 2D point sets. 2707 2708 @param from First input 2D point set. 2709 @param to Second input 2D point set. 2710 @param inliers Output vector indicating which points are inliers. 2711 @param method Robust method used to compute transformation. The following methods are possible: 2712 - cv::RANSAC - RANSAC-based robust method 2713 - cv::LMEDS - Least-Median robust method 2714 RANSAC is the default method. 2715 @param ransacReprojThreshold Maximum reprojection error in the RANSAC algorithm to consider 2716 a point as an inlier. Applies only to RANSAC. 2717 @param maxIters The maximum number of robust method iterations. 2718 @param confidence Confidence level, between 0 and 1, for the estimated transformation. Anything 2719 between 0.95 and 0.99 is usually good enough. Values too close to 1 can slow down the estimation 2720 significantly. Values lower than 0.8-0.9 can result in an incorrectly estimated transformation. 2721 @param refineIters Maximum number of iterations of refining algorithm (Levenberg-Marquardt). 2722 Passing 0 will disable refining, so the output matrix will be output of robust method. 2723 2724 @return Output 2D affine transformation (4 degrees of freedom) matrix \f$2 \times 3\f$ or 2725 empty matrix if transformation could not be estimated. 2726 2727 The function estimates an optimal 2D affine transformation with 4 degrees of freedom limited to 2728 combinations of translation, rotation, and uniform scaling. Uses the selected algorithm for robust 2729 estimation. 2730 2731 The computed transformation is then refined further (using only inliers) with the 2732 Levenberg-Marquardt method to reduce the re-projection error even more. 2733 2734 Estimated transformation matrix is: 2735 \f[ \begin{bmatrix} \cos(\theta) \cdot s & -\sin(\theta) \cdot s & t_x \\ 2736 \sin(\theta) \cdot s & \cos(\theta) \cdot s & t_y 2737 \end{bmatrix} \f] 2738 Where \f$ \theta \f$ is the rotation angle, \f$ s \f$ the scaling factor and \f$ t_x, t_y \f$ are 2739 translations in \f$ x, y \f$ axes respectively. 2740 2741 @note 2742 The RANSAC method can handle practically any ratio of outliers but need a threshold to 2743 distinguish inliers from outliers. The method LMeDS does not need any threshold but it works 2744 correctly only when there are more than 50% of inliers. 2745 2746 @sa estimateAffine2D, getAffineTransform 2747 */ 2748 CV_EXPORTS_W cv::Mat estimateAffinePartial2D(InputArray from, InputArray to, OutputArray inliers = noArray(), 2749 int method = RANSAC, double ransacReprojThreshold = 3, 2750 size_t maxIters = 2000, double confidence = 0.99, 2751 size_t refineIters = 10); 2752 2753 /** @example samples/cpp/tutorial_code/features2D/Homography/decompose_homography.cpp 2754 An example program with homography decomposition. 2755 2756 Check @ref tutorial_homography "the corresponding tutorial" for more details. 2757 */ 2758 2759 /** @brief Decompose a homography matrix to rotation(s), translation(s) and plane normal(s). 2760 2761 @param H The input homography matrix between two images. 2762 @param K The input intrinsic camera calibration matrix. 2763 @param rotations Array of rotation matrices. 2764 @param translations Array of translation matrices. 2765 @param normals Array of plane normal matrices. 2766 2767 This function extracts relative camera motion between two views of a planar object and returns up to 2768 four mathematical solution tuples of rotation, translation, and plane normal. The decomposition of 2769 the homography matrix H is described in detail in @cite Malis. 2770 2771 If the homography H, induced by the plane, gives the constraint 2772 \f[s_i \vecthree{x'_i}{y'_i}{1} \sim H \vecthree{x_i}{y_i}{1}\f] on the source image points 2773 \f$p_i\f$ and the destination image points \f$p'_i\f$, then the tuple of rotations[k] and 2774 translations[k] is a change of basis from the source camera's coordinate system to the destination 2775 camera's coordinate system. However, by decomposing H, one can only get the translation normalized 2776 by the (typically unknown) depth of the scene, i.e. its direction but with normalized length. 2777 2778 If point correspondences are available, at least two solutions may further be invalidated, by 2779 applying positive depth constraint, i.e. all points must be in front of the camera. 2780 */ 2781 CV_EXPORTS_W int decomposeHomographyMat(InputArray H, 2782 InputArray K, 2783 OutputArrayOfArrays rotations, 2784 OutputArrayOfArrays translations, 2785 OutputArrayOfArrays normals); 2786 2787 /** @brief Filters homography decompositions based on additional information. 2788 2789 @param rotations Vector of rotation matrices. 2790 @param normals Vector of plane normal matrices. 2791 @param beforePoints Vector of (rectified) visible reference points before the homography is applied 2792 @param afterPoints Vector of (rectified) visible reference points after the homography is applied 2793 @param possibleSolutions Vector of int indices representing the viable solution set after filtering 2794 @param pointsMask optional Mat/Vector of 8u type representing the mask for the inliers as given by the findHomography function 2795 2796 This function is intended to filter the output of the decomposeHomographyMat based on additional 2797 information as described in @cite Malis . The summary of the method: the decomposeHomographyMat function 2798 returns 2 unique solutions and their "opposites" for a total of 4 solutions. If we have access to the 2799 sets of points visible in the camera frame before and after the homography transformation is applied, 2800 we can determine which are the true potential solutions and which are the opposites by verifying which 2801 homographies are consistent with all visible reference points being in front of the camera. The inputs 2802 are left unchanged; the filtered solution set is returned as indices into the existing one. 2803 2804 */ 2805 CV_EXPORTS_W void filterHomographyDecompByVisibleRefpoints(InputArrayOfArrays rotations, 2806 InputArrayOfArrays normals, 2807 InputArray beforePoints, 2808 InputArray afterPoints, 2809 OutputArray possibleSolutions, 2810 InputArray pointsMask = noArray()); 2811 2812 /** @brief The base class for stereo correspondence algorithms. 2813 */ 2814 class CV_EXPORTS_W StereoMatcher : public Algorithm 2815 { 2816 public: 2817 enum { DISP_SHIFT = 4, 2818 DISP_SCALE = (1 << DISP_SHIFT) 2819 }; 2820 2821 /** @brief Computes disparity map for the specified stereo pair 2822 2823 @param left Left 8-bit single-channel image. 2824 @param right Right image of the same size and the same type as the left one. 2825 @param disparity Output disparity map. It has the same size as the input images. Some algorithms, 2826 like StereoBM or StereoSGBM compute 16-bit fixed-point disparity map (where each disparity value 2827 has 4 fractional bits), whereas other algorithms output 32-bit floating-point disparity map. 2828 */ 2829 CV_WRAP virtual void compute( InputArray left, InputArray right, 2830 OutputArray disparity ) = 0; 2831 2832 CV_WRAP virtual int getMinDisparity() const = 0; 2833 CV_WRAP virtual void setMinDisparity(int minDisparity) = 0; 2834 2835 CV_WRAP virtual int getNumDisparities() const = 0; 2836 CV_WRAP virtual void setNumDisparities(int numDisparities) = 0; 2837 2838 CV_WRAP virtual int getBlockSize() const = 0; 2839 CV_WRAP virtual void setBlockSize(int blockSize) = 0; 2840 2841 CV_WRAP virtual int getSpeckleWindowSize() const = 0; 2842 CV_WRAP virtual void setSpeckleWindowSize(int speckleWindowSize) = 0; 2843 2844 CV_WRAP virtual int getSpeckleRange() const = 0; 2845 CV_WRAP virtual void setSpeckleRange(int speckleRange) = 0; 2846 2847 CV_WRAP virtual int getDisp12MaxDiff() const = 0; 2848 CV_WRAP virtual void setDisp12MaxDiff(int disp12MaxDiff) = 0; 2849 }; 2850 2851 2852 /** @brief Class for computing stereo correspondence using the block matching algorithm, introduced and 2853 contributed to OpenCV by K. Konolige. 2854 */ 2855 class CV_EXPORTS_W StereoBM : public StereoMatcher 2856 { 2857 public: 2858 enum { PREFILTER_NORMALIZED_RESPONSE = 0, 2859 PREFILTER_XSOBEL = 1 2860 }; 2861 2862 CV_WRAP virtual int getPreFilterType() const = 0; 2863 CV_WRAP virtual void setPreFilterType(int preFilterType) = 0; 2864 2865 CV_WRAP virtual int getPreFilterSize() const = 0; 2866 CV_WRAP virtual void setPreFilterSize(int preFilterSize) = 0; 2867 2868 CV_WRAP virtual int getPreFilterCap() const = 0; 2869 CV_WRAP virtual void setPreFilterCap(int preFilterCap) = 0; 2870 2871 CV_WRAP virtual int getTextureThreshold() const = 0; 2872 CV_WRAP virtual void setTextureThreshold(int textureThreshold) = 0; 2873 2874 CV_WRAP virtual int getUniquenessRatio() const = 0; 2875 CV_WRAP virtual void setUniquenessRatio(int uniquenessRatio) = 0; 2876 2877 CV_WRAP virtual int getSmallerBlockSize() const = 0; 2878 CV_WRAP virtual void setSmallerBlockSize(int blockSize) = 0; 2879 2880 CV_WRAP virtual Rect getROI1() const = 0; 2881 CV_WRAP virtual void setROI1(Rect roi1) = 0; 2882 2883 CV_WRAP virtual Rect getROI2() const = 0; 2884 CV_WRAP virtual void setROI2(Rect roi2) = 0; 2885 2886 /** @brief Creates StereoBM object 2887 2888 @param numDisparities the disparity search range. For each pixel algorithm will find the best 2889 disparity from 0 (default minimum disparity) to numDisparities. The search range can then be 2890 shifted by changing the minimum disparity. 2891 @param blockSize the linear size of the blocks compared by the algorithm. The size should be odd 2892 (as the block is centered at the current pixel). Larger block size implies smoother, though less 2893 accurate disparity map. Smaller block size gives more detailed disparity map, but there is higher 2894 chance for algorithm to find a wrong correspondence. 2895 2896 The function create StereoBM object. You can then call StereoBM::compute() to compute disparity for 2897 a specific stereo pair. 2898 */ 2899 CV_WRAP static Ptr<StereoBM> create(int numDisparities = 0, int blockSize = 21); 2900 }; 2901 2902 /** @brief The class implements the modified H. Hirschmuller algorithm @cite HH08 that differs from the original 2903 one as follows: 2904 2905 - By default, the algorithm is single-pass, which means that you consider only 5 directions 2906 instead of 8. Set mode=StereoSGBM::MODE_HH in createStereoSGBM to run the full variant of the 2907 algorithm but beware that it may consume a lot of memory. 2908 - The algorithm matches blocks, not individual pixels. Though, setting blockSize=1 reduces the 2909 blocks to single pixels. 2910 - Mutual information cost function is not implemented. Instead, a simpler Birchfield-Tomasi 2911 sub-pixel metric from @cite BT98 is used. Though, the color images are supported as well. 2912 - Some pre- and post- processing steps from K. Konolige algorithm StereoBM are included, for 2913 example: pre-filtering (StereoBM::PREFILTER_XSOBEL type) and post-filtering (uniqueness 2914 check, quadratic interpolation and speckle filtering). 2915 2916 @note 2917 - (Python) An example illustrating the use of the StereoSGBM matching algorithm can be found 2918 at opencv_source_code/samples/python/stereo_match.py 2919 */ 2920 class CV_EXPORTS_W StereoSGBM : public StereoMatcher 2921 { 2922 public: 2923 enum 2924 { 2925 MODE_SGBM = 0, 2926 MODE_HH = 1, 2927 MODE_SGBM_3WAY = 2, 2928 MODE_HH4 = 3 2929 }; 2930 2931 CV_WRAP virtual int getPreFilterCap() const = 0; 2932 CV_WRAP virtual void setPreFilterCap(int preFilterCap) = 0; 2933 2934 CV_WRAP virtual int getUniquenessRatio() const = 0; 2935 CV_WRAP virtual void setUniquenessRatio(int uniquenessRatio) = 0; 2936 2937 CV_WRAP virtual int getP1() const = 0; 2938 CV_WRAP virtual void setP1(int P1) = 0; 2939 2940 CV_WRAP virtual int getP2() const = 0; 2941 CV_WRAP virtual void setP2(int P2) = 0; 2942 2943 CV_WRAP virtual int getMode() const = 0; 2944 CV_WRAP virtual void setMode(int mode) = 0; 2945 2946 /** @brief Creates StereoSGBM object 2947 2948 @param minDisparity Minimum possible disparity value. Normally, it is zero but sometimes 2949 rectification algorithms can shift images, so this parameter needs to be adjusted accordingly. 2950 @param numDisparities Maximum disparity minus minimum disparity. The value is always greater than 2951 zero. In the current implementation, this parameter must be divisible by 16. 2952 @param blockSize Matched block size. It must be an odd number \>=1 . Normally, it should be 2953 somewhere in the 3..11 range. 2954 @param P1 The first parameter controlling the disparity smoothness. See below. 2955 @param P2 The second parameter controlling the disparity smoothness. The larger the values are, 2956 the smoother the disparity is. P1 is the penalty on the disparity change by plus or minus 1 2957 between neighbor pixels. P2 is the penalty on the disparity change by more than 1 between neighbor 2958 pixels. The algorithm requires P2 \> P1 . See stereo_match.cpp sample where some reasonably good 2959 P1 and P2 values are shown (like 8\*number_of_image_channels\*blockSize\*blockSize and 2960 32\*number_of_image_channels\*blockSize\*blockSize , respectively). 2961 @param disp12MaxDiff Maximum allowed difference (in integer pixel units) in the left-right 2962 disparity check. Set it to a non-positive value to disable the check. 2963 @param preFilterCap Truncation value for the prefiltered image pixels. The algorithm first 2964 computes x-derivative at each pixel and clips its value by [-preFilterCap, preFilterCap] interval. 2965 The result values are passed to the Birchfield-Tomasi pixel cost function. 2966 @param uniquenessRatio Margin in percentage by which the best (minimum) computed cost function 2967 value should "win" the second best value to consider the found match correct. Normally, a value 2968 within the 5-15 range is good enough. 2969 @param speckleWindowSize Maximum size of smooth disparity regions to consider their noise speckles 2970 and invalidate. Set it to 0 to disable speckle filtering. Otherwise, set it somewhere in the 2971 50-200 range. 2972 @param speckleRange Maximum disparity variation within each connected component. If you do speckle 2973 filtering, set the parameter to a positive value, it will be implicitly multiplied by 16. 2974 Normally, 1 or 2 is good enough. 2975 @param mode Set it to StereoSGBM::MODE_HH to run the full-scale two-pass dynamic programming 2976 algorithm. It will consume O(W\*H\*numDisparities) bytes, which is large for 640x480 stereo and 2977 huge for HD-size pictures. By default, it is set to false . 2978 2979 The first constructor initializes StereoSGBM with all the default parameters. So, you only have to 2980 set StereoSGBM::numDisparities at minimum. The second constructor enables you to set each parameter 2981 to a custom value. 2982 */ 2983 CV_WRAP static Ptr<StereoSGBM> create(int minDisparity = 0, int numDisparities = 16, int blockSize = 3, 2984 int P1 = 0, int P2 = 0, int disp12MaxDiff = 0, 2985 int preFilterCap = 0, int uniquenessRatio = 0, 2986 int speckleWindowSize = 0, int speckleRange = 0, 2987 int mode = StereoSGBM::MODE_SGBM); 2988 }; 2989 2990 //! @} calib3d 2991 2992 /** @brief The methods in this namespace use a so-called fisheye camera model. 2993 @ingroup calib3d_fisheye 2994 */ 2995 namespace fisheye 2996 { 2997 //! @addtogroup calib3d_fisheye 2998 //! @{ 2999 3000 enum{ 3001 CALIB_USE_INTRINSIC_GUESS = 1 << 0, 3002 CALIB_RECOMPUTE_EXTRINSIC = 1 << 1, 3003 CALIB_CHECK_COND = 1 << 2, 3004 CALIB_FIX_SKEW = 1 << 3, 3005 CALIB_FIX_K1 = 1 << 4, 3006 CALIB_FIX_K2 = 1 << 5, 3007 CALIB_FIX_K3 = 1 << 6, 3008 CALIB_FIX_K4 = 1 << 7, 3009 CALIB_FIX_INTRINSIC = 1 << 8, 3010 CALIB_FIX_PRINCIPAL_POINT = 1 << 9 3011 }; 3012 3013 /** @brief Projects points using fisheye model 3014 3015 @param objectPoints Array of object points, 1xN/Nx1 3-channel (or vector\<Point3f\> ), where N is 3016 the number of points in the view. 3017 @param imagePoints Output array of image points, 2xN/Nx2 1-channel or 1xN/Nx1 2-channel, or 3018 vector\<Point2f\>. 3019 @param affine 3020 @param K Camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$. 3021 @param D Input vector of distortion coefficients \f$(k_1, k_2, k_3, k_4)\f$. 3022 @param alpha The skew coefficient. 3023 @param jacobian Optional output 2Nx15 jacobian matrix of derivatives of image points with respect 3024 to components of the focal lengths, coordinates of the principal point, distortion coefficients, 3025 rotation vector, translation vector, and the skew. In the old interface different components of 3026 the jacobian are returned via different output parameters. 3027 3028 The function computes projections of 3D points to the image plane given intrinsic and extrinsic 3029 camera parameters. Optionally, the function computes Jacobians - matrices of partial derivatives of 3030 image points coordinates (as functions of all the input parameters) with respect to the particular 3031 parameters, intrinsic and/or extrinsic. 3032 */ 3033 CV_EXPORTS void projectPoints(InputArray objectPoints, OutputArray imagePoints, const Affine3d& affine, 3034 InputArray K, InputArray D, double alpha = 0, OutputArray jacobian = noArray()); 3035 3036 /** @overload */ 3037 CV_EXPORTS_W void projectPoints(InputArray objectPoints, OutputArray imagePoints, InputArray rvec, InputArray tvec, 3038 InputArray K, InputArray D, double alpha = 0, OutputArray jacobian = noArray()); 3039 3040 /** @brief Distorts 2D points using fisheye model. 3041 3042 @param undistorted Array of object points, 1xN/Nx1 2-channel (or vector\<Point2f\> ), where N is 3043 the number of points in the view. 3044 @param K Camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$. 3045 @param D Input vector of distortion coefficients \f$(k_1, k_2, k_3, k_4)\f$. 3046 @param alpha The skew coefficient. 3047 @param distorted Output array of image points, 1xN/Nx1 2-channel, or vector\<Point2f\> . 3048 3049 Note that the function assumes the camera matrix of the undistorted points to be identity. 3050 This means if you want to transform back points undistorted with undistortPoints() you have to 3051 multiply them with \f$P^{-1}\f$. 3052 */ 3053 CV_EXPORTS_W void distortPoints(InputArray undistorted, OutputArray distorted, InputArray K, InputArray D, double alpha = 0); 3054 3055 /** @brief Undistorts 2D points using fisheye model 3056 3057 @param distorted Array of object points, 1xN/Nx1 2-channel (or vector\<Point2f\> ), where N is the 3058 number of points in the view. 3059 @param K Camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$. 3060 @param D Input vector of distortion coefficients \f$(k_1, k_2, k_3, k_4)\f$. 3061 @param R Rectification transformation in the object space: 3x3 1-channel, or vector: 3x1/1x3 3062 1-channel or 1x1 3-channel 3063 @param P New camera matrix (3x3) or new projection matrix (3x4) 3064 @param undistorted Output array of image points, 1xN/Nx1 2-channel, or vector\<Point2f\> . 3065 */ 3066 CV_EXPORTS_W void undistortPoints(InputArray distorted, OutputArray undistorted, 3067 InputArray K, InputArray D, InputArray R = noArray(), InputArray P = noArray()); 3068 3069 /** @brief Computes undistortion and rectification maps for image transform by cv::remap(). If D is empty zero 3070 distortion is used, if R or P is empty identity matrixes are used. 3071 3072 @param K Camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$. 3073 @param D Input vector of distortion coefficients \f$(k_1, k_2, k_3, k_4)\f$. 3074 @param R Rectification transformation in the object space: 3x3 1-channel, or vector: 3x1/1x3 3075 1-channel or 1x1 3-channel 3076 @param P New camera matrix (3x3) or new projection matrix (3x4) 3077 @param size Undistorted image size. 3078 @param m1type Type of the first output map that can be CV_32FC1 or CV_16SC2 . See convertMaps() 3079 for details. 3080 @param map1 The first output map. 3081 @param map2 The second output map. 3082 */ 3083 CV_EXPORTS_W void initUndistortRectifyMap(InputArray K, InputArray D, InputArray R, InputArray P, 3084 const cv::Size& size, int m1type, OutputArray map1, OutputArray map2); 3085 3086 /** @brief Transforms an image to compensate for fisheye lens distortion. 3087 3088 @param distorted image with fisheye lens distortion. 3089 @param undistorted Output image with compensated fisheye lens distortion. 3090 @param K Camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$. 3091 @param D Input vector of distortion coefficients \f$(k_1, k_2, k_3, k_4)\f$. 3092 @param Knew Camera matrix of the distorted image. By default, it is the identity matrix but you 3093 may additionally scale and shift the result by using a different matrix. 3094 @param new_size the new size 3095 3096 The function transforms an image to compensate radial and tangential lens distortion. 3097 3098 The function is simply a combination of fisheye::initUndistortRectifyMap (with unity R ) and remap 3099 (with bilinear interpolation). See the former function for details of the transformation being 3100 performed. 3101 3102 See below the results of undistortImage. 3103 - a\) result of undistort of perspective camera model (all possible coefficients (k_1, k_2, k_3, 3104 k_4, k_5, k_6) of distortion were optimized under calibration) 3105 - b\) result of fisheye::undistortImage of fisheye camera model (all possible coefficients (k_1, k_2, 3106 k_3, k_4) of fisheye distortion were optimized under calibration) 3107 - c\) original image was captured with fisheye lens 3108 3109 Pictures a) and b) almost the same. But if we consider points of image located far from the center 3110 of image, we can notice that on image a) these points are distorted. 3111 3112  3113 */ 3114 CV_EXPORTS_W void undistortImage(InputArray distorted, OutputArray undistorted, 3115 InputArray K, InputArray D, InputArray Knew = cv::noArray(), const Size& new_size = Size()); 3116 3117 /** @brief Estimates new camera matrix for undistortion or rectification. 3118 3119 @param K Camera matrix \f$K = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{_1}\f$. 3120 @param image_size Size of the image 3121 @param D Input vector of distortion coefficients \f$(k_1, k_2, k_3, k_4)\f$. 3122 @param R Rectification transformation in the object space: 3x3 1-channel, or vector: 3x1/1x3 3123 1-channel or 1x1 3-channel 3124 @param P New camera matrix (3x3) or new projection matrix (3x4) 3125 @param balance Sets the new focal length in range between the min focal length and the max focal 3126 length. Balance is in range of [0, 1]. 3127 @param new_size the new size 3128 @param fov_scale Divisor for new focal length. 3129 */ 3130 CV_EXPORTS_W void estimateNewCameraMatrixForUndistortRectify(InputArray K, InputArray D, const Size &image_size, InputArray R, 3131 OutputArray P, double balance = 0.0, const Size& new_size = Size(), double fov_scale = 1.0); 3132 3133 /** @brief Performs camera calibaration 3134 3135 @param objectPoints vector of vectors of calibration pattern points in the calibration pattern 3136 coordinate space. 3137 @param imagePoints vector of vectors of the projections of calibration pattern points. 3138 imagePoints.size() and objectPoints.size() and imagePoints[i].size() must be equal to 3139 objectPoints[i].size() for each i. 3140 @param image_size Size of the image used only to initialize the intrinsic camera matrix. 3141 @param K Output 3x3 floating-point camera matrix 3142 \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . If 3143 fisheye::CALIB_USE_INTRINSIC_GUESS/ is specified, some or all of fx, fy, cx, cy must be 3144 initialized before calling the function. 3145 @param D Output vector of distortion coefficients \f$(k_1, k_2, k_3, k_4)\f$. 3146 @param rvecs Output vector of rotation vectors (see Rodrigues ) estimated for each pattern view. 3147 That is, each k-th rotation vector together with the corresponding k-th translation vector (see 3148 the next output parameter description) brings the calibration pattern from the model coordinate 3149 space (in which object points are specified) to the world coordinate space, that is, a real 3150 position of the calibration pattern in the k-th pattern view (k=0.. *M* -1). 3151 @param tvecs Output vector of translation vectors estimated for each pattern view. 3152 @param flags Different flags that may be zero or a combination of the following values: 3153 - **fisheye::CALIB_USE_INTRINSIC_GUESS** cameraMatrix contains valid initial values of 3154 fx, fy, cx, cy that are optimized further. Otherwise, (cx, cy) is initially set to the image 3155 center ( imageSize is used), and focal distances are computed in a least-squares fashion. 3156 - **fisheye::CALIB_RECOMPUTE_EXTRINSIC** Extrinsic will be recomputed after each iteration 3157 of intrinsic optimization. 3158 - **fisheye::CALIB_CHECK_COND** The functions will check validity of condition number. 3159 - **fisheye::CALIB_FIX_SKEW** Skew coefficient (alpha) is set to zero and stay zero. 3160 - **fisheye::CALIB_FIX_K1..fisheye::CALIB_FIX_K4** Selected distortion coefficients 3161 are set to zeros and stay zero. 3162 - **fisheye::CALIB_FIX_PRINCIPAL_POINT** The principal point is not changed during the global 3163 optimization. It stays at the center or at a different location specified when CALIB_USE_INTRINSIC_GUESS is set too. 3164 @param criteria Termination criteria for the iterative optimization algorithm. 3165 */ 3166 CV_EXPORTS_W double calibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, const Size& image_size, 3167 InputOutputArray K, InputOutputArray D, OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, int flags = 0, 3168 TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 100, DBL_EPSILON)); 3169 3170 /** @brief Stereo rectification for fisheye camera model 3171 3172 @param K1 First camera matrix. 3173 @param D1 First camera distortion parameters. 3174 @param K2 Second camera matrix. 3175 @param D2 Second camera distortion parameters. 3176 @param imageSize Size of the image used for stereo calibration. 3177 @param R Rotation matrix between the coordinate systems of the first and the second 3178 cameras. 3179 @param tvec Translation vector between coordinate systems of the cameras. 3180 @param R1 Output 3x3 rectification transform (rotation matrix) for the first camera. 3181 @param R2 Output 3x3 rectification transform (rotation matrix) for the second camera. 3182 @param P1 Output 3x4 projection matrix in the new (rectified) coordinate systems for the first 3183 camera. 3184 @param P2 Output 3x4 projection matrix in the new (rectified) coordinate systems for the second 3185 camera. 3186 @param Q Output \f$4 \times 4\f$ disparity-to-depth mapping matrix (see reprojectImageTo3D ). 3187 @param flags Operation flags that may be zero or CALIB_ZERO_DISPARITY . If the flag is set, 3188 the function makes the principal points of each camera have the same pixel coordinates in the 3189 rectified views. And if the flag is not set, the function may still shift the images in the 3190 horizontal or vertical direction (depending on the orientation of epipolar lines) to maximize the 3191 useful image area. 3192 @param newImageSize New image resolution after rectification. The same size should be passed to 3193 initUndistortRectifyMap (see the stereo_calib.cpp sample in OpenCV samples directory). When (0,0) 3194 is passed (default), it is set to the original imageSize . Setting it to larger value can help you 3195 preserve details in the original image, especially when there is a big radial distortion. 3196 @param balance Sets the new focal length in range between the min focal length and the max focal 3197 length. Balance is in range of [0, 1]. 3198 @param fov_scale Divisor for new focal length. 3199 */ 3200 CV_EXPORTS_W void stereoRectify(InputArray K1, InputArray D1, InputArray K2, InputArray D2, const Size &imageSize, InputArray R, InputArray tvec, 3201 OutputArray R1, OutputArray R2, OutputArray P1, OutputArray P2, OutputArray Q, int flags, const Size &newImageSize = Size(), 3202 double balance = 0.0, double fov_scale = 1.0); 3203 3204 /** @brief Performs stereo calibration 3205 3206 @param objectPoints Vector of vectors of the calibration pattern points. 3207 @param imagePoints1 Vector of vectors of the projections of the calibration pattern points, 3208 observed by the first camera. 3209 @param imagePoints2 Vector of vectors of the projections of the calibration pattern points, 3210 observed by the second camera. 3211 @param K1 Input/output first camera matrix: 3212 \f$\vecthreethree{f_x^{(j)}}{0}{c_x^{(j)}}{0}{f_y^{(j)}}{c_y^{(j)}}{0}{0}{1}\f$ , \f$j = 0,\, 1\f$ . If 3213 any of fisheye::CALIB_USE_INTRINSIC_GUESS , fisheye::CALIB_FIX_INTRINSIC are specified, 3214 some or all of the matrix components must be initialized. 3215 @param D1 Input/output vector of distortion coefficients \f$(k_1, k_2, k_3, k_4)\f$ of 4 elements. 3216 @param K2 Input/output second camera matrix. The parameter is similar to K1 . 3217 @param D2 Input/output lens distortion coefficients for the second camera. The parameter is 3218 similar to D1 . 3219 @param imageSize Size of the image used only to initialize intrinsic camera matrix. 3220 @param R Output rotation matrix between the 1st and the 2nd camera coordinate systems. 3221 @param T Output translation vector between the coordinate systems of the cameras. 3222 @param flags Different flags that may be zero or a combination of the following values: 3223 - **fisheye::CALIB_FIX_INTRINSIC** Fix K1, K2? and D1, D2? so that only R, T matrices 3224 are estimated. 3225 - **fisheye::CALIB_USE_INTRINSIC_GUESS** K1, K2 contains valid initial values of 3226 fx, fy, cx, cy that are optimized further. Otherwise, (cx, cy) is initially set to the image 3227 center (imageSize is used), and focal distances are computed in a least-squares fashion. 3228 - **fisheye::CALIB_RECOMPUTE_EXTRINSIC** Extrinsic will be recomputed after each iteration 3229 of intrinsic optimization. 3230 - **fisheye::CALIB_CHECK_COND** The functions will check validity of condition number. 3231 - **fisheye::CALIB_FIX_SKEW** Skew coefficient (alpha) is set to zero and stay zero. 3232 - **fisheye::CALIB_FIX_K1..4** Selected distortion coefficients are set to zeros and stay 3233 zero. 3234 @param criteria Termination criteria for the iterative optimization algorithm. 3235 */ 3236 CV_EXPORTS_W double stereoCalibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2, 3237 InputOutputArray K1, InputOutputArray D1, InputOutputArray K2, InputOutputArray D2, Size imageSize, 3238 OutputArray R, OutputArray T, int flags = fisheye::CALIB_FIX_INTRINSIC, 3239 TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 100, DBL_EPSILON)); 3240 3241 //! @} calib3d_fisheye 3242 } // end namespace fisheye 3243 3244 } //end namespace cv 3245 3246 #ifndef DISABLE_OPENCV_24_COMPATIBILITY 3247 #include "opencv2/calib3d/calib3d_c.h" 3248 #endif 3249 3250 #endif 3251