Skip to content

Commit 5b41599

Browse files
reunanenvpisarev
authored andcommitted
Fix pointPolygonTest for large coordinate values (opencv#10222)
* Add test that fails * Fix integer pointPolygonTest for large coordinate values * Review fixes: - change type from long long to int64 - move test code to test_contours.cpp, and make it C++98 compliant * Hopefully fix compiler error by using push_back instead of emplace_back
1 parent 5ce38e5 commit 5b41599

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

modules/imgproc/src/geometry.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ double cv::pointPolygonTest( InputArray _contour, Point2f pt, bool measureDist )
119119

120120
for( i = 0; i < total; i++ )
121121
{
122-
int dist;
123122
v0 = v;
124123
v = cnt[i];
125124

@@ -133,7 +132,8 @@ double cv::pointPolygonTest( InputArray _contour, Point2f pt, bool measureDist )
133132
continue;
134133
}
135134

136-
dist = (ip.y - v0.y)*(v.x - v0.x) - (ip.x - v0.x)*(v.y - v0.y);
135+
int64 dist = static_cast<int64>(ip.y - v0.y)*(v.x - v0.x)
136+
- static_cast<int64>(ip.x - v0.x)*(v.y - v0.y);
137137
if( dist == 0 )
138138
return 0;
139139
if( v.y < v0.y )

modules/imgproc/test/test_contours.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,4 +485,18 @@ TEST(Imgproc_FindContours, border)
485485
ASSERT_TRUE(norm(img - img_draw_contours, NORM_INF) == 0.0);
486486
}
487487

488+
TEST(Imgproc_PointPolygonTest, regression_10222)
489+
{
490+
vector<Point> contour;
491+
contour.push_back(Point(0, 0));
492+
contour.push_back(Point(0, 100000));
493+
contour.push_back(Point(100000, 100000));
494+
contour.push_back(Point(100000, 50000));
495+
contour.push_back(Point(100000, 0));
496+
497+
const Point2f point(40000, 40000);
498+
const double result = cv::pointPolygonTest(contour, point, false);
499+
EXPECT_GT(result, 0) << "Desired result: point is inside polygon - actual result: point is not inside polygon";
500+
}
501+
488502
/* End of file. */

0 commit comments

Comments
 (0)