如何使用BackgroundSubtractorMOG2的图像 [英] How to use BackgroundSubtractorMOG2 for images
问题描述
我是OpenCV的新手,我现在卡住了。我正在处理图像,而不是视频。因为我在项目中有相同的背景,所以如果我可以删除我的背景,我认为工作会更容易。但首先,我要问一件事。我可以将BackgroundSubtractorMOG2用于图像吗?因为它是在视频分析/运动分析标题下。
I am pretty new to OpenCV and I am stuck at the moment. I am dealing with images, not a video. Since I will have same background in my project, I thought it would be easier to work, if I could remove my background. But first, I have to ask one thing. Can I use BackgroundSubtractorMOG2 for images? Because it is under video analysis/motion analysis title.
我阅读了opencv.org上的文档并浏览了无数的例子/教程,但我仍然难以理解MOG2工作。
I read the documentation on opencv.org and looked through countless examples/tutorials but I am still having difficulty understanding how MOG2 works.
快速提问:参数中的历史记录是什么?
Quick question: What is history that in parameters?
所以,我写了一个简单的代码。我得到了一个前景蒙版。那么,下一步是什么?如何删除背景并仅留下我的对象?我不应该首先加载我的背景,然后加载实际图像,以便MOG2可以进行背景减法吗?
So, I have written a simple code. I get a foreground mask. So, what is the next step? How can I remove the background and left with my object only? Shouldn't I load my background first, then the actual image, so that MOG2 could do the background subtraction?
我正在使用OpenCV 2.4.11。
I am using OpenCV 2.4.11.
代码:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
using namespace cv;
using namespace std;
//global variables
int history = 1;
float varThreshold = 16;
bool bShadowDetection = true;
Mat src; //source image
Mat fgMaskMOG2; //fg mask generated by MOG2 method
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
int main(int argc, char* argv[])
{
//create GUI windows
namedWindow("Source");
namedWindow("FG Mask MOG 2");
src = imread("bluePaper1.png", 1);
//create Background Subtractor objects
pMOG2 = new BackgroundSubtractorMOG2(history, varThreshold, bShadowDetection); //MOG2 approach
pMOG2->setInt("nmixtures", 3);
pMOG2->setDouble("fTau", 0.5);
pMOG2->operator()(src, fgMaskMOG2);
imshow("Source", src);
imshow("FG Mask MOG 2", fgMaskMOG2);
waitKey(0);
return 0;
}
源图片:
Source image:
我从MOG2得到的fgMask:
fgMask that I get from MOG2:
推荐答案
高斯混合方法根据固定相机中的帧历史来学习背景,因此您不能仅将其用于一个图像。历史参数显示有多少帧会对背景构造产生影响。
Mixture of Gaussian method learns background according to history of frames in a fixed camera and so you can not use it for only one image. The history parameter shows how many frames would have effect on construction of the background.
阴影检测不是一个依赖于BGS方法的过程,应该同时实现。例如,我们在MOG2文档中有
:
Shadow detection is not a process which depends on BGS method and should be implemented alongside. for example in MOG2 documentation we have:
如果像素是背景的较暗版本,则会检测到阴影。 Tau是定义阴影可以暗多少的阈值。 Tau = 0.5意味着如果一个像素深两倍以上则不是阴影
The shadow is detected if the pixel is a darker version of the background. Tau is a threshold defining how much darker the shadow can be. Tau= 0.5 means that if a pixel is more than twice darker then it is not shadow
如果是你的例子,前景很容易通过简单的帧差异获得,您可以通过上述解决方案轻松删除阴影。
In case of your example the foreground could easily be obtained by a simple frame difference and you can easily remove shadows by the mentioned solution.
您可以通过以下步骤获得前景:
you can have the foreground by the following steps:
- 减去来自已知背景的图像和阈值结果以获取前景蒙版
- 对前景蒙版和给定图像应用AND操作以使对象具有可能的阴影。
- 移除比其对应的更暗的像素(应调整的数量)背景中的像素。
- 进行一些后处理,如形态学和连通分量标记,以获得更好的结果。
- Subtract given image from known background and threshold the result to obtain the foreground mask
- Apply AND operation on foreground mask and the given image to get your object with possible shadows.
- Remove pixels which is darker (amount of it should be adjust) than their corresponding pixel in background.
- Do some post processing like morphological and connected-component-labeling to have a better result.
这篇关于如何使用BackgroundSubtractorMOG2的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!