简体   繁体   English

在 opencv 中使用 houghlines 仅打印一行

[英]In opencv using houghlines prints only one line

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!我开始关注 opencv 上的一些教程并研究 houghlines,并注意到我给它的任何图像都只会返回一行!

I use opencv 4.2.0, and my code is:我使用 opencv 4.2.0,我的代码是:

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()

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.实际上,数据在lines变量中的存储方式在 OpenCV 的较新版本中已更新,因此您面临此问题。

Use the below nested for loop instead of you for loop to draw all lines on the image:使用下面的嵌套 for 循环而不是 for 循环在图像上绘制所有线条:

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.要查看数据是如何存储的,您可以打印lines变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM