繁体   English   中英

Opencv Mog2 背景减法器:Output 是灰度图像

[英]Opencv Mog2 Background subtractor: Output is a gray image

我目前正在尝试使用背景减法器从检测器中去除误报。 Eack 链接读取 mjpeg 视频,然后将减法器应用于每个视频。 该代码有效,结果如下:

背景减法器结果

MoG2 背景分隔符的代码是:

    for index, link in enumerate(onlyfiles):
        print(link)
        subtractor = cv2.createBackgroundSubtractorMOG2(history=20, varThreshold=100, detectShadows=True)
        count=0
        count2=0
        # Create a VideoCapture object and read from input file
        # If the input is the camera, pass 0 instead of the video file name

        cap = cv2.VideoCapture(link)
        # Check if camera opened successfully
        if (cap.isOpened() == False):
            print("Error opening video stream or file")

        # Read until video is completed
        while (cap.isOpened()):
            # Capture frame-by-frame
            ret, frame = cap.read()
            if ret == True:
                print("Frame detected")
                frame1 = frame.copy()  
                gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
                blurred = cv2.bilateralFilter(gray, 9, 9, 9)
                mask = subtractor.apply(blurred)     
                cv2.imshow("mask1", mask)
                # Copy the thresholded image.
                im_floodfill = mask.copy()               
                # Mask used to flood filling.
                # Notice the size needs to be 2 pixels than the image.
                h, w = mask.shape[:2]
                mask1 = np.zeros((h+2, w+2), np.uint8)                 
                # Floodfill from point (0, 0)
                cv2.floodFill(im_floodfill, mask1, (0,0), 255);                
                # Invert floodfilled image
                im_floodfill_inv = cv2.bitwise_not(im_floodfill)                 
                # Combine the two images to get the foreground.
                im_out = mask | im_floodfill_inv
                cv2.imshow("Foreground", im_out)
                cv2.imshow('Video', frame1)
                cv2.waitKey(25)                 

                # Press Q on keyboard to  exit
                if cv2.waitKey(25) & 0xFF == ord('q'):
                    break

            # Break the loop

                cv2.imshow('Video', frame)  
                cv2.waitKey(25)
            else:
                break

        # When everything done, release the video capture object
        cap.release()
        # Closes all the frames
        cv2.destroyAllWindows()

在某些情况下,会输出一批灰度图像,如下所示: 灰度输出图像

为什么会发生这种情况,是由于历史原因吗? 如何纠正?

为了确定这种情况的发生,我输出了黑色像素的数量。零表示图像是灰色的,像素值为 127。

Frame detected
zero pixels:  0
Frame detected
zero pixels:  414628
Frame detected
zero pixels:  414615
Frame detected
zero pixels:  41465

Frame detected
zero pixels:  0
Frame detected
zero pixels:  413462
Frame detected
zero pixels:  414719
Frame detected
zero pixels:  414720
Frame detected
zero pixels:  414720
Frame detected
zero pixels:  414592
Frame detected
zero pixels:  413932
Frame detected
zero pixels:  412518
Frame detected
zero pixels:  412495
Frame detected
zero pixels:  414221

Frame detected
zero pixels:  0
Frame detected
zero pixels:  412651
Frame detected
zero pixels:  414290
Frame detected
zero pixels:  414490
Frame detected
zero pixels:  414707
Frame detected
zero pixels:  414687
Frame detected
zero pixels:  414689
Frame detected
zero pixels:  414665
Frame detected
zero pixels:  414704
Frame detected
zero pixels:  414583

Frame detected
zero pixels:  0

如果该项目与历史参数相关联,那么如何编辑减法器以设置何时可以收集图像进行捕获?

我也在试验这个背景减法器的东西。 我有同样的问题,并以某种方式解决了它。 我认为你不能应用这种某种类型的循环,所以你可能想事先调用它。 就像在你的情况下,在 for 循环之前。 也许它会有所帮助。

尝试更改文件的开头,以便在循环外调用 backgroundsubtractor function。 IE:

subtractor = cv2.createBackgroundSubtractorMOG2()
for index, link in enumerate(onlyfiles):
    print(link)
    count=0
    count2=0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM