test_optflowpyrlk.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // Intel License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000, Intel Corporation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of Intel Corporation may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. namespace opencv_test { namespace {
  43. /* ///////////////////// pyrlk_test ///////////////////////// */
  44. class CV_OptFlowPyrLKTest : public cvtest::BaseTest
  45. {
  46. public:
  47. CV_OptFlowPyrLKTest();
  48. protected:
  49. void run(int);
  50. };
  51. CV_OptFlowPyrLKTest::CV_OptFlowPyrLKTest() {}
  52. void CV_OptFlowPyrLKTest::run( int )
  53. {
  54. int code = cvtest::TS::OK;
  55. const double success_error_level = 0.3;
  56. const int bad_points_max = 8;
  57. /* test parameters */
  58. double max_err = 0.;
  59. int pt_exceed = 0;
  60. int merr_i = 0, merr_nan = 0;
  61. char filename[1000];
  62. cv::Point2f *v = 0, *v2 = 0;
  63. cv::Mat _u, _v, _v2;
  64. cv::Mat imgI, imgJ;
  65. int n = 0, i = 0;
  66. for(;;)
  67. {
  68. snprintf( filename, sizeof(filename), "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" );
  69. {
  70. FileStorage fs(filename, FileStorage::READ);
  71. fs["points"] >> _u;
  72. if( _u.empty() )
  73. {
  74. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  75. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  76. break;
  77. }
  78. }
  79. snprintf( filename, sizeof(filename), "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" );
  80. {
  81. FileStorage fs(filename, FileStorage::READ);
  82. fs["points"] >> _v;
  83. if( _v.empty() )
  84. {
  85. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  86. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  87. break;
  88. }
  89. }
  90. if( _u.cols != 2 || _u.type() != CV_32F ||
  91. _v.cols != 2 || _v.type() != CV_32F ||
  92. _v.rows != _u.rows )
  93. {
  94. ts->printf( cvtest::TS::LOG, "the loaded matrices of points are not valid\n" );
  95. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  96. break;
  97. }
  98. /* read first image */
  99. snprintf( filename, sizeof(filename), "%soptflow/%s", ts->get_data_path().c_str(), "rock_1.bmp" );
  100. imgI = cv::imread( filename, cv::IMREAD_UNCHANGED );
  101. if( imgI.empty() )
  102. {
  103. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  104. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  105. break;
  106. }
  107. /* read second image */
  108. snprintf( filename, sizeof(filename), "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" );
  109. imgJ = cv::imread( filename, cv::IMREAD_UNCHANGED );
  110. if( imgJ.empty() )
  111. {
  112. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  113. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  114. break;
  115. }
  116. n = _u.rows;
  117. std::vector<uchar> status(n, (uchar)0);
  118. /* calculate flow */
  119. calcOpticalFlowPyrLK(imgI, imgJ, _u, _v2, status, cv::noArray(), Size( 41, 41 ), 4,
  120. TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0.01f ), 0 );
  121. v = (cv::Point2f*)_v.ptr();
  122. v2 = (cv::Point2f*)_v2.ptr();
  123. /* compare results */
  124. for( i = 0; i < n; i++ )
  125. {
  126. if( status[i] != 0 )
  127. {
  128. double err;
  129. if( cvIsNaN(v[i].x) || cvIsNaN(v[i].y) )
  130. {
  131. continue;
  132. }
  133. if( cvIsNaN(v2[i].x) || cvIsNaN(v2[i].y) )
  134. {
  135. merr_nan++;
  136. continue;
  137. }
  138. err = fabs(v2[i].x - v[i].x) + fabs(v2[i].y - v[i].y);
  139. if( err > max_err )
  140. {
  141. max_err = err;
  142. merr_i = i;
  143. }
  144. pt_exceed += err > success_error_level;
  145. }
  146. else
  147. {
  148. if( !cvIsNaN( v[i].x ))
  149. {
  150. merr_i = i;
  151. ts->printf( cvtest::TS::LOG, "The algorithm lost the point #%d\n", i );
  152. code = cvtest::TS::FAIL_BAD_ACCURACY;
  153. break;
  154. }
  155. }
  156. }
  157. if( i < n )
  158. break;
  159. if( pt_exceed > bad_points_max )
  160. {
  161. ts->printf( cvtest::TS::LOG,
  162. "The number of poorly tracked points is too big (>=%d)\n", pt_exceed );
  163. code = cvtest::TS::FAIL_BAD_ACCURACY;
  164. break;
  165. }
  166. if( max_err > 1 )
  167. {
  168. ts->printf( cvtest::TS::LOG, "Maximum tracking error is too big (=%g) at %d\n", max_err, merr_i );
  169. code = cvtest::TS::FAIL_BAD_ACCURACY;
  170. break;
  171. }
  172. if( merr_nan > 0 )
  173. {
  174. ts->printf( cvtest::TS::LOG, "NAN tracking result with status != 0 (%d times)\n", merr_nan );
  175. code = cvtest::TS::FAIL_BAD_ACCURACY;
  176. }
  177. break;
  178. }
  179. if( code < 0 )
  180. ts->set_failed_test_info( code );
  181. }
  182. TEST(Video_OpticalFlowPyrLK, accuracy) { CV_OptFlowPyrLKTest test; test.safe_run(); }
  183. TEST(Video_OpticalFlowPyrLK, submat)
  184. {
  185. // see bug #2075
  186. std::string path = cvtest::TS::ptr()->get_data_path() + "../cv/shared/lena.png";
  187. cv::Mat lenaImg = cv::imread(path);
  188. ASSERT_FALSE(lenaImg.empty());
  189. cv::Mat wholeImage;
  190. cv::resize(lenaImg, wholeImage, cv::Size(1024, 1024), 0, 0, cv::INTER_LINEAR_EXACT);
  191. cv::Mat img1 = wholeImage(cv::Rect(0, 0, 640, 360)).clone();
  192. cv::Mat img2 = wholeImage(cv::Rect(40, 60, 640, 360));
  193. std::vector<uchar> status;
  194. std::vector<float> error;
  195. std::vector<cv::Point2f> prev;
  196. std::vector<cv::Point2f> next;
  197. cv::RNG rng(123123);
  198. for(int i = 0; i < 50; ++i)
  199. {
  200. int x = rng.uniform(0, 640);
  201. int y = rng.uniform(0, 360);
  202. prev.push_back(cv::Point2f((float)x, (float)y));
  203. }
  204. ASSERT_NO_THROW(cv::calcOpticalFlowPyrLK(img1, img2, prev, next, status, error));
  205. }
  206. }} // namespace