From 78bd55c8df9bef7b87c4cdce029b805f58345fd5 Mon Sep 17 00:00:00 2001 From: Jim Zhou Date: Fri, 11 Jan 2019 21:58:47 +0800 Subject: [PATCH] Merge pull request #13601 from JimZhou-001:JimZhou-001 * Fix the bug in case determinant of rotation matrix is -1 * calib3d(test): check det(R) == 1 --- modules/calib3d/src/homography_decomp.cpp | 8 +++++++ modules/calib3d/test/test_homography_decomp.cpp | 32 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/modules/calib3d/src/homography_decomp.cpp b/modules/calib3d/src/homography_decomp.cpp index fea8882..3bfb62e 100644 --- a/modules/calib3d/src/homography_decomp.cpp +++ b/modules/calib3d/src/homography_decomp.cpp @@ -185,6 +185,10 @@ bool HomographyDecompZhang::findMotionFrom_tstar_n(const cv::Vec3d& tstar, const temp(1, 1) += 1.0; temp(2, 2) += 1.0; motion.R = getHnorm() * temp.inv(); + if (cv::determinant(motion.R) < 0) + { + motion.R *= -1; + } motion.t = motion.R * tstar; motion.n = n; return passesSameSideOfPlaneConstraint(motion); @@ -312,6 +316,10 @@ void HomographyDecompInria::findRmatFrom_tstar_n(const cv::Vec3d& tstar, const c 0.0, 0.0, 1.0); R = getHnorm() * (I - (2/v) * tstar_m * n_m.t() ); + if (cv::determinant(R) < 0) + { + R *= -1; + } } void HomographyDecompInria::decompose(std::vector& camMotions) diff --git a/modules/calib3d/test/test_homography_decomp.cpp b/modules/calib3d/test/test_homography_decomp.cpp index 45f5ae6..9ddc0e9 100644 --- a/modules/calib3d/test/test_homography_decomp.cpp +++ b/modules/calib3d/test/test_homography_decomp.cpp @@ -134,4 +134,36 @@ private: TEST(Calib3d_DecomposeHomography, regression) { CV_HomographyDecompTest test; test.safe_run(); } + +TEST(Calib3d_DecomposeHomography, issue_4978) +{ + Matx33d K( + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0 + ); + + Matx33d H( + -0.102896, 0.270191, -0.0031153, + 0.0406387, 1.19569, -0.0120456, + 0.445351, 0.0410889, 1 + ); + + vector rotations; + vector translations; + vector normals; + + decomposeHomographyMat(H, K, rotations, translations, normals); + + ASSERT_GT(rotations.size(), (size_t)0u); + for (size_t i = 0; i < rotations.size(); i++) + { + // check: det(R) = 1 + EXPECT_TRUE(std::fabs(cv::determinant(rotations[i]) - 1.0) < 0.01) + << "R: det=" << cv::determinant(rotations[0]) << std::endl << rotations[i] << std::endl + << "T:" << std::endl << translations[i] << std::endl; + } +} + + }} // namespace -- 2.7.4