test_moments.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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/ts/ocl_test.hpp"
  43. namespace opencv_test { namespace {
  44. #define OCL_TUNING_MODE 0
  45. #if OCL_TUNING_MODE
  46. #define OCL_TUNING_MODE_ONLY(code) code
  47. #else
  48. #define OCL_TUNING_MODE_ONLY(code)
  49. #endif
  50. // image moments
  51. class CV_MomentsTest : public cvtest::ArrayTest
  52. {
  53. public:
  54. CV_MomentsTest(bool try_umat);
  55. protected:
  56. enum { MOMENT_COUNT = 25 };
  57. int prepare_test_case( int test_case_idx );
  58. void prepare_to_validation( int /*test_case_idx*/ );
  59. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  60. void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
  61. double get_success_error_level( int test_case_idx, int i, int j );
  62. void run_func();
  63. bool is_binary;
  64. bool try_umat_;
  65. };
  66. CV_MomentsTest::CV_MomentsTest(bool try_umat) :
  67. try_umat_(try_umat)
  68. {
  69. test_array[INPUT].push_back(NULL);
  70. test_array[OUTPUT].push_back(NULL);
  71. test_array[REF_OUTPUT].push_back(NULL);
  72. is_binary = false;
  73. OCL_TUNING_MODE_ONLY(test_case_count = 10);
  74. //element_wise_relative_error = false;
  75. }
  76. void CV_MomentsTest::get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high )
  77. {
  78. cvtest::ArrayTest::get_minmax_bounds( i, j, type, low, high );
  79. int depth = CV_MAT_DEPTH(type);
  80. if( depth == CV_16U )
  81. {
  82. low = Scalar::all(0);
  83. high = Scalar::all(1000);
  84. }
  85. else if( depth == CV_16S )
  86. {
  87. low = Scalar::all(-1000);
  88. high = Scalar::all(1000);
  89. }
  90. else if( depth == CV_32F )
  91. {
  92. low = Scalar::all(-1);
  93. high = Scalar::all(1);
  94. }
  95. }
  96. void CV_MomentsTest::get_test_array_types_and_sizes( int test_case_idx,
  97. vector<vector<Size> >& sizes, vector<vector<int> >& types )
  98. {
  99. RNG& rng = ts->get_rng();
  100. cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  101. int depth = cvtest::randInt(rng) % 4;
  102. depth = depth == 0 ? CV_8U : depth == 1 ? CV_16U : depth == 2 ? CV_16S : CV_32F;
  103. is_binary = cvtest::randInt(rng) % 2 != 0;
  104. OCL_TUNING_MODE_ONLY(
  105. depth = CV_8U;
  106. is_binary = false;
  107. sizes[INPUT][0] = Size(1024,768)
  108. );
  109. types[INPUT][0] = CV_MAKETYPE(depth, 1);
  110. types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_64FC1;
  111. sizes[OUTPUT][0] = sizes[REF_OUTPUT][0] = cvSize(MOMENT_COUNT,1);
  112. if(CV_MAT_DEPTH(types[INPUT][0])>=CV_32S)
  113. sizes[INPUT][0].width = MAX(sizes[INPUT][0].width, 3);
  114. cvmat_allowed = true;
  115. }
  116. double CV_MomentsTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  117. {
  118. int depth = test_mat[INPUT][0].depth();
  119. return depth != CV_32F ? FLT_EPSILON*10 : FLT_EPSILON*100;
  120. }
  121. int CV_MomentsTest::prepare_test_case( int test_case_idx )
  122. {
  123. int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
  124. return code;
  125. }
  126. void CV_MomentsTest::run_func()
  127. {
  128. CvMoments* m = (CvMoments*)test_mat[OUTPUT][0].ptr<double>();
  129. double* others = (double*)(m + 1);
  130. if (try_umat_)
  131. {
  132. UMat u;
  133. test_mat[INPUT][0].clone().copyTo(u);
  134. OCL_TUNING_MODE_ONLY(
  135. static double ttime = 0;
  136. static int ncalls = 0;
  137. moments(u, is_binary != 0);
  138. double t = (double)getTickCount());
  139. Moments new_m = moments(u, is_binary != 0);
  140. OCL_TUNING_MODE_ONLY(
  141. ttime += (double)getTickCount() - t;
  142. ncalls++;
  143. printf("%g\n", ttime/ncalls/u.total()));
  144. *m = cvMoments(new_m);
  145. }
  146. else
  147. cvMoments( test_array[INPUT][0], m, is_binary );
  148. others[0] = cvGetNormalizedCentralMoment( m, 2, 0 );
  149. others[1] = cvGetNormalizedCentralMoment( m, 1, 1 );
  150. others[2] = cvGetNormalizedCentralMoment( m, 0, 2 );
  151. others[3] = cvGetNormalizedCentralMoment( m, 3, 0 );
  152. others[4] = cvGetNormalizedCentralMoment( m, 2, 1 );
  153. others[5] = cvGetNormalizedCentralMoment( m, 1, 2 );
  154. others[6] = cvGetNormalizedCentralMoment( m, 0, 3 );
  155. }
  156. void CV_MomentsTest::prepare_to_validation( int /*test_case_idx*/ )
  157. {
  158. Mat& src = test_mat[INPUT][0];
  159. CvMoments m = cvMoments();
  160. double* mdata = test_mat[REF_OUTPUT][0].ptr<double>();
  161. int depth = src.depth();
  162. int cn = src.channels();
  163. int i, y, x, cols = src.cols;
  164. double xc = 0., yc = 0.;
  165. int coi = 0;
  166. for( y = 0; y < src.rows; y++ )
  167. {
  168. double s0 = 0, s1 = 0, s2 = 0, s3 = 0;
  169. uchar* ptr = src.ptr(y);
  170. for( x = 0; x < cols; x++ )
  171. {
  172. double val;
  173. if( depth == CV_8U )
  174. val = ptr[x*cn + coi];
  175. else if( depth == CV_16U )
  176. val = ((ushort*)ptr)[x*cn + coi];
  177. else if( depth == CV_16S )
  178. val = ((short*)ptr)[x*cn + coi];
  179. else
  180. val = ((float*)ptr)[x*cn + coi];
  181. if( is_binary )
  182. val = val != 0;
  183. s0 += val;
  184. s1 += val*x;
  185. s2 += val*x*x;
  186. s3 += ((val*x)*x)*x;
  187. }
  188. m.m00 += s0;
  189. m.m01 += s0*y;
  190. m.m02 += (s0*y)*y;
  191. m.m03 += ((s0*y)*y)*y;
  192. m.m10 += s1;
  193. m.m11 += s1*y;
  194. m.m12 += (s1*y)*y;
  195. m.m20 += s2;
  196. m.m21 += s2*y;
  197. m.m30 += s3;
  198. }
  199. if( m.m00 != 0 )
  200. {
  201. xc = m.m10/m.m00, yc = m.m01/m.m00;
  202. m.inv_sqrt_m00 = 1./sqrt(fabs(m.m00));
  203. }
  204. for( y = 0; y < src.rows; y++ )
  205. {
  206. double s0 = 0, s1 = 0, s2 = 0, s3 = 0, y1 = y - yc;
  207. uchar* ptr = src.ptr(y);
  208. for( x = 0; x < cols; x++ )
  209. {
  210. double val, x1 = x - xc;
  211. if( depth == CV_8U )
  212. val = ptr[x*cn + coi];
  213. else if( depth == CV_16U )
  214. val = ((ushort*)ptr)[x*cn + coi];
  215. else if( depth == CV_16S )
  216. val = ((short*)ptr)[x*cn + coi];
  217. else
  218. val = ((float*)ptr)[x*cn + coi];
  219. if( is_binary )
  220. val = val != 0;
  221. s0 += val;
  222. s1 += val*x1;
  223. s2 += val*x1*x1;
  224. s3 += ((val*x1)*x1)*x1;
  225. }
  226. m.mu02 += s0*y1*y1;
  227. m.mu03 += ((s0*y1)*y1)*y1;
  228. m.mu11 += s1*y1;
  229. m.mu12 += (s1*y1)*y1;
  230. m.mu20 += s2;
  231. m.mu21 += s2*y1;
  232. m.mu30 += s3;
  233. }
  234. memcpy( mdata, &m, sizeof(m));
  235. mdata += sizeof(m)/sizeof(m.m00);
  236. /* calc normalized moments */
  237. {
  238. double inv_m00 = m.inv_sqrt_m00*m.inv_sqrt_m00;
  239. double s2 = inv_m00*inv_m00; /* 1./(m00 ^ (2/2 + 1)) */
  240. double s3 = s2*m.inv_sqrt_m00; /* 1./(m00 ^ (3/2 + 1)) */
  241. mdata[0] = m.mu20 * s2;
  242. mdata[1] = m.mu11 * s2;
  243. mdata[2] = m.mu02 * s2;
  244. mdata[3] = m.mu30 * s3;
  245. mdata[4] = m.mu21 * s3;
  246. mdata[5] = m.mu12 * s3;
  247. mdata[6] = m.mu03 * s3;
  248. }
  249. double* a = test_mat[REF_OUTPUT][0].ptr<double>();
  250. double* b = test_mat[OUTPUT][0].ptr<double>();
  251. for( i = 0; i < MOMENT_COUNT; i++ )
  252. {
  253. if( fabs(a[i]) < 1e-3 )
  254. a[i] = 0;
  255. if( fabs(b[i]) < 1e-3 )
  256. b[i] = 0;
  257. }
  258. }
  259. // Hu invariants
  260. class CV_HuMomentsTest : public cvtest::ArrayTest
  261. {
  262. public:
  263. CV_HuMomentsTest();
  264. protected:
  265. enum { MOMENT_COUNT = 18, HU_MOMENT_COUNT = 7 };
  266. int prepare_test_case( int test_case_idx );
  267. void prepare_to_validation( int /*test_case_idx*/ );
  268. void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );
  269. void get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high );
  270. double get_success_error_level( int test_case_idx, int i, int j );
  271. void run_func();
  272. };
  273. CV_HuMomentsTest::CV_HuMomentsTest()
  274. {
  275. test_array[INPUT].push_back(NULL);
  276. test_array[OUTPUT].push_back(NULL);
  277. test_array[REF_OUTPUT].push_back(NULL);
  278. }
  279. void CV_HuMomentsTest::get_minmax_bounds( int i, int j, int type, Scalar& low, Scalar& high )
  280. {
  281. cvtest::ArrayTest::get_minmax_bounds( i, j, type, low, high );
  282. low = Scalar::all(-10000);
  283. high = Scalar::all(10000);
  284. }
  285. void CV_HuMomentsTest::get_test_array_types_and_sizes( int test_case_idx,
  286. vector<vector<Size> >& sizes, vector<vector<int> >& types )
  287. {
  288. cvtest::ArrayTest::get_test_array_types_and_sizes( test_case_idx, sizes, types );
  289. types[INPUT][0] = types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_64FC1;
  290. sizes[INPUT][0] = cvSize(MOMENT_COUNT,1);
  291. sizes[OUTPUT][0] = sizes[REF_OUTPUT][0] = cvSize(HU_MOMENT_COUNT,1);
  292. }
  293. double CV_HuMomentsTest::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )
  294. {
  295. return FLT_EPSILON;
  296. }
  297. int CV_HuMomentsTest::prepare_test_case( int test_case_idx )
  298. {
  299. int code = cvtest::ArrayTest::prepare_test_case( test_case_idx );
  300. if( code > 0 )
  301. {
  302. // ...
  303. }
  304. return code;
  305. }
  306. void CV_HuMomentsTest::run_func()
  307. {
  308. cvGetHuMoments( test_mat[INPUT][0].ptr<CvMoments>(),
  309. test_mat[OUTPUT][0].ptr<CvHuMoments>() );
  310. }
  311. void CV_HuMomentsTest::prepare_to_validation( int /*test_case_idx*/ )
  312. {
  313. CvMoments* m = test_mat[INPUT][0].ptr<CvMoments>();
  314. CvHuMoments* hu = test_mat[REF_OUTPUT][0].ptr<CvHuMoments>();
  315. double inv_m00 = m->inv_sqrt_m00*m->inv_sqrt_m00;
  316. double s2 = inv_m00*inv_m00; /* 1./(m00 ^ (2/2 + 1)) */
  317. double s3 = s2*m->inv_sqrt_m00; /* 1./(m00 ^ (3/2 + 1)) */
  318. double nu20 = m->mu20 * s2;
  319. double nu11 = m->mu11 * s2;
  320. double nu02 = m->mu02 * s2;
  321. double nu30 = m->mu30 * s3;
  322. double nu21 = m->mu21 * s3;
  323. double nu12 = m->mu12 * s3;
  324. double nu03 = m->mu03 * s3;
  325. #undef sqr
  326. #define sqr(a) ((a)*(a))
  327. hu->hu1 = nu20 + nu02;
  328. hu->hu2 = sqr(nu20 - nu02) + 4*sqr(nu11);
  329. hu->hu3 = sqr(nu30 - 3*nu12) + sqr(3*nu21 - nu03);
  330. hu->hu4 = sqr(nu30 + nu12) + sqr(nu21 + nu03);
  331. hu->hu5 = (nu30 - 3*nu12)*(nu30 + nu12)*(sqr(nu30 + nu12) - 3*sqr(nu21 + nu03)) +
  332. (3*nu21 - nu03)*(nu21 + nu03)*(3*sqr(nu30 + nu12) - sqr(nu21 + nu03));
  333. hu->hu6 = (nu20 - nu02)*(sqr(nu30 + nu12) - sqr(nu21 + nu03)) +
  334. 4*nu11*(nu30 + nu12)*(nu21 + nu03);
  335. hu->hu7 = (3*nu21 - nu03)*(nu30 + nu12)*(sqr(nu30 + nu12) - 3*sqr(nu21 + nu03)) +
  336. (3*nu12 - nu30)*(nu21 + nu03)*(3*sqr(nu30 + nu12) - sqr(nu21 + nu03));
  337. }
  338. TEST(Imgproc_Moments, accuracy) { CV_MomentsTest test(false); test.safe_run(); }
  339. OCL_TEST(Imgproc_Moments, accuracy) { CV_MomentsTest test(true); test.safe_run(); }
  340. TEST(Imgproc_HuMoments, accuracy) { CV_HuMomentsTest test; test.safe_run(); }
  341. class CV_SmallContourMomentTest : public cvtest::BaseTest
  342. {
  343. public:
  344. CV_SmallContourMomentTest() {}
  345. ~CV_SmallContourMomentTest() {}
  346. protected:
  347. void run(int)
  348. {
  349. try
  350. {
  351. vector<Point> points;
  352. points.push_back(Point(50, 56));
  353. points.push_back(Point(53, 53));
  354. points.push_back(Point(46, 54));
  355. points.push_back(Point(49, 51));
  356. Moments m = moments(points, false);
  357. double area = contourArea(points);
  358. CV_Assert( m.m00 == 0 && m.m01 == 0 && m.m10 == 0 && area == 0 );
  359. }
  360. catch(...)
  361. {
  362. ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
  363. }
  364. }
  365. };
  366. TEST(Imgproc_ContourMoment, small) { CV_SmallContourMomentTest test; test.safe_run(); }
  367. }} // namespace