0
#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循环,这样我就可以只得到一个循环而不是一个接一个(即视频)。并且还删除了“break”语句。

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

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

4

1 回答 1

0

First readed frame is usually(always?) black/gray, try to display next frame - replace this code

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

with this:

cap.read(frame);
cap.read(frame);
imshow("Myvideo",frame);
于 2013-08-30T17:16:43.023 回答