test_optflowpyrlk.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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., sum_err = 0;
  59. int pt_cmpd = 0;
  60. int pt_exceed = 0;
  61. int merr_i = 0, merr_j = 0, merr_k = 0, merr_nan = 0;
  62. char filename[1000];
  63. cv::Point2f *v = 0, *v2 = 0;
  64. cv::Mat _u, _v, _v2;
  65. cv::Mat imgI, imgJ;
  66. int n = 0, i = 0;
  67. for(;;)
  68. {
  69. sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" );
  70. {
  71. FileStorage fs(filename, FileStorage::READ);
  72. fs["points"] >> _u;
  73. if( _u.empty() )
  74. {
  75. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  76. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  77. break;
  78. }
  79. }
  80. sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" );
  81. {
  82. FileStorage fs(filename, FileStorage::READ);
  83. fs["points"] >> _v;
  84. if( _v.empty() )
  85. {
  86. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  87. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  88. break;
  89. }
  90. }
  91. if( _u.cols != 2 || _u.type() != CV_32F ||
  92. _v.cols != 2 || _v.type() != CV_32F ||
  93. _v.rows != _u.rows )
  94. {
  95. ts->printf( cvtest::TS::LOG, "the loaded matrices of points are not valid\n" );
  96. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  97. break;
  98. }
  99. /* read first image */
  100. sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_1.bmp" );
  101. imgI = cv::imread( filename, cv::IMREAD_UNCHANGED );
  102. if( imgI.empty() )
  103. {
  104. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  105. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  106. break;
  107. }
  108. /* read second image */
  109. sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" );
  110. imgJ = cv::imread( filename, cv::IMREAD_UNCHANGED );
  111. if( imgJ.empty() )
  112. {
  113. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  114. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  115. break;
  116. }
  117. n = _u.rows;
  118. std::vector<uchar> status(n, (uchar)0);
  119. /* calculate flow */
  120. calcOpticalFlowPyrLK(imgI, imgJ, _u, _v2, status, cv::noArray(), Size( 41, 41 ), 4,
  121. TermCriteria( TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0.01f ), 0 );
  122. v = (cv::Point2f*)_v.ptr();
  123. v2 = (cv::Point2f*)_v2.ptr();
  124. /* compare results */
  125. for( i = 0; i < n; i++ )
  126. {
  127. if( status[i] != 0 )
  128. {
  129. double err;
  130. if( cvIsNaN(v[i].x) || cvIsNaN(v[i].y) )
  131. {
  132. merr_j++;
  133. continue;
  134. }
  135. if( cvIsNaN(v2[i].x) || cvIsNaN(v2[i].y) )
  136. {
  137. merr_nan++;
  138. continue;
  139. }
  140. err = fabs(v2[i].x - v[i].x) + fabs(v2[i].y - v[i].y);
  141. if( err > max_err )
  142. {
  143. max_err = err;
  144. merr_i = i;
  145. }
  146. pt_exceed += err > success_error_level;
  147. sum_err += err;
  148. pt_cmpd++;
  149. }
  150. else
  151. {
  152. if( !cvIsNaN( v[i].x ))
  153. {
  154. merr_i = i;
  155. merr_k++;
  156. ts->printf( cvtest::TS::LOG, "The algorithm lost the point #%d\n", i );
  157. code = cvtest::TS::FAIL_BAD_ACCURACY;
  158. break;
  159. }
  160. }
  161. }
  162. if( i < n )
  163. break;
  164. if( pt_exceed > bad_points_max )
  165. {
  166. ts->printf( cvtest::TS::LOG,
  167. "The number of poorly tracked points is too big (>=%d)\n", pt_exceed );
  168. code = cvtest::TS::FAIL_BAD_ACCURACY;
  169. break;
  170. }
  171. if( max_err > 1 )
  172. {
  173. ts->printf( cvtest::TS::LOG, "Maximum tracking error is too big (=%g) at %d\n", max_err, merr_i );
  174. code = cvtest::TS::FAIL_BAD_ACCURACY;
  175. break;
  176. }
  177. if( merr_nan > 0 )
  178. {
  179. ts->printf( cvtest::TS::LOG, "NAN tracking result with status != 0 (%d times)\n", merr_nan );
  180. code = cvtest::TS::FAIL_BAD_ACCURACY;
  181. }
  182. break;
  183. }
  184. if( code < 0 )
  185. ts->set_failed_test_info( code );
  186. }
  187. TEST(Video_OpticalFlowPyrLK, accuracy) { CV_OptFlowPyrLKTest test; test.safe_run(); }
  188. TEST(Video_OpticalFlowPyrLK, submat)
  189. {
  190. // see bug #2075
  191. std::string path = cvtest::TS::ptr()->get_data_path() + "../cv/shared/lena.png";
  192. cv::Mat lenaImg = cv::imread(path);
  193. ASSERT_FALSE(lenaImg.empty());
  194. cv::Mat wholeImage;
  195. cv::resize(lenaImg, wholeImage, cv::Size(1024, 1024), 0, 0, cv::INTER_LINEAR_EXACT);
  196. cv::Mat img1 = wholeImage(cv::Rect(0, 0, 640, 360)).clone();
  197. cv::Mat img2 = wholeImage(cv::Rect(40, 60, 640, 360));
  198. std::vector<uchar> status;
  199. std::vector<float> error;
  200. std::vector<cv::Point2f> prev;
  201. std::vector<cv::Point2f> next;
  202. cv::RNG rng(123123);
  203. for(int i = 0; i < 50; ++i)
  204. {
  205. int x = rng.uniform(0, 640);
  206. int y = rng.uniform(0, 360);
  207. prev.push_back(cv::Point2f((float)x, (float)y));
  208. }
  209. ASSERT_NO_THROW(cv::calcOpticalFlowPyrLK(img1, img2, prev, next, status, error));
  210. }
  211. }} // namespace