test_estimaterigid.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // this is test for a deprecated function. let's ignore deprecated warnings in this file
  44. #if defined(__clang__)
  45. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  46. #elif defined(__GNUC__)
  47. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  48. #elif defined(_MSC_VER)
  49. #pragma warning( disable : 4996)
  50. #endif
  51. namespace opencv_test { namespace {
  52. class CV_RigidTransform_Test : public cvtest::BaseTest
  53. {
  54. public:
  55. CV_RigidTransform_Test();
  56. ~CV_RigidTransform_Test();
  57. protected:
  58. void run(int);
  59. bool testNPoints(int);
  60. bool testImage();
  61. };
  62. CV_RigidTransform_Test::CV_RigidTransform_Test()
  63. {
  64. }
  65. CV_RigidTransform_Test::~CV_RigidTransform_Test() {}
  66. struct WrapAff2D
  67. {
  68. const double *F;
  69. WrapAff2D(const Mat& aff) : F(aff.ptr<double>()) {}
  70. Point2f operator()(const Point2f& p)
  71. {
  72. return Point2f( (float)(p.x * F[0] + p.y * F[1] + F[2]),
  73. (float)(p.x * F[3] + p.y * F[4] + F[5]) );
  74. }
  75. };
  76. bool CV_RigidTransform_Test::testNPoints(int from)
  77. {
  78. cv::RNG rng = cv::theRNG();
  79. int progress = 0;
  80. int k, ntests = 10000;
  81. for( k = from; k < ntests; k++ )
  82. {
  83. ts->update_context( this, k, true );
  84. progress = update_progress(progress, k, ntests, 0);
  85. Mat aff(2, 3, CV_64F);
  86. rng.fill(aff, RNG::UNIFORM, Scalar(-2), Scalar(2));
  87. int n = (unsigned)rng % 100 + 10;
  88. Mat fpts(1, n, CV_32FC2);
  89. Mat tpts(1, n, CV_32FC2);
  90. rng.fill(fpts, RNG::UNIFORM, Scalar(0,0), Scalar(10,10));
  91. std::transform(fpts.ptr<Point2f>(), fpts.ptr<Point2f>() + n, tpts.ptr<Point2f>(), WrapAff2D(aff));
  92. Mat noise(1, n, CV_32FC2);
  93. rng.fill(noise, RNG::NORMAL, Scalar::all(0), Scalar::all(0.001*(n<=7 ? 0 : n <= 30 ? 1 : 10)));
  94. tpts += noise;
  95. Mat aff_est = estimateRigidTransform(fpts, tpts, true);
  96. double thres = 0.1*cvtest::norm(aff, NORM_L2);
  97. double d = cvtest::norm(aff_est, aff, NORM_L2);
  98. if (d > thres)
  99. {
  100. double dB=0, nB=0;
  101. if (n <= 4)
  102. {
  103. Mat A = fpts.reshape(1, 3);
  104. Mat B = A - repeat(A.row(0), 3, 1), Bt = B.t();
  105. B = Bt*B;
  106. dB = cv::determinant(B);
  107. nB = cvtest::norm(B, NORM_L2);
  108. if( fabs(dB) < 0.01*nB )
  109. continue;
  110. }
  111. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  112. ts->printf( cvtest::TS::LOG, "Threshold = %f, norm of difference = %f", thres, d );
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. bool CV_RigidTransform_Test::testImage()
  119. {
  120. Mat img;
  121. Mat testImg = imread( string(ts->get_data_path()) + "shared/graffiti.png", 1);
  122. if (testImg.empty())
  123. {
  124. ts->printf( ts->LOG, "test image can not be read");
  125. ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
  126. return false;
  127. }
  128. pyrDown(testImg, img);
  129. Mat aff = cv::getRotationMatrix2D(Point(img.cols/2, img.rows/2), 1, 0.99);
  130. aff.ptr<double>()[2]+=3;
  131. aff.ptr<double>()[5]+=3;
  132. Mat rotated;
  133. warpAffine(img, rotated, aff, img.size());
  134. Mat aff_est = estimateRigidTransform(img, rotated, true);
  135. const double thres = 0.033;
  136. if (cvtest::norm(aff_est, aff, NORM_INF) > thres)
  137. {
  138. ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
  139. ts->printf( cvtest::TS::LOG, "Threshold = %f, norm of difference = %f", thres,
  140. cvtest::norm(aff_est, aff, NORM_INF) );
  141. return false;
  142. }
  143. return true;
  144. }
  145. void CV_RigidTransform_Test::run( int start_from )
  146. {
  147. cvtest::DefaultRngAuto dra;
  148. if (!testNPoints(start_from))
  149. return;
  150. if (!testImage())
  151. return;
  152. ts->set_failed_test_info(cvtest::TS::OK);
  153. }
  154. TEST(Video_RigidFlow, accuracy) { CV_RigidTransform_Test test; test.safe_run(); }
  155. }} // namespace