Taily老段的微信公众号,欢迎交流学习
https://blog.csdn.net/taily_duan/article/details/81214815
// OpenCV3Contrib.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv2/opencv.hpp> //头文件
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>
using namespace cv; //包含cv命名空间
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Mat Frame;
Mat FrameMat;
Mat FrMat;
Mat BkMat;
// std::string video = argv[1];
VideoCapture cap("Duan2.mp4");
//VideoCapture cap("Move.mpg");
// get bounding box
cap >> Frame;
int totalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT);
namedWindow("Video", 1);
namedWindow("Threshold", 1);
namedWindow("Background", 1);
namedWindow("Foreground", 1);
// perform the tracking process
printf("Start the tracking process, press ESC to quit.\n");
for (int nFrmNum = 1; nFrmNum < totalFrameNumber; nFrmNum++) {
cout << nFrmNum << endl;
// get frame from the video
cap >> Frame;
if (nFrmNum == 1){
cvtColor(Frame, BkMat, CV_BGR2GRAY);
cvtColor(Frame, FrMat, CV_BGR2GRAY);
}
else{
cvtColor(Frame, FrameMat, CV_BGR2GRAY);
absdiff(FrameMat, BkMat, FrMat);
threshold(FrMat, FrameMat, 30, 255, CV_THRESH_BINARY);
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
erode(FrameMat, FrameMat, element);
dilate(FrameMat, FrameMat, element);
imshow("Video", FrameMat);
imshow("Threshold", Frame);
imshow("Background", BkMat);
imshow("Foreground", FrMat);
}
if (waitKey(1) == 27)break;
}
waitKey(0);
return 0;
}