test_approxpoly.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. //
  44. // TODO!!!:
  45. // check_slice (and/or check) seem(s) to be broken, or this is a bug in function
  46. // (or its inability to handle possible self-intersections in the generated contours).
  47. //
  48. // At least, if // return TotalErrors;
  49. // is uncommented in check_slice, the test fails easily.
  50. // So, now (and it looks like since 0.9.6)
  51. // we only check that the set of vertices of the approximated polygon is
  52. // a subset of vertices of the original contour.
  53. //
  54. class CV_ApproxPolyTest : public cvtest::BaseTest
  55. {
  56. public:
  57. CV_ApproxPolyTest();
  58. ~CV_ApproxPolyTest();
  59. void clear();
  60. //int write_default_params(CvFileStorage* fs);
  61. protected:
  62. //int read_params( const cv::FileStorage& fs );
  63. int check_slice( CvPoint StartPt, CvPoint EndPt,
  64. CvSeqReader* SrcReader, float Eps,
  65. int* j, int Count );
  66. int check( CvSeq* SrcSeq, CvSeq* DstSeq, float Eps );
  67. bool get_contour( int /*type*/, CvSeq** Seq, int* d,
  68. CvMemStorage* storage );
  69. void run(int);
  70. };
  71. CV_ApproxPolyTest::CV_ApproxPolyTest()
  72. {
  73. }
  74. CV_ApproxPolyTest::~CV_ApproxPolyTest()
  75. {
  76. clear();
  77. }
  78. void CV_ApproxPolyTest::clear()
  79. {
  80. cvtest::BaseTest::clear();
  81. }
  82. /*int CV_ApproxPolyTest::write_default_params( CvFileStorage* fs )
  83. {
  84. cvtest::BaseTest::write_default_params( fs );
  85. if( ts->get_testing_mode() != cvtest::TS::TIMING_MODE )
  86. {
  87. write_param( fs, "test_case_count", test_case_count );
  88. }
  89. return 0;
  90. }
  91. int CV_ApproxPolyTest::read_params( const cv::FileStorage& fs )
  92. {
  93. int code = cvtest::BaseTest::read_params( fs );
  94. if( code < 0 )
  95. return code;
  96. test_case_count = cvReadInt( find_param( fs, "test_case_count" ), test_case_count );
  97. min_log_size = cvtest::clipInt( min_log_size, 1, 10 );
  98. return 0;
  99. }*/
  100. bool CV_ApproxPolyTest::get_contour( int /*type*/, CvSeq** Seq, int* d,
  101. CvMemStorage* storage )
  102. {
  103. RNG& rng = ts->get_rng();
  104. int max_x = INT_MIN, max_y = INT_MIN, min_x = INT_MAX, min_y = INT_MAX;
  105. int i;
  106. CvSeq* seq;
  107. int total = cvtest::randInt(rng) % 1000 + 1;
  108. Point center;
  109. int radius, angle;
  110. double deg_to_rad = CV_PI/180.;
  111. Point pt;
  112. center.x = cvtest::randInt( rng ) % 1000;
  113. center.y = cvtest::randInt( rng ) % 1000;
  114. radius = cvtest::randInt( rng ) % 1000;
  115. angle = cvtest::randInt( rng ) % 360;
  116. seq = cvCreateSeq( CV_SEQ_POLYGON, sizeof(CvContour), sizeof(CvPoint), storage );
  117. for( i = 0; i < total; i++ )
  118. {
  119. int d_radius = cvtest::randInt( rng ) % 10 - 5;
  120. int d_angle = 360/total;//cvtest::randInt( rng ) % 10 - 5;
  121. pt.x = cvRound( center.x + radius*cos(angle*deg_to_rad));
  122. pt.y = cvRound( center.x - radius*sin(angle*deg_to_rad));
  123. radius += d_radius;
  124. angle += d_angle;
  125. cvSeqPush( seq, &pt );
  126. max_x = MAX( max_x, pt.x );
  127. max_y = MAX( max_y, pt.y );
  128. min_x = MIN( min_x, pt.x );
  129. min_y = MIN( min_y, pt.y );
  130. }
  131. *d = (max_x - min_x)*(max_x - min_x) + (max_y - min_y)*(max_y - min_y);
  132. *Seq = seq;
  133. return true;
  134. }
  135. int CV_ApproxPolyTest::check_slice( CvPoint StartPt, CvPoint EndPt,
  136. CvSeqReader* SrcReader, float Eps,
  137. int* _j, int Count )
  138. {
  139. ///////////
  140. Point Pt;
  141. ///////////
  142. bool flag;
  143. double dy,dx;
  144. double A,B,C;
  145. double Sq;
  146. double sin_a = 0;
  147. double cos_a = 0;
  148. double d = 0;
  149. double dist;
  150. ///////////
  151. int j, TotalErrors = 0;
  152. ////////////////////////////////
  153. if( SrcReader == NULL )
  154. {
  155. CV_Assert( false );
  156. return 0;
  157. }
  158. ///////// init line ////////////
  159. flag = true;
  160. dx = (double)StartPt.x - (double)EndPt.x;
  161. dy = (double)StartPt.y - (double)EndPt.y;
  162. if( ( dx == 0 ) && ( dy == 0 ) ) flag = false;
  163. else
  164. {
  165. A = -dy;
  166. B = dx;
  167. C = dy * (double)StartPt.x - dx * (double)StartPt.y;
  168. Sq = sqrt( A*A + B*B );
  169. sin_a = B/Sq;
  170. cos_a = A/Sq;
  171. d = C/Sq;
  172. }
  173. /////// find start point and check distance ////////
  174. for( j = *_j; j < Count; j++ )
  175. {
  176. { CvPoint pt_ = CV_STRUCT_INITIALIZER; CV_READ_SEQ_ELEM(pt_, *SrcReader); Pt = pt_; }
  177. if( StartPt.x == Pt.x && StartPt.y == Pt.y ) break;
  178. else
  179. {
  180. if( flag ) dist = sin_a * Pt.y + cos_a * Pt.x - d;
  181. else dist = sqrt( (double)(EndPt.y - Pt.y)*(EndPt.y - Pt.y) + (EndPt.x - Pt.x)*(EndPt.x - Pt.x) );
  182. if( dist > Eps ) TotalErrors++;
  183. }
  184. }
  185. *_j = j;
  186. //return TotalErrors;
  187. return 0;
  188. }
  189. int CV_ApproxPolyTest::check( CvSeq* SrcSeq, CvSeq* DstSeq, float Eps )
  190. {
  191. //////////
  192. CvSeqReader DstReader;
  193. CvSeqReader SrcReader;
  194. CvPoint StartPt = {0, 0}, EndPt = {0, 0};
  195. ///////////
  196. int TotalErrors = 0;
  197. ///////////
  198. int Count;
  199. int i,j;
  200. CV_Assert( SrcSeq && DstSeq );
  201. ////////// init ////////////////////
  202. Count = SrcSeq->total;
  203. cvStartReadSeq( DstSeq, &DstReader, 0 );
  204. cvStartReadSeq( SrcSeq, &SrcReader, 0 );
  205. CV_READ_SEQ_ELEM( StartPt, DstReader );
  206. for( i = 0 ; i < Count ; )
  207. {
  208. CV_READ_SEQ_ELEM( EndPt, SrcReader );
  209. i++;
  210. if( StartPt.x == EndPt.x && StartPt.y == EndPt.y ) break;
  211. }
  212. ///////// start ////////////////
  213. for( i = 1, j = 0 ; i <= DstSeq->total ; )
  214. {
  215. ///////// read slice ////////////
  216. EndPt.x = StartPt.x;
  217. EndPt.y = StartPt.y;
  218. CV_READ_SEQ_ELEM( StartPt, DstReader );
  219. i++;
  220. TotalErrors += check_slice( StartPt, EndPt, &SrcReader, Eps, &j, Count );
  221. if( j > Count )
  222. {
  223. TotalErrors++;
  224. return TotalErrors;
  225. } //if( !flag )
  226. } // for( int i = 0 ; i < DstSeq->total ; i++ )
  227. return TotalErrors;
  228. }
  229. //extern CvTestContourGenerator cvTsTestContours[];
  230. void CV_ApproxPolyTest::run( int /*start_from*/ )
  231. {
  232. int code = cvtest::TS::OK;
  233. CvMemStorage* storage = 0;
  234. ////////////// Variables ////////////////
  235. int IntervalsCount = 10;
  236. ///////////
  237. //CvTestContourGenerator Cont;
  238. CvSeq* SrcSeq = NULL;
  239. CvSeq* DstSeq;
  240. int iDiam;
  241. float dDiam, Eps, EpsStep;
  242. for( int i = 0; i < 30; i++ )
  243. {
  244. CvMemStoragePos pos;
  245. ts->update_context( this, i, false );
  246. ///////////////////// init contour /////////
  247. dDiam = 0;
  248. while( sqrt(dDiam) / IntervalsCount == 0 )
  249. {
  250. if( storage != 0 )
  251. cvReleaseMemStorage(&storage);
  252. storage = cvCreateMemStorage( 0 );
  253. if( get_contour( 0, &SrcSeq, &iDiam, storage ) )
  254. dDiam = (float)iDiam;
  255. }
  256. dDiam = (float)sqrt( dDiam );
  257. storage = SrcSeq->storage;
  258. ////////////////// test /////////////
  259. EpsStep = dDiam / IntervalsCount ;
  260. for( Eps = EpsStep ; Eps < dDiam ; Eps += EpsStep )
  261. {
  262. cvSaveMemStoragePos( storage, &pos );
  263. ////////// call function ////////////
  264. DstSeq = cvApproxPoly( SrcSeq, SrcSeq->header_size, storage,
  265. CV_POLY_APPROX_DP, Eps );
  266. if( DstSeq == NULL )
  267. {
  268. ts->printf( cvtest::TS::LOG,
  269. "cvApproxPoly returned NULL for contour #%d, epsilon = %g\n", i, Eps );
  270. code = cvtest::TS::FAIL_INVALID_OUTPUT;
  271. goto _exit_;
  272. } // if( DstSeq == NULL )
  273. code = check( SrcSeq, DstSeq, Eps );
  274. if( code != 0 )
  275. {
  276. ts->printf( cvtest::TS::LOG,
  277. "Incorrect result for the contour #%d approximated with epsilon=%g\n", i, Eps );
  278. code = cvtest::TS::FAIL_BAD_ACCURACY;
  279. goto _exit_;
  280. }
  281. cvRestoreMemStoragePos( storage, &pos );
  282. } // for( Eps = EpsStep ; Eps <= Diam ; Eps += EpsStep )
  283. ///////////// free memory ///////////////////
  284. cvReleaseMemStorage(&storage);
  285. } // for( int i = 0; NULL != ( Cont = Contours[i] ) ; i++ )
  286. _exit_:
  287. cvReleaseMemStorage(&storage);
  288. if( code < 0 )
  289. ts->set_failed_test_info( code );
  290. }
  291. TEST(Imgproc_ApproxPoly, accuracy) { CV_ApproxPolyTest test; test.safe_run(); }
  292. //Tests to make sure that unreasonable epsilon (error)
  293. //values never get passed to the Douglas-Peucker algorithm.
  294. TEST(Imgproc_ApproxPoly, bad_epsilon)
  295. {
  296. std::vector<Point2f> inputPoints;
  297. inputPoints.push_back(Point2f(0.0f, 0.0f));
  298. std::vector<Point2f> outputPoints;
  299. double eps = std::numeric_limits<double>::infinity();
  300. ASSERT_ANY_THROW(approxPolyDP(inputPoints, outputPoints, eps, false));
  301. eps = 9e99;
  302. ASSERT_ANY_THROW(approxPolyDP(inputPoints, outputPoints, eps, false));
  303. eps = -1e-6;
  304. ASSERT_ANY_THROW(approxPolyDP(inputPoints, outputPoints, eps, false));
  305. eps = NAN;
  306. ASSERT_ANY_THROW(approxPolyDP(inputPoints, outputPoints, eps, false));
  307. }
  308. }} // namespace