gpt4 book ai didi

opencv - 在 opencv 中使用 HoughLines 进行线检测

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

我想像这样检测图片中的线条: enter image description here

我的代码是这样的:

import numpy as np
import cv2
import math

# prepare the image
image = cv2.imread("image")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3,3), 0)
ret3,thesh = cv2.threshold(blurred,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# line detection
rho=1
theta=math.pi/180
thresh = 10
minLength= 30
maxGap= 5
lines = cv2.HoughLinesP(th3.copy(), 1, rho, thresh, minLength, maxGap)
img = image.copy()
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(image,(x1,y1),(x2,y2),(255,0,0),1)

结果是这样的: enter image description here

似乎 HoughLinesP 无法检测到水平线,无论我扭曲了上面的参数值是什么。有没有办法同时检测水平线和垂直线?

非常感谢!

最佳答案

这里有错误的 HoughLinesP 参数。正确的应该是:

lines = cv2.HoughLinesP(th3, rho, theta, thresh, None, minLength, maxGap)

关于opencv - 在 opencv 中使用 HoughLines 进行线检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56079716/

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