Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/machinevisiontoolbox/ImageLineFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
56 changes: 56 additions & 0 deletions tests/test_image_line_features.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python

import unittest
import unittest.mock

import cv2
import numpy as np
import numpy.testing as nt

Expand Down Expand Up @@ -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()
Loading