diff --git a/modules/imgproc/src/geometry.cpp b/modules/imgproc/src/geometry.cpp index eed9ff3c5c..c7a52b2f46 100644 --- a/modules/imgproc/src/geometry.cpp +++ b/modules/imgproc/src/geometry.cpp @@ -330,20 +330,20 @@ static LineSegmentIntersection parallelInt( Point2f a, Point2f b, Point2f c, Poi static LineSegmentIntersection intersectLineSegments( Point2f a, Point2f b, Point2f c, Point2f d, Point2f& p, Point2f& q ) { - double denom = (a.x - b.x) * (double)(d.y - c.y) - (a.y - b.y) * (double)(d.x - c.x); + double denom = ((double)a.x - b.x) * ((double)d.y - c.y) - ((double)a.y - b.y) * ((double)d.x - c.x); // If denom is zero, then segments are parallel: handle separately. if( denom == 0. ) return parallelInt(a, b, c, d, p, q); - double num = (d.y - a.y) * (double)(a.x - c.x) + (a.x - d.x) * (double)(a.y - c.y); + double num = ((double)d.y - a.y) * ((double)a.x - c.x) + ((double)a.x - d.x) * ((double)a.y - c.y); double s = num / denom; - num = (b.y - a.y) * (double)(a.x - c.x) + (c.y - a.y) * (double)(b.x - a.x); + num = ((double)b.y - a.y) * ((double)a.x - c.x) + ((double)c.y - a.y) * ((double)b.x - a.x); double t = num / denom; - p.x = (float)(a.x + s*(b.x - a.x)); - p.y = (float)(a.y + s*(b.y - a.y)); + p.x = (float)(a.x + s*((double)b.x - a.x)); + p.y = (float)(a.y + s*((double)b.y - a.y)); q = p; return s < 0. || s > 1. || t < 0. || t > 1. ? LS_NO_INTERSECTION : diff --git a/modules/imgproc/test/test_intersectconvexconvex.cpp b/modules/imgproc/test/test_intersectconvexconvex.cpp index 00e3674f48..146c891b84 100644 --- a/modules/imgproc/test/test_intersectconvexconvex.cpp +++ b/modules/imgproc/test/test_intersectconvexconvex.cpp @@ -292,5 +292,27 @@ TEST(Imgproc_IntersectConvexConvex, not_convex) EXPECT_LE(area, 0.f); } +// The intersection was not properly detected when one line sneaked its way in through an edge point +TEST(Imgproc_IntersectConvexConvex, intersection_at_line_transition) +{ + std::vector convex1 = { + { -1.7604526f, -0.00028443217f }, + {1276.5778f , 0.2091252f}, + {1276.4617f , 719.27f}, + { -1.8754264f, 719.06866f} + + }; + std::vector convex2 = { + { 0.f , 0.f }, + {1280.f , 0.f }, + {1280.f , 720.f}, + { 0.f , 720.f } + }; + std::vector intersection; + + float area = cv::intersectConvexConvex(convex1, convex2, intersection, false); + EXPECT_GE(cv::contourArea(convex1), area); + EXPECT_GE(cv::contourArea(convex2), area); +} } // namespace } // opencv_test