PageRenderTime 143ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/source/3rd_party/opencv/sources/modules/video/test/test_optflowpyrlk.cpp

https://gitlab.com/Ruggero/SparkEngine_Desktop
C++ | 244 lines | 151 code | 45 blank | 48 comment | 23 complexity | 4b6f9836b84f24411aa9d2b8602e0d29 MD5 | raw file
  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. /* ///////////////////// pyrlk_test ///////////////////////// */
  43. class CV_OptFlowPyrLKTest : public cvtest::BaseTest
  44. {
  45. public:
  46. CV_OptFlowPyrLKTest();
  47. protected:
  48. void run(int);
  49. };
  50. CV_OptFlowPyrLKTest::CV_OptFlowPyrLKTest() {}
  51. void CV_OptFlowPyrLKTest::run( int )
  52. {
  53. int code = cvtest::TS::OK;
  54. const double success_error_level = 0.3;
  55. const int bad_points_max = 8;
  56. /* test parameters */
  57. double max_err = 0., sum_err = 0;
  58. int pt_cmpd = 0;
  59. int pt_exceed = 0;
  60. int merr_i = 0, merr_j = 0, merr_k = 0;
  61. char filename[1000];
  62. CvPoint2D32f *u = 0, *v = 0, *v2 = 0;
  63. CvMat *_u = 0, *_v = 0, *_v2 = 0;
  64. char* status = 0;
  65. IplImage* imgI = 0;
  66. IplImage* imgJ = 0;
  67. int n = 0, i = 0;
  68. sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_prev.dat" );
  69. _u = (CvMat*)cvLoad( filename );
  70. if( !_u )
  71. {
  72. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  73. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  74. goto _exit_;
  75. }
  76. sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "lk_next.dat" );
  77. _v = (CvMat*)cvLoad( filename );
  78. if( !_v )
  79. {
  80. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  81. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  82. goto _exit_;
  83. }
  84. if( _u->cols != 2 || CV_MAT_TYPE(_u->type) != CV_32F ||
  85. _v->cols != 2 || CV_MAT_TYPE(_v->type) != CV_32F || _v->rows != _u->rows )
  86. {
  87. ts->printf( cvtest::TS::LOG, "the loaded matrices of points are not valid\n" );
  88. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  89. goto _exit_;
  90. }
  91. u = (CvPoint2D32f*)_u->data.fl;
  92. v = (CvPoint2D32f*)_v->data.fl;
  93. /* allocate adidtional buffers */
  94. _v2 = cvCloneMat( _u );
  95. v2 = (CvPoint2D32f*)_v2->data.fl;
  96. /* read first image */
  97. sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_1.bmp" );
  98. imgI = cvLoadImage( filename, -1 );
  99. if( !imgI )
  100. {
  101. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  102. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  103. goto _exit_;
  104. }
  105. /* read second image */
  106. sprintf( filename, "%soptflow/%s", ts->get_data_path().c_str(), "rock_2.bmp" );
  107. imgJ = cvLoadImage( filename, -1 );
  108. if( !imgJ )
  109. {
  110. ts->printf( cvtest::TS::LOG, "could not read %s\n", filename );
  111. code = cvtest::TS::FAIL_MISSING_TEST_DATA;
  112. goto _exit_;
  113. }
  114. n = _u->rows;
  115. status = (char*)cvAlloc(n*sizeof(status[0]));
  116. /* calculate flow */
  117. cvCalcOpticalFlowPyrLK( imgI, imgJ, 0, 0, u, v2, n, cvSize( 41, 41 ),
  118. 4, status, 0, cvTermCriteria( CV_TERMCRIT_ITER|
  119. CV_TERMCRIT_EPS, 30, 0.01f ), 0 );
  120. /* compare results */
  121. for( i = 0; i < n; i++ )
  122. {
  123. if( status[i] != 0 )
  124. {
  125. double err;
  126. if( cvIsNaN(v[i].x) )
  127. {
  128. merr_j++;
  129. continue;
  130. }
  131. err = fabs(v2[i].x - v[i].x) + fabs(v2[i].y - v[i].y);
  132. if( err > max_err )
  133. {
  134. max_err = err;
  135. merr_i = i;
  136. }
  137. pt_exceed += err > success_error_level;
  138. sum_err += err;
  139. pt_cmpd++;
  140. }
  141. else
  142. {
  143. if( !cvIsNaN( v[i].x ))
  144. {
  145. merr_i = i;
  146. merr_k++;
  147. ts->printf( cvtest::TS::LOG, "The algorithm lost the point #%d\n", i );
  148. code = cvtest::TS::FAIL_BAD_ACCURACY;
  149. goto _exit_;
  150. }
  151. }
  152. }
  153. if( pt_exceed > bad_points_max )
  154. {
  155. ts->printf( cvtest::TS::LOG,
  156. "The number of poorly tracked points is too big (>=%d)\n", pt_exceed );
  157. code = cvtest::TS::FAIL_BAD_ACCURACY;
  158. goto _exit_;
  159. }
  160. if( max_err > 1 )
  161. {
  162. ts->printf( cvtest::TS::LOG, "Maximum tracking error is too big (=%g) at %d\n", max_err, merr_i );
  163. code = cvtest::TS::FAIL_BAD_ACCURACY;
  164. goto _exit_;
  165. }
  166. _exit_:
  167. cvFree( &status );
  168. cvReleaseMat( &_u );
  169. cvReleaseMat( &_v );
  170. cvReleaseMat( &_v2 );
  171. cvReleaseImage( &imgI );
  172. cvReleaseImage( &imgJ );
  173. if( code < 0 )
  174. ts->set_failed_test_info( code );
  175. }
  176. TEST(Video_OpticalFlowPyrLK, accuracy) { CV_OptFlowPyrLKTest test; test.safe_run(); }
  177. TEST(Video_OpticalFlowPyrLK, submat)
  178. {
  179. // see bug #2075
  180. std::string path = cvtest::TS::ptr()->get_data_path() + "../cv/shared/lena.png";
  181. cv::Mat lenaImg = cv::imread(path);
  182. ASSERT_FALSE(lenaImg.empty());
  183. cv::Mat wholeImage;
  184. cv::resize(lenaImg, wholeImage, cv::Size(1024, 1024));
  185. cv::Mat img1 = wholeImage(cv::Rect(0, 0, 640, 360)).clone();
  186. cv::Mat img2 = wholeImage(cv::Rect(40, 60, 640, 360));
  187. std::vector<uchar> status;
  188. std::vector<float> error;
  189. std::vector<cv::Point2f> prev;
  190. std::vector<cv::Point2f> next;
  191. cv::RNG rng(123123);
  192. for(int i = 0; i < 50; ++i)
  193. {
  194. int x = rng.uniform(0, 640);
  195. int y = rng.uniform(0, 360);
  196. prev.push_back(cv::Point2f((float)x, (float)y));
  197. }
  198. ASSERT_NO_THROW(cv::calcOpticalFlowPyrLK(img1, img2, prev, next, status, error));
  199. }