test_homography_decomp.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // This is a test file for the function decomposeHomography contributed to OpenCV
  4. // by Samson Yilma.
  5. //
  6. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  7. //
  8. // By downloading, copying, installing or using the software you agree to this license.
  9. // If you do not agree to this license, do not download, install,
  10. // copy or use the software.
  11. //
  12. //
  13. // License Agreement
  14. // For Open Source Computer Vision Library
  15. //
  16. // Copyright (C) 2014, Samson Yilma (samson_yilma@yahoo.com), all rights reserved.
  17. //
  18. // Third party copyrights are property of their respective owners.
  19. //
  20. // Redistribution and use in source and binary forms, with or without modification,
  21. // are permitted provided that the following conditions are met:
  22. //
  23. // * Redistribution's of source code must retain the above copyright notice,
  24. // this list of conditions and the following disclaimer.
  25. //
  26. // * Redistribution's in binary form must reproduce the above copyright notice,
  27. // this list of conditions and the following disclaimer in the documentation
  28. // and/or other materials provided with the distribution.
  29. //
  30. // * The name of the copyright holders may not be used to endorse or promote products
  31. // derived from this software without specific prior written permission.
  32. //
  33. // This software is provided by the copyright holders and contributors "as is" and
  34. // any express or implied warranties, including, but not limited to, the implied
  35. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  36. // In no event shall the Intel Corporation or contributors be liable for any direct,
  37. // indirect, incidental, special, exemplary, or consequential damages
  38. // (including, but not limited to, procurement of substitute goods or services;
  39. // loss of use, data, or profits; or business interruption) however caused
  40. // and on any theory of liability, whether in contract, strict liability,
  41. // or tort (including negligence or otherwise) arising in any way out of
  42. // the use of this software, even if advised of the possibility of such damage.
  43. //
  44. //M*/
  45. #include "test_precomp.hpp"
  46. namespace opencv_test { namespace {
  47. class CV_HomographyDecompTest: public cvtest::BaseTest {
  48. public:
  49. CV_HomographyDecompTest()
  50. {
  51. buildTestDataSet();
  52. }
  53. protected:
  54. void run(int)
  55. {
  56. vector<Mat> rotations;
  57. vector<Mat> translations;
  58. vector<Mat> normals;
  59. decomposeHomographyMat(_H, _K, rotations, translations, normals);
  60. //there should be at least 1 solution
  61. ASSERT_GT(static_cast<int>(rotations.size()), 0);
  62. ASSERT_GT(static_cast<int>(translations.size()), 0);
  63. ASSERT_GT(static_cast<int>(normals.size()), 0);
  64. ASSERT_EQ(rotations.size(), normals.size());
  65. ASSERT_EQ(translations.size(), normals.size());
  66. ASSERT_TRUE(containsValidMotion(rotations, translations, normals));
  67. decomposeHomographyMat(_H, _K, rotations, noArray(), noArray());
  68. ASSERT_GT(static_cast<int>(rotations.size()), 0);
  69. }
  70. private:
  71. void buildTestDataSet()
  72. {
  73. _K = Matx33d(640, 0.0, 320,
  74. 0, 640, 240,
  75. 0, 0, 1);
  76. _H = Matx33d(2.649157564634028, 4.583875997496426, 70.694447785121326,
  77. -1.072756858861583, 3.533262150437228, 1513.656999614321649,
  78. 0.001303887589576, 0.003042206876298, 1.000000000000000
  79. );
  80. //expected solution for the given homography and intrinsic matrices
  81. _R = Matx33d(0.43307983549125, 0.545749113549648, -0.717356090899523,
  82. -0.85630229674426, 0.497582023798831, -0.138414255706431,
  83. 0.281404038139784, 0.67421809131173, 0.682818960388909);
  84. _t = Vec3d(1.826751712278038, 1.264718492450820, 0.195080809998819);
  85. _n = Vec3d(0.244875830334816, 0.480857890778889, 0.841909446789566);
  86. }
  87. bool containsValidMotion(std::vector<Mat>& rotations,
  88. std::vector<Mat>& translations,
  89. std::vector<Mat>& normals
  90. )
  91. {
  92. double max_error = 1.0e-3;
  93. vector<Mat>::iterator riter = rotations.begin();
  94. vector<Mat>::iterator titer = translations.begin();
  95. vector<Mat>::iterator niter = normals.begin();
  96. for (;
  97. riter != rotations.end() && titer != translations.end() && niter != normals.end();
  98. ++riter, ++titer, ++niter) {
  99. double rdist = cvtest::norm(*riter, _R, NORM_INF);
  100. double tdist = cvtest::norm(*titer, _t, NORM_INF);
  101. double ndist = cvtest::norm(*niter, _n, NORM_INF);
  102. if ( rdist < max_error
  103. && tdist < max_error
  104. && ndist < max_error )
  105. return true;
  106. }
  107. return false;
  108. }
  109. Matx33d _R, _K, _H;
  110. Vec3d _t, _n;
  111. };
  112. TEST(Calib3d_DecomposeHomography, regression) { CV_HomographyDecompTest test; test.safe_run(); }
  113. TEST(Calib3d_DecomposeHomography, issue_4978)
  114. {
  115. Matx33d K(
  116. 1.0, 0.0, 0.0,
  117. 0.0, 1.0, 0.0,
  118. 0.0, 0.0, 1.0
  119. );
  120. Matx33d H(
  121. -0.102896, 0.270191, -0.0031153,
  122. 0.0406387, 1.19569, -0.0120456,
  123. 0.445351, 0.0410889, 1
  124. );
  125. vector<Mat> rotations;
  126. vector<Mat> translations;
  127. vector<Mat> normals;
  128. decomposeHomographyMat(H, K, rotations, translations, normals);
  129. ASSERT_GT(rotations.size(), (size_t)0u);
  130. for (size_t i = 0; i < rotations.size(); i++)
  131. {
  132. // check: det(R) = 1
  133. EXPECT_TRUE(std::fabs(cv::determinant(rotations[i]) - 1.0) < 0.01)
  134. << "R: det=" << cv::determinant(rotations[0]) << std::endl << rotations[i] << std::endl
  135. << "T:" << std::endl << translations[i] << std::endl;
  136. }
  137. }
  138. }} // namespace