Search code examples
pythonopencvobject-detection

Lane detection noise removal (getting too many unnecessary lines)


I'm trying to do lane detection, the code is as below, I applied HoughLinesP on canny edge detection's o/p. So the idea is to display only the lines that which are (usually present on the video + more probable to be a lane i.e by picking up the angle). I don't want to use any machine learning algorithms. So please help..

Here are the details :

  • Code :

    import time
    import numpy as np
    import matplotlib.pyplot as plt
    
    vid = cv2.VideoCapture('4.mp4')
    
    while True:
    
        #cv2.namedWindow('frame',cv2.WINDOW_NORMAL)
    
        ret, img_color = vid.read()
    
        if not ret:
            vid = cv2.VideoCapture('5.mp4')
            continue
    
        num_rows, num_cols = img_color.shape[:2]
        rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), 270, 0.56)  #3
        img_rotated = cv2.warpAffine(img_color, rotation_matrix, (num_cols, num_rows))
    
    
        height, width = img_rotated.shape[:2]
        img_resize = cv2.resize(img_rotated,(int(0.8*width), int(0.8*height)), interpolation = cv2.INTER_CUBIC) #2
    
        img_clone = img_resize[10:842,530:1000].copy()
        img_roi = img_resize[10+250:842-200,530:1000]
    
    
        img_gray = cv2.cvtColor(img_roi,cv2.COLOR_BGR2GRAY) #1
    
        kernel = [  [0,-1,0],   [-1,5,-1],  [0,-1,0]    ]
        kernel = np.array(kernel)
        img_sharp = cv2.filter2D(img_gray,-1,kernel)
    
        blur = cv2.GaussianBlur(img_sharp,(5,5),0)
    
        img_canny = cv2.Canny(blur,130,170, apertureSize = 3)   #4
    
        lines = cv2.HoughLinesP(img_canny, 1, np.pi/180, 60, maxLineGap = 240)
        if lines is not None:
            print(len(lines))
            for line in lines:
                x1,y1,x2,y2 = line[0]
                cv2.line(img_clone, (x1,y1+250), (x2,y2+250), (0,255,0), 2)
                #cv2.line(img_clone, (x1,y1), (x2,y2), (255,255,0), 3)
    
    
        cv2.imshow('frame',img_clone)
        cv2.imshow('frame2', img_canny)
    
        k = cv2.waitKey(35) & 0xFF
        if k==27 :
            break
    
    vid.release()   
    cv2.destroyAllWindows() 
    

Here's link to videos I'm using

In 4.mp4 you can see that after running this code, a few seconds later a person comes in and there are so many lines as canny detects so many edges in that region, secondly i've fixated region of image for which i want to be dynamic, the idea is to set the region of image on the basis on more probable lanes. Also there's a cluster of lines appearing, i want to shorten it down to more probable line. Thank you for reading.


Solution

  • You won't get much better results. This is just the nature of your problem. You will now have to go into creating a mathematical model of your lane, and use your hough-lines to correct that model.

    E.g. you could track the lane in certain bands of the image using a kalman filter. You then can use the predict step of this filter when you observe line segments that are within the expected angle around your observation band.