1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "test_precomp.hpp"
44 namespace opencv_test { namespace {
47 cvTsPointConvexPolygon( CvPoint2D32f pt, CvPoint2D32f* v, int n )
49 CvPoint2D32f v0 = v[n-1];
52 for( i = 0; i < n; i++ )
54 CvPoint2D32f v1 = v[i];
55 float dx = pt.x - v0.x, dy = pt.y - v0.y;
56 float dx1 = v1.x - v0.x, dy1 = v1.y - v0.y;
57 double t = (double)dx*dy1 - (double)dx1*dy;
58 if( fabs(t) > DBL_EPSILON )
63 sign = t < 0 ? -1 : 1;
65 else if( fabs(dx) + fabs(dy) < DBL_EPSILON )
70 return i < n ? -1 : 0;
74 cvTsDist( CvPoint2D32f a, CvPoint2D32f b )
76 double dx = a.x - b.x;
77 double dy = a.y - b.y;
78 return sqrt(dx*dx + dy*dy);
81 cvTsDist( const Point2f& a, const Point2f& b )
83 double dx = a.x - b.x;
84 double dy = a.y - b.y;
85 return sqrt(dx*dx + dy*dy);
89 cvTsPtLineDist( CvPoint2D32f pt, CvPoint2D32f a, CvPoint2D32f b )
91 double d0 = cvTsDist( pt, a ), d1;
92 double dd = cvTsDist( a, b );
93 if( dd < FLT_EPSILON )
95 d1 = cvTsDist( pt, b );
96 dd = fabs((double)(pt.x - a.x)*(b.y - a.y) - (double)(pt.y - a.y)*(b.x - a.x))/dd;
102 cvTsPointPolygonTest( CvPoint2D32f pt, const CvPoint2D32f* vv, int n, int* _idx=0, int* _on_edge=0 )
105 Point2f v = vv[n-1], v0;
106 double min_dist_num = FLT_MAX, min_dist_denom = 1;
107 int min_dist_idx = -1, min_on_edge = 0;
111 for( i = 0; i < n; i++ )
113 double dx, dy, dx1, dy1, dx2, dy2, dist_num, dist_denom = 1;
114 int on_edge = 0, idx = i;
117 dx = v.x - v0.x; dy = v.y - v0.y;
118 dx1 = pt.x - v0.x; dy1 = pt.y - v0.y;
119 dx2 = pt.x - v.x; dy2 = pt.y - v.y;
121 if( dx2*dx + dy2*dy >= 0 )
122 dist_num = dx2*dx2 + dy2*dy2;
123 else if( dx1*dx + dy1*dy <= 0 )
125 dist_num = dx1*dx1 + dy1*dy1;
127 if( idx < 0 ) idx = n-1;
131 dist_num = (dy1*dx - dx1*dy);
132 dist_num *= dist_num;
133 dist_denom = dx*dx + dy*dy;
137 if( dist_num*min_dist_denom < min_dist_num*dist_denom )
139 min_dist_num = dist_num;
140 min_dist_denom = dist_denom;
142 min_on_edge = on_edge;
143 if( min_dist_num == 0 )
147 if( (v0.y <= pt.y && v.y <= pt.y) ||
148 (v0.y > pt.y && v.y > pt.y) ||
149 (v0.x < pt.x && v.x < pt.x) )
152 dist_num = dy1*dx - dx1*dy;
154 dist_num = -dist_num;
155 counter += dist_num > 0;
158 result = sqrt(min_dist_num/min_dist_denom);
159 if( counter % 2 == 0 )
163 *_idx = min_dist_idx;
165 *_on_edge = min_on_edge;
171 cvTsMiddlePoint(const cv::Point2f &a, const cv::Point2f &b)
173 return cv::Point2f((a.x + b.x) / 2, (a.y + b.y) / 2);
177 cvTsIsPointOnLineSegment(const cv::Point2f &x, const cv::Point2f &a, const cv::Point2f &b)
179 double d1 = cvTsDist(cvPoint2D32f(x.x, x.y), cvPoint2D32f(a.x, a.y));
180 double d2 = cvTsDist(cvPoint2D32f(x.x, x.y), cvPoint2D32f(b.x, b.y));
181 double d3 = cvTsDist(cvPoint2D32f(a.x, a.y), cvPoint2D32f(b.x, b.y));
183 return (abs(d1 + d2 - d3) <= (1E-5));
187 /****************************************************************************************\
188 * Base class for shape descriptor tests *
189 \****************************************************************************************/
191 class CV_BaseShapeDescrTest : public cvtest::BaseTest
194 CV_BaseShapeDescrTest();
195 virtual ~CV_BaseShapeDescrTest();
199 int read_params( CvFileStorage* fs );
201 int prepare_test_case( int test_case_idx );
202 int validate_test_results( int test_case_idx );
203 virtual void generate_point_set( void* points );
204 virtual void extract_points();
209 bool enable_flt_points;
211 CvMemStorage* storage;
216 double low_high_range;
223 CV_BaseShapeDescrTest::CV_BaseShapeDescrTest()
229 test_case_count = 500;
232 low = high = cvScalarAll(0);
235 enable_flt_points = true;
241 CV_BaseShapeDescrTest::~CV_BaseShapeDescrTest()
247 void CV_BaseShapeDescrTest::clear()
249 cvtest::BaseTest::clear();
250 cvReleaseMemStorage( &storage );
251 cvReleaseMat( &points2 );
257 int CV_BaseShapeDescrTest::read_params( CvFileStorage* fs )
259 int code = cvtest::BaseTest::read_params( fs );
263 test_case_count = cvReadInt( find_param( fs, "struct_count" ), test_case_count );
264 min_log_size = cvReadInt( find_param( fs, "min_log_size" ), min_log_size );
265 max_log_size = cvReadInt( find_param( fs, "max_log_size" ), max_log_size );
267 min_log_size = cvtest::clipInt( min_log_size, 0, 8 );
268 max_log_size = cvtest::clipInt( max_log_size, 0, 10 );
269 if( min_log_size > max_log_size )
272 CV_SWAP( min_log_size, max_log_size, t );
279 void CV_BaseShapeDescrTest::generate_point_set( void* pointsSet )
281 RNG& rng = ts->get_rng();
282 int i, k, n, total, point_type;
287 for( k = 0; k < 4; k++ )
289 a[k] = high.val[k] - low.val[k];
292 memset( &reader, 0, sizeof(reader) );
294 if( CV_IS_SEQ(pointsSet) )
296 CvSeq* ptseq = (CvSeq*)pointsSet;
297 total = ptseq->total;
298 point_type = CV_SEQ_ELTYPE(ptseq);
299 cvStartReadSeq( ptseq, &reader );
303 CvMat* ptm = (CvMat*)pointsSet;
304 assert( CV_IS_MAT(ptm) && CV_IS_MAT_CONT(ptm->type) );
305 total = ptm->rows + ptm->cols - 1;
306 point_type = CV_MAT_TYPE(ptm->type);
307 data = ptm->data.ptr;
310 n = CV_MAT_CN(point_type);
311 point_type = CV_MAT_DEPTH(point_type);
313 assert( (point_type == CV_32S || point_type == CV_32F) && n <= 4 );
315 for( i = 0; i < total; i++ )
321 pi = (int*)reader.ptr;
322 pf = (float*)reader.ptr;
323 CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
327 pi = (int*)data + i*n;
328 pf = (float*)data + i*n;
330 if( point_type == CV_32S )
331 for( k = 0; k < n; k++ )
332 pi[k] = cvRound(cvtest::randReal(rng)*a[k] + b[k]);
334 for( k = 0; k < n; k++ )
335 pf[k] = (float)(cvtest::randReal(rng)*a[k] + b[k]);
340 int CV_BaseShapeDescrTest::prepare_test_case( int test_case_idx )
346 RNG& rng = ts->get_rng();
348 cvtest::BaseTest::prepare_test_case( test_case_idx );
351 size = cvRound( exp((cvtest::randReal(rng) * (max_log_size - min_log_size) + min_log_size)*CV_LOG2) );
352 use_storage = cvtest::randInt(rng) % 2;
353 point_type = CV_MAKETYPE(cvtest::randInt(rng) %
354 (enable_flt_points ? 2 : 1) ? CV_32F : CV_32S, dims);
358 storage = cvCreateMemStorage( (cvtest::randInt(rng)%10 + 1)*1024 );
359 points1 = cvCreateSeq( point_type, sizeof(CvSeq), CV_ELEM_SIZE(point_type), storage );
360 cvSeqPushMulti( points1, 0, size );
365 int rows = 1, cols = size;
366 if( cvtest::randInt(rng) % 2 )
367 rows = size, cols = 1;
369 points2 = cvCreateMat( rows, cols, point_type );
373 for( i = 0; i < 4; i++ )
375 low.val[i] = (cvtest::randReal(rng)-0.5)*low_high_range*2;
376 high.val[i] = (cvtest::randReal(rng)-0.5)*low_high_range*2;
377 if( low.val[i] > high.val[i] )
380 CV_SWAP( low.val[i], high.val[i], t );
382 if( high.val[i] < low.val[i] + 1 )
386 generate_point_set( points );
388 test_cpp = (cvtest::randInt(rng) & 16) == 0;
393 void CV_BaseShapeDescrTest::extract_points()
397 points2 = cvCreateMat( 1, points1->total, CV_SEQ_ELTYPE(points1) );
398 cvCvtSeqToArray( points1, points2->data.ptr );
401 if( CV_MAT_DEPTH(points2->type) != CV_32F && enable_flt_points )
403 CvMat tmp = cvMat( points2->rows, points2->cols,
404 (points2->type & ~CV_MAT_DEPTH_MASK) | CV_32F, points2->data.ptr );
405 cvConvert( points2, &tmp );
410 void CV_BaseShapeDescrTest::run_func(void)
415 int CV_BaseShapeDescrTest::validate_test_results( int /*test_case_idx*/ )
422 /****************************************************************************************\
424 \****************************************************************************************/
426 class CV_ConvHullTest : public CV_BaseShapeDescrTest
430 virtual ~CV_ConvHullTest();
435 int prepare_test_case( int test_case_idx );
436 int validate_test_results( int test_case_idx );
446 CV_ConvHullTest::CV_ConvHullTest()
451 orientation = return_points = 0;
455 CV_ConvHullTest::~CV_ConvHullTest()
461 void CV_ConvHullTest::clear()
463 CV_BaseShapeDescrTest::clear();
464 cvReleaseMat( &hull2 );
470 int CV_ConvHullTest::prepare_test_case( int test_case_idx )
472 int code = CV_BaseShapeDescrTest::prepare_test_case( test_case_idx );
473 int use_storage_for_hull = 0;
474 RNG& rng = ts->get_rng();
479 orientation = cvtest::randInt(rng) % 2 ? CV_CLOCKWISE : CV_COUNTER_CLOCKWISE;
480 return_points = cvtest::randInt(rng) % 2;
482 use_storage_for_hull = (cvtest::randInt(rng) % 2) && !test_cpp;
483 if( use_storage_for_hull )
486 storage = cvCreateMemStorage( (cvtest::randInt(rng)%10 + 1)*1024 );
487 hull_storage = storage;
492 int sz = points1 ? points1->total : points2->cols + points2->rows - 1;
493 int point_type = points1 ? CV_SEQ_ELTYPE(points1) : CV_MAT_TYPE(points2->type);
495 if( cvtest::randInt(rng) % 2 )
500 hull2 = cvCreateMat( rows, cols, return_points ? point_type : CV_32SC1 );
501 hull_storage = hull2;
508 void CV_ConvHullTest::run_func()
511 hull1 = cvConvexHull2( points, hull_storage, orientation, return_points );
514 cv::Mat _points = cv::cvarrToMat(points);
515 bool clockwise = orientation == CV_CLOCKWISE;
519 std::vector<int> _hull;
520 cv::convexHull(_points, _hull, clockwise);
522 memcpy(hull2->data.ptr, &_hull[0], n*sizeof(_hull[0]));
524 else if(_points.type() == CV_32SC2)
526 std::vector<cv::Point> _hull;
527 cv::convexHull(_points, _hull, clockwise);
529 memcpy(hull2->data.ptr, &_hull[0], n*sizeof(_hull[0]));
531 else if(_points.type() == CV_32FC2)
533 std::vector<cv::Point2f> _hull;
534 cv::convexHull(_points, _hull, clockwise);
536 memcpy(hull2->data.ptr, &_hull[0], n*sizeof(_hull[0]));
538 if(hull2->rows > hull2->cols)
539 hull2->rows = (int)n;
541 hull2->cols = (int)n;
546 int CV_ConvHullTest::validate_test_results( int test_case_idx )
548 int code = CV_BaseShapeDescrTest::validate_test_results( test_case_idx );
551 int i, point_count, hull_count;
553 CvSeq header, hheader, *ptseq, *hseq;
554 CvSeqBlock block, hblock;
559 ptseq = cvMakeSeqHeaderForArray( CV_MAT_TYPE(points2->type),
560 sizeof(CvSeq), CV_ELEM_SIZE(points2->type), points2->data.ptr,
561 points2->rows + points2->cols - 1, &header, &block );
562 point_count = ptseq->total;
563 p = (CvPoint2D32f*)(points2->data.ptr);
568 hseq = cvMakeSeqHeaderForArray( CV_MAT_TYPE(hull2->type),
569 sizeof(CvSeq), CV_ELEM_SIZE(hull2->type), hull2->data.ptr,
570 hull2->rows + hull2->cols - 1, &hheader, &hblock );
571 hull_count = hseq->total;
572 hull = cvCreateMat( 1, hull_count, CV_32FC2 );
573 mask = cvCreateMat( 1, hull_count, CV_8UC1 );
575 Mat _mask = cvarrToMat(mask);
577 h = (CvPoint2D32f*)(hull->data.ptr);
579 // extract convex hull points
582 cvCvtSeqToArray( hseq, hull->data.ptr );
583 if( CV_SEQ_ELTYPE(hseq) != CV_32FC2 )
585 CvMat tmp = cvMat( hull->rows, hull->cols, CV_32SC2, hull->data.ptr );
586 cvConvert( &tmp, hull );
592 cvStartReadSeq( hseq, &reader );
594 for( i = 0; i < hull_count; i++ )
596 schar* ptr = reader.ptr;
598 CV_NEXT_SEQ_ELEM( hseq->elem_size, reader );
601 idx = cvSeqElemIdx( ptseq, *(uchar**)ptr );
605 if( idx < 0 || idx >= point_count )
607 ts->printf( cvtest::TS::LOG, "Invalid convex hull point #%d\n", i );
608 code = cvtest::TS::FAIL_INVALID_OUTPUT;
615 // check that the convex hull is a convex polygon
616 if( hull_count >= 3 )
618 CvPoint2D32f pt0 = h[hull_count-1];
619 for( i = 0; i < hull_count; i++ )
622 CvPoint2D32f pt1 = h[i], pt2 = h[j < hull_count ? j : 0];
623 float dx0 = pt1.x - pt0.x, dy0 = pt1.y - pt0.y;
624 float dx1 = pt2.x - pt1.x, dy1 = pt2.y - pt1.y;
625 double t = (double)dx0*dy1 - (double)dx1*dy0;
626 if( (t < 0) ^ (orientation != CV_COUNTER_CLOCKWISE) )
628 ts->printf( cvtest::TS::LOG, "The convex hull is not convex or has a wrong orientation (vtx %d)\n", i );
629 code = cvtest::TS::FAIL_INVALID_OUTPUT;
636 // check that all the points are inside the hull or on the hull edge
637 // and at least hull_point points are at the hull vertices
638 for( i = 0; i < point_count; i++ )
640 int idx = 0, on_edge = 0;
641 double pptresult = cvTsPointPolygonTest( p[i], h, hull_count, &idx, &on_edge );
645 ts->printf( cvtest::TS::LOG, "The point #%d is outside of the convex hull\n", i );
646 code = cvtest::TS::FAIL_BAD_ACCURACY;
650 if( pptresult < FLT_EPSILON && !on_edge )
651 mask->data.ptr[idx] = (uchar)1;
654 if( cvtest::norm( _mask, Mat::zeros(_mask.dims, _mask.size, _mask.type()), NORM_L1 ) != hull_count )
656 ts->printf( cvtest::TS::LOG, "Not every convex hull vertex coincides with some input point\n" );
657 code = cvtest::TS::FAIL_BAD_ACCURACY;
663 cvReleaseMat( &hull );
664 cvReleaseMat( &mask );
666 ts->set_failed_test_info( code );
671 /****************************************************************************************\
673 \****************************************************************************************/
675 class CV_MinAreaRectTest : public CV_BaseShapeDescrTest
678 CV_MinAreaRectTest();
682 int validate_test_results( int test_case_idx );
685 CvPoint2D32f box_pt[4];
689 CV_MinAreaRectTest::CV_MinAreaRectTest()
694 void CV_MinAreaRectTest::run_func()
698 box = cvMinAreaRect2( points, storage );
699 cvBoxPoints( box, box_pt );
703 cv::RotatedRect r = cv::minAreaRect(cv::cvarrToMat(points));
705 r.points((cv::Point2f*)box_pt);
710 int CV_MinAreaRectTest::validate_test_results( int test_case_idx )
713 int code = CV_BaseShapeDescrTest::validate_test_results( test_case_idx );
714 int i, j, point_count = points2->rows + points2->cols - 1;
715 CvPoint2D32f *p = (CvPoint2D32f*)(points2->data.ptr);
716 int mask[] = {0,0,0,0};
718 // check that the bounding box is a rotated rectangle:
719 // 1. diagonals should be equal
720 // 2. they must intersect in their middle points
722 double d0 = cvTsDist( box_pt[0], box_pt[2] );
723 double d1 = cvTsDist( box_pt[1], box_pt[3] );
725 double x0 = (box_pt[0].x + box_pt[2].x)*0.5;
726 double y0 = (box_pt[0].y + box_pt[2].y)*0.5;
727 double x1 = (box_pt[1].x + box_pt[3].x)*0.5;
728 double y1 = (box_pt[1].y + box_pt[3].y)*0.5;
730 if( fabs(d0 - d1) + fabs(x0 - x1) + fabs(y0 - y1) > eps*MAX(d0,d1) )
732 ts->printf( cvtest::TS::LOG, "The bounding box is not a rectangle\n" );
733 code = cvtest::TS::FAIL_INVALID_OUTPUT;
741 double a = 8, c = 8, b = 100, d = 150;
742 CvPoint bp[4], *bpp = bp;
743 cvNamedWindow( "test", 1 );
744 IplImage* img = cvCreateImage( cvSize(500,500), 8, 3 );
746 for( i = 0; i < point_count; i++ )
747 cvCircle(img,cvPoint(cvRound(p[i].x*a+b),cvRound(p[i].y*c+d)), 3, CV_RGB(0,255,0), -1 );
748 for( i = 0; i < n; i++ )
749 bp[i] = cvPoint(cvRound(box_pt[i].x*a+b),cvRound(box_pt[i].y*c+d));
750 cvPolyLine( img, &bpp, &n, 1, 1, CV_RGB(255,255,0), 1, CV_AA, 0 );
751 cvShowImage( "test", img );
753 cvReleaseImage(&img);
757 // check that the box includes all the points
758 // and there is at least one point at (or very close to) every box side
759 for( i = 0; i < point_count; i++ )
761 int idx = 0, on_edge = 0;
762 double pptresult = cvTsPointPolygonTest( p[i], box_pt, 4, &idx, &on_edge );
763 if( pptresult < -eps )
765 ts->printf( cvtest::TS::LOG, "The point #%d is outside of the box\n", i );
766 code = cvtest::TS::FAIL_BAD_ACCURACY;
770 if( pptresult < eps )
772 for( j = 0; j < 4; j++ )
774 double d = cvTsPtLineDist( p[i], box_pt[(j-1)&3], box_pt[j] );
781 if( mask[0] + mask[1] + mask[2] + mask[3] != 4 )
783 ts->printf( cvtest::TS::LOG, "Not every box side has a point nearby\n" );
784 code = cvtest::TS::FAIL_BAD_ACCURACY;
791 ts->set_failed_test_info( code );
796 /****************************************************************************************\
797 * MinEnclosingTriangle Test *
798 \****************************************************************************************/
800 class CV_MinTriangleTest : public CV_BaseShapeDescrTest
803 CV_MinTriangleTest();
807 int validate_test_results( int test_case_idx );
808 std::vector<cv::Point2f> getTriangleMiddlePoints();
810 std::vector<cv::Point2f> convexPolygon;
811 std::vector<cv::Point2f> triangle;
815 CV_MinTriangleTest::CV_MinTriangleTest()
819 std::vector<cv::Point2f> CV_MinTriangleTest::getTriangleMiddlePoints()
821 std::vector<cv::Point2f> triangleMiddlePoints;
823 for (int i = 0; i < 3; i++) {
824 triangleMiddlePoints.push_back(cvTsMiddlePoint(triangle[i], triangle[(i + 1) % 3]));
827 return triangleMiddlePoints;
831 void CV_MinTriangleTest::run_func()
833 std::vector<cv::Point2f> pointsAsVector;
835 cv::cvarrToMat(points).convertTo(pointsAsVector, CV_32F);
837 cv::minEnclosingTriangle(pointsAsVector, triangle);
838 cv::convexHull(pointsAsVector, convexPolygon, true, true);
842 int CV_MinTriangleTest::validate_test_results( int test_case_idx )
844 bool errorEnclosed = false, errorMiddlePoints = false, errorFlush = true;
846 int code = CV_BaseShapeDescrTest::validate_test_results( test_case_idx );
851 double a = 8, c = 8, b = 100, d = 150;
852 CvPoint bp[4], *bpp = bp;
853 cvNamedWindow( "test", 1 );
854 IplImage* img = cvCreateImage( cvSize(500,500), 8, 3 );
856 for( i = 0; i < point_count; i++ )
857 cvCircle(img,cvPoint(cvRound(p[i].x*a+b),cvRound(p[i].y*c+d)), 3, CV_RGB(0,255,0), -1 );
858 for( i = 0; i < n; i++ )
859 bp[i] = cvPoint(cvRound(triangle[i].x*a+b),cvRound(triangle[i].y*c+d));
860 cvPolyLine( img, &bpp, &n, 1, 1, CV_RGB(255,255,0), 1, CV_AA, 0 );
861 cvShowImage( "test", img );
863 cvReleaseImage(&img);
867 int polygonVertices = (int) convexPolygon.size();
869 if (polygonVertices > 2) {
870 // Check if all points are enclosed by the triangle
871 for (int i = 0; (i < polygonVertices) && (!errorEnclosed); i++)
873 if (cv::pointPolygonTest(triangle, cv::Point2f(convexPolygon[i].x, convexPolygon[i].y), true) < (-eps))
874 errorEnclosed = true;
877 // Check if triangle edges middle points touch the polygon
878 std::vector<cv::Point2f> middlePoints = getTriangleMiddlePoints();
880 for (int i = 0; (i < 3) && (!errorMiddlePoints); i++)
882 bool isTouching = false;
884 for (int j = 0; (j < polygonVertices) && (!isTouching); j++)
886 if (cvTsIsPointOnLineSegment(middlePoints[i], convexPolygon[j],
887 convexPolygon[(j + 1) % polygonVertices]))
891 errorMiddlePoints = (isTouching) ? false : true;
894 // Check if at least one of the edges is flush
895 for (int i = 0; (i < 3) && (errorFlush); i++)
897 for (int j = 0; (j < polygonVertices) && (errorFlush); j++)
899 if ((cvTsIsPointOnLineSegment(convexPolygon[j], triangle[i],
900 triangle[(i + 1) % 3])) &&
901 (cvTsIsPointOnLineSegment(convexPolygon[(j + 1) % polygonVertices], triangle[i],
902 triangle[(i + 1) % 3])))
907 // Report any found errors
910 ts->printf( cvtest::TS::LOG,
911 "All points should be enclosed by the triangle.\n" );
912 code = cvtest::TS::FAIL_BAD_ACCURACY;
914 else if (errorMiddlePoints)
916 ts->printf( cvtest::TS::LOG,
917 "All triangle edges middle points should touch the convex hull of the points.\n" );
918 code = cvtest::TS::FAIL_INVALID_OUTPUT;
922 ts->printf( cvtest::TS::LOG,
923 "At least one edge of the enclosing triangle should be flush with one edge of the polygon.\n" );
924 code = cvtest::TS::FAIL_INVALID_OUTPUT;
929 ts->set_failed_test_info( code );
935 /****************************************************************************************\
936 * MinEnclosingCircle Test *
937 \****************************************************************************************/
939 class CV_MinCircleTest : public CV_BaseShapeDescrTest
946 int validate_test_results( int test_case_idx );
953 CV_MinCircleTest::CV_MinCircleTest()
958 void CV_MinCircleTest::run_func()
962 CvPoint2D32f c_center = cvPoint2D32f(center);
963 cvMinEnclosingCircle( points, &c_center, &radius );
968 cv::Point2f tmpcenter;
969 cv::minEnclosingCircle(cv::cvarrToMat(points), tmpcenter, radius);
975 int CV_MinCircleTest::validate_test_results( int test_case_idx )
978 int code = CV_BaseShapeDescrTest::validate_test_results( test_case_idx );
979 int i, j = 0, point_count = points2->rows + points2->cols - 1;
980 Point2f *p = (Point2f*)(points2->data.ptr);
985 double a = 2, b = 200, d = 400;
986 cvNamedWindow( "test", 1 );
987 IplImage* img = cvCreateImage( cvSize(500,500), 8, 3 );
989 for( i = 0; i < point_count; i++ )
990 cvCircle(img,cvPoint(cvRound(p[i].x*a+b),cvRound(p[i].y*a+d)), 3, CV_RGB(0,255,0), -1 );
991 cvCircle( img, cvPoint(cvRound(center.x*a+b),cvRound(center.y*a+d)),
992 cvRound(radius*a), CV_RGB(255,255,0), 1 );
993 cvShowImage( "test", img );
995 cvReleaseImage(&img);
999 // check that the circle contains all the points inside and
1000 // remember at most 3 points that are close to the boundary
1001 for( i = 0; i < point_count; i++ )
1003 double d = cvTsDist(p[i], center);
1006 ts->printf( cvtest::TS::LOG, "The point #%d is outside of the circle\n", i );
1007 code = cvtest::TS::FAIL_BAD_ACCURACY;
1011 if( radius - d < eps*radius && j < 3 )
1015 if( point_count >= 2 && (j < 2 || (j == 2 && cvTsDist(v[0],v[1]) < (radius-1)*2/eps)) )
1017 ts->printf( cvtest::TS::LOG,
1018 "There should be at at least 3 points near the circle boundary or 2 points on the diameter\n" );
1019 code = cvtest::TS::FAIL_BAD_ACCURACY;
1026 ts->set_failed_test_info( code );
1030 /****************************************************************************************\
1031 * MinEnclosingCircle Test 2 *
1032 \****************************************************************************************/
1034 class CV_MinCircleTest2 : public CV_BaseShapeDescrTest
1037 CV_MinCircleTest2();
1040 void run_func(void);
1041 int validate_test_results( int test_case_idx );
1046 CV_MinCircleTest2::CV_MinCircleTest2()
1048 rng = ts->get_rng();
1052 void CV_MinCircleTest2::run_func()
1054 Point2f center = Point2f(rng.uniform(0.0f, 1000.0f), rng.uniform(0.0f, 1000.0f));;
1055 float radius = rng.uniform(0.0f, 500.0f);
1056 float angle = (float)rng.uniform(0.0f, (float)(CV_2PI));
1057 vector<Point2f> pts;
1058 pts.push_back(center + Point2f(radius * cos(angle), radius * sin(angle)));
1059 angle += (float)CV_PI;
1060 pts.push_back(center + Point2f(radius * cos(angle), radius * sin(angle)));
1061 float radius2 = radius * radius;
1062 float x = rng.uniform(center.x - radius, center.x + radius);
1063 float deltaX = x - center.x;
1064 float upperBoundY = sqrt(radius2 - deltaX * deltaX);
1065 float y = rng.uniform(center.y - upperBoundY, center.y + upperBoundY);
1066 pts.push_back(Point2f(x, y));
1067 // Find the minimum area enclosing circle
1070 minEnclosingCircle(pts, calcCenter, calcRadius);
1071 delta = (float)cv::norm(calcCenter - center) + abs(calcRadius - radius);
1074 int CV_MinCircleTest2::validate_test_results( int test_case_idx )
1077 int code = CV_BaseShapeDescrTest::validate_test_results( test_case_idx );
1080 ts->printf( cvtest::TS::LOG, "Delta center and calcCenter > %f\n", eps );
1081 code = cvtest::TS::FAIL_BAD_ACCURACY;
1082 ts->set_failed_test_info( code );
1087 /****************************************************************************************\
1088 * minEnclosingCircle Test 3 *
1089 \****************************************************************************************/
1091 TEST(Imgproc_minEnclosingCircle, basic_test)
1093 vector<Point2f> pts;
1094 pts.push_back(Point2f(0, 0));
1095 pts.push_back(Point2f(10, 0));
1096 pts.push_back(Point2f(5, 1));
1097 const float EPS = 1.0e-3f;
1101 // pts[2] is within the circle with diameter pts[0] - pts[1].
1104 // NB: The triangle is obtuse, so the only pts[0] and pts[1] are on the circle.
1105 minEnclosingCircle(pts, center, radius);
1106 EXPECT_NEAR(center.x, 5, EPS);
1107 EXPECT_NEAR(center.y, 0, EPS);
1108 EXPECT_NEAR(5, radius, EPS);
1110 // pts[2] is on the circle with diameter pts[0] - pts[1].
1113 pts[2] = Point2f(5, 5);
1114 minEnclosingCircle(pts, center, radius);
1115 EXPECT_NEAR(center.x, 5, EPS);
1116 EXPECT_NEAR(center.y, 0, EPS);
1117 EXPECT_NEAR(5, radius, EPS);
1119 // pts[2] is outside the circle with diameter pts[0] - pts[1].
1124 // NB: The triangle is acute, so all 3 points are on the circle.
1125 pts[2] = Point2f(5, 10);
1126 minEnclosingCircle(pts, center, radius);
1127 EXPECT_NEAR(center.x, 5, EPS);
1128 EXPECT_NEAR(center.y, 3.75, EPS);
1129 EXPECT_NEAR(6.25f, radius, EPS);
1131 // The 3 points are colinear.
1132 pts[2] = Point2f(3, 0);
1133 minEnclosingCircle(pts, center, radius);
1134 EXPECT_NEAR(center.x, 5, EPS);
1135 EXPECT_NEAR(center.y, 0, EPS);
1136 EXPECT_NEAR(5, radius, EPS);
1138 // 2 points are the same.
1140 minEnclosingCircle(pts, center, radius);
1141 EXPECT_NEAR(center.x, 5, EPS);
1142 EXPECT_NEAR(center.y, 0, EPS);
1143 EXPECT_NEAR(5, radius, EPS);
1145 // 3 points are the same.
1147 minEnclosingCircle(pts, center, radius);
1148 EXPECT_NEAR(center.x, 10, EPS);
1149 EXPECT_NEAR(center.y, 0, EPS);
1150 EXPECT_NEAR(0, radius, EPS);
1153 TEST(Imgproc_minEnclosingCircle, regression_16051) {
1154 vector<Point2f> pts;
1155 pts.push_back(Point2f(85, 1415));
1156 pts.push_back(Point2f(87, 1415));
1157 pts.push_back(Point2f(89, 1414));
1158 pts.push_back(Point2f(89, 1414));
1159 pts.push_back(Point2f(87, 1412));
1162 minEnclosingCircle(pts, center, radius);
1163 EXPECT_NEAR(center.x, 86.9f, 1e-3);
1164 EXPECT_NEAR(center.y, 1414.1f, 1e-3);
1165 EXPECT_NEAR(2.1024551f, radius, 1e-3);
1168 /****************************************************************************************\
1170 \****************************************************************************************/
1172 class CV_PerimeterTest : public CV_BaseShapeDescrTest
1178 int prepare_test_case( int test_case_idx );
1179 void run_func(void);
1180 int validate_test_results( int test_case_idx );
1187 CV_PerimeterTest::CV_PerimeterTest()
1192 int CV_PerimeterTest::prepare_test_case( int test_case_idx )
1194 int code = CV_BaseShapeDescrTest::prepare_test_case( test_case_idx );
1195 RNG& rng = ts->get_rng();
1201 is_closed = cvtest::randInt(rng) % 2;
1205 points1->flags |= CV_SEQ_KIND_CURVE;
1207 points1->flags |= CV_SEQ_FLAG_CLOSED;
1208 total = points1->total;
1211 total = points2->cols + points2->rows - 1;
1213 if( (cvtest::randInt(rng) % 3) && !test_cpp )
1215 slice.start_index = cvtest::randInt(rng) % total;
1216 slice.end_index = cvtest::randInt(rng) % total;
1219 slice = CV_WHOLE_SEQ;
1225 void CV_PerimeterTest::run_func()
1228 result = cvArcLength( points, slice, points1 ? -1 : is_closed );
1230 result = cv::arcLength(cv::cvarrToMat(points),
1231 !points1 ? is_closed != 0 : (points1->flags & CV_SEQ_FLAG_CLOSED) != 0);
1235 int CV_PerimeterTest::validate_test_results( int test_case_idx )
1237 int code = CV_BaseShapeDescrTest::validate_test_results( test_case_idx );
1238 int i, len = slice.end_index - slice.start_index, total = points2->cols + points2->rows - 1;
1240 Point2f prev_pt, pt;
1246 len = MIN( len, total );
1247 //len -= !is_closed && len == total;
1249 ptr = (CvPoint2D32f*)points2->data.fl;
1250 prev_pt = ptr[(is_closed ? slice.start_index+len-1 : slice.start_index) % total];
1252 for( i = 0; i < len + (len < total && (!is_closed || len==1)); i++ )
1254 pt = ptr[(i + slice.start_index) % total];
1255 double dx = pt.x - prev_pt.x, dy = pt.y - prev_pt.y;
1256 result0 += sqrt(dx*dx + dy*dy);
1260 if( cvIsNaN(result) || cvIsInf(result) )
1262 ts->printf( cvtest::TS::LOG, "cvArcLength() returned invalid value (%g)\n", result );
1263 code = cvtest::TS::FAIL_INVALID_OUTPUT;
1265 else if( fabs(result - result0) > FLT_EPSILON*100*result0 )
1267 ts->printf( cvtest::TS::LOG, "The function returned %g, while the correct result is %g\n", result, result0 );
1268 code = cvtest::TS::FAIL_BAD_ACCURACY;
1272 ts->set_failed_test_info( code );
1277 /****************************************************************************************\
1279 \****************************************************************************************/
1281 class CV_FitEllipseTest : public CV_BaseShapeDescrTest
1284 CV_FitEllipseTest();
1287 int prepare_test_case( int test_case_idx );
1288 void generate_point_set( void* points );
1289 void run_func(void);
1290 int validate_test_results( int test_case_idx );
1291 RotatedRect box0, box;
1292 double min_ellipse_size, max_noise;
1296 CV_FitEllipseTest::CV_FitEllipseTest()
1298 min_log_size = 5; // for robust ellipse fitting a dozen of points is needed at least
1300 min_ellipse_size = 10;
1305 void CV_FitEllipseTest::generate_point_set( void* pointsSet )
1307 RNG& rng = ts->get_rng();
1308 int i, total, point_type;
1313 box0.center.x = (float)((low.val[0] + high.val[0])*0.5);
1314 box0.center.y = (float)((low.val[1] + high.val[1])*0.5);
1315 box0.size.width = (float)(MAX(high.val[0] - low.val[0], min_ellipse_size)*2);
1316 box0.size.height = (float)(MAX(high.val[1] - low.val[1], min_ellipse_size)*2);
1317 box0.angle = (float)(cvtest::randReal(rng)*180);
1318 a = cos(box0.angle*CV_PI/180.);
1319 b = sin(box0.angle*CV_PI/180.);
1321 if( box0.size.width > box0.size.height )
1324 CV_SWAP( box0.size.width, box0.size.height, t );
1326 memset( &reader, 0, sizeof(reader) );
1328 if( CV_IS_SEQ(pointsSet) )
1330 CvSeq* ptseq = (CvSeq*)pointsSet;
1331 total = ptseq->total;
1332 point_type = CV_SEQ_ELTYPE(ptseq);
1333 cvStartReadSeq( ptseq, &reader );
1337 CvMat* ptm = (CvMat*)pointsSet;
1338 assert( CV_IS_MAT(ptm) && CV_IS_MAT_CONT(ptm->type) );
1339 total = ptm->rows + ptm->cols - 1;
1340 point_type = CV_MAT_TYPE(ptm->type);
1341 data = ptm->data.ptr;
1344 CV_Assert(point_type == CV_32SC2 || point_type == CV_32FC2);
1346 for( i = 0; i < total; i++ )
1349 CvPoint2D32f p = {0, 0};
1350 double angle = cvtest::randReal(rng)*CV_PI*2;
1351 double x = box0.size.height*0.5*(cos(angle) + (cvtest::randReal(rng)-0.5)*2*max_noise);
1352 double y = box0.size.width*0.5*(sin(angle) + (cvtest::randReal(rng)-0.5)*2*max_noise);
1353 p.x = (float)(box0.center.x + a*x + b*y);
1354 p.y = (float)(box0.center.y - b*x + a*y);
1358 pp = (CvPoint*)reader.ptr;
1359 CV_NEXT_SEQ_ELEM( sizeof(*pp), reader );
1362 pp = ((CvPoint*)data) + i;
1363 if( point_type == CV_32SC2 )
1365 pp->x = cvRound(p.x);
1366 pp->y = cvRound(p.y);
1369 *(CvPoint2D32f*)pp = p;
1374 int CV_FitEllipseTest::prepare_test_case( int test_case_idx )
1376 min_log_size = MAX(min_log_size,4);
1377 max_log_size = MAX(min_log_size,max_log_size);
1378 return CV_BaseShapeDescrTest::prepare_test_case( test_case_idx );
1382 void CV_FitEllipseTest::run_func()
1385 box = cvFitEllipse2( points );
1387 box = cv::fitEllipse(cv::cvarrToMat(points));
1390 int CV_FitEllipseTest::validate_test_results( int test_case_idx )
1392 int code = CV_BaseShapeDescrTest::validate_test_results( test_case_idx );
1395 if( cvIsNaN(box.center.x) || cvIsInf(box.center.x) ||
1396 cvIsNaN(box.center.y) || cvIsInf(box.center.y) ||
1397 cvIsNaN(box.size.width) || cvIsInf(box.size.width) ||
1398 cvIsNaN(box.size.height) || cvIsInf(box.size.height) ||
1399 cvIsNaN(box.angle) || cvIsInf(box.angle) )
1401 ts->printf( cvtest::TS::LOG, "Some of the computed ellipse parameters are invalid (x=%g,y=%g,w=%g,h=%g,angle=%g)\n",
1402 box.center.x, box.center.y, box.size.width, box.size.height, box.angle );
1403 code = cvtest::TS::FAIL_INVALID_OUTPUT;
1407 box.angle = (float)(90-box.angle);
1410 if( box.angle > 360 )
1413 if( fabs(box.center.x - box0.center.x) > 3 ||
1414 fabs(box.center.y - box0.center.y) > 3 ||
1415 fabs(box.size.width - box0.size.width) > 0.1*fabs(box0.size.width) ||
1416 fabs(box.size.height - box0.size.height) > 0.1*fabs(box0.size.height) )
1418 ts->printf( cvtest::TS::LOG, "The computed ellipse center and/or size are incorrect:\n\t"
1419 "(x=%.1f,y=%.1f,w=%.1f,h=%.1f), while it should be (x=%.1f,y=%.1f,w=%.1f,h=%.1f)\n",
1420 box.center.x, box.center.y, box.size.width, box.size.height,
1421 box0.center.x, box0.center.y, box0.size.width, box0.size.height );
1422 code = cvtest::TS::FAIL_BAD_ACCURACY;
1426 diff_angle = fabs(box0.angle - box.angle);
1427 diff_angle = MIN( diff_angle, fabs(diff_angle - 360));
1428 diff_angle = MIN( diff_angle, fabs(diff_angle - 180));
1430 if( box0.size.height >= 1.3*box0.size.width && diff_angle > 30 )
1432 ts->printf( cvtest::TS::LOG, "Incorrect ellipse angle (=%1.f, should be %1.f)\n",
1433 box.angle, box0.angle );
1434 code = cvtest::TS::FAIL_BAD_ACCURACY;
1443 cvNamedWindow( "test", 0 );
1444 IplImage* img = cvCreateImage( cvSize(cvRound(low_high_range*4),
1445 cvRound(low_high_range*4)), 8, 3 );
1448 box.center.x += (float)low_high_range*2;
1449 box.center.y += (float)low_high_range*2;
1450 cvEllipseBox( img, box, CV_RGB(255,0,0), 3, 8 );
1452 for( int i = 0; i < points2->rows + points2->cols - 1; i++ )
1455 pt.x = cvRound(points2->data.fl[i*2] + low_high_range*2);
1456 pt.y = cvRound(points2->data.fl[i*2+1] + low_high_range*2);
1457 cvCircle( img, pt, 1, CV_RGB(255,255,255), -1, 8 );
1460 cvShowImage( "test", img );
1461 cvReleaseImage( &img );
1468 ts->set_failed_test_info( code );
1474 class CV_FitEllipseSmallTest : public cvtest::BaseTest
1477 CV_FitEllipseSmallTest() {}
1478 ~CV_FitEllipseSmallTest() {}
1483 vector<vector<Point> > c;
1484 c.push_back(vector<Point>());
1486 Point ofs = Point(0,0);//sz.width/2, sz.height/2) - Point(4,4)*scale;
1487 c[0].push_back(Point(2, 0)*scale+ofs);
1488 c[0].push_back(Point(0, 2)*scale+ofs);
1489 c[0].push_back(Point(0, 6)*scale+ofs);
1490 c[0].push_back(Point(2, 8)*scale+ofs);
1491 c[0].push_back(Point(6, 8)*scale+ofs);
1492 c[0].push_back(Point(8, 6)*scale+ofs);
1493 c[0].push_back(Point(8, 2)*scale+ofs);
1494 c[0].push_back(Point(6, 0)*scale+ofs);
1496 RotatedRect e = fitEllipse(c[0]);
1497 CV_Assert( fabs(e.center.x - 4) <= 1. &&
1498 fabs(e.center.y - 4) <= 1. &&
1499 fabs(e.size.width - 9) <= 1. &&
1500 fabs(e.size.height - 9) <= 1. );
1505 // Regression test for incorrect fitEllipse result reported in Bug #3989
1506 // Check edge cases for rotation angles of ellipse ([-180, 90, 0, 90, 180] degrees)
1507 class CV_FitEllipseParallelTest : public CV_FitEllipseTest
1510 CV_FitEllipseParallelTest();
1511 ~CV_FitEllipseParallelTest();
1513 void generate_point_set( void* points );
1514 void run_func(void);
1518 CV_FitEllipseParallelTest::CV_FitEllipseParallelTest()
1520 min_ellipse_size = 5;
1523 void CV_FitEllipseParallelTest::generate_point_set( void* )
1525 RNG& rng = ts->get_rng();
1526 int height = (int)(MAX(high.val[0] - low.val[0], min_ellipse_size));
1527 int width = (int)(MAX(high.val[1] - low.val[1], min_ellipse_size));
1528 const int angle = ( (cvtest::randInt(rng) % 5) - 2 ) * 90;
1529 const int dim = max(height, width);
1530 const Point center = Point(dim*2, dim*2);
1532 if( width > height )
1535 CV_SWAP( width, height, t );
1538 Mat image = Mat::zeros(dim*4, dim*4, CV_8UC1);
1539 ellipse(image, center, Size(height, width), angle,
1540 0, 360, Scalar(255, 0, 0), 1, 8);
1542 box0.center.x = (float)center.x;
1543 box0.center.y = (float)center.y;
1544 box0.size.width = (float)width*2;
1545 box0.size.height = (float)height*2;
1546 box0.angle = (float)angle;
1548 vector<vector<Point> > contours;
1549 findContours(image, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
1550 Mat(contours[0]).convertTo(pointsMat, CV_32F);
1553 void CV_FitEllipseParallelTest::run_func()
1555 box = cv::fitEllipse(pointsMat);
1558 CV_FitEllipseParallelTest::~CV_FitEllipseParallelTest(){
1559 pointsMat.release();
1562 /****************************************************************************************\
1564 \****************************************************************************************/
1566 class CV_FitLineTest : public CV_BaseShapeDescrTest
1572 int prepare_test_case( int test_case_idx );
1573 void generate_point_set( void* points );
1574 void run_func(void);
1575 int validate_test_results( int test_case_idx );
1577 AutoBuffer<float> line, line0;
1583 CV_FitLineTest::CV_FitLineTest()
1585 min_log_size = 5; // for robust line fitting a dozen of points is needed at least
1590 void CV_FitLineTest::generate_point_set( void* pointsSet )
1592 RNG& rng = ts->get_rng();
1593 int i, k, n, total, point_type;
1599 for( k = 0; k < n; k++ )
1601 line0[k+n] = (float)((low.val[k] + high.val[k])*0.5);
1602 line0[k] = (float)(high.val[k] - low.val[k]);
1603 if( cvtest::randInt(rng) % 2 )
1604 line0[k] = -line0[k];
1605 s += (double)line0[k]*line0[k];
1609 for( k = 0; k < n; k++ )
1610 line0[k] = (float)(line0[k]*s);
1612 memset( &reader, 0, sizeof(reader) );
1614 if( CV_IS_SEQ(pointsSet) )
1616 CvSeq* ptseq = (CvSeq*)pointsSet;
1617 total = ptseq->total;
1618 point_type = CV_MAT_DEPTH(CV_SEQ_ELTYPE(ptseq));
1619 cvStartReadSeq( ptseq, &reader );
1623 CvMat* ptm = (CvMat*)pointsSet;
1624 assert( CV_IS_MAT(ptm) && CV_IS_MAT_CONT(ptm->type) );
1625 total = ptm->rows + ptm->cols - 1;
1626 point_type = CV_MAT_DEPTH(CV_MAT_TYPE(ptm->type));
1627 data = ptm->data.ptr;
1630 for( i = 0; i < total; i++ )
1637 pi = (int*)reader.ptr;
1638 pf = (float*)reader.ptr;
1639 CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
1643 pi = (int*)data + i*n;
1644 pf = (float*)data + i*n;
1647 t = (float)((cvtest::randReal(rng)-0.5)*low_high_range*2);
1649 for( k = 0; k < n; k++ )
1651 p[k] = (float)((cvtest::randReal(rng)-0.5)*max_noise*2 + t*line0[k] + line0[k+n]);
1653 if( point_type == CV_32S )
1654 pi[k] = cvRound(p[k]);
1661 int CV_FitLineTest::prepare_test_case( int test_case_idx )
1663 RNG& rng = ts->get_rng();
1664 dims = cvtest::randInt(rng) % 2 + 2;
1665 line.allocate(dims * 2);
1666 line0.allocate(dims * 2);
1667 min_log_size = MAX(min_log_size,5);
1668 max_log_size = MAX(min_log_size,max_log_size);
1669 int code = CV_BaseShapeDescrTest::prepare_test_case( test_case_idx );
1670 dist_type = cvtest::randInt(rng) % 6 + 1;
1671 dist_type += dist_type == CV_DIST_C;
1672 reps = 0.1; aeps = 0.01;
1677 void CV_FitLineTest::run_func()
1680 cvFitLine( points, dist_type, 0, reps, aeps, line.data());
1682 cv::fitLine(cv::cvarrToMat(points), (cv::Vec4f&)line[0], dist_type, 0, reps, aeps);
1684 cv::fitLine(cv::cvarrToMat(points), (cv::Vec6f&)line[0], dist_type, 0, reps, aeps);
1687 int CV_FitLineTest::validate_test_results( int test_case_idx )
1689 int code = CV_BaseShapeDescrTest::validate_test_results( test_case_idx );
1691 double vec_diff = 0, t;
1693 //std::cout << dims << " " << Mat(1, dims*2, CV_32FC1, line.data()) << " " << Mat(1, dims, CV_32FC1, line0.data()) << std::endl;
1695 for( k = 0; k < dims*2; k++ )
1697 if( cvIsNaN(line[k]) || cvIsInf(line[k]) )
1699 ts->printf( cvtest::TS::LOG, "Some of the computed line parameters are invalid (line[%d]=%g)\n",
1701 code = cvtest::TS::FAIL_INVALID_OUTPUT;
1706 if( fabs(line0[1]) > fabs(line0[0]) )
1708 if( fabs(line0[dims-1]) > fabs(line0[max_k]) )
1710 if( line0[max_k] < 0 )
1711 for( k = 0; k < dims; k++ )
1712 line0[k] = -line0[k];
1713 if( line[max_k] < 0 )
1714 for( k = 0; k < dims; k++ )
1717 for( k = 0; k < dims; k++ )
1719 double dt = line[k] - line0[k];
1723 if( sqrt(vec_diff) > 0.05 )
1726 ts->printf( cvtest::TS::LOG,
1727 "The computed line vector (%.2f,%.2f) is different from the actual (%.2f,%.2f)\n",
1728 line[0], line[1], line0[0], line0[1] );
1730 ts->printf( cvtest::TS::LOG,
1731 "The computed line vector (%.2f,%.2f,%.2f) is different from the actual (%.2f,%.2f,%.2f)\n",
1732 line[0], line[1], line[2], line0[0], line0[1], line0[2] );
1733 code = cvtest::TS::FAIL_BAD_ACCURACY;
1737 t = (line[max_k+dims] - line0[max_k+dims])/line0[max_k];
1738 for( k = 0; k < dims; k++ )
1740 double p = line0[k+dims] + t*line0[k] - line[k+dims];
1744 if( sqrt(vec_diff) > 1*MAX(fabs(t),1) )
1747 ts->printf( cvtest::TS::LOG,
1748 "The computed line point (%.2f,%.2f) is too far from the actual line\n",
1749 line[2]+line0[2], line[3]+line0[3] );
1751 ts->printf( cvtest::TS::LOG,
1752 "The computed line point (%.2f,%.2f,%.2f) is too far from the actual line\n",
1753 line[3]+line0[3], line[4]+line0[4], line[5]+line0[5] );
1754 code = cvtest::TS::FAIL_BAD_ACCURACY;
1762 ts->set_failed_test_info( code );
1767 /****************************************************************************************\
1768 * ContourMoments Test *
1769 \****************************************************************************************/
1773 cvTsGenerateTousledBlob( CvPoint2D32f center, CvSize2D32f axes,
1774 double max_r_scale, double angle, CvArr* points, RNG& rng )
1776 int i, total, point_type;
1779 memset( &reader, 0, sizeof(reader) );
1781 if( CV_IS_SEQ(points) )
1783 CvSeq* ptseq = (CvSeq*)points;
1784 total = ptseq->total;
1785 point_type = CV_SEQ_ELTYPE(ptseq);
1786 cvStartReadSeq( ptseq, &reader );
1790 CvMat* ptm = (CvMat*)points;
1791 assert( CV_IS_MAT(ptm) && CV_IS_MAT_CONT(ptm->type) );
1792 total = ptm->rows + ptm->cols - 1;
1793 point_type = CV_MAT_TYPE(ptm->type);
1794 data = ptm->data.ptr;
1797 assert( point_type == CV_32SC2 || point_type == CV_32FC2 );
1799 for( i = 0; i < total; i++ )
1804 double phi0 = 2*CV_PI*i/total;
1805 double phi = CV_PI*angle/180.;
1806 double t = cvtest::randReal(rng)*max_r_scale + (1 - max_r_scale);
1807 double ta = axes.height*t;
1808 double tb = axes.width*t;
1809 double c0 = cos(phi0)*ta, s0 = sin(phi0)*tb;
1810 double c = cos(phi), s = sin(phi);
1811 p.x = (float)(c0*c - s0*s + center.x);
1812 p.y = (float)(c0*s + s0*c + center.y);
1816 pp = (CvPoint*)reader.ptr;
1817 CV_NEXT_SEQ_ELEM( sizeof(*pp), reader );
1820 pp = ((CvPoint*)data) + i;
1822 if( point_type == CV_32SC2 )
1824 pp->x = cvRound(p.x);
1825 pp->y = cvRound(p.y);
1828 *(CvPoint2D32f*)pp = cvPoint2D32f(p);
1833 class CV_ContourMomentsTest : public CV_BaseShapeDescrTest
1836 CV_ContourMomentsTest();
1839 int prepare_test_case( int test_case_idx );
1840 void generate_point_set( void* points );
1841 void run_func(void);
1842 int validate_test_results( int test_case_idx );
1843 CvMoments moments0, moments;
1847 int max_max_r_scale;
1848 double max_r_scale, angle;
1853 CV_ContourMomentsTest::CV_ContourMomentsTest()
1857 max_max_r_scale = 15;
1858 low_high_range = 200;
1859 enable_flt_points = false;
1863 void CV_ContourMomentsTest::generate_point_set( void* pointsSet )
1865 RNG& rng = ts->get_rng();
1868 axes.width = (float)((cvtest::randReal(rng)*0.9 + 0.1)*low_high_range);
1869 axes.height = (float)((cvtest::randReal(rng)*0.9 + 0.1)*low_high_range);
1870 max_sz = MAX(axes.width, axes.height);
1872 img_size.width = img_size.height = cvRound(low_high_range*2.2);
1874 center.x = (float)(img_size.width*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.width - max_sz*2)*0.8);
1875 center.y = (float)(img_size.height*0.5 + (cvtest::randReal(rng)-0.5)*(img_size.height - max_sz*2)*0.8);
1877 assert( 0 < center.x - max_sz && center.x + max_sz < img_size.width &&
1878 0 < center.y - max_sz && center.y + max_sz < img_size.height );
1880 max_r_scale = cvtest::randReal(rng)*max_max_r_scale*0.01;
1881 angle = cvtest::randReal(rng)*360;
1883 cvTsGenerateTousledBlob( cvPoint2D32f(center), cvSize2D32f(axes), max_r_scale, angle, pointsSet, rng );
1886 points1->flags = CV_SEQ_MAGIC_VAL + CV_SEQ_POLYGON;
1890 int CV_ContourMomentsTest::prepare_test_case( int test_case_idx )
1892 min_log_size = MAX(min_log_size,3);
1893 max_log_size = MIN(max_log_size,8);
1894 max_log_size = MAX(min_log_size,max_log_size);
1895 int code = CV_BaseShapeDescrTest::prepare_test_case( test_case_idx );
1900 void CV_ContourMomentsTest::run_func()
1904 cvMoments( points, &moments );
1905 area = cvContourArea( points );
1909 moments = cvMoments(cv::moments(cv::cvarrToMat(points)));
1910 area = cv::contourArea(cv::cvarrToMat(points));
1915 int CV_ContourMomentsTest::validate_test_results( int test_case_idx )
1917 int code = CV_BaseShapeDescrTest::validate_test_results( test_case_idx );
1918 int i, n = (int)(sizeof(moments)/sizeof(moments.inv_sqrt_m00));
1919 CvMat* img = cvCreateMat( img_size.height, img_size.width, CV_8UC1 );
1920 CvPoint* pt = (CvPoint*)points2->data.i;
1921 int count = points2->cols + points2->rows - 1;
1925 cvFillPoly( img, &pt, &count, 1, cvScalarAll(1));
1926 cvMoments( img, &moments0 );
1928 for( i = 0; i < n; i++ )
1930 double t = fabs((&moments0.m00)[i]);
1931 max_v0 = MAX(max_v0, t);
1934 for( i = 0; i <= n; i++ )
1936 double v = i < n ? (&moments.m00)[i] : area;
1937 double v0 = i < n ? (&moments0.m00)[i] : moments0.m00;
1939 if( cvIsNaN(v) || cvIsInf(v) )
1941 ts->printf( cvtest::TS::LOG,
1942 "The contour %s is invalid (=%g)\n", i < n ? "moment" : "area", v );
1943 code = cvtest::TS::FAIL_INVALID_OUTPUT;
1947 if( fabs(v - v0) > 0.1*max_v0 )
1949 ts->printf( cvtest::TS::LOG,
1950 "The computed contour %s is %g, while it should be %g\n",
1951 i < n ? "moment" : "area", v, v0 );
1952 code = cvtest::TS::FAIL_BAD_ACCURACY;
1960 cvCmpS( img, 0, img, CV_CMP_GT );
1961 cvNamedWindow( "test", 1 );
1962 cvShowImage( "test", img );
1965 ts->set_failed_test_info( code );
1968 cvReleaseMat( &img );
1973 ////////////////////////////////////// Perimeter/Area/Slice test ///////////////////////////////////
1975 class CV_PerimeterAreaSliceTest : public cvtest::BaseTest
1978 CV_PerimeterAreaSliceTest();
1979 ~CV_PerimeterAreaSliceTest();
1984 CV_PerimeterAreaSliceTest::CV_PerimeterAreaSliceTest()
1987 CV_PerimeterAreaSliceTest::~CV_PerimeterAreaSliceTest() {}
1989 void CV_PerimeterAreaSliceTest::run( int )
1991 Ptr<CvMemStorage> storage(cvCreateMemStorage());
1992 RNG& rng = theRNG();
1993 const double min_r = 90, max_r = 120;
1995 for( int i = 0; i < 100; i++ )
1997 ts->update_context( this, i, true );
1998 int n = rng.uniform(3, 30);
1999 cvClearMemStorage(storage);
2000 CvSeq* contour = cvCreateSeq(CV_SEQ_POLYGON, sizeof(CvSeq), sizeof(CvPoint), storage);
2001 double dphi = CV_PI*2/n;
2003 center.x = rng.uniform(cvCeil(max_r), cvFloor(640-max_r));
2004 center.y = rng.uniform(cvCeil(max_r), cvFloor(480-max_r));
2006 for( int j = 0; j < n; j++ )
2008 CvPoint pt = CV_STRUCT_INITIALIZER;
2009 double r = rng.uniform(min_r, max_r);
2010 double phi = j*dphi;
2011 pt.x = cvRound(center.x + r*cos(phi));
2012 pt.y = cvRound(center.y - r*sin(phi));
2013 cvSeqPush(contour, &pt);
2016 CvSlice slice = {0, 0};
2019 slice.start_index = rng.uniform(-n/2, 3*n/2);
2020 slice.end_index = rng.uniform(-n/2, 3*n/2);
2021 int len = cvSliceLength(slice, contour);
2025 CvSeq *cslice = cvSeqSlice(contour, slice);
2026 /*printf( "%d. (%d, %d) of %d, length = %d, length1 = %d\n",
2027 i, slice.start_index, slice.end_index,
2028 contour->total, cvSliceLength(slice, contour), cslice->total );
2030 double area0 = cvContourArea(cslice);
2031 double area1 = cvContourArea(contour, slice);
2032 if( area0 != area1 )
2034 ts->printf(cvtest::TS::LOG,
2035 "The contour area slice is computed differently (%g vs %g)\n", area0, area1 );
2036 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
2040 double len0 = cvArcLength(cslice, CV_WHOLE_SEQ, 1);
2041 double len1 = cvArcLength(contour, slice, 1);
2044 ts->printf(cvtest::TS::LOG,
2045 "The contour arc length is computed differently (%g vs %g)\n", len0, len1 );
2046 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );
2050 ts->set_failed_test_info(cvtest::TS::OK);
2054 TEST(Imgproc_ConvexHull, accuracy) { CV_ConvHullTest test; test.safe_run(); }
2055 TEST(Imgproc_MinAreaRect, accuracy) { CV_MinAreaRectTest test; test.safe_run(); }
2056 TEST(Imgproc_MinTriangle, accuracy) { CV_MinTriangleTest test; test.safe_run(); }
2057 TEST(Imgproc_MinCircle, accuracy) { CV_MinCircleTest test; test.safe_run(); }
2058 TEST(Imgproc_MinCircle2, accuracy) { CV_MinCircleTest2 test; test.safe_run(); }
2059 TEST(Imgproc_ContourPerimeter, accuracy) { CV_PerimeterTest test; test.safe_run(); }
2060 TEST(Imgproc_FitEllipse, accuracy) { CV_FitEllipseTest test; test.safe_run(); }
2061 TEST(Imgproc_FitEllipse, parallel) { CV_FitEllipseParallelTest test; test.safe_run(); }
2062 TEST(Imgproc_FitLine, accuracy) { CV_FitLineTest test; test.safe_run(); }
2063 TEST(Imgproc_ContourMoments, accuracy) { CV_ContourMomentsTest test; test.safe_run(); }
2064 TEST(Imgproc_ContourPerimeterSlice, accuracy) { CV_PerimeterAreaSliceTest test; test.safe_run(); }
2065 TEST(Imgproc_FitEllipse, small) { CV_FitEllipseSmallTest test; test.safe_run(); }
2069 PARAM_TEST_CASE(ConvexityDefects_regression_5908, bool, int)
2077 virtual void SetUp()
2079 clockwise = GET_PARAM(0);
2080 start_index = GET_PARAM(1);
2083 const Point2i points[N] = {
2086 Point2i(115, 275), // inner
2088 Point2i(154, 256), // inner
2090 Point2i(185, 256), // inner
2092 Point2i(240, 320), // inner
2097 contour = Mat(N, 1, CV_32SC2);
2098 for (int i = 0; i < N; i++)
2100 contour.at<Point2i>(i) = (!clockwise) // image and convexHull coordinate systems are different
2101 ? points[(start_index + i) % N]
2102 : points[N - 1 - ((start_index + i) % N)];
2107 TEST_P(ConvexityDefects_regression_5908, simple)
2109 std::vector<int> hull;
2110 cv::convexHull(contour, hull, clockwise, false);
2112 std::vector<Vec4i> result;
2113 cv::convexityDefects(contour, hull, result);
2115 EXPECT_EQ(4, (int)result.size());
2118 INSTANTIATE_TEST_CASE_P(Imgproc, ConvexityDefects_regression_5908,
2121 testing::Values(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
2124 TEST(Imgproc_FitLine, regression_15083)
2133 Mat points(5, 1, CV_32SC2, points2i_);
2136 fitLine(points, lineParam, DIST_L1, 0, 0.01, 0.01);
2137 EXPECT_GE(fabs(lineParam[0]), fabs(lineParam[1]) * 4) << lineParam;
2140 TEST(Imgproc_FitLine, regression_4903)
2142 float points2f_[] = {
2150 Mat points(6, 1, CV_32FC2, points2f_);
2153 fitLine(points, lineParam, DIST_WELSCH, 0, 0.01, 0.01);
2154 EXPECT_GE(fabs(lineParam[1]), fabs(lineParam[0]) * 4) << lineParam;