车道线识别 – 图片
参考:https://blog.csdn.net/gloria_iris/article/details/91625656
步骤1:读入图片
步骤2:按颜色删选像素,二值化处理
步骤3:提取感兴趣区域
步骤4:输出图片及画出来的车道线
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# Read in the image
image = mpimg.imread('test2.jpg')
print(type(image)) # 输出原照片像素大小
print(image.shape) # 显示原照片
# Grab the x and y sizes and make two copies of the image
ysize = image.shape[0]
xsize = image.shape[1]
color_select = np.copy(image)
line_image = np.copy(image)
# Define our color criteria 颜色删选阈值设置
red_threshold = 200
green_threshold = 200
blue_threshold = 200
rgb_threshold = [red_threshold, green_threshold, blue_threshold]
# Define a **trapezoid** region of interest (Note: if you run this code参考文章中感兴趣的区域位三角形,此处更改为梯形
# Keep in mind the origin (x=0, y=0) is in the upper left in image processing
# you'll find these are not sensible values!!
# But you'll get a chance to play with them soon in a quiz ;)
# 梯形四个顶点设置,主要依据摄像头拍摄区域而定,与摄像头位置与标定有关系
left_bottom = [0, 540]
right_bottom = [1000, 540]
apex1 = [600, 300]
apex2 = [400, 300]
# Perform a linear fit (y=Ax+B) to each of the three sides of the trapezoid
# np.polyfit returns the coefficients [A, B] of the fit
fit_left = np.polyfit((left_bottom[0], apex1[0]), (left_bottom[1], apex1[1]), 1)
fit_right = np.polyfit((right_bottom[0], apex2[0]), (right_bottom[1], apex2[1]), 1)
fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1], right_bottom[1]), 1)
fit_up = np.polyfit((apex1[0], apex2[0]), (apex1[1], apex2[1]), 1)
# Mask pixels below the threshold
color_thresholds = (image[:, :, 0] < rgb_threshold[0]) | \
\
(image[:, :, 1] < rgb_threshold[1]) | \
\
(image[:, :, 2] < rgb_threshold[2])
# 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])) & \
(YY > (XX * fit_up[0] + fit_up[1]))
# Mask color selection
color_select[color_thresholds] = [0, 0, 0]
# Find where image is both colored right and in the region
line_image[~color_thresholds & region_thresholds] = [255, 0, 0]
# line_image[~region_thresholds] = [0,0,0]
print("--------------------")
print(line_image)
# Display the image and show region and color selections
plt.imshow(image)
plt.waitforbuttonpress()
plt.close()
x = [left_bottom[0], right_bottom[0], apex1[0], apex2[0], left_bottom[0]]
y = [left_bottom[1], right_bottom[1], apex1[1], apex2[1], left_bottom[1]]
plt.figure()
plt.plot(x, y, 'b--', lw=3)
# plt.imshow(color_select)
plt.imshow(line_image)
plt.waitforbuttonpress()
plt.figure()
plt.imshow(color_select)
plt.waitforbuttonpress()