【OpenCV】动态目标检测(背景/场景)

来源:互联网 发布:淘宝助理图片空间上传 编辑:程序博客网 时间:2024/06/07 05:37


// 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 boxcap >> 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 processprintf("Start the tracking process, press ESC to quit.\n");for (int nFrmNum = 1; nFrmNum < totalFrameNumber; nFrmNum++) {cout << nFrmNum << endl;// get frame from the videocap >> 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;}



0 0
原创粉丝点击