到目前为止,我管理OpenCV播放视频。但是我现在应该怎么做来提取帧…?
下面是我写的代码,到目前为止,我的视频播放:
#include<opencvcv.h>
#include<opencvhighgui.h>
#include<opencvml.h>
#include<opencvcxcore.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" );
}
frame
是要提取的帧。如果你想把它转换成cv::Mat你可以用IplImage:
Mat myImage(IplImage);
这里有一个很好的教程。
然而,你在用老方法做。最新版本的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;
}