如何使用opencv和eclipse从视频剪辑中获取单帧

时间:2013-08-30 15:29:23

标签: c++ eclipse opencv video

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

但不幸的是,它只是显示一个空白窗口。 任何人都可以帮助我......提前致谢

1 个答案:

答案 0 :(得分:0)

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

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

用这个:

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