详解车道线检测算法之传统图像处理
# Threshold x gradientsxbinary = np.zeros_like(scaled_sobel)sxbinary[(scaled_sobel >= sx_thresh[0]) & (scaled_sobel <= sx_thresh[1])] = 1
# Threshold S channel of HLSs_binary = np.zeros_like(s_channel)s_binary[(s_channel >= s_thresh[0]) & (s_channel <= s_thresh[1])] = 1
# Threshold L channel of HLSl_binary = np.zeros_like(l_channel)l_binary[(l_channel >= l_thresh[0]) & (l_channel <= l_thresh[1])] = 1
# 将各种处理方式进行融合,得到车道线的二进制图。color_binary = 255*np.dstack(( l_binary, sxbinary, s_binary)).astype('uint8')
combined_binary = np.zeros_like(sxbinary)combined_binary[((l_binary == 1) & (s_binary == 1) | (sxbinary==1))] = 1
combined_binary = 255*np.dstack((combined_binary,combined_binary,combined_binary)).astype('uint8')

透视变换和ROI提取
# 使用透视变换(perspective transform)得到二进制图(binary image)的鸟瞰图(birds-eye view).img=plt.imread('test_image.jpg')corners = np.float32([[190,720],[580,460],[705,460],[1120,720]])# tuningimshape = img.shape
new_top_left=np.array([corners[0,0],0])new_top_right=np.array([corners[3,0],0])
offset=[150,0]src = np.float32([corners[0],corners[1],corners[2],corners[3]])dst = np.float32([corners[0]+offset,new_top_left+offset,new_top_right-offset ,corners[3]-offset])
M = cv2.getPerspectiveTransform(src, dst)warped = cv2.warpPerspective(img, M, img_size , flags=cv2.INTER_LINEAR)
# ROI提取shape = warped.shapevertices = np.array([[(0,0),(shape[1],0),(shape[1],0),(6*shape[1]/7,shape[0]), (shape[1]/7,shape[0]), (0,0)]],dtype=np.int32)mask = np.zeros_like(warped) if len(shape) > 2: channel_count = shape[2] ignore_mask_color = (255,) * channel_countelse: ignore_mask_color = 255cv2.fillPoly(mask, vertices, ignore_mask_color) masked_image = cv2.bitwise_and(img, mask)
利用直方图滤波和滑动窗口进行曲线拟合
# 对二进制图片的像素进行直方图统计,统计左右两侧的峰值点作为左右车道线的起始点坐标进行曲线拟合。(利用前帧图像探索后帧曲线)
def find_peaks(img,thresh): img_half=img[img.shape[0]//2:,:,0] data = np.sum(img_half, axis=0) filtered = scipy.ndimage.filters.gaussian_filter1d(data,20) xs = np.arange(len(filtered)) peak_ind = signal.find_peaks_cwt(filtered, np.arange(20,300)) peaks = np.array(peak_ind) peaks = peaks[filtered[peak_ind]>thresh] return peaks,filtered
def get_next_window(img,center_point,width): ny,nx,_ = img.shape mask = np.zeros_like(img) if (center_point <= width/2): center_point = width/2 if (center_point >= nx-width/2): center_point = nx-width/2 left = center_point - width/2 right = center_point + width/2 vertices = np.array([[(left,0),(left,ny), (right,ny),(right,0)]], dtype=np.int32) ignore_mask_color=(255,255,255) cv2.fillPoly(mask, vertices, ignore_mask_color) masked = cv2.bitwise_and(mask,img)
hist = np.sum(masked[:,:,0],axis=0) if max(hist>10000): center = np.argmax(hist) else: center = center_point return masked,center
def lane_from_window(binary,center_point,width): n_zones=6 ny,nx,nc = binary.shape zones = binary.reshape(n_zones,-1,nx,nc) zones = zones[::-1] window,center = get_next_window(zones[0],center_point,width) for zone in zones[1:]: next_window,center = get_next_window(zone,center,width) window = np.vstack((next_window,window)) return window
left_binary = lane_from_window(warped_binary,380,300)right_binary = lane_from_window(warped_binary,1000,300)
计算车道曲率
ym_per_pix = 30/720 # meters per pixel in y dimensionxm_per_pix = 3.7/700 # meters per pixel in x dimlane_width = 3.7
def cal_curvature(line): fit_coeffs_curv = np.polyfit(y*ym_per_pix, x*xm_per_pix, 2) radius_of_curvature = ((1 + (2*fit_coeffs_curv[0]*y_eval*ym_per_pix + fit_coeffs_curv[1])**2)**1.5) /np.absolute(2*fit_coeffs_curv[0]) return radius_of_curvature left_curvature= cal_curvature(left_line)right_curvature = cal_curvature(right_line)
curvature = 0.5*(round(right_curvature,1) + round(left_curvature,1))
将曲线逆透视到原图片
# 将完成车道线标记的鸟瞰图反透视变换为初始图像视角
newwarp = cv2.warpPerspective(color_warp, Minv, (img.shape[1], img.shape[0]))
将算法应用于视频
output = 'test_video_output.mp4'clip = VideoFileClip("test_video.mp4")out_clip = clip.fl_image(process_image) %time out_clip.write_videofile(output, audio=False)
- End -
图片新闻
最新活动更多
-
5月13日立即预约>>> 【线下会议】恩智浦创新技术峰会·深圳
-
精彩回顾立即查看>> 【在线直播】可视化神器!VisionSym 赋能汽车光学原型开发
-
精彩回顾立即查看>> 12月16-17日 AMD 嵌入式峰会
-
精彩回顾立即查看>> 恩智浦创新技术峰会
-
精彩回顾立即查看>> 【工程师系列】汽车电子技术在线大会
-
精彩回顾立即查看>> Works With 开发者大会深圳站
推荐专题
- 1 改写出行格局!充换电基建的五年蝶变
- 2 纯视觉遭调查 特斯拉自动驾驶遇生死考验
- 3 北京在全国首推“智驾险”,各车企智驾水平要“露底”?
- 4 VLA 与世界模型之争:谁才是辅助驾驶的正确方向?
- 5 卖芯片还是卖平台?地平线与黑芝麻智能悄然走出分水岭
- 6 2026 百人会论坛:地平线|推舱驾融合的“智能体芯片”
- 7 「武汉萝卜快跑」事件背后:有时停下是为了更快的奔跑
- 8 为啥有人认为自动驾驶纯视觉方案比激光雷达方案好?
- 9 中国自动驾驶Robotaxi围攻中东【附投票】:曹操出行、文远知行、小马智行、萝卜快跑四路诸侯,谁才是真正的“沙漠之狐”?
- 10 2026百人会论坛:卓驭科技|从智驾到物理AI,沈劭劼说这是生存判断不是战略判断


分享











发表评论
登录
手机
验证码
手机/邮箱/用户名
密码
立即登录即可访问所有OFweek服务
还不是会员?免费注册
忘记密码其他方式
请输入评论内容...
请输入评论/评论长度6~500个字
暂无评论
暂无评论