cvCreateVideoWriter 多线程下怪问题
warning:
这篇文章距离上次修改已过1635天,其中的信息可能已经有所变动。
最近再做一个视频识别的项目,发现一个奇怪的问题,opencv的cvCreateVideoWriter函数得到正确执行并返回正确的CvVideoWriter结构体指针,但result=cvWriteFrame(writer,pFrameCur);怎么执行result都得0。而且生成的avi文件“神隐”了哪里找都找不到。
在黑屏程序下发现代码执行相当正常。原项目使用了多线程,将多线程改为顺序执行,一切正常,有没有有志之士帮忙解释下这个问题。代码如下:
unsigned __stdcall processImages( void *g)
{
AviProcessor *proc= static_cast(g);//参数g是AviProcessor类指针
int fNumber=0;//帧数
//声明IplImage指针
IplImage* pFrameCur = NULL;
CvMat** pFrameBuffer=new CvMat*[BFFRAME];
CvMat* pFrameCurMat = NULL;//当前矩阵
int lastx=0;
int lasty=0;
int w=0, h=0;
int i=0, j=0;
w = (int)cvGetCaptureProperty(proc->capture,CV_CAP_PROP_FRAME_WIDTH);//获取视频宽度
h = (int)cvGetCaptureProperty(proc->capture,CV_CAP_PROP_FRAME_HEIGHT);//高度
CvVideoWriter *writer = 0;//视频表面绘制
int isColor = 0;
int outCompressCodec = (int)cvGetCaptureProperty(proc->capture, CV_CAP_PROP_FOURCC);//表示codec的四个字符???
int fps = (int)cvGetCaptureProperty(proc->capture, CV_CAP_PROP_FPS); //取得fps
writer=cvCreateVideoWriter("output.avi",outCompressCodec,
fps,cvSize(w,h),isColor);
FILE* fid;
if(!(fid = fopen("subdxy.dat","w")))
printf("no such file");
//particles.output(fid);
fprintf(fid, "the following is subframe dxy\n");//“子帧”
fNumber=0;
while (pFrameCur = cvQueryFrame(pCapture))
{
fNumber++;
int result;
result=cvWriteFrame(writer,pFrameCur);
}
if(pFrameCur)
{
cvReleaseImage(&pFrameCur);
pFrameCur = NULL;
}
if(writer)
{
cvReleaseVideoWriter(&writer);
writer = NULL;
}
fclose(fid);
_endthreadex( 0 );
}