📜  用于模糊视频的 OpenCV C++ 程序

📅  最后修改于: 2021-10-21 05:00:59             🧑  作者: Mango

以下是使用OpenCV工具在C++中对视频进行模糊处理的C++代码说明。

须知:

(1) 代码只能在Linux环境下编译。

(2) 在windows下运行,请使用文件:’blur_video.o’并在cmd中运行。但是,如果它不运行(系统架构中的问题),则通过对代码进行适当且明显的更改来在 Windows 中编译它,例如:使用 代替

(3)编译命令:g++ w blur_vid.cpp o blur_vid `pkgconfig libs opencv`

(4) 运行命令:./blur_vid

(5) 视频 Bumpy.mp4 必须与代码在同一目录中。

在运行代码之前,请确保您的 // 系统上安装了 OpenCV。

代码片段解释:

#include "opencv2/highgui/highgui.hpp"
// highgui - an interface to video and image capturing.
#include  // For dealing with images
#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 
    //fit the 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;
        }
    blur(frame,frame,Size(10,10)); // To blur the image.
    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 上的客座博客写作。