test_decompose_projection.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. namespace opencv_test { namespace {
  44. class CV_DecomposeProjectionMatrixTest : public cvtest::BaseTest
  45. {
  46. public:
  47. CV_DecomposeProjectionMatrixTest();
  48. protected:
  49. void run(int);
  50. };
  51. CV_DecomposeProjectionMatrixTest::CV_DecomposeProjectionMatrixTest()
  52. {
  53. test_case_count = 30;
  54. }
  55. void CV_DecomposeProjectionMatrixTest::run(int start_from)
  56. {
  57. ts->set_failed_test_info(cvtest::TS::OK);
  58. cv::RNG& rng = ts->get_rng();
  59. int progress = 0;
  60. for (int iter = start_from; iter < test_case_count; ++iter)
  61. {
  62. ts->update_context(this, iter, true);
  63. progress = update_progress(progress, iter, test_case_count, 0);
  64. // Create the original (and random) camera matrix, rotation, and translation
  65. cv::Vec2d f, c;
  66. rng.fill(f, cv::RNG::UNIFORM, 300, 1000);
  67. rng.fill(c, cv::RNG::UNIFORM, 150, 600);
  68. double alpha = 0.01*rng.gaussian(1);
  69. cv::Matx33d origK(f(0), alpha*f(0), c(0),
  70. 0, f(1), c(1),
  71. 0, 0, 1);
  72. cv::Vec3d rVec;
  73. rng.fill(rVec, cv::RNG::UNIFORM, -CV_PI, CV_PI);
  74. cv::Matx33d origR;
  75. cv::Rodrigues(rVec, origR); // TODO cvtest
  76. cv::Vec3d origT;
  77. rng.fill(origT, cv::RNG::NORMAL, 0, 1);
  78. // Compose the projection matrix
  79. cv::Matx34d P(3,4);
  80. hconcat(origK*origR, origK*origT, P);
  81. // Decompose
  82. cv::Matx33d K, R;
  83. cv::Vec4d homogCameraCenter;
  84. decomposeProjectionMatrix(P, K, R, homogCameraCenter);
  85. // Recover translation from the camera center
  86. cv::Vec3d cameraCenter(homogCameraCenter(0), homogCameraCenter(1), homogCameraCenter(2));
  87. cameraCenter /= homogCameraCenter(3);
  88. cv::Vec3d t = -R*cameraCenter;
  89. const double thresh = 1e-6;
  90. if (cv::norm(origK, K, cv::NORM_INF) > thresh)
  91. {
  92. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  93. break;
  94. }
  95. if (cv::norm(origR, R, cv::NORM_INF) > thresh)
  96. {
  97. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  98. break;
  99. }
  100. if (cv::norm(origT, t, cv::NORM_INF) > thresh)
  101. {
  102. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  103. break;
  104. }
  105. }
  106. }
  107. TEST(Calib3d_DecomposeProjectionMatrix, accuracy)
  108. {
  109. CV_DecomposeProjectionMatrixTest test;
  110. test.safe_run();
  111. }
  112. TEST(Calib3d_DecomposeProjectionMatrix, degenerate_cases)
  113. {
  114. for (int i = 0; i < 3; i++)
  115. {
  116. for (int j = 0; j < 2; j++)
  117. {
  118. cv::Matx34d P;
  119. P(0, i) = 1;
  120. P(1, (i + j + 1) % 3) = 1;
  121. P(2, (i + 2 * j + 2) % 3) = 1;
  122. cv::Matx33d K, R;
  123. cv::Vec4d t;
  124. decomposeProjectionMatrix(P, K, R, t);
  125. EXPECT_LT(cv::norm(K * R, P.get_minor<3, 3>(0, 0), cv::NORM_INF), 1e-6);
  126. }
  127. }
  128. }
  129. TEST(Calib3d_DecomposeProjectionMatrix, bug_23733)
  130. {
  131. cv::Matx34d P(52, -7, 4, 12,
  132. -6, 49, 12, 8,
  133. 4, 17, 1, 0);
  134. P *= 1e-6;
  135. cv::Matx33d K, R;
  136. cv::Vec4d t;
  137. decomposeProjectionMatrix(P, K, R, t);
  138. EXPECT_LT(cv::norm(R.t() * R - cv::Matx33d::eye(), cv::NORM_INF), 1e-10);
  139. cv::Matx34d M;
  140. cv::hconcat(R, -R * cv::Vec3d(t[0] / t[3], t[1] / t[3], t[2] / t[3]), M);
  141. cv::Matx34d P_recompose = K * M;
  142. EXPECT_LT(cv::norm(P_recompose - P, cv::NORM_INF), 1e-16);
  143. }
  144. }} // namespace