📜  OpenCV C++程序播放视频

📅  最后修改于: 2021-05-30 12:02:52             🧑  作者: Mango

以下是使用工具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上撰写来宾博客。