简体   繁体   English

无法使用带有 OpenCV 的 C++ 从视频中提取帧

[英]Can't extract frames from video using C++ with OpenCV

I want to extract all frames from a short .mp4 video and save them as images to folder.我想从一个简短的 .mp4 视频中提取所有帧并将它们作为图像保存到文件夹中。 But when I call a function cv::VideoCapture::read() it fails to extract a frame.但是当我调用函数 cv::VideoCapture::read() 时,它无法提取帧。 What is the problem with my code?我的代码有什么问题?

#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;
}

My output is always "Failed to extract a frame.".我的输出总是“无法提取帧。”。

On the tutorial you provided above , there is an instruction:在您上面提供的教程中,有一条说明:

Uncheck WITH_FFMPEG取消选中 WITH_FFMPEG

ffmpeg library contains mp4 codecs, you need to check WITH_FFMPEG and USE_FFMPEG in order to read .mp4 and many other formats. ffmpeg 库包含 mp4 编解码器,您需要检查WITH_FFMPEGUSE_FFMPEG以读取 .mp4 和许多其他格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 ip 相机 (rtsp) 与 C++ dll 使用 ZCFAF4DE8D6AAA9548C17936.AFE 抓取帧。 - Can't grab frames from an ip camera (rtsp) with a C++ dll using OpenCv 3.4.5 如何使用 OpenCV 而不是 C++ 将视频分成帧 - How to break a video into frames using OpenCV over C++ 使用 GStreamer OpenCV Videocapture 时的灰色视频帧 C++ - Grey Video frames when using OpenCV Videocapture with GStreamer C++ 如何从视频中选择两个帧? OpenCV的C + + - How to select two frames from a video? opencv c++ 调试一种使用C ++中的OpenCV从视频中查找帧的移动平均值以进行运动检测的方法 - debugging a method for finding running average of frames from video for motion detection using OpenCV in C++ 如何使用c ++提取视频帧并将其另存为图像 - how to Extract Video Frames and Save them as Images using c++ 无论如何,我可以使用数组在opencv c ++中存储所有视频帧(无论视频多长时间)吗? - Is there anyway I can use array to store all video frames (no matter how long the video is) in opencv c++? OpenCV / C ++-从视频文件读取的帧在视频结束后重新启动 - OpenCV/C++ - frames read from video file restart after end of the video 使用opencv / c ++对一系列帧求和 - sum a sequence of frames using opencv/c++ OpenCV(C ++)无法获得XVID视频文件帧速率 - OpenCV (C++) Can't get XVID video file framerate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM