荒野生存国语 迅雷:如何解决opencv读取avi视频文件一闪而过

来源:百度文库 编辑:偶看新闻 时间:2024/04/28 08:45:47
今天直接用cvCreateFileCapture()试图直接读入avi格式的视频图像时,编译没有问题,运行时,视频总是一闪就消失了,开始以为是cvWaitKey()函数的设置出了问题,怎么改也没办法。最后发现其实原因很简单,不过是我自己随便用的一个avi格式的视频opencv不能识别而已,转载网上别人的解决方法

尽管是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. 保存完毕。”
最后再Import已经转换过的avi,就可以顺利运行了。
//============================================================================
// Name        : display_avi.cpp
// Author      : lijun
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include
#include
#include
#include
#include
int g_slider_position = 0;
CvCapture* g_capture = NULL;
void onTrackbarSlide(int pos){
cvSetCaptureProperty(
g_capture,
CV_CAP_PROP_POS_FRAMES,
pos);
}
int main(int argc, char** argv){
//create a window
cvNamedWindow("Example2", CV_WINDOW_AUTOSIZE);
g_capture = cvCreateFileCapture(argv[1]);
int frames = (int)cvGetCaptureProperty(
g_capture,
CV_CAP_PROP_FRAME_COUNT
);
if(frames!=0){
cvCreateTrackbar(
"Position",
"Example2",
&g_slider_position,
frames,
onTrackbarSlide
);
}
IplImage* frame;
while(1){
frame = cvQueryFrame(g_capture);
if(!frame) break;
cvShowImage("Example2", frame);
if(cvWaitKey(33) >= 0) break;
//char c = cvWaitKey(33);
//if(c == 27) break;
}
cvReleaseCapture(&g_capture);
cvDestroyWindow("Example2");
return(0);
}