您的位置:首页 > 运维架构

关于opencv中不能读取视频avi的问题

2018-02-08 17:41 411 查看
这几天用cvCaptureFromAVI的函数,发现同样是.avi文件,有的可以读,有的不能读,而这些文件用wmp都是可以播放的。开始以为是帧率的问题,因为发现网上有个帖子说只能支持15fps的帧率,搞得我到处找video converter,想转换成帧率小的,后来发现好像不是这个问题。于是又找,在一个英文论坛上发现同样的问题,解答是:Get a copy of GSpot to check the video codec.
If the file is compressed but no corresponding VFW codec on your system,
then cvCaptureFromAVI can not read it.
If the file is uncompressed, use mencoder to convert it
to opencv readable format, like this:
mencoder youravi.avi -ovc raw -vf format=i420 -o new.avi发现可能是解码器没有装,下个GSpot分析了下,果然,打不开的文件检测是没有装解码器,而打的开的文件发现格式是BI_RGB Raw Bitmap,因此No Codec Required,所以能够打开。于是,马上在http://www.itime.cn/Soft/mp3soft/2523.html下了个XviD解码器,装好之后,在运行程序,果然可以读了。  今天看到网上很多人问,cvCreateFileCapture读取avi,为什么总是返回NULL. 我查了查文献,总结如下:(源程序附在最下)
问题:为什么我的电脑支持AVI或者能够播出AVI,但为什么使用cvCreateFileCapture函数总返回NULL呢?答案:尽管是AVI文件,但也可能使用了某种codec,例如:MJPEG Decompressor。 需要把它转换OpenCV支持的AVI文件. OpenCV支持的AVI如下:
ContainerFourCCNameDescription
AVI'DIB 'RGB(A)Uncompressed RGB, 24 or 32 bit
AVI'I420'RAW I420Uncompressed YUV, 4:2:0 chroma subsampled
AVI'IYUV'RAW I420identical 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;}http://www.cnblogs.com/frankman/archive/2008/09/03/1282905.htmlhttp://hq.huang.blog.163.com/blog/static/1066821912009758958885/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: