pythonopencvhoughlinesp

Lane Detection tuple error hough_lines OpenCv


I have been working on LaneDetection learning from sentdex from his YouTube channel stating PYTHON PLAYS GTA V series.

Ahead I want to apply my own Lane Detection code but I encounter tuple error I am new to OpenCV and thus failing to grasp the concepts for now

Below is the hough_line function error is being generated which is being called from process function.

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)

error Generated:

/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

Kindly help me. Yours Sincerely, Vidit Shah


Solution

  • img.shape returns with and height from your image in a tupel. Your code does something like this:

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

    Lets have a look at the numpy documentation: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html

    Now lets make ur bad tupel to a tripel. This is how to create an image with 3 8bit channels:

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