In opencv using houghlines prints only one line

廉价感情. 提交于 2021-01-29 13:55:58

问题


I started following some tutorials on opencv and working on houghlines, and noticed that what ever image I give it would only return one line!

I use opencv 4.2.0, and my code is:

import cv2
import numpy as np

image =cv2.imread("sudoku.jpg")
gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edges=cv2.Canny(gray, 100, 170,apertureSize=3)
cv2.imshow(" lines",edges)
cv2.waitKey()
cv2.destroyAllWindows()

lines=cv2.HoughLines(edges, 1, np.pi/180, 240)

for rho,theta in lines[0]:
    a=np.cos(theta)
    b=np.sin(theta)
    x0=a*rho
    y0=b*rho
    x1=int(x0+1000*(-b))
    y1=int(y0+1000*(a))
    x2=int(x0-1000*(-b))
    y2=int(y0-1000*(a))
    cv2.line(image,(x1,y1),(x2,y2),(255,0,0),2)

cv2.imshow("hough lines",image)
cv2.waitKey()
cv2.destroyAllWindows()

回答1:


Actually, the way data is stored in the lines variable is updated in the newer version of OpenCV due to which you are facing this issue.

Use the below nested for loop instead of you for loop to draw all lines on the image:

for line in lines:
    for rho,theta in line:
        a=np.cos(theta)
        b=np.sin(theta)
        x0=a*rho
        y0=b*rho
        x1=int(x0+1000*(-b))
        y1=int(y0+1000*(a))
        x2=int(x0-1000*(-b))
        y2=int(y0-1000*(a))
        cv2.line(image,(x1,y1),(x2,y2),(255,0,0),2)

To see how the data is stored, you can print lines variable.



来源:https://stackoverflow.com/questions/63111041/in-opencv-using-houghlines-prints-only-one-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!