Skip to content

Commit 80037dc

Browse files
committed
fixed fillPoly, the overloaded variant with InputArrayOfArrays parameter (single or multiple polygons)
1 parent 1f2d4e4 commit 80037dc

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

modules/imgproc/src/drawing.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,7 @@ void ellipse2Poly( Point2d center, Size2d axes, int angle,
931931
int delta, std::vector<Point2d>& pts )
932932
{
933933
CV_INSTRUMENT_REGION();
934+
CV_Assert(0 < delta && delta <= 180);
934935

935936
float alpha, beta;
936937
int i;
@@ -2360,7 +2361,9 @@ void cv::fillPoly(InputOutputArray _img, InputArrayOfArrays pts,
23602361
CV_INSTRUMENT_REGION();
23612362

23622363
Mat img = _img.getMat();
2363-
int i, ncontours = (int)pts.total();
2364+
bool manyContours = pts.kind() == _InputArray::STD_VECTOR_VECTOR ||
2365+
pts.kind() == _InputArray::STD_VECTOR_MAT;
2366+
int i, ncontours = manyContours ? (int)pts.total() : 1;
23642367
if( ncontours == 0 )
23652368
return;
23662369
AutoBuffer<Point*> _ptsptr(ncontours);
@@ -2370,7 +2373,7 @@ void cv::fillPoly(InputOutputArray _img, InputArrayOfArrays pts,
23702373

23712374
for( i = 0; i < ncontours; i++ )
23722375
{
2373-
Mat p = pts.getMat(i);
2376+
Mat p = pts.getMat(manyContours ? i : -1);
23742377
CV_Assert(p.checkVector(2, CV_32S) >= 0);
23752378
ptsptr[i] = p.ptr<Point>();
23762379
npts[i] = p.rows*p.cols*p.channels()/2;

modules/imgproc/test/test_drawing.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,4 +593,42 @@ TEST(Drawing, regression_16308)
593593
EXPECT_NE(0, (int)img.at<uchar>(99, 50));
594594
}
595595

596+
TEST(Drawing, fillpoly_circle)
597+
{
598+
Mat img_c(640, 480, CV_8UC3, Scalar::all(0));
599+
Mat img_fp = img_c.clone(), img_fcp = img_c.clone(), img_fp3 = img_c.clone();
600+
601+
Point center1(img_c.cols/2, img_c.rows/2);
602+
Point center2(img_c.cols/10, img_c.rows*3/4);
603+
Point center3 = Point(img_c.cols, img_c.rows) - center2;
604+
int radius = img_c.rows/4;
605+
int radius_small = img_c.cols/15;
606+
Scalar color(0, 0, 255);
607+
608+
circle(img_c, center1, radius, color, -1);
609+
610+
// check that circle, fillConvexPoly and fillPoly
611+
// give almost the same result then asked to draw a single circle
612+
vector<Point> vtx;
613+
ellipse2Poly(center1, Size(radius, radius), 0, 0, 360, 1, vtx);
614+
fillConvexPoly(img_fcp, vtx, color);
615+
fillPoly(img_fp, vtx, color);
616+
double diff_fp = cv::norm(img_c, img_fp, NORM_L1)/(255*radius*2*CV_PI);
617+
double diff_fcp = cv::norm(img_c, img_fcp, NORM_L1)/(255*radius*2*CV_PI);
618+
EXPECT_LT(diff_fp, 1.);
619+
EXPECT_LT(diff_fcp, 1.);
620+
621+
// check that fillPoly can draw 3 disjoint circles at once
622+
circle(img_c, center2, radius_small, color, -1);
623+
circle(img_c, center3, radius_small, color, -1);
624+
625+
vector<vector<Point> > vtx3(3);
626+
vtx3[0] = vtx;
627+
ellipse2Poly(center2, Size(radius_small, radius_small), 0, 0, 360, 1, vtx3[1]);
628+
ellipse2Poly(center3, Size(radius_small, radius_small), 0, 0, 360, 1, vtx3[2]);
629+
fillPoly(img_fp3, vtx3, color);
630+
double diff_fp3 = cv::norm(img_c, img_fp3, NORM_L1)/(255*(radius+radius_small*2)*2*CV_PI);
631+
EXPECT_LT(diff_fp3, 1.);
632+
}
633+
596634
}} // namespace

0 commit comments

Comments
 (0)