简体   繁体   English

如何使用OpenCV BackgroundSubtractorMOG2获取移动对象的掩码

[英]How to get moving object's mask using OpenCV BackgroundSubtractorMOG2

I want to mask the moving objects from video. 我想屏蔽视频中的移动物体。 I found that OpenCV has some built-in BackgroundSubtractors which could possibly saving my time a lot. 我发现OpenCV有一些内置的BackgroundSubtractors可以节省我的时间。 However, according to the official reference , the function: 但是,根据官方参考 ,功能:

void BackgroundSubtractorMOG2::operator()(InputArray image, OutputArray fgmask, double learningRate=-1)

should output a mask, fgmask, but it doesn't. 应该输出一个掩码,fgmask,但它没有。 The fgmask variable will contain the "contour of the mask" instead after invoking above method. 在调用上述方法之后,fgmask变量将包含“掩码的轮廓”。 That's weird. 这很奇怪。 All I want is a simple closed region filled with white color(for example) to represent the moving objects. 我想要的只是一个填充白色的简单封闭区域(例如)来表示移动的物体。 How could I do that? 我怎么能这样做?

Any reply or recommendation would be very appreciate. 任何回复或建议将非常感谢。 Thanks a lot. 非常感谢。

Here's my code: 这是我的代码:

int main(int argc, char *argv[])
{
    cv::BackgroundSubtractorMOG2 bg = BackgroundSubtractorMOG2(30,16.0,false);
    cv::VideoCapture cap(0);
    cv::Mat frame, mask, _frame, _fmask;
    cvNamedWindow("mask", CV_WINDOW_AUTOSIZE);
    for(;;)
    {
        cap >> frame;
        bg(frame,fmask,-1);

        _frame = IplImage(frame);
        _fmask = IplImage(fmask);

        cvShowImage("mask", &_fmask);
        if(cv::waitKey(30) >= 0) break;
    }
    return 0;
}

A snapshot of the output video is: 输出视频的快照是: 在此输入图像描述

ps My working environment is OpenCV2.4.3 on OSX 10.8 and XCode 4.5.2 with apple LLVM compiler 4.1. ps我的工作环境是OSX 10.8上的OpenCV2.4.3和带有Apple LLVM编译器4.1的XCode 4.5.2。

If you want to acquire the whole objects filled with white pixels in the foreground then I would ask you to tell me something about your experience. 如果你想获得前景中充满白色像素的整个物体,那么我会请你告诉我一些你的经历。

My question is, for the code, you mentioned above, do you get more white pixels when you generate more motion in front of your camera? 我的问题是,对于上面提到的代码,当你在相机前产生更多动作时,你会得到更多的白色像素吗?

If yes then there are two paramenters to learn about for your requirement. 如果是,则有两个参数可供您了解。

First is the History parameter. 首先是History参数。 which you have configured as 30 in the constructor BackgroundSubtractorMOG2(30,16.0,false); 你在构造函数BackgroundSubtractorMOG2中配置为30 (30,16.0,false); . You can test this param by incresing, say to 300. It will maintain the motion history of the object in the foreground. 您可以通过incresing测试此参数,比如说300.它将保持对象在前景中的运动历史。 So if you have moved completely from your starting location within the 300 frames then you will get whole object covered with white pixels as you want. 因此,如果您已经完全从300帧内的起始位置移动,那么您可以根据需要获得覆盖有白色像素的整个对象。 but it will be erased gradually. 但它会逐渐被抹去。 So it cannot give you the 100% solution. 所以它不能给你100%的解决方案。

The second parameter is called learning rate. 第二个参数称为学习率。 In the code you mentioned bg(frame,fmask,-1); 在你提到的代码中bg(frame,fmask,-1); where -1 is your learning rate. 其中-1是你的学习率。 you can set it to 0.0 to 1.0 and default is -1. 您可以将其设置为0.0到1.0,默认值为-1。 When you set it 0, you will get what you want for the objects which are not part of the frame in the starting of the video. 当您将其设置为0时,您将获得所需的对象,这些对象不是视频开头的帧的一部分。 You can call this kind of object "foreign objects". 你可以将这种对象称为“异物”。 You will get foreign object covered with white pixels. 您将获得覆盖有白色像素的异物。

Explore your testing from the information I have mentioned above and share your experience. 从我上面提到的信息中探索您的测试并分享您的经验。

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

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