[英]Frame Extraction Only few seconds in the beginning of video file using OpenCV
在回答上一个问题之后 ,我想从视频中提取帧。 但是视频开始时只有2秒。 我想尽可能多地使用原始视频,这就是为什么我不想剪切原始视频然后对其进行处理。
这是我提取框架的代码。 但这将从视频中提取所有帧:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int c = 0;
string int2str(int &);
int main(int argc, char **argv) {
string s;
VideoCapture cap("test_video.mp4");
if (!cap.isOpened())
{
cout << "Cannot open the video file" << endl;
return -1;
}
double fps = cap.get(CV_CAP_PROP_FPS);
cout << "Frame per seconds : " << fps << endl;
namedWindow("MyVideo", CV_WINDOW_NORMAL);
resizeWindow("MyVideo", 600, 600);
while (1)
{
Mat frame;
Mat Gray_frame;
bool bSuccess = cap.read(frame);
if (!bSuccess)
{
cout << "Cannot read the frame from video file" << endl;
break;
}
s = int2str(c);
//cout<<("%d\n",frame )<<endl;
c++;
imshow("MyVideo", frame);
imwrite("frame" + s + ".jpg", frame);
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
string int2str(int &i) {
string s;
stringstream ss(s);
ss << i;
return ss.str();
}
和建议? 谢谢。
好像您已经知道该视频的FPS。 难道不只是计算达到FPS * 2后提取并破坏多少帧的问题?
double fps = cap.get(CV_CAP_PROP_FPS);
int framesToExtract = fps * 2;
for (int frames = 0; frames < framesToExtract; ++frames)
{
... // your processing here
}
另外,我看了您以前的问题,似乎您对FPS有误解?
FPS =每秒帧数,这表示视频每秒有多少帧。 假设您的视频为120 FPS。 这意味着在一秒钟的视频中有120帧,两秒钟之内有240帧,依此类推。
因此(VIDEO_FPS * x)可以让您在x秒的视频帧数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.