Advanced Lane Detection源码解读(一)

2024-02-12 04:32

本文主要是介绍Advanced Lane Detection源码解读(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 源码解读

1.1 文件目录结构

源码文件下载自GitHub:advanced_lane_detection-master

  • advanced_lane_detection-master
    • camera_cal 存放用于相机标定棋盘格照片
      • *.jpg
    • output_images
      • *.png
    • test_images
      • *.jpg
    • calibrate_camera.p

      pickle文件,由calibrate_camera.py生成并存盘,包含键为mtx和dist的字典,存储了相机校正矩阵。

    • calibrate_camera.py
      def calibrate_camera():

      读取camera_cal/*.jpg照片,调用cv2.findChessboardCorners函数找到棋盘格的角点,再调用cv2.calibrateCamera的得到相机校正矩阵,并返回。

    • combined_thresh.py
      包含以下函数:
      • abs_sobel_thresh(img, orient='x', thresh_min=20, thresh_max=100)

        输入img为RGB图像,在该函数内部会被转为灰度图像。计算其在x或y方向的梯度。
        返回一个0-1的二值图像,选出了x或y梯度在最大、最小范围之间的像素点。

      • mag_thresh(img, sobel_kernel=3, mag_thresh=(30, 100))

        输入img为RGB图像,在该函数内部会被转为灰度图像。计算其梯度值。
        返回一个0-1的二值图像,选出了x或y梯度在最大、最小范围之间的像素点。

      • dir_threshold(img, sobel_kernel=3, thresh=(0, np.pi/2))

        输入img为RGB图像,在该函数内部会被转为灰度图像。计算其梯度方向角。
        返回一个0-1的二值图像,选出了梯度方向角在最大、最小范围之间的像素点。

      • hls_thresh(img, thresh=(100, 255))

        输入img为RGB图像,在该函数内部会被转为HLS图像。提取S通道,选出S通道值在最大、最小范围之间的像素点。

      • combined_thresh(img)

        输入img为RGB图像,分别调用以上的x方向梯度、梯度值、梯度方向角、HLS的S阈值及同时满足这几个阈值范围的五个二值图像。
        return combined, abs_bin, mag_bin, dir_bin, hls_bin

    • gen_example_images.py
    1. 读取test_images文件夹下的jpg文件,调用img = cv2.undistort(img, mtx, dist, None, mtx)函数进行畸变校正,使用matplot进行显示,并将显示结果存为undistort_*.png文件。
    2. 调用img, abs_bin, mag_bin, dir_bin, hls_bin = combined_thresh(img)函数,生成相对应的二值图。使用matplot进行显示,并将显示结果存为binary_*.png文件。
    3. 调用img, binary_unwarped, m, m_inv = perspective_transform(img)生成鸟瞰视图,使用matplot进行显示,并将显示结果存为warped_*.png文件。
    4. 调用ret = line_fit(img)得到曲线拟合后的参数,调用viz2(img, ret, save_file=save_file)进行可视化,并将可视化的结果存为polyfit_*.png文件。
    5. 读取文件,调用undist = cv2.undistort(orig, mtx, dist, None, mtx)进行畸变校正。
    6. 调用left_curve, right_curve = calc_curve(left_lane_inds, right_lane_inds, nonzerox, nonzeroy),计算得到左右车道线曲线参数。
    7. 计算得到vehicle_offset *= xm_per_pix,车辆相对车道线的偏移值。
    8. 调用img = final_viz(undist, left_fit, right_fit, m_inv, left_curve, right_curve, vehicle_offset)将左右车道线和车道的偏移叠加到原图像,并进行显示。
    • Line.py
      • 定义类class Line():
        • 包含的属性:
          n: 移动平均的窗口大小
          detected:
          二次曲线的系数 x = A ∗ y 2 + B ∗ y + C x = A*y^2 + B*y + C x=Ay2+By+C
          二次曲线系数的平均值:A_avg,B_avg,C_avg
        • 包含的方法:
          get_fit(self):返回系数平均值
          def add_fit(self, fit_coeffs):增加一组A,B,C,返回A_avg,B_avg,C_avg
    • line_fit.py
      • 定义函数def line_fit(binary_warped)
        • 输入二值图像。
        • 返回一字典,包含左右车道线的参数。
          	ret = {}ret['left_fit'] = left_fit # left_fit = np.polyfit(lefty, leftx, 2)ret['right_fit'] = right_fitret['nonzerox'] = nonzeroxret['nonzeroy'] = nonzeroyret['out_img'] = out_imgret['left_lane_inds'] = left_lane_indsret['right_lane_inds'] = right_lane_inds
          
        • gen_example_images.py中被调用。img, binary_unwarped, m, m_inv = perspective_transform(img)ret = line_fit(img)
        • line_fit_video.py中被调用。ret = line_fit(binary_warped)
      • 定义函数def tune_fit(binary_warped, left_fit, right_fit)
      • 定义函数def viz1(binary_warped, ret, save_file=None)
      • 定义函数def viz2(binary_warped, ret, save_file=None)
      • 定义函数def calc_curve(left_lane_inds, right_lane_inds, nonzerox, nonzeroy)
        • gen_example_images.py中被调用,left_curve, right_curve = calc_curve(left_lane_inds, right_lane_inds, nonzerox, nonzeroy)
      • 定义函数def calc_vehicle_offset(undist, left_fit, right_fit)
      • 定义函数def final_viz(undist, left_fit, right_fit, m_inv, left_curve, right_curve, vehicle_offset)
    • line_fit_video.py 主程序运行入口
    • perspective_transform.py
      • 定义函数def perspective_transform(img):
      • return warped, unwarped, m, m_inv 返回透视变换后的图像、再反透视变换后的图像、透视变换和反透视变换矩阵。

1.2 源码解析

1.2.1 主程序line_fit_video.py

  • 运行方法。命令行输入python line_fit_video.py,程序会读取project_video.mp4,并且输出标注后的视频out.mp4。
  • 源码如下
  • 相关变量的读取、初始化
# Global variables (just to make the moviepy video annotation work)
with open('calibrate_camera.p', 'rb') as f:save_dict = pickle.load(f)
mtx = save_dict['mtx']
dist = save_dict['dist']
window_size = 5  # how many frames for line smoothing
left_line = Line(n=window_size)
right_line = Line(n=window_size)
detected = False  # did the fast line fit detect the lines?
left_curve, right_curve = 0., 0.  # radius of curvature for left and right lanes
left_lane_inds, right_lane_inds = None, None  # for calculating curvature
  • 定义单帧图像的注释函数
# MoviePy video annotation will call this function
def annotate_image(img_in):"""Annotate the input image with lane line markingsReturns annotated image"""# 使用global函数声明全局变量(只是声明,而不是定义)global mtx, dist, left_line, right_line, detectedglobal left_curve, right_curve, left_lane_inds, right_lane_inds# Undistort, threshold, perspective transformundist = cv2.undistort(img_in, mtx, dist, None, mtx)img, abs_bin, mag_bin, dir_bin, hls_bin = combined_thresh(undist)binary_warped, binary_unwarped, m, m_inv = perspective_transform(img)# Perform polynomial fitif not detected: # 如果不是检测目的,即为第一帧图像。运行彻底拟合。# Slow line fitret = line_fit(binary_warped) # 将阈值分割和透视变换后的图像送入line_fit()函数,返回车道检测后的参数,这里是最重要的一个函数left_fit = ret['left_fit']right_fit = ret['right_fit']nonzerox = ret['nonzerox']nonzeroy = ret['nonzeroy']left_lane_inds = ret['left_lane_inds']right_lane_inds = ret['right_lane_inds']# Get moving average of line fit coefficients # 使用移动平均法得到曲线拟合的参数left_fit = left_line.add_fit(left_fit)right_fit = right_line.add_fit(right_fit)# Calculate curvatureleft_curve, right_curve = calc_curve(left_lane_inds, right_lane_inds, nonzerox, nonzeroy)detected = True  # slow line fit always detects the lineelse:  # implies detected == True# Fast line fitleft_fit = left_line.get_fit()right_fit = right_line.get_fit()ret = tune_fit(binary_warped, left_fit, right_fit) # 如果不是第一帧,则调用tune_fit()函数进行拟合left_fit = ret['left_fit']right_fit = ret['right_fit']nonzerox = ret['nonzerox']nonzeroy = ret['nonzeroy']left_lane_inds = ret['left_lane_inds']right_lane_inds = ret['right_lane_inds']# Only make updates if we detected lines in current frameif ret is not None:left_fit = ret['left_fit']right_fit = ret['right_fit']nonzerox = ret['nonzerox']nonzeroy = ret['nonzeroy']left_lane_inds = ret['left_lane_inds']right_lane_inds = ret['right_lane_inds']left_fit = left_line.add_fit(left_fit)right_fit = right_line.add_fit(right_fit)left_curve, right_curve = calc_curve(left_lane_inds, right_lane_inds, nonzerox, nonzeroy)else:detected = Falsevehicle_offset = calc_vehicle_offset(undist, left_fit, right_fit)# Perform final visualization on top of original undistorted imageresult = final_viz(undist, left_fit, right_fit, m_inv, left_curve, right_curve, vehicle_offset)return result
  • 定义视频标注函数
def annotate_video(input_file, output_file):""" Given input_file video, save annotated video to output_file """video = VideoFileClip(input_file)annotated_video = video.fl_image(annotate_image)annotated_video.write_videofile(output_file, audio=False)
  • 调用以上函数进行测试
if __name__ == '__main__':# Annotate the videoannotate_video('project_video.mp4', 'out.mp4')# Show example annotated image on screen for sanity checkimg_file = 'test_images/test2.jpg'img = mpimg.imread(img_file)result = annotate_image(img)result = annotate_image(img)result = annotate_image(img)plt.imshow(result)plt.show()

这篇关于Advanced Lane Detection源码解读(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/701669

相关文章

解读GC日志中的各项指标用法

《解读GC日志中的各项指标用法》:本文主要介绍GC日志中的各项指标用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、基础 GC 日志格式(以 G1 为例)1. Minor GC 日志2. Full GC 日志二、关键指标解析1. GC 类型与触发原因2. 堆

Java设计模式---迭代器模式(Iterator)解读

《Java设计模式---迭代器模式(Iterator)解读》:本文主要介绍Java设计模式---迭代器模式(Iterator),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录1、迭代器(Iterator)1.1、结构1.2、常用方法1.3、本质1、解耦集合与遍历逻辑2、统一

MySQL之InnoDB存储页的独立表空间解读

《MySQL之InnoDB存储页的独立表空间解读》:本文主要介绍MySQL之InnoDB存储页的独立表空间,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、独立表空间【1】表空间大小【2】区【3】组【4】段【5】区的类型【6】XDES Entry区结构【

MySQL主从复制与读写分离的用法解读

《MySQL主从复制与读写分离的用法解读》:本文主要介绍MySQL主从复制与读写分离的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、主从复制mysql主从复制原理实验案例二、读写分离实验案例安装并配置mycat 软件设置mycat读写分离验证mycat读

Python的端到端测试框架SeleniumBase使用解读

《Python的端到端测试框架SeleniumBase使用解读》:本文主要介绍Python的端到端测试框架SeleniumBase使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录SeleniumBase详细介绍及用法指南什么是 SeleniumBase?SeleniumBase

Nacos注册中心和配置中心的底层原理全面解读

《Nacos注册中心和配置中心的底层原理全面解读》:本文主要介绍Nacos注册中心和配置中心的底层原理的全面解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录临时实例和永久实例为什么 Nacos 要将服务实例分为临时实例和永久实例?1.x 版本和2.x版本的区别

C++类和对象之默认成员函数的使用解读

《C++类和对象之默认成员函数的使用解读》:本文主要介绍C++类和对象之默认成员函数的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、默认成员函数有哪些二、各默认成员函数详解默认构造函数析构函数拷贝构造函数拷贝赋值运算符三、默认成员函数的注意事项总结一

MySQL的ALTER TABLE命令的使用解读

《MySQL的ALTERTABLE命令的使用解读》:本文主要介绍MySQL的ALTERTABLE命令的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、查看所建表的编China编程码格式2、修改表的编码格式3、修改列队数据类型4、添加列5、修改列的位置5.1、把列

Linux CPU飙升排查五步法解读

《LinuxCPU飙升排查五步法解读》:本文主要介绍LinuxCPU飙升排查五步法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录排查思路-五步法1. top命令定位应用进程pid2.php top-Hp[pid]定位应用进程对应的线程tid3. printf"%

解读@ConfigurationProperties和@value的区别

《解读@ConfigurationProperties和@value的区别》:本文主要介绍@ConfigurationProperties和@value的区别及说明,具有很好的参考价值,希望对大家... 目录1. 功能对比2. 使用场景对比@ConfigurationProperties@Value3. 核