OPENCV 检测图像内的动态

/**
* @file bg_sub.cpp
* @brief Background subtraction tutorial sample code
* @author Domenico D. Bloisi
*/

#include <iostream>
#include <sstream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>

using namespace cv;
using namespace std;

const char* params
= "{ help h         |                   | Print usage }"
"{ input          | ../data/vtest.avi | Path to a video or a sequence of image }"
"{ algo           | MOG2              | Background subtraction method (KNN, MOG2) }";

int main(int argc, char* argv[])
{
	CommandLineParser parser(argc, argv, params);
	parser.about("This program shows how to use background subtraction methods provided by "
		" OpenCV. You can process both videos and images.\n");
	if (parser.has("help"))
	{
		//print help information
		parser.printMessage();
	}

	//! [create]
	//create Background Subtractor objects
	Ptr<BackgroundSubtractor> pBackSub;
	if (parser.get<String>("algo") == "MOG2")
		pBackSub = createBackgroundSubtractorMOG2();
	else
		pBackSub = createBackgroundSubtractorKNN();
	//! [create]

	//! [capture]
//	VideoCapture capture(parser.get<String>("input"));
//	VideoCapture capture(0);
	VideoCapture capture("F://opencv//VS_demo//opencvdemo//data//vtest.avi");
	if (!capture.isOpened()) {
		//error in opening the video input
		cerr << "Unable to open: " << parser.get<String>("input") << endl;
		return 0;
	}
	//! [capture]

	Mat frame, fgMask;
	while (true) {
		capture >> frame;
		if (frame.empty())
			break;

		//! [apply]
		//update the background model
		pBackSub->apply(frame, fgMask);
		//! [apply]

		//! [display_frame_number]
		//get the frame number and write it on the current frame
		rectangle(frame, cv::Point(10, 2), cv::Point(100, 20),
			cv::Scalar(255, 255, 255), -1);
		stringstream ss;
		ss << capture.get(CAP_PROP_POS_FRAMES);
		string frameNumberString = ss.str();
		putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
			FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
		//! [display_frame_number]

		//! [show]
		//show the current frame and the fg masks
		imshow("Frame", frame);
		imshow("FG Mask", fgMask);
		//! [show]

		//get the input from the keyboard
		int keyboard = waitKey(30);
		if (keyboard == 'q' || keyboard == 27)
			break;
	}

	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值