优达学城自动驾驶汽车-Project 1: Finding Lane Lines 学习笔记1

根据像素的亮度来识别车

车道识别对于Computer vision 来说是一个相对简单的任务。原因是车道一般是白色或黄色 并且很容易和图片中其他的像素区别开来。
在RGB 三信道 的图片中, 白色有很高的R, G, B 的像素值。 所以我们就可以根据这个来threshold并且把低于threshold的像素给filter out 掉。 这里用Python来做演示:

#import necessary libraries
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

# Read in the image
image = mpimg.imread('test.jpg')

# Grab the x and y size and make a copy of the image
ysize = image.shape[0]
xsize = image.shape[1]
# Note: always make a copy rather than simply using "="
#since "=" will change the original image.
color_select = np.copy(image)

# Define our color selection criteria
red_threshold = 230
green_threshold = 230
blue_threshold = 230
rgb_threshold = [red_threshold, green_threshold, blue_threshold]

# Identify pixels below the threshold. Only if all the RGB channel value
# greater than threshold, will we keep it.
thresholds = (image[:,:,0] < rgb_threshold[0]) \
            | (image[:,:,1] < rgb_threshold[1]) \
            | (image[:,:,2] < rgb_threshold[2])
color_select[thresholds] = [0,0,0]

# Display the image
plt.subplot(121)
plt.imshow(image)
plt.axis('off')
plt.subplot(122)                 
plt.imshow(color_select)
plt.axis('off')
plt.show()

得到的结果如下:
优达学城自动驾驶汽车-Project 1: Finding Lane Lines 学习笔记1_第1张图片
因为车道外的其他区域也有很多亮的区域, 我们可以手动来做一个mask 区域然后只threshold这块区域里面的部分:

left_bottom = [0, 539] # left bottom point of the triangle
right_bottom = [900, 300] # right bottom point of the triangle
apex = [400, 0] # vertex of the triangle

# Fit lines (y=Ax+B) to identify the  3 sided region of interest
# np.polyfit() returns the coefficients [A, B] of the fit
fit_left = np.polyfit((left_bottom[0], apex[0]), (left_bottom[1], apex[1]), 1)
fit_right = np.polyfit((right_bottom[0], apex[0]), (right_bottom[1], apex[1]), 1)
fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1], right_bottom[1]), 1)

# Find the region inside the lines
XX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))
region_thresholds = (YY > (XX*fit_left[0] + fit_left[1])) & \
                    (YY > (XX*fit_right[0] + fit_right[1])) & \
                    (YY < (XX*fit_bottom[0] + fit_bottom[1]))

# Color pixels red which are inside the region of interest
region_select[region_thresholds] = [255, 0, 0]

# Display the image
plt.imshow(region_select)

优达学城自动驾驶汽车-Project 1: Finding Lane Lines 学习笔记1_第2张图片
最后可以把识别到的路线放回到原来的图片里面去:
优达学城自动驾驶汽车-Project 1: Finding Lane Lines 学习笔记1_第3张图片

这个方法翠然简单方便但是很不robust, 因为仅仅根据像素的亮度在很多时候会得到错误的结果(比如树荫下或者其他颜色的车道)并且我们事先要知道一个区域才能比较准确的得到结果。 下面, 会介绍更加可靠的方法来解决这个问题。

你可能感兴趣的:(udacity)