BackgroundSubtractorMOG2在opencv2和opencv3的使用区别

在opencv2.x版本的使用:

#include"opencv2/opencv.hpp"
#include"opencv2/core/core.hpp"
#include"opencv2/highgui/highgui.hpp"
#include"opencv2/imgproc/imgproc.hpp"
#include"opencv2/video/background_segm.hpp"
#include
using namespace cv;
using namespace std;

int main()
{
	VideoCapture capture(0);
	BackgroundSubtractorMOG2 bg_model;
	Mat image, fgimage, fgmask;

	while (1)
	{
		capture >> image;
		if (!image.data)
		{
			cerr << "picture error!";
			return -1;
		}
		if (fgimage.empty())
			fgimage.create(image.size(), image.type());
		bg_model(image, fgmask, -1);
		fgimage = Scalar::all(0);
		image.copyTo(fgimage, fgmask);
		Mat bgimage;
		bg_model.getBackgroundImage(bgimage);

		imshow("image", image);
		imshow("fgimage", fgimage);
		imshow("fgmask", fgmask);
		if (!bgimage.empty())
			imshow("bgimage", bgimage);

		waitKey(30);
	}
	return 0;

在opencv3.x版本的使用:

#include"opencv2/opencv.hpp"
#include"opencv2/core/core.hpp"
#include"opencv2/highgui/highgui.hpp"
#include"opencv2/imgproc/imgproc.hpp"
#include"opencv2/video/background_segm.hpp"
#include
using namespace cv;
using namespace std;

int main()
{
	VideoCapture capture(0);
	Ptr bg_model=createBackgroundSubtractorMOG2();
	Mat image, fgimage, fgmask;

	while (1)
	{
		capture >> image;
		if (!image.data)
		{
			cerr << "picture error!";
			return -1;
		}
		if (fgimage.empty())
			fgimage.create(image.size(), image.type());
		bg_model->apply(image, fgmask, -1);
		fgimage = Scalar::all(0);
		image.copyTo(fgimage, fgmask);
		Mat bgimage;
		bg_model->getBackgroundImage(bgimage);

		imshow("image", image);
		imshow("fgimage", fgimage);
		imshow("fgmask", fgmask);
		if (!bgimage.empty())
			imshow("bgimage", bgimage);

		waitKey(30);
	}
	return 0;
}

注意:OpenCV2.x时有BackgroundSubtractorMOG2和BackgroundSubtractorMOG这两种类,但到了OpenCV3.x时只有BackgroundSubtractorMOG2。

你可能感兴趣的:(opencv)