Hough Lines Detection在帧与帧之间不一致

时间:2017-09-08 03:03:30

标签: opencv hough-transform canny-operator

即使场景是静态的,我得到的HOUGH线(红色和白色)从一帧视频到下一帧也不一样。

Canny结果在帧到帧之间也有很多变化。这个问题在我的测试用例中并没有那么糟糕,但对于一个真实的街景,Canny检测到的边缘确实在帧与帧之间发生了变化。

可以看出,很多线路也只是错过了。

我意识到帧与帧之间的噪声不同,但转换为灰度和后续模糊会使输入图像非常接近(至少在我看来)。

发生了什么事,有什么方法可以解决这个问题吗?

Same Scene - Different Results

# Python 2/3 compatibility
import sys
PY3 = sys.version_info[0] == 3

if PY3:
    xrange = range

import numpy as np
import cv2
import math
from time import sleep

cap = cv2.VideoCapture(0)

if __name__ == '__main__':

    SLOPE = 2.0

    while(True):

        sleep(0.2)

        ret, src = cap.read()
        gray            = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
        gray_blur       = cv2.medianBlur(gray, 5)
        gray_blur_canny = cv2.Canny(gray_blur,  25, 150)

        cv2.imshow("src", src)
        cv2.imshow("gray_blur", gray_blur)
        cv2.imshow("gray_blur_canny", gray_blur_canny)

        cimg = src.copy() # numpy function

        lines = cv2.HoughLinesP(
                    gray_blur_canny, 
                    1, 
                    math.pi/180.0, 
                    40, 
                    np.array([]), 
                    50, 
                    10)
        if lines is not None:
            a,b,c = lines.shape
            for i in range(a):
                numer = lines[i][0][3] - lines[i][0][1] + 0.001;
                denom = lines[i][0][2] - lines[i][0][0];
                if (denom == 0):
                    denom = 0.001;
                slope = abs(numer/denom);
                print slope
                if (slope > SLOPE):
                        cv2.line(
                            cimg, 
                            (lines[i][0][0], lines[i][0][1]), 
                            (lines[i][0][2], lines[i][0][3]), 
                            (0, 0, 255), 
                            3, 
                            cv2.LINE_AA)
                if (slope < (1.0/SLOPE)):
                        cv2.line(
                            cimg, 
                            (lines[i][0][0], lines[i][0][1]), 
                            (lines[i][0][2], lines[i][0][3]), 
                            (200, 200, 200), 
                            3, 
                            cv2.LINE_AA)

        cv2.imshow("hough lines", cimg)
        ch = cv2.waitKey(1)
        if ch == 27:
            break

    cv2.destroyAllWindows()

0 个答案:

没有答案