Skip to content

Commit 08b6abd

Browse files
committed
Merge pull request opencv#19068 from alalek:issue_18157
2 parents 33761ee + 3e5d7e1 commit 08b6abd

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

modules/imgproc/misc/java/test/ImgprocTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,8 +1405,8 @@ public void testMinAreaRect() {
14051405

14061406
RotatedRect rrect = Imgproc.minAreaRect(points);
14071407

1408-
assertEquals(new Size(2, 5), rrect.size);
1409-
assertEquals(-90., rrect.angle);
1408+
assertEquals(new Size(5, 2), rrect.size);
1409+
assertEquals(0., rrect.angle);
14101410
assertEquals(new Point(3.5, 2), rrect.center);
14111411
}
14121412

modules/imgproc/src/rotcalipers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ cv::RotatedRect cv::minAreaRect( InputArray _points )
352352
Point2f out[3];
353353
RotatedRect box;
354354

355-
convexHull(_points, hull, true, true);
355+
convexHull(_points, hull, false, true);
356356

357357
if( hull.depth() != CV_32F )
358358
{

modules/imgproc/test/test_convhull.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,5 +2306,83 @@ TEST(Imgproc_ConvexHull, overflow)
23062306
ASSERT_EQ(hull, hullf);
23072307
}
23082308

2309+
static
2310+
bool checkMinAreaRect(const RotatedRect& rr, const Mat& c, double eps = 0.5f)
2311+
{
2312+
int N = c.rows;
2313+
2314+
Mat rr_pts;
2315+
boxPoints(rr, rr_pts);
2316+
2317+
double maxError = 0.0;
2318+
int nfailed = 0;
2319+
for (int i = 0; i < N; i++)
2320+
{
2321+
double d = pointPolygonTest(rr_pts, c.at<Point2f>(i), true);
2322+
maxError = std::max(-d, maxError);
2323+
if (d < -eps)
2324+
nfailed++;
2325+
}
2326+
2327+
if (nfailed)
2328+
std::cout << "nfailed=" << nfailed << " (total=" << N << ") maxError=" << maxError << std::endl;
2329+
return nfailed == 0;
2330+
}
2331+
2332+
TEST(Imgproc_minAreaRect, reproducer_18157)
2333+
{
2334+
const int N = 168;
2335+
float pts_[N][2] = {
2336+
{ 1903, 266 }, { 1897, 267 }, { 1893, 268 }, { 1890, 269 },
2337+
{ 1878, 275 }, { 1875, 277 }, { 1872, 279 }, { 1868, 282 },
2338+
{ 1862, 287 }, { 1750, 400 }, { 1748, 402 }, { 1742, 407 },
2339+
{ 1742, 408 }, { 1740, 410 }, { 1738, 412 }, { 1593, 558 },
2340+
{ 1590, 560 }, { 1588, 562 }, { 1586, 564 }, { 1580, 570 },
2341+
{ 1443, 709 }, { 1437, 714 }, { 1435, 716 }, { 1304, 848 },
2342+
{ 1302, 850 }, { 1292, 860 }, { 1175, 979 }, { 1172, 981 },
2343+
{ 1049, 1105 }, { 936, 1220 }, { 933, 1222 }, { 931, 1224 },
2344+
{ 830, 1326 }, { 774, 1383 }, { 769, 1389 }, { 766, 1393 },
2345+
{ 764, 1396 }, { 762, 1399 }, { 760, 1402 }, { 757, 1408 },
2346+
{ 757, 1410 }, { 755, 1413 }, { 754, 1416 }, { 753, 1420 },
2347+
{ 752, 1424 }, { 752, 1442 }, { 753, 1447 }, { 754, 1451 },
2348+
{ 755, 1454 }, { 757, 1457 }, { 757, 1459 }, { 761, 1467 },
2349+
{ 763, 1470 }, { 765, 1473 }, { 767, 1476 }, { 771, 1481 },
2350+
{ 779, 1490 }, { 798, 1510 }, { 843, 1556 }, { 847, 1560 },
2351+
{ 851, 1564 }, { 863, 1575 }, { 907, 1620 }, { 909, 1622 },
2352+
{ 913, 1626 }, { 1154, 1866 }, { 1156, 1868 }, { 1158, 1870 },
2353+
{ 1207, 1918 }, { 1238, 1948 }, { 1252, 1961 }, { 1260, 1968 },
2354+
{ 1264, 1971 }, { 1268, 1974 }, { 1271, 1975 }, { 1273, 1977 },
2355+
{ 1283, 1982 }, { 1286, 1983 }, { 1289, 1984 }, { 1294, 1985 },
2356+
{ 1300, 1986 }, { 1310, 1986 }, { 1316, 1985 }, { 1320, 1984 },
2357+
{ 1323, 1983 }, { 1326, 1982 }, { 1338, 1976 }, { 1341, 1974 },
2358+
{ 1344, 1972 }, { 1349, 1968 }, { 1358, 1960 }, { 1406, 1911 },
2359+
{ 1421, 1897 }, { 1624, 1693 }, { 1788, 1528 }, { 1790, 1526 },
2360+
{ 1792, 1524 }, { 1794, 1522 }, { 1796, 1520 }, { 1798, 1518 },
2361+
{ 1800, 1516 }, { 1919, 1396 }, { 1921, 1394 }, { 2038, 1275 },
2362+
{ 2047, 1267 }, { 2048, 1265 }, { 2145, 1168 }, { 2148, 1165 },
2363+
{ 2260, 1052 }, { 2359, 952 }, { 2434, 876 }, { 2446, 863 },
2364+
{ 2450, 858 }, { 2453, 854 }, { 2455, 851 }, { 2457, 846 },
2365+
{ 2459, 844 }, { 2460, 842 }, { 2460, 840 }, { 2462, 837 },
2366+
{ 2463, 834 }, { 2464, 830 }, { 2465, 825 }, { 2465, 809 },
2367+
{ 2464, 804 }, { 2463, 800 }, { 2462, 797 }, { 2461, 794 },
2368+
{ 2456, 784 }, { 2454, 781 }, { 2452, 778 }, { 2450, 775 },
2369+
{ 2446, 770 }, { 2437, 760 }, { 2412, 734 }, { 2410, 732 },
2370+
{ 2408, 730 }, { 2382, 704 }, { 2380, 702 }, { 2378, 700 },
2371+
{ 2376, 698 }, { 2372, 694 }, { 2370, 692 }, { 2368, 690 },
2372+
{ 2366, 688 }, { 2362, 684 }, { 2360, 682 }, { 2252, 576 },
2373+
{ 2250, 573 }, { 2168, 492 }, { 2166, 490 }, { 2085, 410 },
2374+
{ 2026, 352 }, { 1988, 315 }, { 1968, 296 }, { 1958, 287 },
2375+
{ 1953, 283 }, { 1949, 280 }, { 1946, 278 }, { 1943, 276 },
2376+
{ 1940, 274 }, { 1936, 272 }, { 1934, 272 }, { 1931, 270 },
2377+
{ 1928, 269 }, { 1925, 268 }, { 1921, 267 }, { 1915, 266 }
2378+
};
2379+
2380+
Mat contour(N, 1, CV_32FC2, (void*)pts_);
2381+
2382+
RotatedRect rr = cv::minAreaRect(contour);
2383+
2384+
EXPECT_TRUE(checkMinAreaRect(rr, contour)) << rr.center << " " << rr.size << " " << rr.angle;
2385+
}
2386+
23092387
}} // namespace
23102388
/* End of file. */

modules/python/test/test_legacy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_geometry(self):
7676
mc, mr = cv.minEnclosingCircle(a)
7777

7878
be0 = ((150.2511749267578, 150.77322387695312), (158.024658203125, 197.57696533203125), 37.57804489135742)
79-
br0 = ((161.2974090576172, 154.41793823242188), (199.2301483154297, 207.7177734375), -9.164555549621582)
79+
br0 = ((161.2974090576172, 154.41793823242188), (207.7177734375, 199.2301483154297), 80.83544921875)
8080
mc0, mr0 = (160.41790771484375, 144.55152893066406), 136.713500977
8181

8282
self.check_close_boxes(be, be0, 5, 15)

0 commit comments

Comments
 (0)