gpt4 book ai didi

opencv - cv2 霍夫线的霍夫空间

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:00 24 4
gpt4 key购买 nike

当我认为真正的拟合应该提供水平线时,我在使用 cv2.Houghlines() 显示垂直线时遇到了一些麻烦。这是我正在使用的代码片段:

        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)

这是输入和输出:

我认为,如果可以查看霍夫空间图像,将更容易提取出正在发生的事情。但是,该文档没有提供有关如何显示完整霍夫空间的信息。如何显示整个霍夫变换空间?我尝试将阈值降低到 1,但它没有提供图像。

最佳答案

也许你在计算角度时出错了。请随意展示一些代码。

下面是如何在图像中显示所有霍夫线的示例:

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

原图:

enter image description here

结果:

enter image description here

关于opencv - cv2 霍夫线的霍夫空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37493882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com