Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96168
  • 博文数量: 25
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 221
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-08 16:03
个人简介

一万年太久,只争朝夕

文章分类
文章存档

2016年(4)

2015年(19)

2014年(2)

我的朋友

分类: C/C++

2016-01-12 12:44:10


点击(此处)折叠或打开

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<imgproc\imgproc.hpp>
  4. #include<highgui\highgui.hpp>
  5. #include<windows.h>
  6. using namespace cv;

  7. #define NUM_FRAME 600
  8. #define IMAGE2VIDEO 1
  9. #define VIDEO2IMAGE 0

  10. void Video2Image(char* filename)
  11. {
  12.     //初始化一个视频文件捕捉器
  13.     CvCapture* capture = cvCaptureFromAVI(filename);
  14.     //获取视频信息
  15.     cvQueryFrame(capture);
  16.     int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
  17.     int frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
  18.     int fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
  19.     int numFrames = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
  20.     printf("tvideo height : %dntvideo width : %dntfps : %dntframe numbers : %dn", frameH, frameW, fps, numFrames);
  21.     //定义和初始化变量
  22.     int i = 0;
  23.     IplImage* img = 0;
  24.     char image_name[13];
  25.     cvNamedWindow( "mainWin", CV_WINDOW_AUTOSIZE );
  26.     //读取和显示
  27.     while(1)
  28.     {
  29.         img = cvQueryFrame(capture);    //获取一帧图片
  30.         cvShowImage( "mainWin", img );    //将其显示
  31.         cvWaitKey(1);
  32.         sprintf(image_name, "%s%d%s", "image", ++i, ".jpg");//保存的图片名
  33.         cvSaveImage(image_name, img); //保存一帧图片
  34.         Sleep(1000);
  35.         if(i == NUM_FRAME)
  36.             break;
  37.     }
  38.     cvReleaseCapture(&capture);
  39.     cvDestroyWindow("mainWin");
  40. }

  41. void Image2Video()
  42. {
  43.     int i = 0;
  44.     IplImage* img = NULL;
  45.     char image_name[13];
  46.     char key = 0;
  47.     //初始化视频编写器,参数根据实际视频文件修改
  48.     CvVideoWriter *writer = NULL;
  49.     int isColor = 1;
  50.     int fps = 30;
  51.     int frameW = 320;
  52.     int frameH = 240;
  53.     writer = cvCreateVideoWriter("out.avi",CV_FOURCC('X','V','I','D'),fps,cvSize(frameW,frameH),isColor);
  54.     VideoCapture cap(0);    //打开默认摄像头
  55.     if(!cap.isOpened())
  56.         exit(1);

  57.     //创建窗口
  58.     cvNamedWindow( "mainWin", CV_WINDOW_AUTOSIZE );
  59.     Mat Image(cvSize(320,240),CV_8UC3);
  60.     while(i < NUM_FRAME)
  61.     {
  62. #if 0
  63.         sprintf(image_name, "%s%d%s", "image", ++i, ".jpg");
  64.         img = cvLoadImage(image_name);
  65. #endif

  66.         cap>>Image;
  67.         imshow("mainWin", Image);
  68.         key = cvWaitKey(20);

  69.         img = &IplImage(Image);
  70.         
  71.         cvWriteFrame(writer, img);
  72.         i++;
  73.     }
  74.     cvReleaseVideoWriter(&writer);
  75.     cvDestroyWindow("mainWin");
  76. }

  77. int main(int argc, char *argv[])
  78. {
  79.     char filename[13] = "out.avi";
  80. #if IMAGE2VIDEO
  81.     Image2Video();
  82. #else if VIDEO2IMAGE
  83.     Video2Image(filename);
  84. #endif
  85.     return 0;
  86. }

阅读(860) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~