From 8a0ea789e7eb0f205a37f89106fda771a6b17aeb Mon Sep 17 00:00:00 2001 From: Liane Lin <134530442+liane-lin@users.noreply.github.com> Date: Fri, 30 May 2025 13:20:01 +0100 Subject: [PATCH] Merge pull request #27149 from liane-lin:4.x Fix #25696: Solved the problem in Subdiv2D, empty delaunay triangulation #27149 Detailed description Expected behaviour: Given 4 points, where no three points are collinear, the Delaunay Triangulation Algorithm should return 2 triangles. Actual: The algorithm returns zero triangles in this particular case. Fix: The radius of the circumcircle tends to infinity when the points are closer to form collinear points, so the problem occurs because the super-triangles are not large enough, which then results in certain edges are not swapped. The proposed solution just increases the super triangle, duplicating the value of constant for example. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/imgproc/src/subdivision2d.cpp | 2 +- modules/imgproc/test/test_subdivision2d.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/modules/imgproc/src/subdivision2d.cpp b/modules/imgproc/src/subdivision2d.cpp index 2a2ab1dfeb..86a0f72852 100644 --- a/modules/imgproc/src/subdivision2d.cpp +++ b/modules/imgproc/src/subdivision2d.cpp @@ -493,7 +493,7 @@ void Subdiv2D::initDelaunay( Rect rect ) { CV_INSTRUMENT_REGION(); - float big_coord = 3.f * MAX( rect.width, rect.height ); + float big_coord = 6.f * MAX( rect.width, rect.height ); float rx = (float)rect.x; float ry = (float)rect.y; diff --git a/modules/imgproc/test/test_subdivision2d.cpp b/modules/imgproc/test/test_subdivision2d.cpp index 17549b6b15..a940adfc55 100644 --- a/modules/imgproc/test/test_subdivision2d.cpp +++ b/modules/imgproc/test/test_subdivision2d.cpp @@ -50,4 +50,18 @@ TEST(Imgproc_Subdiv2D_getTriangleList, regression_5788) EXPECT_EQ(trig_cnt, 105); } +TEST(Imgproc_Subdiv2D, issue_25696) { + std::vector points{ + {0, 0}, {40, 40}, {84, 104}, {86, 108} + }; + + cv::Rect subdivRect{cv::Point{-10, -10}, cv::Point{96, 118}}; + cv::Subdiv2D subdiv{subdivRect}; + subdiv.insert(points); + + std::vector triangles; + subdiv.getTriangleList(triangles); + + ASSERT_EQ(static_cast(2), triangles.size()); +} }}