如何从AVI视频中提取帧

时间:2013-01-18 17:03:44

标签: c++ opencv

到目前为止嘿偷看我管理OpenCV播放video.avi但我现在该怎么办才能提取帧??

下面是我到目前为止编写的代码,我的视频正在播放:

#include<opencv\cv.h>
#include<opencv\highgui.h>
#include<opencv\ml.h>
#include<opencv\cxcore.h>



int main( int argc, char** argv ) {
cvNamedWindow( "DisplayVideo", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateFileCapture( argv[1] );
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "DisplayVideo", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow("DisplayVideo" );
}

1 个答案:

答案 0 :(得分:0)

frame 您正在提取的帧。如果你想将它转换为cv :: Mat,你可以通过创建一个带有IplImage的垫来做到这一点:

Mat myImage(IplImage);

There is a nice tutorial on it here

然而,你是用旧的方式做的。最新版本的OpenCV具有最新的摄像头捕获功能,您应该这样做:

#include "cv.h"
#include "highgui.h"

using namespace cv;

int main()
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    namedWindow("Output",1);

    while(true)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera


        //Do your processing here
        ...

        //Show the image

        imshow("Output", frame);
        if(waitKey(30) >= 0) break;
    }

    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}