Opencv混合高斯模型前景分离
#include "stdio.h" #include "string.h" #include "iostream" #include "opencv/cv.h" #include "opencv/cxcore.h" #include "opencv/cvaux.h" #include "opencv/highgui.h" #include "opencv/ml.h" #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/video/video.hpp" #include "opencv2/videostab/videostab.hpp" #include "opencv2/stitching/stitcher.hpp" #include "opencv2/contrib/contrib.hpp" #include "opencv2/objdetect/objdetect.hpp" #pragma comment(lib,"opencv_calib3d2410d.lib") #pragma comment(lib,"opencv_contrib2410d.lib") #pragma comment(lib,"opencv_core2410d.lib") #pragma comment(lib,"opencv_features2d2410d.lib") #pragma comment(lib,"opencv_highgui2410d.lib") #pragma comment(lib,"opencv_imgproc2410d.lib") #pragma comment(lib,"opencv_objdetect2410d.lib") #pragma comment(lib,"opencv_video2410d.lib") #pragma comment(lib,"opencv_flann2410d.lib") #pragma comment(lib,"opencv_gpu2410d.lib") #pragma comment(lib,"opencv_legacy2410d.lib") #pragma comment(lib,"opencv_ml2410d.lib") #pragma comment(lib,"opencv_nonfree2410d.lib") #pragma comment(lib,"opencv_ocl2410d.lib") #pragma comment(lib,"opencv_photo2410d.lib") #pragma comment(lib,"opencv_stitching2410d.lib") #pragma comment(lib,"opencv_superres2410d.lib") #pragma comment(lib,"opencv_ts2410d.lib") #pragma comment(lib,"opencv_stitching2410d.lib") using namespace cv; using namespace std; int main() { VideoCapture capture(0); if (!capture.isOpened()) { cout << "读取视频失败" << endl; return -1; } Mat frame; Mat foreground; //使用默认参数调用混合高斯模型 BackgroundSubtractorMOG mog; bool stop(false); Sleep(3000); while (!stop) { if (!capture.read(frame)) { cout << "从视频中读取图像失败或者读完整个视频" << endl; return -2; } cv::flip(frame,frame,1); cvtColor(frame, frame, CV_RGB2GRAY); imshow("输入视频", frame); //参数为,输入图像、输出图像、学习速率 mog(frame, foreground, 0.1); imshow("前景", foreground); //按ESC键退出,按其他键会停止在当前帧 if (waitKey(33) > 0) { destroyAllWindows(); break; } } waitKey(0); return 0; }