车道线识别--图片

车道线识别 – 图片

参考: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()

原图
line_image
color_select

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值