今天直接用cvCreateFileCapture()试图直接读入avi格式的视频图像时,编译没有问题,运行时,视频总是一闪就消失了,开始以为是cvWaitKey()函数的设置出了问题,怎么改也没办法。最后发现其实原因很简单,不过是我自己随便用的一个avi格式的视频opencv不能识别而已,转载网上别人的解决方法
“
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 |
最后再Import已经转换过的avi,就可以顺利运行了。
//============================================================================
// Name : display_avi.cpp
// Author : lijun
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
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);
}
附:出现xvid找不到安装一下我资源里的xvid解码器 就Ok