自动驾驶python代码_寻找自动驾驶汽车的车道,代码问题

我在网上自学图像识别。

按照下面的代码,我遇到了两个问题。在

我正在使用Anaconda的Sypder(python3.7)我没有得到一个视频输出,cv2.imshow()只显示了一张图片(可能是由于某些错误导致视频被卡住)

在make_坐标函数下发生错误:

斜率,截距=直线参数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

希望有人能帮我

谢谢。在

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值