python - 如何检测 OpenCV 中的线条?

标签 python opencv image-processing

我正在尝试检测 parking 线,如下所示。

Empty parking lot

我希望得到的是交叉线上的清晰线条和 (x,y) 位置。但是,结果并不乐观。

Parking lot with Hough Lines drawn

我猜主要有两个原因:

  1. 有些线条非常破损或缺失。就连人眼也能清晰 识别它们。即使 HoughLine 可以帮助连接一些缺失的 线,因为 HoughLine 有时会连接不必要的线 一起,我宁愿手动做。

  2. 有一些重复的行。

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

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

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) 

Binary image

2。重复膨胀和腐 eclipse ,直到图像不能改变(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)

 After the erosion and dialation

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

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)

After Canny

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

最佳答案

这是我的管道,也许它可以给你一些帮助。

首先,得到灰度图,处理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)

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

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

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

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)

最后,在你的 srcImage 上画线。

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

这是我最后的表演。

最终图像:

enter image description here

关于python - 如何检测 OpenCV 中的线条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322630/

相关文章:

python 基于标准的计数示例

python - 将带有 2 层定界符的字符串转换为字典 - python

python - 如何根据连续数据的存在来绘制线条

android - 在android中使用opencv加载YAML模型文件时出错

python - 将多个返回语句拆分为不同的函数

c++ - core_c.h的OpenCV包含错误

python - 预测鼠标书写的数字

Python - 在不使用内置旋转方法的情况下旋转图像

image-processing - 单个位图图像的 H.264/H.265 压缩

Python OpenCV 如何在矩形内绘制图像的矩形中心和裁剪图像?