From 17faee5d814ff48a2b8810e1d13abd21544027b4 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sun, 20 Dec 2020 02:05:46 +0000 Subject: [PATCH] imgproc: add rotatedRectangleIntersection empty input handling --- modules/imgproc/src/intersection.cpp | 6 ++++++ modules/imgproc/test/test_intersection.cpp | 25 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/modules/imgproc/src/intersection.cpp b/modules/imgproc/src/intersection.cpp index 84dbc8b8f1..3f749896a4 100644 --- a/modules/imgproc/src/intersection.cpp +++ b/modules/imgproc/src/intersection.cpp @@ -54,6 +54,12 @@ int rotatedRectangleIntersection( const RotatedRect& rect1, const RotatedRect& r // L2 metric const float samePointEps = std::max(1e-16f, 1e-6f * (float)std::max(rect1.size.area(), rect2.size.area())); + if (rect1.size.empty() || rect2.size.empty()) + { + intersectingRegion.release(); + return INTERSECT_NONE; + } + Point2f vec1[4], vec2[4]; Point2f pts1[4], pts2[4]; diff --git a/modules/imgproc/test/test_intersection.cpp b/modules/imgproc/test/test_intersection.cpp index 93909b3a9e..7527dd9a22 100644 --- a/modules/imgproc/test/test_intersection.cpp +++ b/modules/imgproc/test/test_intersection.cpp @@ -366,4 +366,29 @@ TEST(Imgproc_RotatedRectangleIntersection, regression_12221_2) EXPECT_LE(intersections.size(), (size_t)8); } +TEST(Imgproc_RotatedRectangleIntersection, regression_18520) +{ + RotatedRect rr_empty( + Point2f(2, 2), + Size2f(0, 0), // empty + 0); + RotatedRect rr( + Point2f(50, 50), + Size2f(4, 4), + 0); + + { + std::vector intersections; + int interType = cv::rotatedRectangleIntersection(rr_empty, rr, intersections); + EXPECT_EQ(INTERSECT_NONE, interType) << "rr_empty, rr"; + EXPECT_EQ((size_t)0, intersections.size()) << "rr_empty, rr"; + } + { + std::vector intersections; + int interType = cv::rotatedRectangleIntersection(rr, rr_empty, intersections); + EXPECT_EQ(INTERSECT_NONE, interType) << "rr, rr_empty"; + EXPECT_EQ((size_t)0, intersections.size()) << "rr, rr_empty"; + } +} + }} // namespace