根据像素的亮度来识别车
车道识别对于Computer vision 来说是一个相对简单的任务。原因是车道一般是白色或黄色 并且很容易和图片中其他的像素区别开来。
在RGB 三信道 的图片中, 白色有很高的R, G, B 的像素值。 所以我们就可以根据这个来threshold并且把低于threshold的像素给filter out 掉。 这里用Python来做演示:
#import necessary librariesimport matplotlib.pyplot as pltimport matplotlib.image as mpimgimport 便宜美国vps numpy as np# Read in the imageimage = mpimg.imread(‘test.jpg’)# Grab the x and y size and make a copy of the imageysize = 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 criteriared_threshold = 230green_threshold = 230blue_threshold = 230rgb_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 imageplt.subplot(121)plt.imshow(image)plt.axis(‘off’)plt.subplot(122) plt.imshow(color_select)plt.axis(‘off’)plt.show()
得到的结果如下:
因为车道外的其他区域也有很多亮的区域, 我们可以手动来做一个mask 区域然后只threshold这块区域里面的部分:
left_bottom = [0, 539] # left bottom point of the triangleright_bottom = [900, 300] # right bottom point of the triangleapex = [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 fitfit_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 linesXX, 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 interestregion_select[region_thresholds] = [255, 0, 0]# Display the imageplt.imshow(region_select)
最后可以把识别到的路线放回到原来的图片里面去:
这个方法翠然简单方便但是很不robust, 因为仅仅根据像素的亮度在很多时候会得到错误的结果(比如树荫下或者其他颜色的车道)并且我们事先要知道一个区域才能比较准确的得到结果。 下面, 会介绍更加可靠的方法来解决这个问题。
88032599