如何检测绿色的车道和足球场的边界?

时间:2018-08-25 13:53:28

标签: python opencv computer-vision hough-transform

我想以某种方式非常准确地检测绿色通道和足球场的边界。

如果我的代码和结果在这里,但看起来好像我错过了一些东西,我想结果会好得多

img = cv2.imread(im_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

kernel_size = 3

bilarImg = cv2.bilateralFilter(gray,7,7,7)
image_enhanced = cv2.equalizeHist(bilarImg)

plt.imshow(image_enhanced)

masked_edges = cv2.Canny(image_enhanced, 100, 170, apertureSize = 3)

plt.imshow(masked_edges, cmap='gray')

# Define the Hough transform parameters
# Make a blank the same size as our image to draw on
rho = 2 # distance resolution in pixels of the Hough grid
theta = np.pi/180 # angular resolution in radians of the Hough grid
threshold = 110     # minimum number of votes (intersections in Hough grid cell)
min_line_length = 90 #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
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]),
                            min_line_length, max_line_gap)

# Iterate over the output "lines" and draw lines on the blank
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),(255,255,255),2)

# Create a "color" binary image to combine with line image
color_edges = np.dstack((masked_edges, masked_edges, masked_edges)) 

# Draw the lines on the edge image
combo = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0) 

# remove small objects
se1 = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
combo = cv2.morphologyEx(combo, cv2.MORPH_OPEN, se1)

cv2.imwrite("gray.png", image_enhanced)
cv2.imwrite("res.png", combo)

这是src图片: src image

和结果 enter image description here enter image description here

理想情况下,我只会保留绿色车道的田野边界和垂直线。

4 个答案:

答案 0 :(得分:0)

这是一项艰巨的任务。我认为,您应该为此领域找到一个面具。遮罩可帮助您识别现场人员和杂讯(作为按颜色使用分段的选项)。如您所说,您可以使用Hough方法确定行。您必须使用参数才能获得最佳效果。总之,您可以找到每条线的方程式,并计算 tan(a)。使用 tan(a),您可以识别足球场的水平垂直线。

答案 1 :(得分:0)

我建议将色彩空间分为RGB到绿色空间。 您可以使用Split函数。

答案 2 :(得分:0)

我没有尝试。但是我想您可以使用本地二进制模式来区分绿色通道和足球场的边界。这是一个很好的源代码教程,可能会给您一些好主意。 https://www.pyimagesearch.com/2015/12/07/local-binary-patterns-with-python-opencv/

答案 3 :(得分:0)

您可以按照以下步骤获得所需的结果:

1)将RGB彩色图像更改为HSV或HSL等其他域。

2)然后使用更改的色域图像分割出具有绿色的区域。例如:对于H,S和L的特定范围,您将能够从图像中提取绿色。

3)进行形态学封闭操作,以防万一有不连续性或形状不正确(在您的情况下为矩形)。

4)可能会出现一些绿色区域以外的随机噪声,您可以通过轮廓检测并仅选择最大的轮廓(在这种情况下,最大的轮廓将是该字段)来消除这些噪声。

5)现在,一旦获得了ROI(从最大轮廓线上方开始),就可以使用Hough Line变换或线段检测器(LSD)在原始图像中使用此ROI检测直线。

6)如果单条线中有破损,则可以进行线装配并连接单条线的各个部分。

注意:对于上述几乎所有方法,您都可以在OpenCV中找到等效的功能。