下面是对使用OpenCV工具用C++播放视频的C++代码的解释。
须知:
(1) 代码只能在Linux环境下编译。
(2) 在windows下运行,请使用文件:’play_video.o’并在cmd中运行。但是,如果它不运行(系统架构中的问题),则编译它
通过对代码进行适当且明显的更改,例如:使用
(3)编译命令:g++ -w vid.cpp -o vid `pkg-config –libs opencv`
(4) 运行命令:./vid
(5) 请确保视频:“Bumpy.mp4”在同一位置。
在运行代码之前,请确保您的系统上安装了 OpenCV。
代码片段:
#include "opencv2/highgui/highgui.hpp"
// highgui - an interface to video and image capturing.
#include
// The header files for performing input and output.
using namespace cv;
// Namespace where all the C++ OpenCV functionality resides.
using namespace std;
// For input output operations.
int main()
{
VideoCapture cap("Bumpy.mp4");
// cap is the object of class video capture that tries to capture Bumpy.mp4
if ( !cap.isOpened() ) // isOpened() returns true if capturing has been initialized.
{
cout << "Cannot open the video file. \n";
return -1;
}
double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
// The function get is used to derive a property from the element.
// Example:
// CV_CAP_PROP_POS_MSEC : Current Video capture timestamp.
// CV_CAP_PROP_POS_FRAMES : Index of the next frame.
namedWindow("A_good_name",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
// first argument: name of the window.
// second argument: flag- types:
// WINDOW_NORMAL : The user can resize the window.
// WINDOW_AUTOSIZE : The window size is automatically adjusted to fitvthe displayed image() ), and you cannot change the window size manually.
// WINDOW_OPENGL : The window will be created with OpenGL support.
while(1)
{
Mat frame;
// Mat object is a basic image container. frame is an object of Mat.
if (!cap.read(frame)) // if not success, break loop
// read() decodes and captures the next frame.
{
cout<<"\n Cannot read the video file. \n";
break;
}
imshow("A_good_name", frame);
// first argument: name of the window.
// second argument: image to be shown(Mat object).
if(waitKey(30) == 27) // Wait for 'esc' key press to exit
{
break;
}
}
return 0;
}
// END OF PROGRAM
关于作者:
Aditya Prakash 是印度学院的本科生信息技术,瓦多达拉。他主要用 C++ 编写代码。他的座右铭是:到目前为止一切顺利。他打板球,看超级英雄电影,踢足球,是回答问题的忠实粉丝。
如果您还想在这里展示您的博客,请参阅 GBlog,了解 GeeksforGeeks 上的客座博客写作。