0

我一直在学习 LaneDetection,从他的 YouTube 频道中的 sentdex 中学习 PYTHON PLAYS GTA V 系列。

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

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

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

请帮助我。此致,维迪特沙阿

4

1 回答 1

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) 
于 2017-07-28T16:33:58.233 回答