无人驾驶学习—第一课
(本课程内容来自优达学城无人驾驶纳米学位)
车道线识别
内容:
识别出一幅图像中的车道线并将其标注出来
过程:
1、准备一张包含有车道线的图片;
2、读取图片并转换为灰度图;
3、使用一个5*5大小的算子对图片进行高斯平滑;
4、设置Canny边缘检测的参数:low_threshold = 50、high_threshold = 150,得到边缘;
5、通过参数设置感兴趣区域,截取出边缘图中感兴趣区域,得到掩膜下的车道线;
6、设置霍夫变换参数,在掩膜下的车道线图片中将车道线标注出来;
7、将标注出来的车道线相加道边缘图中,得到最终的车道线图;
图片:
代码:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
def lane_identification():
# Read the image and convert it to grayscale image
image = mpimg.imread('lane-linep.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
# Define a kernel size and apply Gaussian smoothing
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
# Define parameters for Canny and apply
low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
# create a masked edges image using cv2.fillPoly()
mask = np.zeros_like(edges)
ignore_mask_color = 255
# This time we are defining a four sided polygon to mask
imshape = image.shape
vertices = np.array([[(0,imshape[0]),(450, 290), (490, 290), (imshape[1],imshape[0])]], dtype=np.int32)
cv2.fillPoly(mask, vertices, ignore_mask_color)
masked_edges = cv2.bitwise_and(edges, mask)
# Define the Hough transform parameters
rho = 2 # distance resolution in pixels of the Hough grid
theta = np.pi/180 # angular resolution in radians of the Hough grid
threshold = 15 # minimum number of votes (intersections in Hough grid cell)
min_line_length = 40 #minimum number of pixels making up a line
max_line_gap = 20 # maximum gap in pixels between connectable line segments
line_image = np.copy(image)*0 # creating a blank to draw lines on
# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line segments
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 a blank image
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)
# Create a "color" binary image to combine with line image
color_edges = np.dstack((edges, edges, edges))
# Draw the lines on the edge image
lines_edges = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0)
plt.imshow(lines_edges)
plt.show()
过程图片:
1、灰度图:
2、边缘检测图:
3、掩膜下的车道线:
4、标注出来的车道线:
5、边缘图中的车道线:
第一课较简单,只识别出图片中车道线,后续继续加油,本博客内容不得转载,不得作为他用,仅互相学习,谢谢。