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

标签 opencv houghlinesp houghlines

我想像这样检测图片中的线条: 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/

相关文章:

c++ - OpenCV imwrite() 不保存图像

image-processing - 不规则形状的 OpenCV 质心

python - OpenCV 的 HoughLines 以什么顺序列出 [rho,theta] 矩阵中检测到的线?

opencv - 如何在极坐标中找到垂直线?

python - OpenCV Python : Detecting lines only in ROI

Python OpenCV : Hough Transform does not detect obvious lines

c++ - 在 OpenCV 中查找 SparseMat 的最大和最小位置

opencv - 如何在OpenCV中进行字符匹配

python - 通过霍夫变换检测框

opencv - 使用Hough变换OpenCV Android进行矩形文档检测