如何使用OpenCV和Eclipse从视频剪辑中获取单个帧

how to get a single frame from a video clip using opencv and eclipse

本文关键字:获取 单个帧 视频 何使用 OpenCV Eclipse      更新时间:2023-10-16
#include <iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
using namespace std;
using namespace cv;
int main(int argc, const char* argv[]) {
VideoCapture cap(0);
if(!cap.isOpened())
        {
            cout<<"can't open video file"<<endl;
            return -1;
        }
namedWindow("Myvideo",CV_WINDOW_AUTOSIZE);
while(1)
{
    Mat frame;
    bool bsuccess=cap.read(frame);
    if(!bsuccess)
    {
        cout<<"can't read a frame"<<endl;
        break;
    }
    imshow("Myvideo",frame);
    if(waitKey(30)==27)
    {
        cout<<"Esc key is pressed by the user"<<endl;
        break;
    }
}
return 0;
}

上面的代码只是为了从相机捕获视频。但我想从这个视频中获取图像(即只有一帧(。有人可以告诉我如何做到这一点。我实际上试图删除 while 循环,这样我就可以只得到一个循环,而不是一个接一个地获取(即视频(。并且还删除了"中断"语句。

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>`
using namespace std;
using namespace cv;
int main(int argc, const char* argv[]) {
VideoCapture cap(0);
if(!cap.isOpened())
        {
            cout<<"can't open video file"<<endl;
            return -1;
        }
namedWindow("Myvideo",CV_WINDOW_AUTOSIZE);
    Mat frame;
    cap.read(frame);
    imshow("Myvideo",frame);
    if(waitKey(30)==27)
    {
        cout<<"Esc key is pressed by the user"<<endl;
    }
return 0;
}

但不幸的是,它只是显示一个空白窗口。谁能帮我...提前致谢

第一个读取的帧通常(总是?(黑色/灰色,尝试显示下一帧 - 替换此代码

cap.read(frame);
imshow("Myvideo",frame);

有了这个:

cap.read(frame);
cap.read(frame);
imshow("Myvideo",frame);