From 3e5d7e1718cf6470c34fb29db9d913d6cd352168 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 10 Dec 2020 08:07:50 +0000 Subject: [PATCH] imgproc: fix minAreaRect() --- .../imgproc/misc/java/test/ImgprocTest.java | 4 +- modules/imgproc/src/rotcalipers.cpp | 2 +- modules/imgproc/test/test_convhull.cpp | 78 +++++++++++++++++++ modules/python/test/test_legacy.py | 2 +- 4 files changed, 82 insertions(+), 4 deletions(-) diff --git a/modules/imgproc/misc/java/test/ImgprocTest.java b/modules/imgproc/misc/java/test/ImgprocTest.java index 5734b06d60..9ea58591bc 100644 --- a/modules/imgproc/misc/java/test/ImgprocTest.java +++ b/modules/imgproc/misc/java/test/ImgprocTest.java @@ -1405,8 +1405,8 @@ public class ImgprocTest extends OpenCVTestCase { RotatedRect rrect = Imgproc.minAreaRect(points); - assertEquals(new Size(2, 5), rrect.size); - assertEquals(-90., rrect.angle); + assertEquals(new Size(5, 2), rrect.size); + assertEquals(0., rrect.angle); assertEquals(new Point(3.5, 2), rrect.center); } diff --git a/modules/imgproc/src/rotcalipers.cpp b/modules/imgproc/src/rotcalipers.cpp index 6a327b5b39..527f71a247 100644 --- a/modules/imgproc/src/rotcalipers.cpp +++ b/modules/imgproc/src/rotcalipers.cpp @@ -352,7 +352,7 @@ cv::RotatedRect cv::minAreaRect( InputArray _points ) Point2f out[3]; RotatedRect box; - convexHull(_points, hull, true, true); + convexHull(_points, hull, false, true); if( hull.depth() != CV_32F ) { diff --git a/modules/imgproc/test/test_convhull.cpp b/modules/imgproc/test/test_convhull.cpp index 5e353286fe..f1d739b0e0 100644 --- a/modules/imgproc/test/test_convhull.cpp +++ b/modules/imgproc/test/test_convhull.cpp @@ -2306,5 +2306,83 @@ TEST(Imgproc_ConvexHull, overflow) ASSERT_EQ(hull, hullf); } +static +bool checkMinAreaRect(const RotatedRect& rr, const Mat& c, double eps = 0.5f) +{ + int N = c.rows; + + Mat rr_pts; + boxPoints(rr, rr_pts); + + double maxError = 0.0; + int nfailed = 0; + for (int i = 0; i < N; i++) + { + double d = pointPolygonTest(rr_pts, c.at(i), true); + maxError = std::max(-d, maxError); + if (d < -eps) + nfailed++; + } + + if (nfailed) + std::cout << "nfailed=" << nfailed << " (total=" << N << ") maxError=" << maxError << std::endl; + return nfailed == 0; +} + +TEST(Imgproc_minAreaRect, reproducer_18157) +{ + const int N = 168; + float pts_[N][2] = { + { 1903, 266 }, { 1897, 267 }, { 1893, 268 }, { 1890, 269 }, + { 1878, 275 }, { 1875, 277 }, { 1872, 279 }, { 1868, 282 }, + { 1862, 287 }, { 1750, 400 }, { 1748, 402 }, { 1742, 407 }, + { 1742, 408 }, { 1740, 410 }, { 1738, 412 }, { 1593, 558 }, + { 1590, 560 }, { 1588, 562 }, { 1586, 564 }, { 1580, 570 }, + { 1443, 709 }, { 1437, 714 }, { 1435, 716 }, { 1304, 848 }, + { 1302, 850 }, { 1292, 860 }, { 1175, 979 }, { 1172, 981 }, + { 1049, 1105 }, { 936, 1220 }, { 933, 1222 }, { 931, 1224 }, + { 830, 1326 }, { 774, 1383 }, { 769, 1389 }, { 766, 1393 }, + { 764, 1396 }, { 762, 1399 }, { 760, 1402 }, { 757, 1408 }, + { 757, 1410 }, { 755, 1413 }, { 754, 1416 }, { 753, 1420 }, + { 752, 1424 }, { 752, 1442 }, { 753, 1447 }, { 754, 1451 }, + { 755, 1454 }, { 757, 1457 }, { 757, 1459 }, { 761, 1467 }, + { 763, 1470 }, { 765, 1473 }, { 767, 1476 }, { 771, 1481 }, + { 779, 1490 }, { 798, 1510 }, { 843, 1556 }, { 847, 1560 }, + { 851, 1564 }, { 863, 1575 }, { 907, 1620 }, { 909, 1622 }, + { 913, 1626 }, { 1154, 1866 }, { 1156, 1868 }, { 1158, 1870 }, + { 1207, 1918 }, { 1238, 1948 }, { 1252, 1961 }, { 1260, 1968 }, + { 1264, 1971 }, { 1268, 1974 }, { 1271, 1975 }, { 1273, 1977 }, + { 1283, 1982 }, { 1286, 1983 }, { 1289, 1984 }, { 1294, 1985 }, + { 1300, 1986 }, { 1310, 1986 }, { 1316, 1985 }, { 1320, 1984 }, + { 1323, 1983 }, { 1326, 1982 }, { 1338, 1976 }, { 1341, 1974 }, + { 1344, 1972 }, { 1349, 1968 }, { 1358, 1960 }, { 1406, 1911 }, + { 1421, 1897 }, { 1624, 1693 }, { 1788, 1528 }, { 1790, 1526 }, + { 1792, 1524 }, { 1794, 1522 }, { 1796, 1520 }, { 1798, 1518 }, + { 1800, 1516 }, { 1919, 1396 }, { 1921, 1394 }, { 2038, 1275 }, + { 2047, 1267 }, { 2048, 1265 }, { 2145, 1168 }, { 2148, 1165 }, + { 2260, 1052 }, { 2359, 952 }, { 2434, 876 }, { 2446, 863 }, + { 2450, 858 }, { 2453, 854 }, { 2455, 851 }, { 2457, 846 }, + { 2459, 844 }, { 2460, 842 }, { 2460, 840 }, { 2462, 837 }, + { 2463, 834 }, { 2464, 830 }, { 2465, 825 }, { 2465, 809 }, + { 2464, 804 }, { 2463, 800 }, { 2462, 797 }, { 2461, 794 }, + { 2456, 784 }, { 2454, 781 }, { 2452, 778 }, { 2450, 775 }, + { 2446, 770 }, { 2437, 760 }, { 2412, 734 }, { 2410, 732 }, + { 2408, 730 }, { 2382, 704 }, { 2380, 702 }, { 2378, 700 }, + { 2376, 698 }, { 2372, 694 }, { 2370, 692 }, { 2368, 690 }, + { 2366, 688 }, { 2362, 684 }, { 2360, 682 }, { 2252, 576 }, + { 2250, 573 }, { 2168, 492 }, { 2166, 490 }, { 2085, 410 }, + { 2026, 352 }, { 1988, 315 }, { 1968, 296 }, { 1958, 287 }, + { 1953, 283 }, { 1949, 280 }, { 1946, 278 }, { 1943, 276 }, + { 1940, 274 }, { 1936, 272 }, { 1934, 272 }, { 1931, 270 }, + { 1928, 269 }, { 1925, 268 }, { 1921, 267 }, { 1915, 266 } + }; + + Mat contour(N, 1, CV_32FC2, (void*)pts_); + + RotatedRect rr = cv::minAreaRect(contour); + + EXPECT_TRUE(checkMinAreaRect(rr, contour)) << rr.center << " " << rr.size << " " << rr.angle; +} + }} // namespace /* End of file. */ diff --git a/modules/python/test/test_legacy.py b/modules/python/test/test_legacy.py index db6e1c4f38..ab0a8bdc35 100644 --- a/modules/python/test/test_legacy.py +++ b/modules/python/test/test_legacy.py @@ -76,7 +76,7 @@ class Hackathon244Tests(NewOpenCVTests): mc, mr = cv.minEnclosingCircle(a) be0 = ((150.2511749267578, 150.77322387695312), (158.024658203125, 197.57696533203125), 37.57804489135742) - br0 = ((161.2974090576172, 154.41793823242188), (199.2301483154297, 207.7177734375), -9.164555549621582) + br0 = ((161.2974090576172, 154.41793823242188), (207.7177734375, 199.2301483154297), 80.83544921875) mc0, mr0 = (160.41790771484375, 144.55152893066406), 136.713500977 self.check_close_boxes(be, be0, 5, 15)