本文介绍了如何检测 OpenCV 中的线条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测停车线,如下所示.

我希望得到的是清晰的线条和交叉线中的(x,y)位置.然而,结果并不是很有希望.

我想主要有两个原因:

  1. 有些行非常破碎或缺失.就连人眼也能看清楚识别它们.尽管 HoughLine 可以帮助连接一些缺失的线,因为 HoughLine 有时会连接不必要的线在一起,我宁愿手动完成.

  2. 有一些重复的行.

工作的一般管道如下所示:

1.选择一些特定的颜色(白色或黄色)

导入 cv2将 numpy 导入为 np导入 matplotlib从 matplotlib.pyplot 导入 imshow从 matplotlib 导入 pyplot 作为 plt# 白色遮罩img = cv2.imread(filein)#converted = convert_hls(img)图像 = cv2.cvtColor(img,cv2.COLOR_BGR2HLS)下 = np.uint8([0, 200, 0])上 = np.uint8([255, 255, 255])white_mask = cv2.inRange(图像,下,上)#黄色遮罩下 = np.uint8([10, 0, 100])上 = np.uint8([40, 255, 255])Yellow_mask = cv2.inRange(图像,下,上)# 合并掩码掩码 = cv2.bitwise_or(white_mask,yellow_mask)结果 = img.copy()cv2.imshow(面具",面具)

2.重复膨胀和腐蚀,直到图像无法改变(

3.应用canny过滤线条并使用HoughLinesP获取线条

edges = cv2.Canny(skel, 50, 150)cv2.imshow(边缘",边缘)线 = cv2.HoughLinesP(edges,1,np.pi/180,40,minLineLength=30,maxLineGap=30)我 = 0对于行 [0] 中的 x1,y1,x2,y2:我+=1cv2.line(result,(x1,y1),(x2,y2),(255,0,0),1)打印我cv2.imshow(res",result)cv2.waitKey(0)

我想知道为什么在选择某种颜色的第一步后,线条被打破并且有噪音.我认为在这一步中我们应该做一些事情来使虚线成为一条完整的、噪音较小的线.然后尝试应用一些东西来做 Canny 和 Hough 线.有什么想法吗?

解决方案

这是我的管道,也许可以帮到你.

首先,获取灰度图并进行高斯模糊处理.

img = cv2.imread('src.png')灰色 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)内核大小 = 5模糊灰色 = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)

二、处理边缘检测使用Canny.

low_threshold = 50高阈值 = 150边缘 = cv2.Canny(blur_gray,low_threshold,high_threshold)

然后,使用 HoughLinesP 获取线条.您可以调整参数以获得更好的性能.

rho = 1 # 霍夫网格的像素距离分辨率theta = np.pi/180 # 霍夫网格的弧度角分辨率阈值 = 15 # 最小投票数(霍夫网格单元中的交叉点)min_line_length = 50 # 组成一条线的最小像素数max_line_gap = 20 # 可连接线段之间的最大像素间距line_image = np.copy(img) * 0 # 创建一个空白来绘制线条# 在边缘检测到的图像上运行 Hough# 输出lines"是一个包含检测到的线段端点的数组lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),min_line_length, max_line_gap)对于线中线:对于 x1,y1,x2,y2 在线:cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)

最后,在 srcImage 上绘制线条.

# 在图像上绘制线条lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)

这是我最后的表演.

最终图像:

I am trying to detect lines in parking as shown below.

What I hope to get is the clear lines and (x,y) position in the crossed line. However, the result is not very promising.

I guess it is due to two main reasons:

  1. Some lines are very broken or missing. Even human eyes can clearlyidentify them. Even though HoughLine can help to connect some missinglines, since HoughLine sometimes would connect unnecessary linestogether, I 'd rather to do it manually.

  2. There are some repeated lines.

The general pipeline for the work is shown as below:

1. Select some specific colors (white or yellow)

import cv2
import numpy as np
import matplotlib
from matplotlib.pyplot import imshow
from matplotlib import pyplot as plt

# white color mask
img = cv2.imread(filein)
#converted = convert_hls(img)
image = cv2.cvtColor(img,cv2.COLOR_BGR2HLS)
lower = np.uint8([0, 200, 0])
upper = np.uint8([255, 255, 255])
white_mask = cv2.inRange(image, lower, upper)
# yellow color mask
lower = np.uint8([10, 0,   100])
upper = np.uint8([40, 255, 255])
yellow_mask = cv2.inRange(image, lower, upper)
# combine the mask
mask = cv2.bitwise_or(white_mask, yellow_mask)
result = img.copy()
cv2.imshow("mask",mask)

2. Repeat the dilation and erosion until the image can not be changed (reference )

height,width = mask.shape
skel = np.zeros([height,width],dtype=np.uint8)      #[height,width,3]
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
temp_nonzero = np.count_nonzero(mask)
while(np.count_nonzero(mask) != 0 ):
    eroded = cv2.erode(mask,kernel)
    cv2.imshow("eroded",eroded)
    temp = cv2.dilate(eroded,kernel)
    cv2.imshow("dilate",temp)
    temp = cv2.subtract(mask,temp)
    skel = cv2.bitwise_or(skel,temp)
    mask = eroded.copy()

cv2.imshow("skel",skel)
#cv2.waitKey(0)

3. Apply the canny to filter the lines and use HoughLinesP to get the lines

edges = cv2.Canny(skel, 50, 150)
cv2.imshow("edges",edges)
lines = cv2.HoughLinesP(edges,1,np.pi/180,40,minLineLength=30,maxLineGap=30)
i = 0
for x1,y1,x2,y2 in lines[0]:
    i+=1
    cv2.line(result,(x1,y1),(x2,y2),(255,0,0),1)
print i

cv2.imshow("res",result)
cv2.waitKey(0)

I wonder why after the first step of selecting certain color, the lines are broken and with noises. I would think in this step we should do something to make the broken line a complete, less noisy line. And then try to apply something to do the Canny and Hough lines. Any ideas?

解决方案

Here is my pipeline, maybe it can give you some help.

First, get the gray image and process GaussianBlur.

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

kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)

Second, process edge detection use Canny.

low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)

Then, use HoughLinesP to get the lines. You can adjust the parameters for better performance.

rho = 1  # distance resolution in pixels of the Hough grid
theta = np.pi / 180  # angular resolution in radians of the Hough grid
threshold = 15  # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50  # minimum number of pixels making up a line
max_line_gap = 20  # maximum gap in pixels between connectable line segments
line_image = np.copy(img) * 0  # creating a blank to draw lines on

# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                    min_line_length, max_line_gap)

for line in lines:
    for x1,y1,x2,y2 in line:
    cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)

Finally, draw the lines on your srcImage.

# Draw the lines on the  image
lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)

Here is my final performance.

Final Image:

这篇关于如何检测 OpenCV 中的线条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:07