无人驾驶学习---第一课

无人驾驶学习—第一课

(本课程内容来自优达学城无人驾驶纳米学位)
车道线识别
内容:
识别出一幅图像中的车道线并将其标注出来
过程:
1、准备一张包含有车道线的图片;
2、读取图片并转换为灰度图;
3、使用一个5*5大小的算子对图片进行高斯平滑;
4、设置Canny边缘检测的参数:low_threshold = 50、high_threshold = 150,得到边缘;
5、通过参数设置感兴趣区域,截取出边缘图中感兴趣区域,得到掩膜下的车道线;
6、设置霍夫变换参数,在掩膜下的车道线图片中将车道线标注出来;
7、将标注出来的车道线相加道边缘图中,得到最终的车道线图;

图片:
在这里插入图片描述
代码:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2

def lane_identification():
	# Read the image and convert it to grayscale image 
    image = mpimg.imread('lane-linep.jpg')
    gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)

    # Define a kernel size and apply Gaussian smoothing
    kernel_size = 5
    blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)

    # Define parameters for Canny and apply
    low_threshold = 50
    high_threshold = 150
    edges = cv2.Canny(blur_gray, low_threshold, high_threshold)

    # create a masked edges image using cv2.fillPoly()
    mask = np.zeros_like(edges)   
    ignore_mask_color = 255 

    # This time we are defining a four sided polygon to mask
    imshape = image.shape
    vertices = np.array([[(0,imshape[0]),(450, 290), (490, 290), (imshape[1],imshape[0])]], dtype=np.int32)
    cv2.fillPoly(mask, vertices, ignore_mask_color)
    masked_edges = cv2.bitwise_and(edges, mask)

    # Define the Hough transform parameters
    rho = 2 # distance resolution in pixels of the Hough grid
    theta = np.pi/180 # angular resolution in radians of the Hough grid
    threshold = 15     # minimum number of votes (intersections in Hough grid cell)
    min_line_length = 40 #minimum number of pixels making up a line
    max_line_gap = 20    # maximum gap in pixels between connectable line segments
    line_image = np.copy(image)*0 # creating a blank to draw lines on

    # Run Hough on edge detected image
    # Output "lines" is an array containing endpoints of detected line segments
    lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]),
                                min_line_length, max_line_gap)

    # Iterate over the output "lines" and draw lines on a blank image
    for line in lines:
        for x1,y1,x2,y2 in line:
            cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)

    # Create a "color" binary image to combine with line image
    color_edges = np.dstack((edges, edges, edges)) 

    # Draw the lines on the edge image
    lines_edges = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0) 
    plt.imshow(lines_edges)
    plt.show()

过程图片:

1、灰度图:
在这里插入图片描述

2、边缘检测图:
在这里插入图片描述
3、掩膜下的车道线:
在这里插入图片描述
4、标注出来的车道线:
在这里插入图片描述
5、边缘图中的车道线:
在这里插入图片描述
第一课较简单,只识别出图片中车道线,后续继续加油,本博客内容不得转载,不得作为他用,仅互相学习,谢谢。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值