OpenCV机器视觉-识别红绿颜色

识别红绿颜色


识别车道线


车道线检测


使用opencv来完成一个车道线检测的案例

完成这样的案例我们需要经历哪些步骤呢 ? 我们先来思考一下解决问题的思路.当前情况下,摄像头拍出了很多的东西,例如路边的杂草远方的山.但是在我们自动驾驶的过程中,我们并不需要这么多东西,所以我们要考虑提取感兴趣的区域.有了感兴趣的区域之后,我们接下来就需要来识别道路.大家可能会想道路可能会有弯道,但是在小范围内,它还是直线,所以我们可以使用前面我们学过的霍夫直线来进行检测。

这张摄像头拍摄到的照片
在这里插入图片描述

这张是我们使用canny边缘检测算法得到的图片

在这里插入图片描述

然后使用多边形截去不感兴趣的区域

在这里插入图片描述

通过以上三个步骤之后,下面我们就可以使用霍夫直线的方式,提取到车辆直线啦!

示例代码


import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

def canny(image):
    gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY);
    blur = cv.GaussianBlur(gray,(5,5),0)
    canny= cv.Canny(blur,50,100)
    return canny

def roi(image):
    height = image.shape[0]
    polygons = np.array([[(200,height),(1100,height),(550,250)]])
    mask = np.zeros_like(image);
    cv.fillPoly(mask,polygons,255)

    masked_image = cv.bitwise_and(image,mask)
    return masked_image

def display_lines(image,lines):
    line_image = np.zeros_like(image);

    for line in lines:
        x1,y1,x2,y2 = line.reshape(4)
        cv.line(line_image,(x1,y1),(x2,y2),(255,255,0),10);

    return line_image

def make_coordinates(image,parameters):
    slope = parameters[0]
    intercept = parameters[1]

    y1 = image.shape[0]
    y2 = int(y1*0.6)

    x1 = int((y1 - intercept)/slope)
    x2 = int((y2 - intercept)/slope)

    return np.array([x1,y1,x2,y2])



def average_slop_intercept(image,lines):
    left_fit = []
    right_fit = []
    print(lines)
    for line in lines:
        x1, y1, x2, y2 = line.reshape(4)
        print(x1,y1,x2,y2)
        parameters = np.polyfit((x1,x2),(y1,y2),1)
        print(parameters)
        slope = parameters[0]
        intercept = parameters[1]

        if slope > 0 :
            left_fit.append((slope,intercept))
        else:
            right_fit.append((slope,intercept))


    #yong
    result_lines = []
    if len(left_fit):
        left_fit_average = np.average(left_fit, axis=0)
        left_line = make_coordinates(image,left_fit_average)
        result_lines.append(left_line)
    if len(right_fit)>0:
        right_fit_average = np.average(right_fit, axis=0)
        right_line = make_coordinates(image,right_fit_average)
        result_lines.append(right_line)

    return result_lines

cv.namedWindow("result",cv.WINDOW_NORMAL)

capture = cv.VideoCapture()
capture.open("test2.mp4")

if capture.isOpened():
    flag = True
    while flag:
        flag,image = capture.read();
        if not flag: break
        # image = cv.imread("test_image.jpg")
        lane_image = image # np.copy(image);
        print(lane_image.shape)
        canny_image = canny(lane_image)

        cropped_image = roi(canny_image)

        lines = cv.HoughLinesP(cropped_image,2,np.pi/180,100,minLineLength=40,maxLineGap=5)

        average_lines = average_slop_intercept(image,lines)
        lines_image = display_lines(lane_image,average_lines)

        combo_image = cv.addWeighted(lane_image,0.8,lines_image,1,1)

        cv.imshow("result",combo_image)
        cv.waitKey(10)

cv.waitKey(0)
cv.destroyAllWindows()

运行结果(动态)


注意:可能会出现Could not load the Qt platform plugin “xcb” 的错误,

安装pip install opencv-python-headless

如果不能解决问题,则需要安装sudo apt install --reinstall libxcb-xinerama0

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值