From c1fdbd3a47d7b40f31b3ae5388aec0d55b860e55 Mon Sep 17 00:00:00 2001 From: Peter Corke Date: Sun, 22 Nov 2026 20:24:19 +1000 Subject: [PATCH] fix: stop the Hough accumulator looping forever with OpenCV 5.1 OpenCV 4 and 5.0 return None from cv2.HoughLines when it finds no lines, and accumulator() relied on that to stop raising the threshold. OpenCV 5.1 returns an empty array, so the loop never ended. lines() and lines_p() now treat an empty array like None as well. Co-Authored-By: Claude Sonnet 5.5 --- src/machinevisiontoolbox/ImageLineFeatures.py | 11 ++-- tests/test_image_line_features.py | 56 +++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/machinevisiontoolbox/ImageLineFeatures.py b/src/machinevisiontoolbox/ImageLineFeatures.py index 121982ad..05d3356c 100644 --- a/src/machinevisiontoolbox/ImageLineFeatures.py +++ b/src/machinevisiontoolbox/ImageLineFeatures.py @@ -115,7 +115,9 @@ def lines(self, minvotes: int) -> np.ndarray: lines = cv2.HoughLines( image=self.image, rho=self.drho, theta=self.dtheta, threshold=minvotes ) - if lines is None: + # OpenCV 4 and 5.0 return None when there are no lines, 5.1 returns an + # empty array + if lines is None or len(lines) == 0: return np.zeros((0, 2)) else: return np.array((lines[:, 0, 1], lines[:, 0, 0])).T @@ -158,7 +160,7 @@ def lines_p( minLineLength=minlinelength, maxLineGap=maxlinegap, ) - if lines is None: + if lines is None or len(lines) == 0: return np.zeros((0, 4)) # --- OpenCV 4/5 compat ----------------------------------------- # OpenCV 5's HoughLinesP returns a true (N,4) array; OpenCV 4 @@ -233,8 +235,9 @@ def accumulator(self, skip: int = 1) -> None: image=self.image, rho=self.drho, theta=self.dtheta, threshold=t ) - if lines is None: - # no lines found at this threshold, bail out + if lines is None or len(lines) == 0: + # no lines found at this threshold, bail out. OpenCV 5.1 + # returns an empty array, not None, so test for both. self.t = t - 1 break diff --git a/tests/test_image_line_features.py b/tests/test_image_line_features.py index 58513128..9f8dd83c 100644 --- a/tests/test_image_line_features.py +++ b/tests/test_image_line_features.py @@ -1,7 +1,9 @@ #!/usr/bin/env python import unittest +import unittest.mock +import cv2 import numpy as np import numpy.testing as nt @@ -63,6 +65,60 @@ def test_plot_lines(self): pass +class TestHoughNoLines(unittest.TestCase): + """OpenCV 4 and 5.0 return None from HoughLines when nothing is found, + OpenCV 5.1 returns an empty array. The accumulator used to loop forever + on the second.""" + + def setUp(self): + arr = np.zeros((60, 60), dtype="uint8") + arr[30, 10:50] = 255 + self.hough = Image(arr).Hough() + + @staticmethod + def as_opencv_5_1(function): + """Wrap a cv2 Hough function so that "no lines" is an empty array.""" + calls = [] + + def wrapper(*args, **kwargs): + calls.append(1) + if len(calls) > 1000: + raise RuntimeError("the loop did not terminate") + result = function(*args, **kwargs) + if result is None: + shape = (0, 1, 4) if function is cv2.HoughLinesP else (0, 1, 2) + return np.zeros(shape, dtype=np.float32) + return result + + return wrapper + + def test_accumulator_stops_when_there_are_no_more_lines(self): + import cv2 + + with unittest.mock.patch.object( + cv2, "HoughLines", self.as_opencv_5_1(cv2.HoughLines) + ): + self.hough.accumulator() + self.assertIsNotNone(self.hough.A) + self.assertEqual(self.hough.t, 39) # 40 votes survive a threshold of 39 + + def test_lines_with_an_empty_array(self): + import cv2 + + with unittest.mock.patch.object( + cv2, "HoughLines", self.as_opencv_5_1(cv2.HoughLines) + ): + self.assertEqual(self.hough.lines(minvotes=100).shape, (0, 2)) + + def test_lines_p_with_an_empty_array(self): + import cv2 + + with unittest.mock.patch.object( + cv2, "HoughLinesP", self.as_opencv_5_1(cv2.HoughLinesP) + ): + self.assertEqual(self.hough.lines_p(minvotes=100).shape, (0, 4)) + + # ----------------------------------------------------------------------- # if __name__ == "__main__": unittest.main()