From 14deab252bc50f2f99d0944bce3d80b04450ea1b Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Thu, 2 Jun 2016 11:00:23 +0200 Subject: [PATCH] Fix houghlines.py when no lines found In the C++ equivalent of this example a check is made whether the vector (here in Python we have a list) actually has any lines in it that is whether the Hough lines function has managed to find any in the given image. This check is missing for the Python example and if no lines are found the application breaks. --- samples/python/houghlines.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/samples/python/houghlines.py b/samples/python/houghlines.py index 120d8bbaae..98c3640147 100755 --- a/samples/python/houghlines.py +++ b/samples/python/houghlines.py @@ -36,16 +36,17 @@ if __name__ == '__main__': else: # HoughLines lines = cv2.HoughLines(dst, 1, math.pi/180.0, 50, np.array([]), 0, 0) - a,b,c = lines.shape - for i in range(a): - rho = lines[i][0][0] - theta = lines[i][0][1] - a = math.cos(theta) - b = math.sin(theta) - x0, y0 = a*rho, b*rho - pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) ) - pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) ) - cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA) + if lines != None: + a,b,c = lines.shape + for i in range(a): + rho = lines[i][0][0] + theta = lines[i][0][1] + a = math.cos(theta) + b = math.sin(theta) + x0, y0 = a*rho, b*rho + pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) ) + pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) ) + cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA) cv2.imshow("source", src) cv2.imshow("detected lines", cdst)