opencv学习中——对视频的处理

1.将视频保存为连续的图片:

#include "stdafx.h"
#include 
#include "opencv2/opencv.hpp"
#include 

int main(int argc, char *argv[])
{
    CvCapture* capture = cvCaptureFromAVI("G:\\21.flv");

//capture = cvCreateFileCapture("G:\\21.flv");
    IplImage* img = 0;
    char image_name[100];
    cvNamedWindow("testfire");
    //读取和显示  
    while (1)
    {
        img = cvQueryFrame(capture); //获取一帧图片  
        if (img == NULL)
            break;

        cvShowImage("testfire", img); //将其显示  
        char key = cvWaitKey(20);
        sprintf(image_name, "%s%d%s", "video-capture\\video-capture", ++i, ".jpg");//保存的图片名  
        cvSaveImage(image_name, img);   //保存一帧图片  
    }

    cvReleaseCapture(&capture);
    cvDestroyWindow("testfire");

    return 0;
}

sprintf(cstr, “%s%d%s”, “images\image”, n++, “.jpg”),
    第一个参数cstr为目标串,值为后面一系列字串的拼接体;
    第二个参数为后面各字串原本的类型格式,当然是共同写在一个双引号中;
    第三个参数(即后面所有的)为需要进行拼接的各种类型值;
    还有就是只要cstr长度足够,可以对任意个字串进行拼接并赋给它。

2.读取视频的宽度高度等参数:

#include "stdafx.h"
#include 
#include "opencv2/opencv.hpp"

int read_video_info(int para_num, char* paras[])
{
    IplImage *pFrame = NULL;
    CvCapture* pCapture = NULL;

  //创建窗口
  cvNamedWindow("video", 1);


  //参数验证
  if( para_num > 2 ){
      fprintf(stderr, "Usage: 程序名称 [video_file_name]\n");
      return -1;
    }

  //打开摄像头
  if (para_num ==1)
    if( !(pCapture = cvCaptureFromCAM(-1))){
        fprintf(stderr, "Can not open camera.\n");
        return -2;
      }

  //读取视频文件
  if(para_num == 2)
    if( !(pCapture = cvCaptureFromFile(paras[1]))){
        fprintf(stderr, "Can not open video file %s\n", paras[1]);
        return -2;
      }
  //逐帧读取视频
  while(pFrame = cvQueryFrame( pCapture ))
    {
      cvShowImage("video",pFrame);

      char c=cvWaitKey(33);  
      if (c == 27) break; //当输入ESC键时,退出窗口; 

    }
  cvDestroyWindow("video");
  cvReleaseImage(&pFrame);


   //获取视频文件信息
    int frameH    = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_HEIGHT);  
    int frameW    = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_WIDTH);  
    int fps       = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FPS);  
    int numFrames = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_COUNT);  
    printf("vedio's width = %d\t height = %d\n video's fps = %d\t numFrames = %d", frameW, frameH, fps, numFrames);

    getchar();
    return 0;
}

使用VideoCapture类读取图像序列:
1.打开一段视频或默认的摄像头:

  //打开视频文件:其实就是建立一个VideoCapture结构  
    VideoCapture capture("../video.avi"); // 方法1  
    capture.open("../video.avi"); // 方法2  

2.获取视频帧

    capture.read(frame);  // 方法一 读取视频的当前帧到Mat frame中   
    capture.grab();       // 方法二

    capture.retrieve(frame); 
    capture>>frame;       // 方法三   

3.获取视频的参数

VideoCapture的get方法可以获取帧率、总帧数、尺寸、格式等参数
    double rate=capture.get(CV_CAP_PROP_FPS); // 获取   
    long nFrame=static_cast<long>(capture.get(CV_CAP_PROP_FRAME_COUNT)); // 获取总帧数  

4.设置视频帧的读取位置,设置视频的帧率、亮度

// 第100帧   
double position=100.0;  
capture.set(CV_CAP_PROP_POS_FRAMES,position);  

// 第1e6毫秒    
double position=1e6;   
capture.set(CV_CAP_PROP_POS_MSEC,position);  

// 视频1/2位置    
double position=0.5;   
capture.set(CV_CAP_PROP_POS_AVI_RATIO,position); 

你可能感兴趣的:(opencv学习之路)