现在的位置: 首页 > 综合 > 正文

OpenCV读取AVI文件

2013年10月06日 ⁄ 综合 ⁄ 共 1585字 ⁄ 字号 评论关闭

转自  http://hq.huang.blog.163.com/blog/static/1066821912009758958885/

本人也遇到了类似的问题,经验证,可行.特记录一下, 以下是原文内容.

 

今天看到网上很多人问,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;
}

抱歉!评论已关闭.