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

用opencv读取视频保存视频

2013-09-07 11:02 405 查看
不得不说opencv是个强大的东东,以前做一个项目的一个模块时使用到进行图形处理,这次是想将一个视频的播放放慢,以前在网上看到opencv有这个功能,今天就不小心尝试了下,东西不多,主要是做个小记录还有一点要注意的小问题说一下

,代码不多,基本上也都是copy的网上的

#include <iostream>
#include <assert.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <math.h>

using namespace std;
#ifdef NDEBUG
#pragma comment(lib, "highgui210.lib")
#pragma comment(lib, "cxcore210.lib")
#pragma comment(lib, "ml210.lib")
#pragma comment(lib, "cv210.lib")
#else
#pragma comment(lib, "highgui210d.lib")
#pragma comment(lib, "cxcore210d.lib")
#pragma comment(lib, "ml210d.lib")
#pragma comment(lib, "cv210d.lib")
#endif

char g_fileName[] = "C:\\Users\\Desktop\\test.avi";
char g_winodwName[] = "Cv Test";
int main()
{
::cvNamedWindow("g_winodwName", CV_WINDOW_AUTOSIZE);
CvCapture *pCvCapture = NULL;
pCvCapture = cvCreateFileCapture(g_fileName);
assert(NULL != pCvCapture);
IplImage *pIplFrame = NULL;

char out1[] = "C:\\Users\\Desktop\\out1.avi";
double fps1 = cvGetCaptureProperty(pCvCapture,
CV_CAP_PROP_FPS);

CvSize size1 = cvSize((int)cvGetCaptureProperty(pCvCapture,
CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(pCvCapture,
CV_CAP_PROP_FRAME_HEIGHT));

CvVideoWriter *wrVideo1 = cvCreateVideoWriter(out1,
CV_FOURCC('X','V','I','D'),
10,
size1);
IplImage *gray1 = cvCreateImage(size1,8,1);

while (true)
{
pIplFrame = cvQueryFrame(pCvCapture);
if (NULL == pIplFrame)
{
break;
}
else
{
::cvShowImage(g_winodwName, pIplFrame);
//保存视频文件
cvCvtColor(pIplFrame,gray1,CV_RGB2GRAY);
cvWriteFrame(wrVideo1,gray1);

if (27 == ::cvWaitKey(120))
{
break;
}
}
}

::cvReleaseImage(&pIplFrame);
::cvDestroyWindow(g_winodwName);
::cvReleaseImage(&gray1);
::cvReleaseVideoWriter(&wrVideo1);

return cin.get();
}


有几个小地方需要注意下,第一个就是cvCreateFileCapture()这个函数如果路径正确还是返回NULL,则一般是因为没有安装解码器,一般有两个解码器k_lite和xvid,具体哪一个能行我不肯定,因为我试的时候两个都装了然后就正确运行了,还有一个就是cvCreateVideoWriter() 这个函数里面的参数,第三个参数是设置视频播放帧,数字越小,播放的速度越慢,应该每秒的帧数,保存视频的原理也是将图像帧保存到视频文件中。也就是说视频的播放是一个个图片帧的快闪。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: