为自动驾驶汽车寻找车道,代码问题

Find Lanes for Self-Driving Cars, code problem

我在网上自学图像识别。 下面的代码below.I遇到了两个问题

我正在使用 Anaconda 的 Sypder (python 3.7)

  1. 我没有得到视频输出,cv2.imshow()只显示了一张图片(可能视频因为一些bug卡住了)

  2. make_coordinate函数下出现错误:

    斜率,截距 = line_parameters

TypeError: cannot unpack non-iterable numpy.float64 object

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


def make_coordinate(image,line_parameters):
    slope,intercept = line_parameters
    y1=image.shape[0]
    y2=int(y1*(3/5))
    x1=int((y1-intercept)/slope)
    x2=int((y2-intercept)/slope)
    return np.array([x1,y1,x2,y2])


def average_slope_intercept(image,lines): ##produce the best fit lines
    left_fit=[]
    right_fit=[]
    for line in lines:
        x1,y1,x2,y2=line.reshape(4)
        parameters=np.polyfit((x1,x2),(y1,y2),1)
        slope=parameters[0]
        intercept=parameters[1]
        
        if slope<0:
            left_fit.append((slope,intercept)) ##z left lane has negative slope.
        else:
            right_fit.append((slope,intercept))
        
    left_fit_average=np.average(left_fit,axis=0)
    right_fit_average=np.average(right_fit,axis=0)
    left_line=make_coordinate(image,left_fit_average)
    right_line=make_coordinate(image,right_fit_average)
    
    return np.array([left_line,right_line])


    
    
def Canny(image):
    gray=cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)  ## convert the image to gray color
    blur= cv2.GaussianBlur(gray,(5,5),0) ## blur the image to reduce noise{source,kernel,deviation}
    canny=cv2.Canny(blur,50,150)  ## trace out the lines that have sharp change in color
    return canny

def display_lines(image,lines): ##input slope and intercept to generate lines.
    line_image=np.zeros_like(image)
    if lines is not None:
        for line in lines:
            x1,y1,x2,y2=line.reshape(4)
            cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)  ## color in RGB ,line thickness
            
    return line_image
            
    


def region_of_interest(image):
    height=image.shape[0]
    polygon=np.array([[(200,height),(1100,height),(550,250)]]) ##A triangle with bottem from 200 to 1100 and tip at (550,250)
    mask=np.zeros_like(image)
    cv2.fillPoly(mask,polygon,255)  ##put the triangle into a black background.
    masked_image=cv2.bitwise_and(image,mask)  ## trace the lanes into black background using masking
    return masked_image

#
#
#read_image=cv2.imread('test_image.jpg') ##read the image
#image=np.copy(read_image) ## copy the image
#
#canny_image=Canny(image)  ## trace out the lines that have sharp change in color
#
#cropped_image=region_of_interest(canny_image)  ## trace the interested lines into black background
#
#lines=cv2.HoughLinesP(cropped_image,2,np.pi/180,100,np.array([]),minLineLength=0,maxLineGap=5)  ##2 pixel, 1 degree in radian,threshold 100,array,length of line in pixel will accept,maximum length of distance of pixel can be connect to a line.
#averaged_lines=average_slope_intercept(image,lines)
#line_image=display_lines(image,averaged_lines)
#
#combined_image=cv2.addWeighted(image,0.8,line_image,1,1) ##trace out the ideal path in the original image
#
#
#
#
#plt.imshow(canny_image)
#plt.show()

cap=cv2.VideoCapture('test2.mp4')
while(cap.isOpened()):
    _,frame = cap.read()
    
    canny_image=Canny(frame)  ## trace out the lines that have sharp change in color

    cropped_image=region_of_interest(canny_image)  ## trace the interested lines into black background

    lines=cv2.HoughLinesP(cropped_image,2,np.pi/180,100,np.array([]),minLineLength=0,maxLineGap=5)  ##2 pixel, 1 degree in radian,threshold 100,array,length of line in pixel will accept,maximum length of distance of pixel can be connect to a line.
    averaged_lines=average_slope_intercept(frame,lines)
    line_image=display_lines(frame,averaged_lines)

    combined_image=cv2.addWeighted(frame,0.8,line_image,1,1) ##trace out the ideal path in the original image

cv2.imshow('result',combined_image)  ##display the image
cv2.waitKey(5) ## delay in display

希望有人能帮助我

谢谢。

你的第一个问题是因为你把行

cv2.imshow('result',combined_image)  ##display the image
cv2.waitKey(5) ## delay in display

while(cap.isOpened()): 循环之外。这意味着 opencv 只会在整个 while 完成后显示您的图像,这意味着在视频结束时。所以它只显示最后一帧。要解决这个问题,请将以上两行放在 while 循环中。

第二个问题是因为 slope,intercept = line_parameters 假设 line_parameters 是 2 个元素的元组,斜率和截距。但是,如果我们查看 line_parameters 的来源,我们可以看到它将是一个 numpy 数组。您不能像元组一样解压 numpy 数组。例如,您可以使用 slope,intercept = tuple(line_parameters)