gpt4 book ai didi

python-2.7 - OpenCV/Python 中的霍夫线

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

我正在尝试使用 python 中的 opencv 在图像中查找霍夫线。

我的代码是:

import cv2
import numpy as np

img = cv2.imread('DLMIA.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


edges = cv2.Canny(gray,100,200,apertureSize = 3)
cv2.imshow('edges',edges)
cv2.waitKey(0)

minLineLength = 30
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imshow('hough',img)
cv2.waitKey(0)

我使用的图片是this .

我的结果图像是 this .

我的代码示例取自 here .

生成的图像与上一个链接中提到的不同。有什么帮助吗?

最佳答案

我找到了解决方案。

代码示例仅显示了第一条 hough 行。

如果你想打印图像上的所有线条,你必须打印所有线条。

这是修改后的代码:

import cv2
import numpy as np

img = cv2.imread('dave.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


edges = cv2.Canny(gray,100,200,apertureSize = 3)
cv2.imshow('edges',edges)
cv2.waitKey(0)

minLineLength = 30
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,15,minLineLength=minLineLength,maxLineGap=maxLineGap)
for x in range(0, len(lines)):
for x1,y1,x2,y2 in lines[x]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imshow('hough',img)
cv2.waitKey(0)

关于python-2.7 - OpenCV/Python 中的霍夫线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33541551/

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