diff --git a/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.rst b/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.rst index 7c1300223f09..a6027c274765 100644 --- a/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.rst +++ b/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.rst @@ -843,11 +843,11 @@ findHomography ------------------ Finds a perspective transformation between two planes. -.. ocv:function:: Mat findHomography( InputArray srcPoints, InputArray dstPoints, int method=0, double ransacReprojThreshold=3, OutputArray mask=noArray() ) +.. ocv:function:: Mat findHomography( InputArray srcPoints, InputArray dstPoints, int method=0, double ransacReprojThreshold=3, OutputArray mask=noArray(), const int maxIters = 2000, const double confidence = 0.995) .. ocv:pyfunction:: cv2.findHomography(srcPoints, dstPoints[, method[, ransacReprojThreshold[, mask]]]) -> retval, mask -.. ocv:cfunction:: int cvFindHomography( const CvMat* src_points, const CvMat* dst_points, CvMat* homography, int method=0, double ransacReprojThreshold=3, CvMat* mask=0 ) +.. ocv:cfunction:: int cvFindHomography( const CvMat* src_points, const CvMat* dst_points, CvMat* homography, int method=0, double ransacReprojThreshold=3, CvMat* mask=0, int maxIters = 2000, double confidence = 0.995) :param srcPoints: Coordinates of the points in the original plane, a matrix of the type ``CV_32FC2`` or ``vector`` . @@ -871,6 +871,10 @@ Finds a perspective transformation between two planes. :param mask: Optional output mask set by a robust method ( ``RANSAC`` or ``LMEDS`` ). Note that the input mask values are ignored. + :param maxIters: The maximum number of RANSAC iterations, 2000 is the maximum it can be. + + :param confidence: Confidence level, between 0 and 1. + The functions find and return the perspective transformation :math:`H` between the source and the destination planes: .. math:: diff --git a/modules/calib3d/include/opencv2/calib3d.hpp b/modules/calib3d/include/opencv2/calib3d.hpp index ff82677b61d3..d96a92b60502 100644 --- a/modules/calib3d/include/opencv2/calib3d.hpp +++ b/modules/calib3d/include/opencv2/calib3d.hpp @@ -109,7 +109,8 @@ CV_EXPORTS_W void Rodrigues( InputArray src, OutputArray dst, OutputArray jacobi //! computes the best-fit perspective transformation mapping srcPoints to dstPoints. CV_EXPORTS_W Mat findHomography( InputArray srcPoints, InputArray dstPoints, int method = 0, double ransacReprojThreshold = 3, - OutputArray mask=noArray()); + OutputArray mask=noArray(), const int maxIters = 2000, + const double confidence = 0.995); //! variant of findHomography for backward compatibility CV_EXPORTS Mat findHomography( InputArray srcPoints, InputArray dstPoints, diff --git a/modules/calib3d/include/opencv2/calib3d/calib3d_c.h b/modules/calib3d/include/opencv2/calib3d/calib3d_c.h index d8bf5567a0fa..c99c25a4d959 100644 --- a/modules/calib3d/include/opencv2/calib3d/calib3d_c.h +++ b/modules/calib3d/include/opencv2/calib3d/calib3d_c.h @@ -141,7 +141,9 @@ CVAPI(int) cvFindHomography( const CvMat* src_points, CvMat* homography, int method CV_DEFAULT(0), double ransacReprojThreshold CV_DEFAULT(3), - CvMat* mask CV_DEFAULT(0)); + CvMat* mask CV_DEFAULT(0), + int maxIters CV_DEFAULT(2000), + double confidence CV_DEFAULT(0.995)); /* Computes RQ decomposition for 3x3 matrices */ CVAPI(void) cvRQDecomp3x3( const CvMat *matrixM, CvMat *matrixR, CvMat *matrixQ, diff --git a/modules/calib3d/src/compat_ptsetreg.cpp b/modules/calib3d/src/compat_ptsetreg.cpp index 50ba34777788..a56eafb9ddd8 100644 --- a/modules/calib3d/src/compat_ptsetreg.cpp +++ b/modules/calib3d/src/compat_ptsetreg.cpp @@ -299,7 +299,8 @@ CV_IMPL int cvRANSACUpdateNumIters( double p, double ep, int modelPoints, int ma CV_IMPL int cvFindHomography( const CvMat* _src, const CvMat* _dst, CvMat* __H, int method, - double ransacReprojThreshold, CvMat* _mask ) + double ransacReprojThreshold, CvMat* _mask, int maxIters, + double confidence) { cv::Mat src = cv::cvarrToMat(_src), dst = cv::cvarrToMat(_dst); @@ -308,9 +309,20 @@ CV_IMPL int cvFindHomography( const CvMat* _src, const CvMat* _dst, CvMat* __H, if( dst.channels() == 1 && (dst.rows == 2 || dst.rows == 3) && dst.cols > 3 ) cv::transpose(dst, dst); + if ( maxIters < 0 ) + maxIters = 0; + if ( maxIters > 2000 ) + maxIters = 2000; + + if ( confidence < 0 ) + confidence = 0; + if ( confidence > 1 ) + confidence = 1; + const cv::Mat H = cv::cvarrToMat(__H), mask = cv::cvarrToMat(_mask); cv::Mat H0 = cv::findHomography(src, dst, method, ransacReprojThreshold, - _mask ? cv::_OutputArray(mask) : cv::_OutputArray()); + _mask ? cv::_OutputArray(mask) : cv::_OutputArray(), maxIters, + confidence); if( H0.empty() ) { diff --git a/modules/calib3d/src/fundam.cpp b/modules/calib3d/src/fundam.cpp index 3c112a72abe5..c700ece70d05 100644 --- a/modules/calib3d/src/fundam.cpp +++ b/modules/calib3d/src/fundam.cpp @@ -274,10 +274,9 @@ class HomographyRefineCallback : public LMSolver::Callback cv::Mat cv::findHomography( InputArray _points1, InputArray _points2, - int method, double ransacReprojThreshold, OutputArray _mask ) + int method, double ransacReprojThreshold, OutputArray _mask, + const int maxIters, const double confidence) { - const double confidence = 0.995; - const int maxIters = 2000; const double defaultRANSACReprojThreshold = 3; bool result = false;