1

我想检测这样的图片中的线条: 在此处输入图像描述

我的代码是这样的:

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)

结果是这样的: 在此处输入图像描述

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

非常感谢 !

4

2 回答 2

0

如果查找线只是客观的,那么我建议您使用Sobel运算符,因为它会检测 x 和 y 方向的梯度。

步骤:
- 将图像转换为-在 x 和 y 方向上 grayscale
获取操作员实现的图像 在 y 方向上的 x 方向 这将分别为您提供垂直线和水平线 - 创建可用于执行二进制掩码的缩放图像。- 创建一个像这样的二进制掩码,其中 scax = x 中的缩放 sobel 图像,而 scay 是 y 中的缩放 sobel 图像。sobelcv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
np.uint8sbin[(scax >= 90) & (scax <= 150) | (scay >= 90) & (scay <= 150)] = 1

您可以使用最小和最大阈值来获得线渐变。

您将看到这样的输出,检测到线条的边缘: 在此处输入图像描述

于 2019-05-10T17:52:42.680 回答
0

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

lines = cv2.HoughLinesP(th3, rho, theta, thresh, None, minLength, maxGap)
于 2020-09-09T10:22:54.713 回答