读入视频文件并缩放后存入磁盘

练习:从本地读入视频数据并将缩放变换后的彩色图像存入磁盘


#include "cv.h"
#include "highgui.h"
int main()
{
	//定义两个窗口用来显示视频缩放后的结果
	cvNamedWindow("in",1);
	cvNamedWindow("out",1);

	//读入视频文件
	CvCapture *capture=cvCreateFileCapture("testVideo.avi");
	if(!capture)
	return -1;
	IplImage *frame=cvQueryFrame(capture);//获取视频帧
	assert(frame->width%2==0&&frame->height%2==0);
	IplImage *out=cvCreateImage(cvSize(frame->width/2,frame->height/2),frame->depth,frame->nChannels);

	//定义新视频帧大小
	CvSize size=cvSize((int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH)/2,
				  (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT)/2);
	//获取原视频帧率
	int   fps=(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
	CvVideoWriter *writer=cvCreateVideoWriter("out.avi",CV_FOURCC('X','V','I','D'),fps,size);
	while((frame=cvQueryFrame(capture))!=NULL)
	{
	   cvPyrDown(frame,out,CV_GAUSSIAN_5x5);
	out->origin=1;
	cvWriteFrame(writer,out);
	cvShowImage("in",frame);
	cvShowImage("out",out);
	cvWaitKey(33);
	}
	cvReleaseVideoWriter(&writer);
	cvReleaseImage(&out);
	cvReleaseCapture(&capture);
	cvDestroyWindow("in");
	cvDestroyWindow("out");
	return 0;
}
程序运行结果:


可以看到out.avi保存成功了,如下图:


阅读更多
文章标签: opencv 视频
个人分类: 学习opencv笔记
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭