test_compose_rt.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 Differential
  45. {
  46. public:
  47. typedef Mat_<double> mat_t;
  48. Differential(double eps_, const mat_t& rv1_, const mat_t& tv1_, const mat_t& rv2_, const mat_t& tv2_)
  49. : rv1(rv1_), tv1(tv1_), rv2(rv2_), tv2(tv2_), eps(eps_), ev(3, 1) {}
  50. void dRv1(mat_t& dr3_dr1, mat_t& dt3_dr1)
  51. {
  52. dr3_dr1.create(3, 3); dt3_dr1.create(3, 3);
  53. for(int i = 0; i < 3; ++i)
  54. {
  55. ev.setTo(Scalar(0)); ev(i, 0) = eps;
  56. composeRT( rv1 + ev, tv1, rv2, tv2, rv3_p, tv3_p);
  57. composeRT( rv1 - ev, tv1, rv2, tv2, rv3_m, tv3_m);
  58. dr3_dr1.col(i) = rv3_p - rv3_m;
  59. dt3_dr1.col(i) = tv3_p - tv3_m;
  60. }
  61. dr3_dr1 /= 2 * eps; dt3_dr1 /= 2 * eps;
  62. }
  63. void dRv2(mat_t& dr3_dr2, mat_t& dt3_dr2)
  64. {
  65. dr3_dr2.create(3, 3); dt3_dr2.create(3, 3);
  66. for(int i = 0; i < 3; ++i)
  67. {
  68. ev.setTo(Scalar(0)); ev(i, 0) = eps;
  69. composeRT( rv1, tv1, rv2 + ev, tv2, rv3_p, tv3_p);
  70. composeRT( rv1, tv1, rv2 - ev, tv2, rv3_m, tv3_m);
  71. dr3_dr2.col(i) = rv3_p - rv3_m;
  72. dt3_dr2.col(i) = tv3_p - tv3_m;
  73. }
  74. dr3_dr2 /= 2 * eps; dt3_dr2 /= 2 * eps;
  75. }
  76. void dTv1(mat_t& drt3_dt1, mat_t& dt3_dt1)
  77. {
  78. drt3_dt1.create(3, 3); dt3_dt1.create(3, 3);
  79. for(int i = 0; i < 3; ++i)
  80. {
  81. ev.setTo(Scalar(0)); ev(i, 0) = eps;
  82. composeRT( rv1, tv1 + ev, rv2, tv2, rv3_p, tv3_p);
  83. composeRT( rv1, tv1 - ev, rv2, tv2, rv3_m, tv3_m);
  84. drt3_dt1.col(i) = rv3_p - rv3_m;
  85. dt3_dt1.col(i) = tv3_p - tv3_m;
  86. }
  87. drt3_dt1 /= 2 * eps; dt3_dt1 /= 2 * eps;
  88. }
  89. void dTv2(mat_t& dr3_dt2, mat_t& dt3_dt2)
  90. {
  91. dr3_dt2.create(3, 3); dt3_dt2.create(3, 3);
  92. for(int i = 0; i < 3; ++i)
  93. {
  94. ev.setTo(Scalar(0)); ev(i, 0) = eps;
  95. composeRT( rv1, tv1, rv2, tv2 + ev, rv3_p, tv3_p);
  96. composeRT( rv1, tv1, rv2, tv2 - ev, rv3_m, tv3_m);
  97. dr3_dt2.col(i) = rv3_p - rv3_m;
  98. dt3_dt2.col(i) = tv3_p - tv3_m;
  99. }
  100. dr3_dt2 /= 2 * eps; dt3_dt2 /= 2 * eps;
  101. }
  102. private:
  103. const mat_t& rv1, tv1, rv2, tv2;
  104. double eps;
  105. Mat_<double> ev;
  106. Differential& operator=(const Differential&);
  107. Mat rv3_m, tv3_m, rv3_p, tv3_p;
  108. };
  109. class CV_composeRT_Test : public cvtest::BaseTest
  110. {
  111. public:
  112. CV_composeRT_Test() {}
  113. ~CV_composeRT_Test() {}
  114. protected:
  115. void run(int)
  116. {
  117. ts->set_failed_test_info(cvtest::TS::OK);
  118. Mat_<double> rvec1(3, 1), tvec1(3, 1), rvec2(3, 1), tvec2(3, 1);
  119. randu(rvec1, Scalar(0), Scalar(6.29));
  120. randu(rvec2, Scalar(0), Scalar(6.29));
  121. randu(tvec1, Scalar(-2), Scalar(2));
  122. randu(tvec2, Scalar(-2), Scalar(2));
  123. Mat rvec3, tvec3;
  124. composeRT(rvec1, tvec1, rvec2, tvec2, rvec3, tvec3);
  125. Mat rvec3_exp, tvec3_exp;
  126. Mat rmat1, rmat2;
  127. cv::Rodrigues(rvec1, rmat1); // TODO cvtest
  128. cv::Rodrigues(rvec2, rmat2); // TODO cvtest
  129. cv::Rodrigues(rmat2 * rmat1, rvec3_exp); // TODO cvtest
  130. tvec3_exp = rmat2 * tvec1 + tvec2;
  131. const double thres = 1e-5;
  132. if (cv::norm(rvec3_exp, rvec3) > thres || cv::norm(tvec3_exp, tvec3) > thres)
  133. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  134. const double eps = 1e-3;
  135. Differential diff(eps, rvec1, tvec1, rvec2, tvec2);
  136. Mat dr3dr1, dr3dt1, dr3dr2, dr3dt2, dt3dr1, dt3dt1, dt3dr2, dt3dt2;
  137. composeRT(rvec1, tvec1, rvec2, tvec2, rvec3, tvec3,
  138. dr3dr1, dr3dt1, dr3dr2, dr3dt2, dt3dr1, dt3dt1, dt3dr2, dt3dt2);
  139. Mat_<double> dr3_dr1, dt3_dr1;
  140. diff.dRv1(dr3_dr1, dt3_dr1);
  141. if (cv::norm(dr3_dr1, dr3dr1) > thres || cv::norm(dt3_dr1, dt3dr1) > thres)
  142. {
  143. ts->printf( cvtest::TS::LOG, "Invalid derivates by r1\n" );
  144. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  145. }
  146. Mat_<double> dr3_dr2, dt3_dr2;
  147. diff.dRv2(dr3_dr2, dt3_dr2);
  148. if (cv::norm(dr3_dr2, dr3dr2) > thres || cv::norm(dt3_dr2, dt3dr2) > thres)
  149. {
  150. ts->printf( cvtest::TS::LOG, "Invalid derivates by r2\n" );
  151. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  152. }
  153. Mat_<double> dr3_dt1, dt3_dt1;
  154. diff.dTv1(dr3_dt1, dt3_dt1);
  155. if (cv::norm(dr3_dt1, dr3dt1) > thres || cv::norm(dt3_dt1, dt3dt1) > thres)
  156. {
  157. ts->printf( cvtest::TS::LOG, "Invalid derivates by t1\n" );
  158. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  159. }
  160. Mat_<double> dr3_dt2, dt3_dt2;
  161. diff.dTv2(dr3_dt2, dt3_dt2);
  162. if (cv::norm(dr3_dt2, dr3dt2) > thres || cv::norm(dt3_dt2, dt3dt2) > thres)
  163. {
  164. ts->printf( cvtest::TS::LOG, "Invalid derivates by t2\n" );
  165. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  166. }
  167. }
  168. };
  169. TEST(Calib3d_ComposeRT, accuracy) { CV_composeRT_Test test; test.safe_run(); }
  170. }} // namespace