<pre name="code" class="cpp"><pre name="code" class="cpp">// FrontSight.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <video/video.hpp>
#include <iostream>
#ifdef _DEBUG
#pragma comment(lib, "opencv_core231d.lib")
#pragma comment(lib, "opencv_highgui231d.lib")
#pragma comment(lib, "opencv_imgproc231d.lib")
#pragma comment(lib, "opencv_calib3d231d.lib")
#pragma comment(lib, "opencv_features2d231d.lib")
#pragma comment(lib, "opencv_contrib231d.lib")
#pragma comment(lib, "opencv_ml231d.lib")
#pragma comment(lib, "opencv_video231d.lib")
#else
#pragma comment(lib, "opencv_core231.lib")
#pragma comment(lib, "opencv_highgui231.lib")
#pragma comment(lib, "opencv_imgproc231.lib")
#pragma comment(lib, "opencv_calib3d231.lib")
#pragma comment(lib, "opencv_features2d231.lib")
#pragma comment(lib, "opencv_contrib231.lib")
#pragma comment(lib, "opencv_ml231.lib")
#pragma comment(lib, "opencv_video231.lib")
#endif
using namespace cv;
using namespace std;
int main()
{
Mat frame;
Mat fground;
Mat bkground;
VideoCapture capture;
capture.open(0);
if (!capture.isOpened())
{
return 0;
}
namedWindow("Fground");
namedWindow("SrcImg");
namedWindow("Bkground");
BackgroundSubtractorMOG2 mog;//BackgroundSubtractorMOG2 BackgroundSubtractorMOG无法获取背景
while (1)
{
if (!capture.read(frame))
{
break;
}
mog(frame, fground,0.05);
//腐蚀膨胀
//erode(fground,fground,cv::Mat());
//dilate(fground,fground,cv::Mat());
threshold(fground, fground, 128, 255, THRESH_BINARY_INV);
//获取背景
mog.getBackgroundImage(bkground);
imshow("Fground", fground);
imshow("SrcImg", frame);
imshow("Bkground",bkground);
if (waitKey(10) == 27)
{
break;
}
}
}