您的位置:首页 > 运维架构

OpenCV Learning: 第2章视频写入遇到的问题

2013-10-31 10:27 411 查看
原文写于2013年3月22日

写入原始视频可以正常保存,现在frame经过灰度化处理之后想保存视频,但是不知怎地就是无法保存,难道cvWriteFrame只支持3通道的image保存吗?等待解决...

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

int main( ) {
CvCapture* capture = cvCaptureFromFile( "E:\\test.avi" );
if (!capture)
{printf("unable to load a video");
cvWaitKey(0);
exit(0);
}
cvNamedWindow("haha", 1);
IplImage* frame;
double fps = cvGetCaptureProperty (
capture,
CV_CAP_PROP_FPS);
printf("fps=%d n",(int)fps);
/*
Here with the help cvGetCaptureproperty we are trying to acquire the widht and height of the video… and store it in CvSize structure….Please do not get overwhelmed…
treat CvSize structure as a normal easy to understand structure and read on…
*/
CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT)
);
printf("frame (w, h) = (%d, %d)n",size.width,size.height);
/*Creating the Video writer If you do not know about the Video writer study this post and come back to tis line*/
CvVideoWriter  *writer = cvCreateVideoWriter(
"partofvideo.avi",/*filename*/
CV_FOURCC('X','V','I','D'), /*codec type*/
fps,
size,
1
);
IplImage* gray_frame = cvCreateImage(size, IPL_DEPTH_8U, 1);
int counter=0;
while( (frame=cvQueryFrame(capture)) != NULL ) {
counter++;
/*discard all the frames before a certain time and accept the frames in a certain time period and write the frames in the new video*/
cvConvertImage(frame, gray_frame, 0);
printf("entered\n");
cvWriteFrame( writer, gray_frame );

cvShowImage("haha", gray_frame);
if((cvWaitKey(100)) == 27)
break;
}
cvReleaseImage(&frame);
cvReleaseImage(&gray_frame);
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &capture );
cvDestroyWindow("haha");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: