对于某些上下文,我正在关注本教程: https://www.youtube.com/watch?v=eLTLtUVuuy4 (OpenCV Python 教程 - 为自动驾驶汽车寻找车道(计算机视觉基础教程))
这是我正在执行车道检测的视频: https://www.istockphoto.com/video/exciting-journey-on-road-through-the-desert-california-usa-gm820334744-133238849
我得到的错误: 文件“c:\Users\hello\Desktop inding-lanes\image_test.py”,第 93 行,位于 平均线 = average_slope_intercept(帧,线) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 文件“c:\Users\hello\Desktop inding-lanes\image_test.py”,第 23 行,在 average_slope_intercept 中 对于行中的行: TypeError: 'NoneType' 对象不可迭代
代码在单帧上运行时有效,但在视频上运行时停止工作 我已经尝试过弄乱 HoughLinesP 函数,但那并没有真正做任何事情。
谢谢!
这是我的代码:
import cv2
import numpy as np
#make coordinates from slope and intercept given the image
def make_coordinates(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):
left_fit = []
right_fit = []
for line in lines:
#reshape all lines into a one dimentional array with 4 elements, which will be x1, y1, x2, y2
x1, y1, x2, y2 = line.reshape(4)
parameters = np.polyfit((x1, x2),(y1, y2), 1)
#get the slope and intercept from parameters
slope = parameters[0]
intercept = parameters [1]
if slope < 0:
left_fit.append((slope, intercept))
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_coordinates(image, left_fit_average)
right_line = make_coordinates(image, right_fit_average)
return np.array([left_line, right_line])
def canny(lane_image):
#apply canny edge detection technique: first make it black and white, then blur it, and finally apply canny function
gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
canny = cv2.Canny(gray, 50, 150)
return canny
#outputs a display image based on source image and lines from hough transformation
def display_lines(image, lines):
line_image = np.zeros_like(image)
if lines is not None:#if lines are not empty
for x1, y1, x2, y2 in lines:
#draws line segment connecting two points (2nd and 3rd argument )over an image (1st argument),
#then gives it a colour and thickeness (last two arguments)
cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 10)
return line_image
#this function makes a region of interest, i.e a black mask over the main image that hides unimportant stuff
def region_of_interest(image):
height = image.shape [0]
polygons = np.array([
#these are the coordinates of the trianle taken by printing the image using matlibplot
[(300, height), (1200, 500), (790, 300)]
])
mask = np.zeros_like(image)
#superimpose the polygon on the mask, filling the area of the polygon with pixels of 255 intensity (white)
cv2.fillPoly(mask, polygons, 255)
masked_image = cv2.bitwise_and(image, mask)
return masked_image
cap = cv2.VideoCapture("test3.mp4")
while(cap.isOpened()):
_, frame = cap.read()
canny_image = canny(frame)
cropped_image = region_of_interest(canny_image)
lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength=30, maxLineGap=5)
averaged_lines = average_slope_intercept(frame, lines)
line_image = display_lines(frame, averaged_lines)
combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)
cv2.imshow("result ", combo_image)
if cv2.waitKey(1) == ord('t'):
break
cap.release()
cv2.destroyAllWindows()