无法将C ++与OpenCV一起从视频中提取帧 [英] Can't extract frames from video using C++ with OpenCV
本文介绍了无法将C ++与OpenCV一起从视频中提取帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从一个短的.mp4视频中提取所有帧,并将它们另存为图像到文件夹中.但是,当我调用函数cv :: VideoCapture :: read()时,它无法提取帧.我的代码有什么问题?
I want to extract all frames from a short .mp4 video and save them as images to folder. But when I call a function cv::VideoCapture::read() it fails to extract a frame. 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:
取消选中WITH_FFMPEG
Uncheck WITH_FFMPEG
ffmpeg库包含mp4编解码器,您需要检查WITH_FFMPEG
和USE_FFMPEG
才能读取.mp4和许多其他格式.
ffmpeg library contains mp4 codecs, you need to check WITH_FFMPEG
and USE_FFMPEG
in order to read .mp4 and many other formats.
这篇关于无法将C ++与OpenCV一起从视频中提取帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文