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