标签:
#include <stdio.h> #include <iostream> #include <fstream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/objdetect/objdetect.hpp> #include <opencv2/ml/ml.hpp> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main(int argc, char** argv) { std::string videoFile = "3.AVI"; int c,pause=0; cv::VideoCapture capture; capture.open(videoFile); //CvVideoWriter* writer=cvCreateVideoWriter("3outGMM.avi",CV_FOURCC(‘x‘,‘v‘,‘i‘,‘d‘),25,cvSize(240,320)); if (!capture.isOpened()) { std::cout<<"read video failure"<<std::endl; return -1; } cv::BackgroundSubtractorMOG2 mog; cv::BackgroundSubtractorMOG2 mog1; cv::Mat foreground; cv::Mat background; cv::Mat foreground1; cv::Mat background1; cv::Mat frame; long frameNo = 0; double t=(double)cvGetTickCount(); while (capture.read(frame)) { ++frameNo; std::cout<<frameNo<<std::endl; if(frameNo==100||frameNo==680||frameNo==750) cout<<"pause"<<endl; // 运动前景检测,并更新背景 mog(frame, foreground, 0.001); mog1(frame,foreground1,0.01); cv::imshow("slowundoing",foreground); cv::imshow("fastundoing",foreground1); // 腐蚀 cv::erode(foreground, foreground, cv::Mat()); // 膨胀 cv::dilate(foreground, foreground, cv::Mat()); cv::erode(foreground1, foreground1, cv::Mat()); // 膨胀 cv::dilate(foreground1, foreground1, cv::Mat()); mog.getBackgroundImage(background); // 返回当前背景图像 mog1.getBackgroundImage(background1); //cv::imshow("slowvideo", foreground); //cv::imshow("slowbackground", background); //cv::imshow("fastvideo", foreground1); //cv::imshow("fastbackground", background1); cv::imshow("srcvideo",frame); if (cv::waitKey(25) > 0) { break; } //cvWriteFrame(writer,background); } //cvReleaseVideoWriter(&writer); t=((double)cvGetTickCount()-t)/(cvGetTickFrequency()*1000); cout<<t<<"ms"<<endl; system("pause"); return 0; }
标签:
原文地址:http://www.cnblogs.com/royjwy/p/4453700.html