From 94957e4e64a12990b8e9e40858b526992d90d05e Mon Sep 17 00:00:00 2001 From: Shaanganesh Date: Mon, 16 Nov 2026 12:44:15 +0530 Subject: [PATCH] imgproc: prevent sub-threshold circles from overwriting valid circles in HoughCircles --- modules/imgproc/src/hough.cpp | 5 +++-- modules/imgproc/test/test_houghcircles.cpp | 26 ++++++++++++++++++++++ modules/python/test/test_houghcircles.py | 5 +++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/modules/imgproc/src/hough.cpp b/modules/imgproc/src/hough.cpp index b9731acc89..426bba517e 100644 --- a/modules/imgproc/src/hough.cpp +++ b/modules/imgproc/src/hough.cpp @@ -1528,8 +1528,9 @@ protected: curCount += bins[j]; } float rCur = (upbin + j)/2.f /nBinsPerDr * dr + minRadius; - if((curCount * rBest >= maxCount * rCur) || - (rBest < FLT_EPSILON && curCount >= maxCount)) + if(curCount > accThreshold && + ((curCount * rBest >= maxCount * rCur) || + (rBest < FLT_EPSILON && curCount >= maxCount))) { rBest = rCur; maxCount = curCount; diff --git a/modules/imgproc/test/test_houghcircles.cpp b/modules/imgproc/test/test_houghcircles.cpp index 74608112a7..fa43d48b85 100644 --- a/modules/imgproc/test/test_houghcircles.cpp +++ b/modules/imgproc/test/test_houghcircles.cpp @@ -318,4 +318,30 @@ TEST_P(HoughCirclesTest, ManySmallCircles) INSTANTIATE_TEST_CASE_P(HoughGradient, HoughCirclesTest, testing::Values(HOUGH_GRADIENT)); INSTANTIATE_TEST_CASE_P(HoughGradientAlt, HoughCirclesTest, testing::Values(HOUGH_GRADIENT_ALT)); +TEST(Imgproc_HoughCircles, regression_23423) +{ + Mat img = Mat::zeros(200, 200, CV_8UC1); + Point center(100, 100); + + // Outer circle at R = 60 (~130 count > accThreshold = 40) + ellipse(img, center, Size(60,60), 0, 0, 270, Scalar(255), 1); + // Inner circle at r = 10 (~31 count < accThreshold = 40) + circle(img, center, 10, Scalar(255), -1); + + std::vector circles; + HoughCircles(img, circles, HOUGH_GRADIENT, 1.0, 20.0, 100.0, 40.0, 5, 80); + + bool foundOuterCircle = false; + for (size_t i = 0; i < circles.size(); i++) + { + if (std::abs(circles[i][0] - 100.5f) <= 2.0f && + std::abs(circles[i][1] - 100.5f) <= 2.0f && + std::abs(circles[i][2] - 60.0f) <= 2.0f) + { + foundOuterCircle = true; + break; + } + } + EXPECT_TRUE(foundOuterCircle) << "Detected circles: " << Mat(circles); +} }} // namespace diff --git a/modules/python/test/test_houghcircles.py b/modules/python/test/test_houghcircles.py index 942e2c8e34..cf27df9eed 100644 --- a/modules/python/test/test_houghcircles.py +++ b/modules/python/test/test_houghcircles.py @@ -44,7 +44,7 @@ class houghcircles_test(NewOpenCVTests): img = cv.cvtColor(src, cv.COLOR_BGR2GRAY) img = cv.medianBlur(img, 5) - circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30)[0] + circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 20)[0] testCircles = [[38, 181, 17.6], [99.7, 166, 13.12], @@ -90,9 +90,10 @@ class houghcircles_test(NewOpenCVTests): minRadius=1, maxRadius=30) - self.assertEqual(circles_acc.shape, (1, 2, 4)) + self.assertEqual(circles_acc.shape, (1, 3, 4)) self.assertEqual(circles_acc[0, 0, 3], 66.) self.assertEqual(circles_acc[0, 1, 3], 62.) + self.assertEqual(circles_acc[0, 2, 3], 52.) def test_houghcircles_alt(self):