无人驾驶学习笔记

无人驾驶学习笔记-寻找车道-1

本人报了udacity的无人驾驶纳米学位课程,从今天开始写学习笔记:

python实现颜色选择

让我们编写一个简单的Python的颜色选择app。
不需要下载和安装任何东西,您也可以在浏览器中看到代码执行结果。
无人驾驶学习笔记_第1张图片
我们将使用上面的图片。
查看下面的代码。 首先,我从matplotlib导入pyplot和图像。 我也导入numpy操作图像。

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

# 读取图片,打印图片的相关信息
image = mpimg.imread('test.jpg')
print('This image is: ',type(image), 
         'with dimensions:', image.shape)

# 得到长度和宽度
ysize = image.shape[0]
xsize = image.shape[1]
color_select = np.copy(image)
# Define our color selection criteria
# Note: if you run this code, you'll find these are not sensible values!!
# But you'll get a chance to play with them soon in a quiz
red_threshold = 0
green_threshold = 0
blue_threshold = 0
rgb_threshold = [red_threshold, green_threshold, blue_threshold]
# Identify pixels below the threshold
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.imshow(color_select)

结果color_select是其中高于阈值的像素已被保留并且低于阈值的像素被黑化的图像。
在上面的代码段中,red_threshold,green_threshold和blue_threshold都设置为0,这意味着所有像素都将包含在选择中。
在下一个测验中,您将修改red_threshold,green_threshold和blue_threshold的值,直到您保留尽可能多的车道线,同时放弃其他所有内容。 您的输出图像应如下所示。

无人驾驶学习笔记_第2张图片

区域遮蔽

真棒!现在你已经看到,使用简单的颜色选择,我们已经设法消除图像中除了车道线之外的几乎一切。
然而,在这一点上,仍然难以自动地提取精确的线,因为我们仍然具有在边缘周围检测到的不是车道线的一些其他对象。
在这种情况下,我假设拍摄图像的前置摄像头安装在汽车的固定位置,使得车道线将始终出现在图像的相同大致区域中。 接下来,我将利用这一点,通过添加一个标准,只考虑在我们期望找到车道线的区域中的颜色选择的像素。

查看下面的代码。 变量left_bottom,right_bottom和apex代表一个三角形区域的顶点,我想保留为我的颜色选择,而掩蔽其他一切。 这里我使用三角形掩码来说明最简单的情况,但后来你将使用四边形,原则上,你可以使用任何多边形。

你可能感兴趣的:(无人驾驶学习笔记)