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


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;
}


 

 

 

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值