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

在2013年的3月22日,博主遇到了使用OpenCV进行视频处理的一个问题。当将原始视频帧转换为灰度图像后尝试保存视频时,发现无法成功保存。疑问是否OpenCV的cvWriteFrame函数仅支持3通道图像的保存。这个问题有待进一步解决。

原文写于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;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值