服务器之家

服务器之家 > 正文

python+opencv实现车道线检测

时间:2021-09-06 00:18     来源/作者:毛钱儿

python+opencv车道线检测(简易实现),供大家参考,具体内容如下

技术栈:python+opencv

实现思路:

1、canny边缘检测获取图中的边缘信息;
2、霍夫变换寻找图中直线;
3、绘制梯形感兴趣区域获得车前范围;
4、得到并绘制车道线;

效果展示:

python+opencv实现车道线检测

代码实现:

  1. import cv2
  2. import numpy as np
  3.  
  4. def canny():
  5. gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
  6. #高斯滤波
  7. blur = cv2.GaussianBlur(gray, (5, 5), 0)
  8. #边缘检测
  9. canny_img = cv2.Canny(blur, 50, 150)
  10. return canny_img
  11.  
  12. def region_of_interest(r_image):
  13. h = r_image.shape[0]
  14. w = r_image.shape[1]
  15. # 这个区域不稳定,需要根据图片更换
  16. poly = np.array([
  17. [(100, h), (500, h), (290, 180), (250, 180)]
  18. ])
  19. mask = np.zeros_like(r_image)
  20. # 绘制掩膜图像
  21. cv2.fillPoly(mask, poly, 255)
  22. # 获得ROI区域
  23. masked_image = cv2.bitwise_and(r_image, mask)
  24. return masked_image
  25.  
  26. if __name__ == '__main__':
  27. image = cv2.imread('test.jpg')
  28. lane_image = np.copy(image)
  29. canny = canny()
  30. cropped_image = region_of_interest(canny)
  31. cv2.imshow("result", cropped_image)
  32. cv2.waitKey(0)

霍夫变换加线性拟合改良:

效果图:

python+opencv实现车道线检测

代码实现:

主要增加了根据斜率作线性拟合过滤无用点后连线的操作;

  1. import cv2
  2. import numpy as np
  3.  
  4. def canny():
  5. gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
  6. blur = cv2.GaussianBlur(gray, (5, 5), 0)
  7.  
  8. canny_img = cv2.Canny(blur, 50, 150)
  9. return canny_img
  10.  
  11. def region_of_interest(r_image):
  12. h = r_image.shape[0]
  13. w = r_image.shape[1]
  14.  
  15. poly = np.array([
  16. [(100, h), (500, h), (280, 180), (250, 180)]
  17. ])
  18. mask = np.zeros_like(r_image)
  19. cv2.fillPoly(mask, poly, 255)
  20. masked_image = cv2.bitwise_and(r_image, mask)
  21. return masked_image
  22.  
  23. def get_lines(img_lines):
  24. if img_lines is not None:
  25. for line in lines:
  26. for x1, y1, x2, y2 in line:
  27. # 分左右车道
  28. k = (y2 - y1) / (x2 - x1)
  29. if k < 0:
  30. lefts.append(line)
  31. else:
  32. rights.append(line)
  33.  
  34. def choose_lines(after_lines, slo_th): # 过滤斜率差别较大的点
  35. slope = [(y2 - y1) / (x2 - x1) for line in after_lines for x1, x2, y1, y2 in line] # 获得斜率数组
  36. while len(after_lines) > 0:
  37. mean = np.mean(slope) # 计算平均斜率
  38. diff = [abs(s - mean) for s in slope] # 每条线斜率与平均斜率的差距
  39. idx = np.argmax(diff) # 找到最大斜率的索引
  40. if diff[idx] > slo_th: # 大于预设的阈值选取
  41. slope.pop(idx)
  42. after_lines.pop(idx)
  43. else:
  44. break
  45.  
  46. return after_lines
  47.  
  48. def clac_edgepoints(points, y_min, y_max):
  49. x = [p[0] for p in points]
  50. y = [p[1] for p in points]
  51.  
  52. k = np.polyfit(y, x, 1) # 曲线拟合的函数,找到xy的拟合关系斜率
  53. func = np.poly1d(k) # 斜率代入可以得到一个y=kx的函数
  54.  
  55. x_min = int(func(y_min)) # y_min = 325其实是近似找了一个
  56. x_max = int(func(y_max))
  57.  
  58. return [(x_min, y_min), (x_max, y_max)]
  59.  
  60. if __name__ == '__main__':
  61. image = cv2.imread('F:\\A_javaPro\\test.jpg')
  62. lane_image = np.copy(image)
  63. canny_img = canny()
  64. cropped_image = region_of_interest(canny_img)
  65. lefts = []
  66. rights = []
  67. lines = cv2.HoughLinesP(cropped_image, 1, np.pi / 180, 15, np.array([]), minLineLength=40, maxLineGap=20)
  68. get_lines(lines) # 分别得到左右车道线的图片
  69.  
  70. good_leftlines = choose_lines(lefts, 0.1) # 处理后的点
  71. good_rightlines = choose_lines(rights, 0.1)
  72.  
  73. leftpoints = [(x1, y1) for left in good_leftlines for x1, y1, x2, y2 in left]
  74. leftpoints = leftpoints + [(x2, y2) for left in good_leftlines for x1, y1, x2, y2 in left]
  75.  
  76. rightpoints = [(x1, y1) for right in good_rightlines for x1, y1, x2, y2 in right]
  77. rightpoints = rightpoints + [(x2, y2) for right in good_rightlines for x1, y1, x2, y2 in right]
  78.  
  79. lefttop = clac_edgepoints(leftpoints, 180, image.shape[0]) # 要画左右车道线的端点
  80. righttop = clac_edgepoints(rightpoints, 180, image.shape[0])
  81.  
  82. src = np.zeros_like(image)
  83.  
  84. cv2.line(src, lefttop[0], lefttop[1], (255, 255, 0), 7)
  85. cv2.line(src, righttop[0], righttop[1], (255, 255, 0), 7)
  86.  
  87. cv2.imshow('line Image', src)
  88. src_2 = cv2.addWeighted(image, 0.8, src, 1, 0)
  89. cv2.imshow('Finally Image', src_2)
  90.  
  91. cv2.waitKey(0)

待改进:

代码实用性差,几乎不能用于实际,但是可以作为初学者的练手项目;
斑马线检测思路:获取车前感兴趣区域,判断白色像素点比例即可实现;
行人检测思路:opencv有内置行人检测函数,基于内置的训练好的数据集;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/qq_41473522/article/details/105426589

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部