diff --git a/modules/calib3d/include/opencv2/calib3d.hpp b/modules/calib3d/include/opencv2/calib3d.hpp index fd35b7d5f3..aadfff7b1c 100644 --- a/modules/calib3d/include/opencv2/calib3d.hpp +++ b/modules/calib3d/include/opencv2/calib3d.hpp @@ -726,8 +726,8 @@ correctly only when there are more than 50% of inliers. Finally, if there are no noise is rather small, use the default method (method=0). The function is used to find initial intrinsic and extrinsic matrices. Homography matrix is -determined up to a scale. Thus, it is normalized so that \f$h_{33}=1\f$. Note that whenever an \f$H\f$ matrix -cannot be estimated, an empty one will be returned. +determined up to a scale. If \f$h_{33}\f$ is non-zero, the matrix is normalized so that \f$h_{33}=1\f$. +@note Whenever an \f$H\f$ matrix cannot be estimated, an empty one will be returned. @sa getAffineTransform, estimateAffine2D, estimateAffinePartial2D, getPerspectiveTransform, warpPerspective, diff --git a/modules/calib3d/src/fundam.cpp b/modules/calib3d/src/fundam.cpp index ed48645a4d..599250114b 100644 --- a/modules/calib3d/src/fundam.cpp +++ b/modules/calib3d/src/fundam.cpp @@ -49,6 +49,13 @@ namespace cv { +static inline double scaleFor(double x){ + return (std::fabs(x) > std::numeric_limits::epsilon()) ? 1./x : 1.; +} +static inline float scaleFor(float x){ + return (std::fabs(x) > std::numeric_limits::epsilon()) ? 1.f/x : 1.f; +} + /** * This class estimates a homography \f$H\in \mathbb{R}^{3\times 3}\f$ * between \f$\mathbf{x} \in \mathbb{R}^3\f$ and @@ -177,8 +184,7 @@ public: eigen( _LtL, matW, matV ); _Htemp = _invHnorm*_H0; _H0 = _Htemp*_Hnorm2; - _H0.convertTo(_model, _H0.type(), 1./_H0.at(2,2) ); - + _H0.convertTo(_model, _H0.type(), scaleFor(_H0.at(2,2))); return 1; } @@ -197,14 +203,14 @@ public: const Point2f* M = m1.ptr(); const Point2f* m = m2.ptr(); const double* H = model.ptr(); - float Hf[] = { (float)H[0], (float)H[1], (float)H[2], (float)H[3], (float)H[4], (float)H[5], (float)H[6], (float)H[7] }; + float Hf[] = { (float)H[0], (float)H[1], (float)H[2], (float)H[3], (float)H[4], (float)H[5], (float)H[6], (float)H[7], (float)H[8] }; _err.create(count, 1, CV_32F); float* err = _err.getMat().ptr(); for( i = 0; i < count; i++ ) { - float ww = 1.f/(Hf[6]*M[i].x + Hf[7]*M[i].y + 1.f); + float ww = 1.f/(Hf[6]*M[i].x + Hf[7]*M[i].y + Hf[8]); float dx = (Hf[0]*M[i].x + Hf[1]*M[i].y + Hf[2])*ww - m[i].x; float dy = (Hf[3]*M[i].x + Hf[4]*M[i].y + Hf[5])*ww - m[i].y; err[i] = dx*dx + dy*dy; @@ -231,8 +237,9 @@ public: if( _Jac.needed()) { _Jac.create(count*2, param.rows, CV_64F); + _Jac.setTo(0.); J = _Jac.getMat(); - CV_Assert( J.isContinuous() && J.cols == 8 ); + CV_Assert( J.isContinuous() && J.cols == 9 ); } const Point2f* M = src.ptr(); @@ -244,7 +251,7 @@ public: for( i = 0; i < count; i++ ) { double Mx = M[i].x, My = M[i].y; - double ww = h[6]*Mx + h[7]*My + 1.; + double ww = h[6]*Mx + h[7]*My + h[8]; ww = fabs(ww) > DBL_EPSILON ? 1./ww : 0; double xi = (h[0]*Mx + h[1]*My + h[2])*ww; double yi = (h[3]*Mx + h[4]*My + h[5])*ww; @@ -254,9 +261,7 @@ public: if( Jptr ) { Jptr[0] = Mx*ww; Jptr[1] = My*ww; Jptr[2] = ww; - Jptr[3] = Jptr[4] = Jptr[5] = 0.; Jptr[6] = -Mx*ww*xi; Jptr[7] = -My*ww*xi; - Jptr[8] = Jptr[9] = Jptr[10] = 0.; Jptr[11] = Mx*ww; Jptr[12] = My*ww; Jptr[13] = ww; Jptr[14] = -Mx*ww*yi; Jptr[15] = -My*ww*yi; @@ -419,7 +424,7 @@ cv::Mat cv::findHomography( InputArray _points1, InputArray _points2, dst = dst1; if( method == RANSAC || method == LMEDS ) cb->runKernel( src, dst, H ); - Mat H8(8, 1, CV_64F, H.ptr()); + Mat H8(9, 1, CV_64F, H.ptr()); LMSolver::create(makePtr(src, dst), 10)->run(H8); } } @@ -1002,14 +1007,6 @@ void cv::computeCorrespondEpilines( InputArray _points, int whichImage, } } -static inline double scaleFor(double x){ - return (std::fabs(x) > std::numeric_limits::epsilon()) ? 1./x : 1.; -} -static inline float scaleFor(float x){ - return (std::fabs(x) > std::numeric_limits::epsilon()) ? 1.f/x : 1.f; -} - - void cv::convertPointsFromHomogeneous( InputArray _src, OutputArray _dst ) { CV_INSTRUMENT_REGION(); diff --git a/modules/calib3d/test/test_homography.cpp b/modules/calib3d/test/test_homography.cpp index 41188a066d..2ceb05f28a 100644 --- a/modules/calib3d/test/test_homography.cpp +++ b/modules/calib3d/test/test_homography.cpp @@ -704,4 +704,27 @@ TEST(Calib3d_Homography, minPoints) EXPECT_THROW(findHomography(p1, p2, RANSAC, 0.01, mask), cv::Exception); } +TEST(Calib3d_Homography, not_normalized) +{ + Mat_ p1({5, 2}, {-1, -1, -2, -2, -1, 1, -2, 2, -1, 0}); + Mat_ p2({5, 2}, {0, -1, -1, -1, 0, 0, -1, 0, 0, -0.5}); + Mat_ ref({3, 3}, { + 0.74276086, 0., 0.74276086, + 0.18569022, 0.18569022, 0., + -0.37138043, 0., 0. + }); + + for (int method : std::vector({0, RANSAC, LMEDS})) + { + Mat h = findHomography(p1, p2, method); + for (auto it = h.begin(); it != h.end(); ++it) { + ASSERT_FALSE(cvIsNaN(*it)) << format("method %d\nResult:\n", method) << h; + } + if (h.at(0, 0) * ref.at(0, 0) < 0) { + h *= -1; + } + ASSERT_LE(cv::norm(h, ref, NORM_INF), 1e-8) << format("method %d\nResult:\n", method) << h; + } +} + }} // namespace