消除车道检测噪声(获取过多不必要的行)

时间:2019-02-08 11:33:24

标签: python opencv object-detection

我正在尝试进行车道检测,代码如下,我在Canny边缘检测的o / p上应用了HoughLinesP。因此,想法是仅显示那些(通常在视频中出现的行+更可能是一条车道,即通过拾取角度)。我不想使用任何机器学习算法。所以请帮忙。

以下是详细信息:

  • 代码:

    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

在4.mp4中,您可以看到运行此代码后,几秒钟后一个人进来了,因为行数太多,因为canny在该区域检测到了许多边缘,其次,我固定了图像区域我想保持动态,其想法是根据更多可能的车道设置图像区域。 也出现了许多行,我想将其缩短为更可能的行。感谢您的阅读。

1 个答案:

答案 0 :(得分:0)

您将不会获得更好的结果。这只是您问题的本质。现在,您必须创建车道的数学模型,并使用粗线修正该模型。

例如您可以使用卡尔曼滤镜在图像的某些波段中跟踪车道。然后,当您观察到围绕观察带的期望角度内的线段时,可以使用此过滤器的预测步骤。

相关问题