无人驾驶工程师学习笔记(二)——Find Lane Lines

identifying lane lines by color

one image including red green blue
each of these color channels contains pixels whose values range from zero to 255

0 is the darkest value
255 is the brightest calue

lib——matplotlib and numpy.copy

  • Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。
    官方指南:https://matplotlib.org/
    其中文教程网站:https://www.matplotlib.org.cn/
  • NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
    官网:https://numpy.org/

use this picture as the input

在这里插入图片描述

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

image = mpimg.imread('test.jpg')

上述代码中读出来的代码矩阵是三维矩阵,

[[[122 185 229] [122 185 229] [123 186 230] … [123 184 229] [123
184 229] [123 184 229]]

[[122 185 229] [122 185 229] [122 185 229] … [123 184 229] [123 184
229] [123 184 229]]

[[122 185 229] [122 185 229] [122 185 229] … [123 184 229] [123
184 229] [123 184 229]]

[[154 166 154] [156 168 156] [153 165 153] … [180 197 181] [181
198 182] [181 198 182]]

[[162 174 164] [160 172 162] [152 164 154] … [174 192 176] [175
193 177] [176 194 178]]

[[156 168 158] [148 160 150] [137 149 139] … [161 179 163]
[162 180 164] [162 180 164]]]

其中每一个[122 185 229]代表一个像素点的RGB信息,矩阵行代表图片横向像素点,矩阵列代表图片纵向像素点。

表示图像的三维数组,按照行(row),列(column),通道(channel)顺序来索引,即:

image[rows:columns:channels]

索引第0行的像素: image[0]

索引第1列的像素: image[:,1]

索引b通道的值: image[:,:,1]

索引第0行,第1列的像素: image[0:1]

thresholds = (image[:,:,0] < rgb_threshold[0]) \
            | (image[:,:,1] < rgb_threshold[1]) \
            | (image[:,:,2] < rgb_threshold[2])
color_select = np.copy(image)
color_select[thresholds] = [0,0,0]

image[:,:,0] 表示所有行所有列的像素点的0通道的值。即所有像素点的第一个值:[162 174 164] 。
threshold是筛选出一个二维矩阵,这个二位数组的每个数据为0则代表image的该像素点不符合筛选条件,为1表示符合筛选条件。

color_select[thresholds] 的作用为将thresholds中值为1的位置的color_select的像素点的值设为0,0,0。

这里我们需要将白素的车道线筛选出来,所以我们将小于一定值的像素点设为全黑,保留车道线的像素点,即可获得下面的效果。
在这里插入图片描述

lib——numpy.polyfit()/ numpy.meshgrid()/ numpy.arange()

  • numpy.polyfit:
    z1 = np.polyfit(X, Y, 1) #一次多项式拟合,相当于线性拟合

z = np.polyfit(x, y, 3)
z
array([ 0.08703704, -0.81349206, 1.69312169, -0.03968254]) # 返回的是高次项到低次项的因子

  • numpy.meshgrid()——生成网格点坐标矩阵。每个交叉点都是网格点,描述这些网格点的坐标的矩阵,就是坐标矩阵。
    在这里插入图片描述
    如果是一个(256, 100)的整数矩阵网格,要怎样构造数据呢?
  1. 方法1:将x轴上的100个整数点组成的行向量,重复256次,构成shape(256,100)的X矩阵;将y轴上的256个整数点组成列向量,重复100次构成shape(256,100)的Y矩阵
    显然方法1的数据构造过程很繁琐,也不方便调用.

  2. 使用meshgrid方法,你只需要构造一个表示x轴上的坐标的向量和一个表示y轴上的坐标的向量;然后作为参数给到meshgrid(),该函数就会返回相应维度的两个矩阵;

例如,x为[0,1,2],y为[3,4],将x,y传入meshgrid(),最后返回的X矩阵为
[[0,1,2]
[0,1,2]]
Y矩阵为:
[[3,3,3]
[4,4,4]]

  • Numpy 中 arange() 主要是用于生成数组numpy.arange(start, stop, step, dtype = None)

start —— 开始位置,数字,可选项,默认起始值为0
stop —— 停止位置,数字
step —— 步长,数字,可选项, 默认步长为1,如果指定了step,则还必须给出start。
dtype —— 输出数组的类型。 如果未给出dtype,则从其他输入参数推断数据类型。
返回:
arange:ndarray
均匀间隔值的数组。
注意:对于浮点参数(参数为浮点),结果的长度为ceil((stop - start)/ step)) 由于浮点溢出,此规则可能导致最后一个元素大于stop。因此要特别注意。

left_bottom = [0, 539]
right_bottom = [900, 300]
apex = [400, 0]

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)
#生成三角形的三条边的函数
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]))
# Mask color selection 判断图片上的每个像素点是否在三角形的区域内,如果在则region_thresholds中标记该像素点的bool为1
color_select[color_thresholds] = [0,0,0]
# Find where image is both colored right and in the region,
# 将三角形区域内(region_thresholds)的车道线像素点(~color_thresholds )变成红色。
line_image[~color_thresholds & region_thresholds] = [255,0,0]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值