pythonopencvhough-transformhoughlinesp

Python and OpenCV - Dominant Line in Hough Line transform


For my program, using OpenCV and Python, I'm trying to detect road lanes. In order to accomplish this, I used Hough Line Transform to detect lines. However, it finds many lines right next to each other and I'm trying to find a way to make an average line that's in between all of those other lines. Tips?

Here's my code:

    import numpy as np
    import cv2

    cap = cv2.VideoCapture('CVfootage.mov')

    while(True):
        ret, frame = cap.read()
        frame = frame[200:720, 0:1280]    
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        blur = cv2.GaussianBlur(gray,(7,7),0)
        edges = cv2.Canny(blur, 50, 150)

        lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=100,maxLineGap=80)

        a,b,c = lines.shape
        for i in range(a):
            cv2.line(blur, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)

        cv2.imshow('edges', edges)
        cv2.imshow('hough', blur)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

Solution

  • you need to adjust the parameter of the houghlinesP function

    if you want less lines next to each other : increase 'rho'

    if you want less lines in general : increase threshold