天天看点

教你用OpenCV将视频转存为图片序列

在做实验的时候我们经常得到的是监控视频,可是程序中要用到的是视频的图片序列或部分图片,这时就需要将用OpenCV将视频转存为图片序列。源代码如下,已经在VC++2008和OpenCV2.1下调试通过。

// avi2img.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

using namespace std;

int main(int argc, _TCHAR* argv[])

{

 char * filename = "D://video//1.avi";

    int n =1;

 int ret = 0;

 char path[256];

 IplImage *pFrame = NULL;

 IplImage *img = NULL;

 CvCapture * pCapture = cvCaptureFromAVI(filename);

 if (!pCapture)

 {

  printf("Can not read the avi file./n");

  return -1;

 }

 cvNamedWindow("avi2img",1);

    while (pFrame = cvQueryFrame(pCapture))

    {

  cvShowImage("avi2img",pFrame);

  img = cvCreateImage(cvSize(pFrame->width,pFrame->height),pFrame->depth,pFrame->nChannels);

  cvCopyImage(pFrame,img);

  cvFlip(img,img);

  sprintf_s(path,"D://result//img//%d.jpg",n);

  n++;

  cvSaveImage(path,img);

  cvWaitKey(30);

  cvReleaseImage(&img);

    }

 cvDestroyWindow("avi2img");

 cvReleaseCapture(&pCapture);

 return 0;

}

教你用OpenCV将视频转存为图片序列

本文为原创,本人个人网站菜鸟学习网:http://www.pphust.cn 。本人代写课程设计、毕业设计等等。。有意可以联系我QQ:410812128。 非诚勿扰。

广告:海豚娱乐网(安徽卫视粉丝网)http://www.iahtv.com  泰娱乐论坛:http://bbs.cntvlm.com  手机充值店:http://iahtv.taobao.com

继续阅读