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

问题描述 投票:0回答:1

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

我使用的Spyder从蟒蛇(Python的3.7)

  1. 我没有得到一个视频输出,该cv2.imshow()只显示一张图片(也许视频被卡住,由于一些bug)
  2. 化妆协调作用下发生的错误: 斜率,截距=行参数

类型错误:无法解压缩非迭代numpy.float64对象

一世

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

希望有人能帮助我与

谢谢 。

python opencv opencv3.0
1个回答
2
投票

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

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

while(cap.isOpened()):循环之外。这意味着OpenCV的将显示整体,而完成后,才意味着在视频的结尾你的形象。因此,它只能显示最后一帧。为了解决这个问题,把上述2行的while循环中。

第二个问题是因为slope,intercept = line_parameters假定line_parameters是2个元素,斜率和截距的一个元组。但是,如果我们看看那里line_parameters从何而来,我们可以看到,这将是一个numpy的阵列。你不能解开一个numpy的阵列状的元组。相反,你可以使用slope,intercept = tuple(line_parameters)例如。

© www.soinside.com 2019 - 2024. All rights reserved.