小言_互联网的博客

python进阶——自动驾驶寻找车道

243人阅读  评论(0)

  大家好,我是csdn的博主:lqj_本人

这是我的个人博客主页:

lqj_本人的博客_CSDN博客-微信小程序,前端,python领域博主lqj_本人擅长微信小程序,前端,python,等方面的知识https://blog.csdn.net/lbcyllqj?spm=1011.2415.3001.5343哔哩哔哩欢迎关注:小淼前端

小淼前端的个人空间_哔哩哔哩_bilibili

本篇文章主要讲述python的人工智能视觉模块自动驾驶原理,本篇文章已经成功收录到我们python专栏中:

https://blog.csdn.net/lbcyllqj/category_12089557.htmlhttps://blog.csdn.net/lbcyllqj/category_12089557.html

 前言

本程序主要讲述python的AI视觉方面的应用:自动驾驶寻找车道。

推荐博客好文章

(上过csdn热榜top5的优质好文!)

1.若不知道怎么安装opencv或者使用的请看我的这篇文章(曾上过csdn综合热榜的top1):

python进阶——人工智能视觉识别_lqj_本人的博客-CSDN博客

2.基于opencv的人工智能视觉实现的目标实时跟踪功能(曾上过csdn综合热榜的top5):

python进阶——人工智能实时目标跟踪_lqj_本人的博客-CSDN博客

3.基于PaddlenHub模块以及playsound模块实现口罩检测并实时语音报警(曾上过csdn综合热榜的top1):

python进阶——AI视觉实现口罩检测实时语音报警系统_lqj_本人的博客-CSDN博客

项目前须知

1.opencv的图像灰度转化方法

gray = cv2.cvtColor("图像", cv2.COLOR_RGB2GRAY)

2.opencv检测图像边缘

高斯模糊图像

cv2.GaussianBlur(gray, (5, 5), 0)

获取精明图像

canny = cv2.Canny(blur, 50, 150)

3.matplotlib绘制图像库的使用

项目详情

我们先拿到实时摄像的某一帧的图像

导入库


  
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt

边缘检测

进行图像的灰度转化以及图像的边缘检测


  
  1. def canny( image):
  2. """1.图像的灰度转化"""
  3. #把某一帧的图片转换成灰度图像
  4. gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
  5. """2.检测图像边缘"""
  6. #高斯模糊图像
  7. blur = cv2.GaussianBlur(gray, ( 5, 5), 0)
  8. #获取精明的图片
  9. canny = cv2.Canny(blur, 50, 150)
  10. return canny
  11. image = cv2.imread( '1.jpg')
  12. lane_image = np.copy(image)
  13. canny = canny(lane_image)
  14. plt.imshow(canny)
  15. plt.show()

得到绘图结果

 因为中国的车道时沿右边行驶的,所以我们可以在绘图的图像中清楚的看见X轴与Y轴的数码,由X轴的(400,0)位置到X轴的大约(1100,0)位置是右车道的宽度,然后我们再来看Y轴的数码,大约在150的位置是我们可视范围内的右车道的尽头点,又因为(400,0)到(1100,0)的距离为700px,所以我们可以得到可视范围内的右车道的尽头点为(700,150)。

根据上述位置的计算,我们可以得出一个右车道中的三角形


  
  1. def region_of_interest( image):
  2. height = image.shape[ 0]
  3. polygons = np.array([
  4. [( 400,height),( 1100,height),( 700, 150)]
  5. ])
  6. mask = np.zeros_like(image)
  7. cv2.fillPoly(mask,polygons, 255)
  8. return mask
  9. image = cv2.imread( '1.jpg')
  10. lane_image = np.copy(image)
  11. canny = canny(lane_image)
  12. cv2.imshow( 'result',region_of_interest(canny))
  13. cv2.waitKey( 0)

得出检测三角形

生成蒙版

 将检测到的图像由255(白色)表示,周围区域用0(黑色表示)

 有时候三角形不是正好与我们看到的进到点到左右两侧点的形状正好相似,所以我们需要自己微调一下


  
  1. polygons = np.array([
  2. [( 400,height),( 1200,height),( 800, 200)]
  3. ])

然后,我们可以对我们的图像进行右车道三角形的裁剪

    masked_image = cv2.bitwise_and(image,mask)

  
  1. cropped_image = region_of_interest(canny)
  2. cv2.imshow( 'result',cropped_image)

边缘检测与蒙版产生的效果

裁剪显示图像

定义车道起始点位置


  
  1. def make_coordinates( image,line_parameters):
  2. slope,intercept = line_parameters
  3. print(image.shape)
  4. y1 = image.shape[ 0]
  5. y2 = int(y1*( 3/ 5))
  6. x1 = int((y1 - intercept)/slope)
  7. x2 = int((y2 - intercept)/slope)
  8. return np.array([x1,y1,x2,y2])

霍夫变换的直线检测

用到的是Opencv封装好的函数cv.HoughLinesP函数,使用到的参数如下:

image:输入图像,通常为canny边缘检测处理后的图像
rho:线段以像素为单位的距离精度
theta:像素以弧度为单位的角度精度(np.pi/180较为合适)
threshold:霍夫平面累加的阈值
minLineLength:线段最小长度(像素级)
maxLineGap:最大允许断裂长度

lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5)

绘制车道


  
  1. def display_lines( image,lines):
  2. line_image = np.zeros_like(image)
  3. if lines is not None:
  4. for line in lines:
  5. # print(line)
  6. x1,y1,x2,y2 = line.reshape( 4)
  7. cv2.line(line_image,(x1,y1),(x2,y2),( 255, 100, 10), 10)
  8. return line_image

效果图像

 图像与绘制车道融合

视频流中位置检测


  
  1. def average_slope_intercept( image,lines):
  2. left_fit = []
  3. right_fit = []
  4. if lines is None:
  5. return None
  6. for line in lines:
  7. x1,y1,x2,y2 = line.reshape( 4)
  8. parameters = np.polyfit((x1,x2),(y1,y2), 1)
  9. # print(parameters)
  10. slope = parameters[ 0]
  11. intercept = parameters[ 1]
  12. if slope < 0:
  13. left_fit.append((slope,intercept))
  14. else:
  15. right_fit.append((slope,intercept))
  16. print(left_fit)
  17. print(right_fit)

打印左右位置结果

 检测数每一帧的左右位置结果


  
  1. left_fit_average = np.average(left_fit,axis= 0)
  2. right_fit_average = np.average(right_fit,axis= 0)
  3. print(left_fit_average, '左')
  4. print(right_fit_average, '右')
  5. left_line = make_coordinates(image,left_fit_average)
  6. right_line = make_coordinates(image,right_fit_average)
  7. return np.array([left_line,right_line])

导入视频流做最后处理


  
  1. cap = cv2.VideoCapture( '3.mp4')
  2. # try:
  3. while cap.isOpened():
  4. _,frame = cap.read()
  5. canny_image = canny(frame)
  6. cropped_image = region_of_interest(canny_image)
  7. lines = cv2.HoughLinesP(cropped_image, 2, np.pi/ 180, 100, np.array([]), minLineLength= 40, maxLineGap= 5)
  8. averaged_lines = average_slope_intercept(frame, lines)
  9. line_image = display_lines(frame, averaged_lines)
  10. combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
  11. # cv2.resizeWindow("result", 1080, 960);
  12. cv2.imshow( 'result', line_image)
  13. cv2.waitKey( 10)

完整代码


  
  1. import cv2
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. def make_coordinates( image,line_parameters):
  5. slope,intercept = line_parameters
  6. print(image.shape)
  7. y1 = image.shape[ 0]
  8. y2 = int(y1*( 3/ 5))
  9. x1 = int((y1 - intercept)/slope)
  10. x2 = int((y2 - intercept)/slope)
  11. return np.array([x1,y1,x2,y2])
  12. def average_slope_intercept( image,lines):
  13. left_fit = []
  14. right_fit = []
  15. if lines is None:
  16. return None
  17. for line in lines:
  18. x1,y1,x2,y2 = line.reshape( 4)
  19. parameters = np.polyfit((x1,x2),(y1,y2), 1)
  20. # print(parameters)
  21. slope = parameters[ 0]
  22. intercept = parameters[ 1]
  23. if slope < 0:
  24. left_fit.append((slope,intercept))
  25. else:
  26. right_fit.append((slope,intercept))
  27. # print(left_fit)
  28. # print(right_fit)
  29. left_fit_average = np.average(left_fit,axis= 0)
  30. right_fit_average = np.average(right_fit,axis= 0)
  31. print(left_fit_average, '左')
  32. print(right_fit_average, '右')
  33. left_line = make_coordinates(image,left_fit_average)
  34. right_line = make_coordinates(image,right_fit_average)
  35. return np.array([left_line,right_line])
  36. def canny( image):
  37. """1.图像的灰度转化"""
  38. #把某一帧的图片转换成灰度图像
  39. gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
  40. """2.检测图像边缘"""
  41. #高斯模糊图像
  42. blur = cv2.GaussianBlur(gray, ( 5, 5), 0)
  43. #获取精明的图片
  44. canny = cv2.Canny(blur, 50, 150)
  45. return canny
  46. #每一行都是一个二维数组,包含我们的线坐标,形式为[[x1,yl,x2,y2]]。这些坐标指定了线条的参数,以及线条相对与图像空间位置,确保他们被放置在正确的位置
  47. def display_lines( image,lines):
  48. line_image = np.zeros_like(image)
  49. if lines is not None:
  50. for line in lines:
  51. # print(line)
  52. x1,y1,x2,y2 = line.reshape( 4)
  53. cv2.line(line_image,(x1,y1),(x2,y2),( 255, 100, 10), 10)
  54. return line_image
  55. def region_of_interest( image):
  56. height = image.shape[ 0]
  57. polygons = np.array([
  58. [( 300,height),( 650,height),( 500, 150)]
  59. ])
  60. mask = np.zeros_like(image)
  61. cv2.fillPoly(mask,polygons, 255)
  62. masked_image = cv2.bitwise_and(image,mask)
  63. return masked_image
  64. # image = cv2.imread('1.png')
  65. # lane_image = np.copy(image)
  66. # canny_image = canny(lane_image)
  67. # cropped_image = region_of_interest(canny_image)
  68. # lines = cv2.HoughLinesP(cropped_image,2,np.pi/180,100,np.array([]),minLineLength=40,maxLineGap=5)
  69. # averaged_lines = average_slope_intercept(lane_image,lines)
  70. # line_image = display_lines(lane_image,averaged_lines)
  71. # combo_image = cv2.addWeighted(lane_image,0.8,line_image,1,1)
  72. # cv2.imshow('result',combo_image)
  73. # cv2.waitKey(0)
  74. cap = cv2.VideoCapture( '3.mp4')
  75. # try:
  76. while cap.isOpened():
  77. _,frame = cap.read()
  78. canny_image = canny(frame)
  79. cropped_image = region_of_interest(canny_image)
  80. lines = cv2.HoughLinesP(cropped_image, 2, np.pi/ 180, 100, np.array([]), minLineLength= 40, maxLineGap= 5)
  81. averaged_lines = average_slope_intercept(frame, lines)
  82. line_image = display_lines(frame, averaged_lines)
  83. combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
  84. # cv2.resizeWindow("result", 1080, 960);
  85. cv2.imshow( 'result', combo_image)
  86. cv2.waitKey( 10)

用前须知

根据自己的需要适当微调参数:


  
  1. def region_of_interest( image):
  2. height = image.shape[ 0]
  3. polygons = np.array([
  4. [( 300,height),( 650,height),( 500, 150)]
  5. ])
  6. mask = np.zeros_like(image)
  7. cv2.fillPoly(mask,polygons, 255)
  8. masked_image = cv2.bitwise_and(image,mask)
  9. return masked_image

效果显示


转载:https://blog.csdn.net/lbcyllqj/article/details/128937913
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场