c++ - 无法使用 C++ 和 OpenCV 从视频中提取帧

标签 c++ opencv

我想从一个简短的 .mp4 视频中提取所有帧并将它们作为图像保存到文件夹中。但是当我调用函数 cv::VideoCapture::read() 时,它无法提取帧。我的代码有什么问题?

#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio.hpp>

int main(int argc, const char * argv[]) {
     cv::VideoCapture cap("test.mp4");

     if (!cap.isOpened()) {
         std::cout << "Cannot open the video file.\n";
         return -1;
     }

     for (int frame_count = 0; frame_count < cap.get(cv::CAP_PROP_FRAME_COUNT); frame_count++) {
          cap.set(cv::CAP_PROP_POS_FRAMES, frame_count);
          cv::Mat frame;

          if (!cap.read(frame)) {
              std::cout << "Failed to extract a frame.\n";
              return -1;
          }

          std::string image_path = "frames/" + std::to_string(frame_count) + ".jpg"; 
          cv::imwrite(image_path, frame);
    }

    return 0;
}

我的输出始终是“无法提取帧。”。

最佳答案

上教程啦provided above ,有一条指令:

Uncheck WITH_FFMPEG

ffmpeg 库包含 mp4 编解码器,您需要检查 WITH_FFMPEGUSE_FFMPEG 才能读取 .mp4 和许多其他格式。

关于c++ - 无法使用 C++ 和 OpenCV 从视频中提取帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48147178/

相关文章:

python-2.7 - 使用opencv检测python中的圆形黑色 Blob

android - 改进 Tesseract 的阈值结果

c++ - 使用cin时是否需要将一个变量存储两次?

c++ - lambda 中的递归调用 (C++11)

c++ - Qt - 滚动图表的可见区域

c - 树莓派和OpenCV的C语言库

python - 如何在 Mac OS 的 anaconda 中安装 caffe?

python - 如何将训练后的 SVM 输出保存到文件中? (OpenCV/SimpleCV)

c++ - 当我从赋值运算符按值返回时,首先调用复制构造函数的机制和依据是什么?

c++ - 在 Windows 上使用 MinGW 进行通配