C++ 无法在OpenCV中创建/保存输出avi文件
我有以下检测BLOB的源代码,我使用的是MS2008,OpenVC2.1C++ 无法在OpenCV中创建/保存输出avi文件,c++,visual-studio-2008,video,opencv,C++,Visual Studio 2008,Video,Opencv,我有以下检测BLOB的源代码,我使用的是MS2008,OpenVC2.1 #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
#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;
}
输出#0,avi,到'monoimg.avi':
流#0.0:视频mgeg4,yuv420p,q=2-3190K tbn
[mpeg4@0x37e5c0]未设置帧速率
OpenCV错误:未知函数、文件中的参数错误(无法打开编解码器“mpeg 4”:未指定错误)
C:\User\VP\ocv\opencv\src\highgui\cvcap\u ffmpeg.cpp,第1306行第一步getCaptureProperties在实际获取任何东西时有点差劲,因此您应该检查fps是否确实具有您认为的功能。有些编解码器无法在特定的帧速率下进行编码,所以请尝试将fps显式设置为30,然后看看它是否有效 否则,您将丢失它所说的mpeg 4编解码器。我建议: 1.)下载一些编解码器,然后重试。 可能有你想要的东西 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编解码器包,我尝试将fps手动设置为30,同时我也尝试了FLV1,但这次我得到以下错误输出#0,avi,到'monoimg.avi':Stream#0.0:Video:flv,yuv420p,q=2-31,90k tbn,50 tbc编译器没有对齐堆栈变量。Libavcodec编写错误,可能非常慢或崩溃。这不是libavcodec中的错误,而是编译器中的错误。您可以尝试使用gcc>=4.2重新编译。不要向FFmpeg开发人员报告崩溃。图片大小无效(0x0)最后一条消息重复2次avi@0x37bfd0]维度未设置fps为30在osx opencv2.2和MPEG-4编解码器中手动为我工作请帮我回答这个问题好吗
CvVideoWriter *writer=cvCreateVideoWriter(string1, CV_FOURCC('F', 'L', 'V', '1') ,fps,size) ;