简体   繁体   中英

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

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

the above code is just to capture a video from camera. But I want to get a image(ie only one frame) from this video. Can someone please tell me how to do this. I actually tried to remove the while loop, so that I can get just one loop instead of getting one after the other(ie video). And also removed the "break" statement.

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

but unfortunately, it is just showing a blank window. Can anybody help me out... Thanks in advance

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM