test_camshift.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. #include "opencv2/video/tracking.hpp"
  43. namespace opencv_test { namespace {
  44. using namespace cv;
  45. class CV_TrackBaseTest : public cvtest::BaseTest
  46. {
  47. public:
  48. CV_TrackBaseTest();
  49. virtual ~CV_TrackBaseTest();
  50. void clear();
  51. protected:
  52. int read_params( const cv::FileStorage& fs );
  53. void run_func(void);
  54. int prepare_test_case( int test_case_idx );
  55. int validate_test_results( int test_case_idx );
  56. void generate_object();
  57. int min_log_size, max_log_size;
  58. Mat img;
  59. RotatedRect box0;
  60. Size img_size;
  61. TermCriteria criteria;
  62. int img_type;
  63. };
  64. CV_TrackBaseTest::CV_TrackBaseTest()
  65. {
  66. img = 0;
  67. test_case_count = 100;
  68. min_log_size = 5;
  69. max_log_size = 8;
  70. }
  71. CV_TrackBaseTest::~CV_TrackBaseTest()
  72. {
  73. clear();
  74. }
  75. void CV_TrackBaseTest::clear()
  76. {
  77. img.release();
  78. cvtest::BaseTest::clear();
  79. }
  80. int CV_TrackBaseTest::read_params( const cv::FileStorage& fs )
  81. {
  82. int code = cvtest::BaseTest::read_params( fs );
  83. if( code < 0 )
  84. return code;
  85. read( find_param( fs, "test_case_count" ), test_case_count, test_case_count );
  86. read( find_param( fs, "min_log_size" ), min_log_size, min_log_size );
  87. read( find_param( fs, "max_log_size" ), max_log_size, max_log_size );
  88. min_log_size = cvtest::clipInt( min_log_size, 1, 10 );
  89. max_log_size = cvtest::clipInt( max_log_size, 1, 10 );
  90. if( min_log_size > max_log_size )
  91. {
  92. std::swap( min_log_size, max_log_size );
  93. }
  94. return 0;
  95. }
  96. void CV_TrackBaseTest::generate_object()
  97. {
  98. int x, y;
  99. double cx = box0.center.x;
  100. double cy = box0.center.y;
  101. double width = box0.size.width*0.5;
  102. double height = box0.size.height*0.5;
  103. double angle = box0.angle*CV_PI/180.;
  104. double a = sin(angle), b = -cos(angle);
  105. double inv_ww = 1./(width*width), inv_hh = 1./(height*height);
  106. img = Mat::zeros( img_size.height, img_size.width, img_type );
  107. // use the straightforward algorithm: for every pixel check if it is inside the ellipse
  108. for( y = 0; y < img_size.height; y++ )
  109. {
  110. uchar* ptr = img.ptr(y);
  111. float* fl = (float*)ptr;
  112. double x_ = (y - cy)*b, y_ = (y - cy)*a;
  113. for( x = 0; x < img_size.width; x++ )
  114. {
  115. double x1 = (x - cx)*a - x_;
  116. double y1 = (x - cx)*b + y_;
  117. if( x1*x1*inv_hh + y1*y1*inv_ww <= 1. )
  118. {
  119. if( img_type == CV_8U )
  120. ptr[x] = (uchar)1;
  121. else
  122. fl[x] = (float)1.f;
  123. }
  124. }
  125. }
  126. }
  127. int CV_TrackBaseTest::prepare_test_case( int test_case_idx )
  128. {
  129. RNG& rng = ts->get_rng();
  130. cvtest::BaseTest::prepare_test_case( test_case_idx );
  131. float m;
  132. clear();
  133. box0.size.width = (float)exp((cvtest::randReal(rng) * (max_log_size - min_log_size) + min_log_size)*CV_LOG2);
  134. box0.size.height = (float)exp((cvtest::randReal(rng) * (max_log_size - min_log_size) + min_log_size)*CV_LOG2);
  135. box0.angle = (float)(cvtest::randReal(rng)*180.);
  136. if( box0.size.width > box0.size.height )
  137. {
  138. std::swap( box0.size.width, box0.size.height );
  139. }
  140. m = MAX( box0.size.width, box0.size.height );
  141. img_size.width = cvRound(cvtest::randReal(rng)*m*0.5 + m + 1);
  142. img_size.height = cvRound(cvtest::randReal(rng)*m*0.5 + m + 1);
  143. img_type = cvtest::randInt(rng) % 2 ? CV_32F : CV_8U;
  144. img_type = CV_8U;
  145. box0.center.x = (float)(img_size.width*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.width - m));
  146. box0.center.y = (float)(img_size.height*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.height - m));
  147. criteria = TermCriteria( TermCriteria::EPS + TermCriteria::MAX_ITER, 10, 0.1 );
  148. generate_object();
  149. return 1;
  150. }
  151. void CV_TrackBaseTest::run_func(void)
  152. {
  153. }
  154. int CV_TrackBaseTest::validate_test_results( int /*test_case_idx*/ )
  155. {
  156. return 0;
  157. }
  158. ///////////////////////// CamShift //////////////////////////////
  159. class CV_CamShiftTest : public CV_TrackBaseTest
  160. {
  161. public:
  162. CV_CamShiftTest();
  163. protected:
  164. void run_func(void);
  165. int prepare_test_case( int test_case_idx );
  166. int validate_test_results( int test_case_idx );
  167. void generate_object();
  168. RotatedRect box;
  169. Rect init_rect;
  170. int area0;
  171. };
  172. CV_CamShiftTest::CV_CamShiftTest()
  173. {
  174. }
  175. int CV_CamShiftTest::prepare_test_case( int test_case_idx )
  176. {
  177. RNG& rng = ts->get_rng();
  178. double m;
  179. int code = CV_TrackBaseTest::prepare_test_case( test_case_idx );
  180. int i, area;
  181. if( code <= 0 )
  182. return code;
  183. area0 = countNonZero(img);
  184. for(i = 0; i < 100; i++)
  185. {
  186. m = MAX(box0.size.width,box0.size.height)*0.8;
  187. init_rect.x = cvFloor(box0.center.x - m*(0.45 + cvtest::randReal(rng)*0.2));
  188. init_rect.y = cvFloor(box0.center.y - m*(0.45 + cvtest::randReal(rng)*0.2));
  189. init_rect.width = cvCeil(box0.center.x + m*(0.45 + cvtest::randReal(rng)*0.2) - init_rect.x);
  190. init_rect.height = cvCeil(box0.center.y + m*(0.45 + cvtest::randReal(rng)*0.2) - init_rect.y);
  191. if( init_rect.x < 0 || init_rect.y < 0 ||
  192. init_rect.x + init_rect.width >= img_size.width ||
  193. init_rect.y + init_rect.height >= img_size.height )
  194. continue;
  195. Mat temp = img(init_rect);
  196. area = countNonZero( temp );
  197. if( area >= 0.1*area0 )
  198. break;
  199. }
  200. return i < 100 ? code : 0;
  201. }
  202. void CV_CamShiftTest::run_func(void)
  203. {
  204. box = CamShift( img, init_rect, criteria );
  205. }
  206. int CV_CamShiftTest::validate_test_results( int /*test_case_idx*/ )
  207. {
  208. int code = cvtest::TS::OK;
  209. double m = MAX(box0.size.width, box0.size.height);
  210. double diff_angle;
  211. if( cvIsNaN(box.size.width) || cvIsInf(box.size.width) || box.size.width <= 0 ||
  212. cvIsNaN(box.size.height) || cvIsInf(box.size.height) || box.size.height <= 0 ||
  213. cvIsNaN(box.center.x) || cvIsInf(box.center.x) ||
  214. cvIsNaN(box.center.y) || cvIsInf(box.center.y) ||
  215. cvIsNaN(box.angle) || cvIsInf(box.angle) || box.angle < -180 || box.angle > 180 )
  216. {
  217. ts->printf( cvtest::TS::LOG, "Invalid RotatedRect was returned by CamShift\n" );
  218. code = cvtest::TS::FAIL_INVALID_OUTPUT;
  219. goto _exit_;
  220. }
  221. box.angle = (float)(180 - box.angle);
  222. if( fabs(box.size.width - box0.size.width) > box0.size.width*0.2 ||
  223. fabs(box.size.height - box0.size.height) > box0.size.height*0.3 )
  224. {
  225. ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D size (=%.1f x %.1f, should be %.1f x %.1f)\n",
  226. box.size.width, box.size.height, box0.size.width, box0.size.height );
  227. code = cvtest::TS::FAIL_BAD_ACCURACY;
  228. goto _exit_;
  229. }
  230. if( fabs(box.center.x - box0.center.x) > m*0.1 ||
  231. fabs(box.center.y - box0.center.y) > m*0.1 )
  232. {
  233. ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D position (=(%.1f, %.1f), should be (%.1f, %.1f))\n",
  234. box.center.x, box.center.y, box0.center.x, box0.center.y );
  235. code = cvtest::TS::FAIL_BAD_ACCURACY;
  236. goto _exit_;
  237. }
  238. if( box.angle < 0 )
  239. box.angle += 180;
  240. diff_angle = fabs(box0.angle - box.angle);
  241. diff_angle = MIN( diff_angle, fabs(box0.angle - box.angle + 180));
  242. if( fabs(diff_angle) > 30 && box0.size.height > box0.size.width*1.2 )
  243. {
  244. ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D angle (=%1.f, should be %1.f)\n",
  245. box.angle, box0.angle );
  246. code = cvtest::TS::FAIL_BAD_ACCURACY;
  247. goto _exit_;
  248. }
  249. _exit_:
  250. if( code < 0 )
  251. {
  252. ts->set_failed_test_info( code );
  253. }
  254. return code;
  255. }
  256. ///////////////////////// MeanShift //////////////////////////////
  257. class CV_MeanShiftTest : public CV_TrackBaseTest
  258. {
  259. public:
  260. CV_MeanShiftTest();
  261. protected:
  262. void run_func(void);
  263. int prepare_test_case( int test_case_idx );
  264. int validate_test_results( int test_case_idx );
  265. void generate_object();
  266. Rect init_rect, rect;
  267. int area0, area;
  268. };
  269. CV_MeanShiftTest::CV_MeanShiftTest()
  270. {
  271. }
  272. int CV_MeanShiftTest::prepare_test_case( int test_case_idx )
  273. {
  274. RNG& rng = ts->get_rng();
  275. double m;
  276. int code = CV_TrackBaseTest::prepare_test_case( test_case_idx );
  277. int i;
  278. if( code <= 0 )
  279. return code;
  280. area0 = countNonZero(img);
  281. for(i = 0; i < 100; i++)
  282. {
  283. m = (box0.size.width + box0.size.height)*0.5;
  284. init_rect.x = cvFloor(box0.center.x - m*(0.4 + cvtest::randReal(rng)*0.2));
  285. init_rect.y = cvFloor(box0.center.y - m*(0.4 + cvtest::randReal(rng)*0.2));
  286. init_rect.width = cvCeil(box0.center.x + m*(0.4 + cvtest::randReal(rng)*0.2) - init_rect.x);
  287. init_rect.height = cvCeil(box0.center.y + m*(0.4 + cvtest::randReal(rng)*0.2) - init_rect.y);
  288. if( init_rect.x < 0 || init_rect.y < 0 ||
  289. init_rect.x + init_rect.width >= img_size.width ||
  290. init_rect.y + init_rect.height >= img_size.height )
  291. continue;
  292. Mat temp = img(init_rect);
  293. area = countNonZero( temp );
  294. if( area >= 0.5*area0 )
  295. break;
  296. }
  297. return i < 100 ? code : 0;
  298. }
  299. void CV_MeanShiftTest::run_func(void)
  300. {
  301. rect = init_rect;
  302. meanShift( img, rect, criteria );
  303. }
  304. int CV_MeanShiftTest::validate_test_results( int /*test_case_idx*/ )
  305. {
  306. int code = cvtest::TS::OK;
  307. Point2f c;
  308. double m = MAX(box0.size.width, box0.size.height), delta;
  309. c.x = (float)(rect.x + rect.width*0.5);
  310. c.y = (float)(rect.y + rect.height*0.5);
  311. if( fabs(c.x - box0.center.x) > m*0.1 ||
  312. fabs(c.y - box0.center.y) > m*0.1 )
  313. {
  314. ts->printf( cvtest::TS::LOG, "Incorrect CvBox2D position (=(%.1f, %.1f), should be (%.1f, %.1f))\n",
  315. c.x, c.y, box0.center.x, box0.center.y );
  316. code = cvtest::TS::FAIL_BAD_ACCURACY;
  317. goto _exit_;
  318. }
  319. delta = m*0.7;
  320. if( rect.x < box0.center.x - delta ||
  321. rect.y < box0.center.y - delta ||
  322. rect.x + rect.width > box0.center.x + delta ||
  323. rect.y + rect.height > box0.center.y + delta )
  324. {
  325. ts->printf( cvtest::TS::LOG,
  326. "Incorrect ConnectedComp ((%d,%d,%d,%d) is not within (%.1f,%.1f,%.1f,%.1f))\n",
  327. rect.x, rect.y, rect.x + rect.width, rect.y + rect.height,
  328. box0.center.x - delta, box0.center.y - delta, box0.center.x + delta, box0.center.y + delta );
  329. code = cvtest::TS::FAIL_BAD_ACCURACY;
  330. }
  331. _exit_:
  332. if( code < 0 )
  333. {
  334. ts->set_failed_test_info( code );
  335. }
  336. return code;
  337. }
  338. TEST(Video_CAMShift, accuracy) { CV_CamShiftTest test; test.safe_run(); }
  339. TEST(Video_MeanShift, accuracy) { CV_MeanShiftTest test; test.safe_run(); }
  340. }} // namespace
  341. /* End of file. */