OpenGL de プログラミング - OpenCV::動画再生
現在地 >> メニュー >> サンプルコード::OpenCV >> OpenCV::動画再生

問題


動画ファイルを用意し、それを再生せよ。

答え


#include <cv.h>
#include <highgui.h>
#include <cstdio>

char* filename = "test.avi";


int main( int argc, char **argv)
{

IplImage* imgA;
CvCapture* cap;


cap = cvCaptureFromFile(filename);
if(cap == NULL)
{
printf("[%s] : Can't load .\n",filename);
exit(0);
}

imgA = cvQueryFrame(cap);
if(imgA == NULL)
{
printf("Can't Get frame.\n");
exit(0);
}


cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
char key;
for( int frame = 1; imgA ; imgA = cvQueryFrame(cap), frame++ )
{
cvShowImage("window",imgA);
key=(char)cvWaitKey(41);
if(key=='\033')
{
exit(0);
}
}


cvWaitKey(0);

cvReleaseCapture(& cap);
cvDestroyWindow("window");

return 0;
}

メモ


ビデオ関係の関数は
http://opencv.jp/document/opencvref_highgui.html
を参照した。

aviファイルのみ動作確認済。

「cvCaptureFromFile()」は「cvCreateFileCapture()」でも同じ。