calib3d.hpp 21.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
10
//                          License Agreement
11 12 13 14
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#ifndef __OPENCV_CALIB3D_HPP__
#define __OPENCV_CALIB3D_HPP__

47
#include "opencv2/core.hpp"
48
#include "opencv2/features2d.hpp"
49

50 51
namespace cv
{
52

53 54 55 56 57 58 59
//! type of the robust estimation algorithm
enum { LMEDS  = 4, //!< least-median algorithm
       RANSAC = 8  //!< RANSAC algorithm
     };

enum { ITERATIVE = 0,
       EPNP      = 1, // F.Moreno-Noguer, V.Lepetit and P.Fua "EPnP: Efficient Perspective-n-Point Camera Pose Estimation"
E
edgarriba 已提交
60 61 62
       P3P       = 2, // X.S. Gao, X.-R. Hou, J. Tang, H.-F. Chang; "Complete Solution Classification for the Perspective-Three-Point Problem"
       DLS       = 3  // Joel A. Hesch and Stergios I. Roumeliotis. "A Direct Least-Squares (DLS) Method for PnP"
};
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

enum { CALIB_CB_ADAPTIVE_THRESH = 1,
       CALIB_CB_NORMALIZE_IMAGE = 2,
       CALIB_CB_FILTER_QUADS    = 4,
       CALIB_CB_FAST_CHECK      = 8
     };

enum { CALIB_CB_SYMMETRIC_GRID  = 1,
       CALIB_CB_ASYMMETRIC_GRID = 2,
       CALIB_CB_CLUSTERING      = 4
     };

enum { CALIB_USE_INTRINSIC_GUESS = 0x00001,
       CALIB_FIX_ASPECT_RATIO    = 0x00002,
       CALIB_FIX_PRINCIPAL_POINT = 0x00004,
       CALIB_ZERO_TANGENT_DIST   = 0x00008,
       CALIB_FIX_FOCAL_LENGTH    = 0x00010,
       CALIB_FIX_K1              = 0x00020,
       CALIB_FIX_K2              = 0x00040,
       CALIB_FIX_K3              = 0x00080,
       CALIB_FIX_K4              = 0x00800,
       CALIB_FIX_K5              = 0x01000,
       CALIB_FIX_K6              = 0x02000,
       CALIB_RATIONAL_MODEL      = 0x04000,
       CALIB_THIN_PRISM_MODEL    = 0x08000,
       CALIB_FIX_S1_S2_S3_S4     = 0x10000,
       // only for stereo
       CALIB_FIX_INTRINSIC       = 0x00100,
       CALIB_SAME_FOCAL_LENGTH   = 0x00200,
       // for stereo rectification
       CALIB_ZERO_DISPARITY      = 0x00400
     };
95

96 97 98 99 100 101
//! the algorithm for finding fundamental matrix
enum { FM_7POINT = 1, //!< 7-point algorithm
       FM_8POINT = 2, //!< 8-point algorithm
       FM_LMEDS  = 4, //!< least-median algorithm
       FM_RANSAC = 8  //!< RANSAC algorithm
     };
A
Alexander Shishkov 已提交
102

103

104

105
//! converts rotation vector to rotation matrix or vice versa using Rodrigues transformation
106
CV_EXPORTS_W void Rodrigues( InputArray src, OutputArray dst, OutputArray jacobian = noArray() );
107

108
//! computes the best-fit perspective transformation mapping srcPoints to dstPoints.
109
CV_EXPORTS_W Mat findHomography( InputArray srcPoints, InputArray dstPoints,
110
                                 int method = 0, double ransacReprojThreshold = 3,
111
                                 OutputArray mask=noArray());
112 113

//! variant of findHomography for backward compatibility
114
CV_EXPORTS Mat findHomography( InputArray srcPoints, InputArray dstPoints,
115
                               OutputArray mask, int method = 0, double ransacReprojThreshold = 3 );
116

117
//! Computes RQ decomposition of 3x3 matrix
118
CV_EXPORTS_W Vec3d RQDecomp3x3( InputArray src, OutputArray mtxR, OutputArray mtxQ,
119 120 121
                                OutputArray Qx = noArray(),
                                OutputArray Qy = noArray(),
                                OutputArray Qz = noArray());
122

123
//! Decomposes the projection matrix into camera matrix and the rotation martix and the translation vector
124
CV_EXPORTS_W void decomposeProjectionMatrix( InputArray projMatrix, OutputArray cameraMatrix,
125
                                             OutputArray rotMatrix, OutputArray transVect,
126 127 128 129
                                             OutputArray rotMatrixX = noArray(),
                                             OutputArray rotMatrixY = noArray(),
                                             OutputArray rotMatrixZ = noArray(),
                                             OutputArray eulerAngles =noArray() );
130

131
//! computes derivatives of the matrix product w.r.t each of the multiplied matrix coefficients
132
CV_EXPORTS_W void matMulDeriv( InputArray A, InputArray B, OutputArray dABdA, OutputArray dABdB );
133

134
//! composes 2 [R|t] transformations together. Also computes the derivatives of the result w.r.t the arguments
135 136
CV_EXPORTS_W void composeRT( InputArray rvec1, InputArray tvec1,
                             InputArray rvec2, InputArray tvec2,
137
                             OutputArray rvec3, OutputArray tvec3,
138 139 140 141
                             OutputArray dr3dr1 = noArray(), OutputArray dr3dt1 = noArray(),
                             OutputArray dr3dr2 = noArray(), OutputArray dr3dt2 = noArray(),
                             OutputArray dt3dr1 = noArray(), OutputArray dt3dt1 = noArray(),
                             OutputArray dt3dr2 = noArray(), OutputArray dt3dt2 = noArray() );
142

143
//! projects points from the model coordinate space to the image coordinates. Also computes derivatives of the image coordinates w.r.t the intrinsic and extrinsic camera parameters
144 145 146
CV_EXPORTS_W void projectPoints( InputArray objectPoints,
                                 InputArray rvec, InputArray tvec,
                                 InputArray cameraMatrix, InputArray distCoeffs,
147
                                 OutputArray imagePoints,
148 149
                                 OutputArray jacobian = noArray(),
                                 double aspectRatio = 0 );
150

151
//! computes the camera pose from a few 3D points and the corresponding projections. The outliers are not handled.
A
Alexander Shishkov 已提交
152
CV_EXPORTS_W bool solvePnP( InputArray objectPoints, InputArray imagePoints,
153
                            InputArray cameraMatrix, InputArray distCoeffs,
154
                            OutputArray rvec, OutputArray tvec,
155
                            bool useExtrinsicGuess = false, int flags = ITERATIVE );
156

A
Alexander Shishkov 已提交
157
//! computes the camera pose from a few 3D points and the corresponding projections. The outliers are possible.
E
edgarriba 已提交
158
CV_EXPORTS_W bool solvePnPRansac( InputArray objectPoints, InputArray imagePoints,
159 160 161
                                  InputArray cameraMatrix, InputArray distCoeffs,
                                  OutputArray rvec, OutputArray tvec,
                                  bool useExtrinsicGuess = false, int iterationsCount = 100,
E
edgarriba 已提交
162
                                  float reprojectionError = 8.0, float confidence = 0.99,
163
                                  OutputArray inliers = noArray(), int flags = ITERATIVE );
A
Alexander Shishkov 已提交
164

165
//! initializes camera matrix from a few 3D points and the corresponding projections.
166 167
CV_EXPORTS_W Mat initCameraMatrix2D( InputArrayOfArrays objectPoints,
                                     InputArrayOfArrays imagePoints,
168
                                     Size imageSize, double aspectRatio = 1.0 );
169

170
//! finds checkerboard pattern of the specified size in the image
171 172
CV_EXPORTS_W bool findChessboardCorners( InputArray image, Size patternSize, OutputArray corners,
                                         int flags = CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE );
173

174
//! finds subpixel-accurate positions of the chessboard corners
175
CV_EXPORTS bool find4QuadCornerSubpix( InputArray img, InputOutputArray corners, Size region_size );
176

177
//! draws the checkerboard pattern (found or partly found) in the image
178
CV_EXPORTS_W void drawChessboardCorners( InputOutputArray image, Size patternSize,
179
                                         InputArray corners, bool patternWasFound );
180 181

//! finds circles' grid pattern of the specified size in the image
182
CV_EXPORTS_W bool findCirclesGrid( InputArray image, Size patternSize,
183
                                   OutputArray centers, int flags = CALIB_CB_SYMMETRIC_GRID,
R
Roman Donchenko 已提交
184
                                   const Ptr<FeatureDetector> &blobDetector = makePtr<SimpleBlobDetector>());
185

186
//! finds intrinsic and extrinsic camera parameters from several fews of a known calibration pattern.
187
CV_EXPORTS_W double calibrateCamera( InputArrayOfArrays objectPoints,
188 189
                                     InputArrayOfArrays imagePoints, Size imageSize,
                                     InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
190
                                     OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
191 192
                                     int flags = 0, TermCriteria criteria = TermCriteria(
                                        TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON) );
193

194
//! computes several useful camera characteristics from the camera matrix, camera frame resolution and the physical sensor size.
195 196 197 198 199
CV_EXPORTS_W void calibrationMatrixValues( InputArray cameraMatrix, Size imageSize,
                                           double apertureWidth, double apertureHeight,
                                           CV_OUT double& fovx, CV_OUT double& fovy,
                                           CV_OUT double& focalLength, CV_OUT Point2d& principalPoint,
                                           CV_OUT double& aspectRatio );
200 201

//! finds intrinsic and extrinsic parameters of a stereo camera
202
CV_EXPORTS_W double stereoCalibrate( InputArrayOfArrays objectPoints,
203 204 205 206
                                     InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2,
                                     InputOutputArray cameraMatrix1, InputOutputArray distCoeffs1,
                                     InputOutputArray cameraMatrix2, InputOutputArray distCoeffs2,
                                     Size imageSize, OutputArray R,OutputArray T, OutputArray E, OutputArray F,
207 208
                                     int flags = CALIB_FIX_INTRINSIC,
                                     TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 1e-6) );
209

210

211
//! computes the rectification transformation for a stereo camera from its intrinsic and extrinsic parameters
212
CV_EXPORTS_W void stereoRectify( InputArray cameraMatrix1, InputArray distCoeffs1,
213 214 215 216 217 218 219
                                 InputArray cameraMatrix2, InputArray distCoeffs2,
                                 Size imageSize, InputArray R, InputArray T,
                                 OutputArray R1, OutputArray R2,
                                 OutputArray P1, OutputArray P2,
                                 OutputArray Q, int flags = CALIB_ZERO_DISPARITY,
                                 double alpha = -1, Size newImageSize = Size(),
                                 CV_OUT Rect* validPixROI1 = 0, CV_OUT Rect* validPixROI2 = 0 );
220

221
//! computes the rectification transformation for an uncalibrated stereo camera (zero distortion is assumed)
222 223
CV_EXPORTS_W bool stereoRectifyUncalibrated( InputArray points1, InputArray points2,
                                             InputArray F, Size imgSize,
224
                                             OutputArray H1, OutputArray H2,
225
                                             double threshold = 5 );
226 227

//! computes the rectification transformations for 3-head camera, where all the heads are on the same line.
228 229 230 231 232 233
CV_EXPORTS_W float rectify3Collinear( InputArray cameraMatrix1, InputArray distCoeffs1,
                                      InputArray cameraMatrix2, InputArray distCoeffs2,
                                      InputArray cameraMatrix3, InputArray distCoeffs3,
                                      InputArrayOfArrays imgpt1, InputArrayOfArrays imgpt3,
                                      Size imageSize, InputArray R12, InputArray T12,
                                      InputArray R13, InputArray T13,
234 235 236
                                      OutputArray R1, OutputArray R2, OutputArray R3,
                                      OutputArray P1, OutputArray P2, OutputArray P3,
                                      OutputArray Q, double alpha, Size newImgSize,
237
                                      CV_OUT Rect* roi1, CV_OUT Rect* roi2, int flags );
238

239
//! returns the optimal new camera matrix
240
CV_EXPORTS_W Mat getOptimalNewCameraMatrix( InputArray cameraMatrix, InputArray distCoeffs,
241 242 243
                                            Size imageSize, double alpha, Size newImgSize = Size(),
                                            CV_OUT Rect* validPixROI = 0,
                                            bool centerPrincipalPoint = false);
V
Vadim Pisarevsky 已提交
244

245
//! converts point coordinates from normal pixel coordinates to homogeneous coordinates ((x,y)->(x,y,1))
246
CV_EXPORTS_W void convertPointsToHomogeneous( InputArray src, OutputArray dst );
247

248
//! converts point coordinates from homogeneous to normal pixel coordinates ((x,y,z)->(x/z, y/z))
249
CV_EXPORTS_W void convertPointsFromHomogeneous( InputArray src, OutputArray dst );
250

251
//! for backward compatibility
252
CV_EXPORTS void convertPointsHomogeneous( InputArray src, OutputArray dst );
253

254
//! finds fundamental matrix from a set of corresponding 2D points
255
CV_EXPORTS_W Mat findFundamentalMat( InputArray points1, InputArray points2,
256 257 258
                                     int method = FM_RANSAC,
                                     double param1 = 3., double param2 = 0.99,
                                     OutputArray mask = noArray() );
259

260
//! variant of findFundamentalMat for backward compatibility
261
CV_EXPORTS Mat findFundamentalMat( InputArray points1, InputArray points2,
262 263
                                   OutputArray mask, int method = FM_RANSAC,
                                   double param1 = 3., double param2 = 0.99 );
264

265
//! finds essential matrix from a set of corresponding 2D points using five-point algorithm
266
CV_EXPORTS_W Mat findEssentialMat( InputArray points1, InputArray points2,
267 268 269
                                 double focal = 1.0, Point2d pp = Point2d(0, 0),
                                 int method = RANSAC, double prob = 0.999,
                                 double threshold = 1.0, OutputArray mask = noArray() );
270 271

//! decompose essential matrix to possible rotation matrix and one translation vector
272
CV_EXPORTS_W void decomposeEssentialMat( InputArray E, OutputArray R1, OutputArray R2, OutputArray t );
273 274

//! recover relative camera pose from a set of corresponding 2D points
275
CV_EXPORTS_W int recoverPose( InputArray E, InputArray points1, InputArray points2,
276
                            OutputArray R, OutputArray t,
277
                            double focal = 1.0, Point2d pp = Point2d(0, 0),
278
                            InputOutputArray mask = noArray() );
279

280

281
//! finds coordinates of epipolar lines corresponding the specified points
282 283
CV_EXPORTS_W void computeCorrespondEpilines( InputArray points, int whichImage,
                                             InputArray F, OutputArray lines );
284

285 286 287 288
CV_EXPORTS_W void triangulatePoints( InputArray projMatr1, InputArray projMatr2,
                                     InputArray projPoints1, InputArray projPoints2,
                                     OutputArray points4D );

289 290 291
CV_EXPORTS_W void correctMatches( InputArray F, InputArray points1, InputArray points2,
                                  OutputArray newPoints1, OutputArray newPoints2 );

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
//! filters off speckles (small regions of incorrectly computed disparity)
CV_EXPORTS_W void filterSpeckles( InputOutputArray img, double newVal,
                                  int maxSpeckleSize, double maxDiff,
                                  InputOutputArray buf = noArray() );

//! computes valid disparity ROI from the valid ROIs of the rectified images (that are returned by cv::stereoRectify())
CV_EXPORTS_W Rect getValidDisparityROI( Rect roi1, Rect roi2,
                                        int minDisparity, int numberOfDisparities,
                                        int SADWindowSize );

//! validates disparity using the left-right check. The matrix "cost" should be computed by the stereo correspondence algorithm
CV_EXPORTS_W void validateDisparity( InputOutputArray disparity, InputArray cost,
                                     int minDisparity, int numberOfDisparities,
                                     int disp12MaxDisp = 1 );

//! reprojects disparity image to 3D: (x,y,d)->(X,Y,Z) using the matrix Q returned by cv::stereoRectify
CV_EXPORTS_W void reprojectImageTo3D( InputArray disparity,
                                      OutputArray _3dImage, InputArray Q,
                                      bool handleMissingValues = false,
                                      int ddepth = -1 );

CV_EXPORTS_W  int estimateAffine3D(InputArray src, InputArray dst,
                                   OutputArray out, OutputArray inliers,
                                   double ransacThreshold = 3, double confidence = 0.99);

317

318 319 320 321 322
CV_EXPORTS_W int decomposeHomographyMat(InputArray H,
                                        InputArray K,
                                        OutputArrayOfArrays rotations,
                                        OutputArrayOfArrays translations,
                                        OutputArrayOfArrays normals);
323

V
Vadim Pisarevsky 已提交
324 325 326
class CV_EXPORTS_W StereoMatcher : public Algorithm
{
public:
327 328 329
    enum { DISP_SHIFT = 4,
           DISP_SCALE = (1 << DISP_SHIFT)
         };
330

V
Vadim Pisarevsky 已提交
331 332
    CV_WRAP virtual void compute( InputArray left, InputArray right,
                                  OutputArray disparity ) = 0;
333

334 335
    CV_WRAP virtual int getMinDisparity() const = 0;
    CV_WRAP virtual void setMinDisparity(int minDisparity) = 0;
V
Vadim Pisarevsky 已提交
336

337 338
    CV_WRAP virtual int getNumDisparities() const = 0;
    CV_WRAP virtual void setNumDisparities(int numDisparities) = 0;
339

340 341
    CV_WRAP virtual int getBlockSize() const = 0;
    CV_WRAP virtual void setBlockSize(int blockSize) = 0;
V
Vadim Pisarevsky 已提交
342

343 344 345 346 347 348 349 350 351
    CV_WRAP virtual int getSpeckleWindowSize() const = 0;
    CV_WRAP virtual void setSpeckleWindowSize(int speckleWindowSize) = 0;

    CV_WRAP virtual int getSpeckleRange() const = 0;
    CV_WRAP virtual void setSpeckleRange(int speckleRange) = 0;

    CV_WRAP virtual int getDisp12MaxDiff() const = 0;
    CV_WRAP virtual void setDisp12MaxDiff(int disp12MaxDiff) = 0;
};
V
Vadim Pisarevsky 已提交
352

353

354

355
class CV_EXPORTS_W StereoBM : public StereoMatcher
356 357
{
public:
358 359 360
    enum { PREFILTER_NORMALIZED_RESPONSE = 0,
           PREFILTER_XSOBEL              = 1
         };
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

    CV_WRAP virtual int getPreFilterType() const = 0;
    CV_WRAP virtual void setPreFilterType(int preFilterType) = 0;

    CV_WRAP virtual int getPreFilterSize() const = 0;
    CV_WRAP virtual void setPreFilterSize(int preFilterSize) = 0;

    CV_WRAP virtual int getPreFilterCap() const = 0;
    CV_WRAP virtual void setPreFilterCap(int preFilterCap) = 0;

    CV_WRAP virtual int getTextureThreshold() const = 0;
    CV_WRAP virtual void setTextureThreshold(int textureThreshold) = 0;

    CV_WRAP virtual int getUniquenessRatio() const = 0;
    CV_WRAP virtual void setUniquenessRatio(int uniquenessRatio) = 0;

    CV_WRAP virtual int getSmallerBlockSize() const = 0;
    CV_WRAP virtual void setSmallerBlockSize(int blockSize) = 0;

    CV_WRAP virtual Rect getROI1() const = 0;
    CV_WRAP virtual void setROI1(Rect roi1) = 0;

    CV_WRAP virtual Rect getROI2() const = 0;
    CV_WRAP virtual void setROI2(Rect roi2) = 0;
385 386
};

387
CV_EXPORTS_W Ptr<StereoBM> createStereoBM(int numDisparities = 0, int blockSize = 21);
388

389 390

class CV_EXPORTS_W StereoSGBM : public StereoMatcher
391 392
{
public:
393 394 395
    enum { MODE_SGBM = 0,
           MODE_HH   = 1
         };
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

    CV_WRAP virtual int getPreFilterCap() const = 0;
    CV_WRAP virtual void setPreFilterCap(int preFilterCap) = 0;

    CV_WRAP virtual int getUniquenessRatio() const = 0;
    CV_WRAP virtual void setUniquenessRatio(int uniquenessRatio) = 0;

    CV_WRAP virtual int getP1() const = 0;
    CV_WRAP virtual void setP1(int P1) = 0;

    CV_WRAP virtual int getP2() const = 0;
    CV_WRAP virtual void setP2(int P2) = 0;

    CV_WRAP virtual int getMode() const = 0;
    CV_WRAP virtual void setMode(int mode) = 0;
411 412
};

413

414
CV_EXPORTS_W Ptr<StereoSGBM> createStereoSGBM(int minDisparity, int numDisparities, int blockSize,
415 416 417 418
                                            int P1 = 0, int P2 = 0, int disp12MaxDiff = 0,
                                            int preFilterCap = 0, int uniquenessRatio = 0,
                                            int speckleWindowSize = 0, int speckleRange = 0,
                                            int mode = StereoSGBM::MODE_SGBM);
419

420
} // cv
421 422

#endif