Opencv 简单视频播放器

最近看了一下[1]_2011_OpenCV 2 Computer Vision Application Programming Cookbook.pdf,写了一个利用Opencv库实现的简单视频播放器。源码如下所示,英文注释大家应该可以看懂的。O(∩_∩)O~

// C++ header and namespace
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

// Opencv header and namespace
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/video.hpp>
using namespace cv;

bool JumpToFrame(false);

int main(int argc, char* argv[])
{	
	//!< Check out Input video
	if (argc != 2)
	{
		cerr << "Usage: VideoPlayer.exe VideoFilename." << endl;
		exit(1);
	}

	//!< Check out Open Video
	VideoCapture capture(argv[1]);
	if (!capture.isOpened())
	{
		return 1;
	}

#pragma region InfoOfVideo
	
	long    NumberOfFrame = static_cast<long>(capture.get(CV_CAP_PROP_FRAME_COUNT));
	double  HeightOfFrame = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
	double  WidthOfFrame  = capture.get(CV_CAP_PROP_FRAME_WIDTH);
	double  FpsOfVideo    = capture.get(CV_CAP_PROP_FPS);	
	
	cout << "The name of the input video is " << argv[1] << "." << endl;
	cout << "NumberOfFrame : " << NumberOfFrame << endl;
	cout << "HeightOfFrame : " << HeightOfFrame << endl;
	cout << "WidthOfFrame  : " << WidthOfFrame << endl;
	cout << "FpsOfVieo     : " << FpsOfVideo << endl;

#pragma endregion

	// !< JumpToFrame function
	while (JumpToFrame)
	{
		double Position = 0.0;
		cout << "Please input the number of frame which you want jump to!" << endl;
		cin >> Position;
		capture.set(CV_CAP_PROP_POS_FRAMES, Position);
	}

	// !< Delay between each frame in ms corresponds to video frame rate(fps)
	Mat frame;
	bool stop(false);
	int delay = 1000 / FpsOfVideo;
	namedWindow("Extracted Frame");

	while (!stop)
	{
		//read next frame if any
		if (!capture.read(frame))
		{
			break;
		}
		imshow("Extracted Frame", frame);
		//introduce a delay or press key to stop
		if (waitKey(delay) >= 0)
		{
			stop = true;
		}
	}

	// !< Close the video file.
	// Not required since called by destructor
	capture.release();

	return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Digital2Slave

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值