车道检测元组错误hough_lines OpenCv

时间:2017-07-28 15:52:52

标签: python opencv houghlinesp

我一直在从他的YouTube频道发表关于PYTHON PLAYS GTA V系列的sentdex的LaneDetection学习。

未来我想应用我自己的车道检测代码,但我遇到了元组错误 我是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

请帮助我。 此致, Vidit Shah

1 个答案:

答案 0 :(得分:0)

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)