Opencv cv2 Hough线的Hough空间
我觉得cv2.Houghlines()在显示垂直线时遇到了一些问题,因为我认为真正适合的应该是水平线。 以下是我正在使用的代码片段:Opencv cv2 Hough线的Hough空间,opencv,computer-vision,transform,hough-transform,Opencv,Computer Vision,Transform,Hough Transform,我觉得cv2.Houghlines()在显示垂直线时遇到了一些问题,因为我认为真正适合的应该是水平线。 以下是我正在使用的代码片段: rho_resoultion = 1 theta_resolution = np.pi/180 threshold = 200 lines = cv2.HoughLines(image, rho_resoultion, theta_resolution, threshold) #
rho_resoultion = 1
theta_resolution = np.pi/180
threshold = 200
lines = cv2.HoughLines(image, rho_resoultion, theta_resolution, threshold)
# print(lines)
for line in lines:
rho, theta = line[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,255,255),1)
cv2.namedWindow('thing', cv2.WINDOW_NORMAL)
cv2.imshow("thing", image)
cv2.waitKey(0)
这是输入和输出:
我认为,如果可以查看Hough空间图像,那么提取发生了什么会更容易。
但是,文档没有提供如何显示整个hough空间的信息。
如何显示整个Hough变换空间?
我试图将阈值降低到1,但它没有提供图像。也许你在计算角度时出错了。请随意显示一些代码 以下是如何在图像中显示所有Hough线的示例:
import cv2
import numpy as np
img = cv2.imread('sudoku.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
lines = cv2.HoughLines(edges,1,np.pi/180,200)
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(img,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imshow('Houghlines',img)
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()
原始图像:
结果:
也许你在计算角度时出错了。请随意显示一些代码 以下是如何在图像中显示所有Hough线的示例:
import cv2
import numpy as np
img = cv2.imread('sudoku.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
lines = cv2.HoughLines(edges,1,np.pi/180,200)
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(img,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imshow('Houghlines',img)
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()
原始图像:
结果:
这实际上与我使用的代码非常相似,但结果非常糟糕。您可以看到我在问题中添加的结果。这实际上与我使用的代码非常相似,但结果非常糟糕。你可以看到我在问题中添加的结果。