OpenCV1.0讀取AVI文件

 今天看到網上很多人問,cvCreateFileCapture讀取avi,爲什麼總是返回NULL. 我查了查文獻,總結如下:
(源程序附在最下)

問題:爲什麼我的電腦支持AVI或者能夠播出AVI,但爲什麼使用cvCreateFileCapture函數總返回NULL呢?
答案:儘管是AVI文件,但也可能使用了某種codec,例如:MJPEG Decompressor。 需要把它轉換OpenCV支持的AVI文件. OpenCV支持的AVI如下:

Container

FourCC

Name

Description

AVI

'DIB '

RGB(A)

Uncompressed RGB, 24 or 32 bit

AVI

'I420'

RAW I420

Uncompressed YUV, 4:2:0 chroma subsampled

AVI

'IYUV'

RAW I420

identical to I420


轉換格式解決方法:
解決方法1下載mencoder.exe, 在window命令行下使用:
 mencoder in.avi -ovc raw -vf format=i420 -o out.avi
(注:我測試了這個方法,沒有成功,原因不詳,希望有朋友們能夠詳細討論一下。)

解決方法2:下載VitualDub, 我使用1.9.4版本
a. File->Open Video File;
b. Video->Filters->Add->Convert format; 選擇4:2:0 Planar YCbCr (YV12)或者 32-Bit RGB。
c. Save as AVI. 保存完畢。
(注:成功使用。)

大家可以測試一下。如果還是不行,請回復,共同討論一下。


我的源代碼,(VS C++ 2008 Expression, WinXP sp3, )
//------------------------------------------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
using namespace std;
int main(int argc, char** argv[])
{
int key=0;
char* filename="D:/fruit.avi";
CvCapture* capture = cvCreateFileCapture(filename);
double fps=cvGetCaptureProperty(capture, CV_CAP_PROP_FPS );
cout<<"fps="<<fps<<endl;//print the frame rate per second

if(capture==NULL) {
cout<<"NO capture"<<endl;
char a=getchar();
return 1;
};    

IplImage* frame;
cvNamedWindow("PlayAVI", CV_WINDOW_AUTOSIZE);
while(1) {
frame = cvQueryFrame( capture );
if(!frame) break;

cvShowImage("PlayAVI", frame );
         key = cvWaitKey(33);// quit when users press 'ESC'
if( key == 27 ) break;
}
cvReleaseCapture(&capture);
cvDestroyWindow("PlayAVI"); 

return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章