import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图片
img = cv2.imread("test_images/straight_lines1.jpg")
cv2.imshow("img", img)
#转换为灰度图像
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
cv2.imshow("img_gray", img_gray)
#高斯滤波
img_gaussian = cv2.GaussianBlur(img_gray, (11, 11), 0.0)
cv2.imshow("img_gaussian", img_gaussian)
#Canny算法,边缘检测
img_edge = cv2.Canny(img_gaussian, 90, 180)
cv2.imshow("img_edge", img_edge)
#感兴趣区域提取
plt.imshow(img)
#plt.show()
mask = np.zeros_like(img_edge)
vertices = np.array([[220, 670], [610, 410], [650, 410], [1100, 670]])
cv2.fillPoly(mask, [vertices], 255)
img_edge_roi = cv2.bitwise_and(img_edge, mask)
cv2.imshow('img_edge_roi', img_edge_roi)
#直线检测
lines = cv2.HoughLinesP(img_edge_roi, 1, np.pi/180, 20, minLineLength = 10, maxLineGap = 10)
#print(lines.shape)
#画线
img_line = np.zeros_like(img)
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(img_line, (x1, y1), (x2, y2), [0, 0, 255], 2)
cv2.imshow('img_line', img_line)
#去除斜率差异较大的线段
def clean_lines(lines, threshold):
#计算斜率
slope = [(y2 - y1) / (x2 - x1) for line in lines for x1, x2, y1, y2 in line]
while(len(slope) > 0):
#计算斜率平均值
mean = np.mean(slope)
#计算差值
diff = [abs(s - mean) for s in slope]
#找出差值最大的下标
index = np.argmax(diff)
# 如果下标对应的差值大于threshold就删去该线段
if diff[index] > threshold:
slope.pop(index)
lines.pop(index)
else:
break
#拟合直线,计算车道线端点
def cal_lane_vertices(point_list, y_start, y_end):
x = [p[0] for p in point_list]
y = [p[1] for p in point_list]
# 一次函数拟合,fit为(k, b)
fit = np.polyfit(y, x, 1)
#得到x = ky + b
fit_fn = np.poly1d(fit)
x_start = int(fit_fn(y_start))
x_end = int(fit_fn(y_end))
return [(x_start, y_start), (x_end, y_end)]
#画车道线
def draw_lanes(img, lines, color = [0, 0, 255]):
#左线段列表
right_lines = []
#右线段列表
left_lines = []
# 1.根据斜率分左右线段
for line in lines:
for x1, y1, x2, y2 in line:
if y2 - y1 != 0 and x2 - x1 != 0:
#计算斜率
slope = (y2 - y1) / (x2 - x1)
if slope > 0:
right_lines.append(line)
else:
left_lines.append(line)
if (len(left_lines) <= 0 or len(right_lines) <= 0):
return img
#2.去除斜率差异过大的线段
clean_lines(right_lines, 0.1)
clean_lines(left_lines, 0.1)
#3.直线拟合
left_points = [(x1, y1) for line in left_lines for x1, y1, x2, y2 in line]
left_points = left_points + [(x2, y2) for line in left_lines for x1, y1, x2, y2 in line]
right_points = [(x1, y1) for line in right_lines for x1, y1, x2, y2 in line]
right_points = right_points + [(x2, y2) for line in right_lines for x1, y1, x2, y2 in line]
#计算车道线端点
left_vertices = cal_lane_vertices(left_points, img.shape[0] - 50, 450)
right_vertices = cal_lane_vertices(right_points, img.shape[0] - 50, 450)
#以矩形的形式拟合
vertices = np.array([left_vertices[0], left_vertices[1], right_vertices[1], right_vertices[0]])
cv2.fillPoly(img, [vertices], color)
return img
img_lane = draw_lanes(img, lines, color = [0, 0, 255])
cv2.imshow('img_lane', img_lane)
cv2.waitKey(-1)
车道线检测matlab代码lane detect
版权申诉
5星 · 超过95%的资源 101 浏览量
2022-03-13
17:32:05
上传
评论 5
收藏 20.76MB ZIP 举报
CAE工作者
- 粉丝: 180
- 资源: 1857
最新资源
- Surfer,线性函数
- MyBatis 的动态 SQL 是其核心特性之一.txt
- 时代的sdddsddsddsd
- 基于哈希链表的简单人员信息管理系统
- 其他类别JdonFramework开源框架 v5.1 Build20071025-jdonframework-5.1.rar
- 2001~2022年上市公司数字赋能指数.dta
- 2001~2022年上市公司数字赋能指数.xlsx
- 信息办公石大在线财务管理系统(含源码)-shidacaiwu.rar
- 信息办公电信计费系统完整代码-netctossconformity.rar
- matlab实现TD-SCDMA中初始同步捕捉DwPTS下行同步导频时隙的仿真.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页