车道检测元组错误hough_lines OpenCv



我一直在研究LaneDetection,从他的YouTube频道的sentdex学习,说PYTHON玩GTA V系列。

前面我想应用自己的车道检测代码,但我遇到了元组错误 我是OpenCV的新手,因此暂时无法掌握这些概念

下面是正在生成的hough_line函数错误,该错误正在从进程函数调用。

def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
"""
`img` should be the output of a Canny transform.
Returns an image with hough lines drawn.
"""
lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len,
maxLineGap=max_line_gap)
line_img = np.zeros((img.shape, 3), dtype=np.uint8)
draw_lines(line_img, lines)
return line_img
def process_image(img):
img_test = grayscale(img)
img_test = gaussian_blur(img_test, 7)
img_test = canny(img_test, 50, 150)
imshape = img.shape
vertices = np.array([[(100,imshape[0]),(400, 330), (600, 330), (imshape[1],imshape[0])]], dtype=np.int32)
img_test = region_of_interest(img_test, vertices)
rho = 2 # distance resolution in pixels of the Hough grid
theta = np.pi/180 # angular resolution in radians of the Hough grid
threshold = 55     # minimum number of votes (intersections in Hough grid cell)
min_line_length = 40 #minimum number of pixels making up a line
max_line_gap = 100    # maximum gap in pixels between connectable line segments
line_image = np.copy(img)*0 # creating a blank to draw lines on
img_test = hough_lines(img_test, rho, theta, threshold, min_line_length, max_line_gap)
return img_test
img = cv2.imread("img.jpeg")
res=process_image(img)
cv2.imshow("Image",res)
cv2.waitKey(0)

错误 生成:

/Users/ViditShah/anaconda/envs/py27/bin/python /Users/ViditShah/Downloads/untitled1/detection2.py
Traceback (most recent call last):
File "/Users/ViditShah/Downloads/untitled1/detection2.py", line 124, in <module>
res=process_image(img)
File "/Users/ViditShah/Downloads/untitled1/detection2.py", line 120, in process_image
img_test = hough_lines(img_test, rho, theta, threshold, min_line_length, max_line_gap)
File "/Users/ViditShah/Downloads/untitled1/detection2.py", line 102, in hough_lines
line_img = np.zeros((img.shape, 3), dtype=np.uint8)
TypeError: 'tuple' object cannot be interpreted as an index
Process finished with exit code 1

请帮助我。 你的真诚, 维迪特·沙阿

img.shape在元音中返回图像的高度和高度。您的代码执行以下操作:

line_img = np.zeros(( (WIDTH,HEIGHT), 3), dtype=np.uint8)

让我们看一下 numpy 文档:https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html

现在让我们把你的坏图佩尔变成三倍。这是创建具有 3 个 8 位通道的图像的方法:

(w,h) = img.shape
np.zeros((w,h,3), dtype=np.uint8) 

最新更新