• 售前

  • 售后

热门帖子
入门百科

python+opencv实现车道线检测

[复制链接]
李凯908 显示全部楼层 发表于 2021-10-25 19:09:09 |阅读模式 打印 上一主题 下一主题
python+opencv车道线检测(浅易实现),供大家参考,详细内容如下
技能栈:python+opencv
实现思绪:

1、canny边缘检测获取图中的边缘信息;
2、霍夫变更探求图中直线;
3、绘制梯形感爱好区域得到车前范围;
4、得到并绘制车道线;

效果展示:


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

效果图:

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

代码实用性差,几乎不能用于现实,但是可以作为初学者的练手项目;
斑马线检测思绪:获取车前感爱好区域,判断白色像素点比例即可实现;
行人检测思绪:opencv有内置行人检测函数,基于内置的训练好的数据集;
以上就是本文的全部内容,希望对大家的学习有所资助,也希望大家多多支持草根技术分享。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作