c++ - 使用opencv将视频写入文件时遇到问题

标签 c++ visual-studio opencv

我正在尝试在 visualstudio 2013 中使用 C++ 中的 OpenCV 将捕获的视频写入文件。程序似乎从我笔记本电脑上的网络摄像头捕获视频,并且还能够将每个帧保存为图像,但是当我将帧写入一个视频文件,我最终得到一个 6kb 的文件。程序没有给我任何错误,因为我已按照 OpenCV 文档中的说明进行操作。

我正在粘贴程序以供审核。请建议我如何才能使它成为一个成功的程序。

谢谢。

#include <stdio.h>
#include <iostream>
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"

using namespace std;
using namespace cv;

int main()
{
    VideoCapture video_capture(0);

    if (!video_capture.isOpened())
    {
        cout << "Error in opening video feed!" << endl;
        getchar();
        return -1;
    }

    // Creating the window to view video feed
    String window_name = "Video_Feed";
    namedWindow(window_name, CV_WINDOW_AUTOSIZE);

    //
    Mat frame;

    // Filename
    String filename = "...\\first_recording.avi";

    // four character code
    int fcc = CV_FOURCC('M', 'P', '4', '2');

    // frames per sec
    int fps = 10;

    // frame size
    Size frame_size(CV_CAP_PROP_FRAME_WIDTH, CV_CAP_PROP_FRAME_HEIGHT);

    VideoWriter video_writer = VideoWriter(filename,fcc,fps,frame_size,true);

    if (!video_writer.isOpened())// || video_writer.isOpened == NULL)
    {
        cout << "Error in opening video writer feed!" << endl;
        getchar();
        return -1;
    }

    int frame_count = 0;

    while (frame_count < 100)
    {
        bool cap_success = video_capture.read(frame);

        if (!cap_success)
        {
            cout << "Error in capturing the image from the camera feed!" << endl;
            getchar();
            break;
        }

        imshow(window_name, frame);
        //imwrite("cap.jpg", frame);
        video_writer.write(frame);

        switch (waitKey(10))
        {
        case 27:
            return 0;
            break;
        }
        frame_count++;
    }

    //scvReleaseVideoWriter;
    destroyWindow(window_name);
    return 0;

}

最佳答案

请找到下面的一段代码来编写视频文件。

 int main(int argc, char* argv[])
    {
        // Load input video
        cv::VideoCapture input_cap("test8.avi");
        if (!input_cap.isOpened())
        {
            std::cout << "!!! Input video could not be opened" << std::endl;
            return -1;
        }

    // Setup output video
    cv::VideoWriter output_cap("output.avi",  
                               input_cap.get(CV_CAP_PROP_FOURCC),
                               input_cap.get(CV_CAP_PROP_FPS), 
                               cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH), input_cap.get(CV_CAP_PROP_FRAME_HEIGHT)));

    if (!output_cap.isOpened())
    {
        std::cout << "!!! Output video could not be opened" << std::endl;
        return -1;
    }

    // Loop to read frames from the input capture and write it to the output capture
    cv::Mat frame;
    while (true)
    {       
        if (!input_cap.read(frame))             
            break;

        output_cap.write(frame);
    }

    // Release capture interfaces   
    input_cap.release();
    output_cap.release();

    return 0;
}

关于c++ - 使用opencv将视频写入文件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446710/

相关文章:

c++ - 新字符数组不会初始化为全零

visual-studio - TFS 中的代码审查工作流程 + 功能分支

c++ - 在 visual studio 项目中为 opencv 设置包含文件依赖项

python - 如何使用 python 在 OpenCv 中查找轮廓的颜色

c++ - 画线/删除部分(Qt/C++)

c++ - 使用鼠标滚轮时如何更新光标?

c++ - 调整大小而不是滚动的 QListWidget

c# - 在右键单击菜单上打开属性页时 Visual Studio 2015 挂起

c++ - 即使为其相关的 dll 正确生成了 lib 文件,CMake 生成的 MSVC 项目也找不到符号

python - 2019年Kinect-Python-OpenCV的状态