无法创建/保存输出的AVI文件在OpenCV中
问题描述:
我有以下的源代码来检测BLOB和我使用的MS 2008,OpenVC 2.1无法创建/保存输出的AVI文件在OpenCV中
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
/*You may change the values of the sthreshold and hlower and hupper to get different results....*/
const int sthreshold=210;
const double hlower=178;
const double hupper=3;
int main(int argc, char* argv[]) {
int i,j,k;//for iterations
int height,width,step,channels;/*HSV means the frame after color conversion*/
int heightmono,widthmono,stepmono,channelsmono;/*mono means the frame which has the monochrome image*/
const char string1[]="monoimg.avi";/*This is the name of the video which would be the outcome of the blob detection program..*/
uchar *data,*datamono;
i=j=k=0;
IplImage *frame = 0;
int key = 0;/*Initializing the capture from the video...*/
CvCapture* capture = cvCreateFileCapture("partofvideo3.avi");
double fps = cvGetCaptureProperty (/*getting the capture properties......the frame rate..*/
capture,CV_CAP_PROP_FPS);
CvSize size = cvSize(
(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT)
);
CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('D', 'I', 'V', 'X') ,fps,size) ;
if(writer !=NULL)
printf("Loaded\n");
else
printf("Not Loaded\n");
/* always check */
if (!capture) {
fprintf (stderr, "Cannot open video file!\n");
return(1);
}
height = frame->height;
width = frame->width;
step = frame->widthStep;
channels = frame->nChannels;
data = (uchar *)frame->imageData;
cvNamedWindow("monoimage", CV_WINDOW_AUTOSIZE);
cvNamedWindow("original frame", CV_WINDOW_AUTOSIZE);
for (;;) {/*keep looping till we are out of frames...*/
if (!cvGrabFrame(capture)) {
break;
}
frame = cvRetrieveFrame(capture);
IplImage *colimgbot = cvCreateImage(cvGetSize(frame), 8, 3);
IplImage *monoimgbot = cvCreateImage(cvGetSize(frame), 8, 1);
cvCvtColor(frame,frame,CV_RGB2HSV);
for(i=0;i< (height);i++)
{
for(j=0;j<(width);j++)
{
if((data[(height-i)*step+j*channels]<=hlower) && (data[(height-i)*step+j*channels]>=hupper))
{
if((data[(height-i)*step+j*(channels)+1])>sthreshold)
/*"height-i" because if we use only "i" we were getting vertically inverted result...hence reinverting the same
would do the required....*/
datamono[i*stepmono+j*channelsmono]=255;
else
datamono[i*stepmono+j*channelsmono]=0;}
else datamono[i*stepmono+j*channelsmono]=0;
}
}
cvErode(monoimgbot,monoimgbot,0,14);
cvDilate(monoimgbot,monoimgbot,0,15);
cvWriteFrame(writer, monoimgbot);
cvShowImage("original frame", frame);
cvShowImage("monoimage", monoimgbot);
if((cvWaitKey(10) & 255) == 27) break;
}
cvReleaseVideoWriter(&writer) ;
cvDestroyWindow("monoimage");
cvReleaseCapture(&capture);
return 0;
}
当我运行程序我得到以下运行时错误 当下面的行遇到
CvVideoWriter* writer=cvCreateVideoWriter(string1, CV_FOURCC(‘D’,'I’,'V’,'X’),fps,size) ;
输出#0,AVI,为 'monoimg.avi': 流#0.0:视频mgeg4,YUV420P,q = 2-31,90K TBN [MPEG4 @ 0x37e5c0]帧率不设置为 OpenCV错误:错误参数(无法打开的编解码器“MPEG 4':未指定的错误)的未知函数,文件 C:\用户\ VP \ OCV \的OpenCV \ SRC \ highgui \ cvcap_ffmpeg.cpp,线1306
答
首先getCaptureProperties在实际取得任何东西时都会觉得很糟糕,所以你应该检查一下fps实际上有你认为它的作用。一些编解码器不能在某些帧速率下编码,所以试着明确地将fps设置为30,看看它是否有效。
否则你会错过mpeg 4编解码器。我推荐:
1.)下载一些编解码器,然后重试。 http://www.divx.com/en/software/divx-plus/codec-pack可能有你在找什么。
2)你可以改变
CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('D', 'I', 'V', 'X') ,fps,size) ;
线使用一些其他的编解码器。我使用了几个编解码器,并在我的系统上放置了编码7分钟视频的时间。
(\P,\I,\M,\1) ;= MPEG-1 codec (112913.386195 msecs) (104 MB)
(\M,\J,\P,\G) ;= motion-jpeg codec (crashed)
(\M,\P,\4,\2) ;= MPEG-4.2 codec (107184.186774 msecs) (82 MB)
(\D,\I,\V,\3) ;= MPEG-4.3 codec (118308.933328 msecs) (83 MB)
(\D,\I,\V,\X) ;= MPEG-4 codec (99037.738131 msecs) (85 MB)
(\U,\2,\6,\3) ;= H263 codec (101141.993551 msecs) (89 MB)
(\I,\2,\6,\3) ;= H263I codec (crashed)
(\F,\L,\V,\1) ;= FLV1 codec (104307.567802 msecs) (93 MB)
特别是我会建议尝试FLV1编解码器,因为我已经有了很多运气。所以总结尝试:
CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('F', 'L', 'V', '1') ,fps,size) ;
祝你好运!
我已经安装了DIVX解码包,我曾试图在同一时间我试图FLV1也由30手动设置fps的,但这次我收到以下错误 输出#0,AVI,以“monoimg.avi”: 流#0.0:视频:flv,yuv420p,q = 2-31,90k tbn,50 tbc 编译器未对齐堆栈变量。 Libavcodec已被编译错误 并可能非常缓慢或崩溃。这不是libavcodec, 中的错误,而是编译器中的错误。您可以尝试使用gcc> = 4.2进行重新编译。 不要向FFmpeg开发人员报告崩溃。 图片大小无效(为0x0) 最后一条消息重复2次 AVI @ 0x37bfd0]尺寸没有设置 – Hunt 2010-07-25 17:31:55
设置fps至30手动为我工作在OSX opencv2.2和编解码器支持MPEG-4 – PhoebeB 2010-12-29 11:29:13
请你帮我回答这个问题问题http://stackoverflow.com/questions/9063746/opencv-cv-fourccf-lv-1-not-working – Wazzzy 2012-01-31 13:58:39