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.
pull/6622/head
Aleksandar Atanasov 9 years ago
parent cf0df733da
commit 14deab252b
  1. 21
      samples/python/houghlines.py

@ -36,16 +36,17 @@ if __name__ == '__main__':
else: # HoughLines else: # HoughLines
lines = cv2.HoughLines(dst, 1, math.pi/180.0, 50, np.array([]), 0, 0) lines = cv2.HoughLines(dst, 1, math.pi/180.0, 50, np.array([]), 0, 0)
a,b,c = lines.shape if lines != None:
for i in range(a): a,b,c = lines.shape
rho = lines[i][0][0] for i in range(a):
theta = lines[i][0][1] rho = lines[i][0][0]
a = math.cos(theta) theta = lines[i][0][1]
b = math.sin(theta) a = math.cos(theta)
x0, y0 = a*rho, b*rho b = math.sin(theta)
pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) ) x0, y0 = a*rho, b*rho
pt2 = ( int(x0-1000*(-b)), int(y0-1000*(a)) ) pt1 = ( int(x0+1000*(-b)), int(y0+1000*(a)) )
cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA) 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("source", src)
cv2.imshow("detected lines", cdst) cv2.imshow("detected lines", cdst)

Loading…
Cancel
Save