簡體   English   中英

使用OpenCV提取視頻文件開頭僅幾秒鍾

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM