Reading Video with the cv::VideoCapture Object
  
  对象创建的三种方法:
  
  // 1. Input filename
  
  cv::VideoCapture::VideoCapture(
  
  const string& filename
  
  );
  
  // 2. Video capture device id
  
  cv::VideoCapture::VideoCapture(
  
  int device
  
  );
  
  // 3.
  
  cv::VideoCapture::VideoCapture();
  
  构造函数的第一种:
  
  在第一种方法中,传入视频路径,创建对象后,需要加入视频是否成功打开:
  
  // e.g.
  
  cv::VideoCapture cap("1.mp4");
  
  if (!cap.isOpen(www.quwanyule157.com)) {
  
  std::cout << "Failed to open the video" << std::endl;
  
  视频打不开的常见原因:
  
  视频路径错误
  
  视频编码格式未知
  
  后一种原因的解决方案:
  
  you will need to have the appropriate library already residing on your computer in order to successfully read the video file.
  
  构造函数的第二种:
  
  通常为设备的代号,整型
  
  小tip:
  
  cv::VideoCapture cap(-1);
  
  1
  
  可打开一个窗口供以选择捕获视频的设备
  
  构造函数的第三种:
  
  cv::VideoCapture cap;
  
  cap.open("video.avi");
  
  1
  
  2
  
  读取视频帧数据
  
  // 1. read function
  
  bool cv::VideoCapture::read(
  
  cv::OutputArray image yongshiyule178.com/// Image into which to read data, return true for read frame successfully, false for failue
  
  );
  
  // 2. >> operator
  
  cv::VideoCapture& cv:www.wanmeiyuele.cn:VideoCapture::operator>>( cv::Mat& image // Image into which to read data
  
  );
  
  // NOTE:
  
  // << return a reference to the original object, should check the return array is not empty
  
  // 3. grab && retrieve function
  
  bool cv::VideoCapture::grab(www.mhylpt.com/ void ); // is designed simply to get it onto the computer as quickly as possible
  
  bool cv::VideoCapture::retrieve(
  
  cv::OutputArray image, // Image into which to read data
  
  int channel = 0 // Used for multihead devices
  
  );
  
  // NOTE:
  
  // can read images from mutiple cameras
  
  // more general
  
  设置&获取视频属性
  
  double cv::VideoCapture::get(
  
  int propid //www.mcyllpt.com Property identifier (see Table 8-4)
  
  );
  
  bool cv::VideoCapture::set(
  
  int propid,
  
  double value // Value to which to set the property
  
  //Example 8-1. Unpacking a four-character code to identify a video codec
  
  cv::VideoCapture cap( "my_video.avi" );
  
  unsigned f = (unsigned)cap.get( cv::CAP_PROP_FOURCC );
  
  char fourcc[] = {
  
  (char) f, // First character is lowest bits
  
  (char)(f >> 8), // Next character is bits 8-15
  
  (char)(f >> 16), // Next character is bits 16-23
  
  (char)(f >> 24), // Last character is bits 24-31
  
  '\0' // and don't forget to terminate
  
  Reading Video with the cv::VideoCapture Object
  
  两种构造方式:
  
  // 1. an uninitialized video object
  
  cv::VideoWriter::VideoWriter();
  
  // 2. an initialized video object
  
  cv::VideoWriter::VideoWriter(
  
  const string& filename, // output filename
  
  int fourcc, // codec, use CV_FOURCC(www.michenggw.com/) macro
  
  double fps, // Frame rate (stored in output file)
  
  cv::Size frame_size, // Size of individual images
  
  bool is_color = true // if false, you can pass gray frames
  
  );
  
  // e.g.
  
  Don’t forget that you are providing characters (single quotes), not strings (double quotes).
  
  同cv::VideoCapture,也有cv::VideoWriter::isOpened()方法暗示是否可以写视频,如果返回false,可能存在以下几点原因:
  
  没有写权限
  
  编码格式不可用
  
  NOTES:
  
  The codecs available to you depend on your operating system installation and the additional libraries you have installed.
  
  For portable code, it is especially important to be able to gracefully handle the case in which the desired codec is not available on any particular machine.
  
  写入视频的两个函数:
  
  // 1. write function
  
  cv::VideoWriter::write(
  
  const Mat& image // Image to write as next frame
  
  );
  
  /*
  
  NOTES:
  
  - This image must have the same size as the size you gave to the writer when you configured it in the first place. - If you told the writer that the frames would be in color, this must also be a three-channel image.
  
  */
  
  // 2. << Operator
  
  my_video_writer << my_frame;
  
  /*
  
  NOTE:
  
  once you have your video writer open, you can write images to the video stream in the same manner you would write to cout or a file ofstream object
  
  简单实例
  
  #include<opencv2/opencv.hpp>
  
  #include<iostream>
  
  using namespace cv;
  
  int main() {
  
  // params & input path && output path configure
  
  int caseVideo = 6;
  
  char pathInVideo[200], pathOutVideo[200];
  
  sprintf(pathInVideo, "%d.avi", caseVideo);
  
  sprintf(pathOutVideo, "%d_output.avi", caseVideo);
  
  // read video
  
  cv::VideoCapture cap(pathInVideo);
  
  if (!cap.isOpened()) {
  
  std::cerr << "Failed to open the video" << std::endl;
  
  return -1;
  
  }
  
  int frameLength = cap.get(CAP_PROP_FRAME_COUNT);
  
  // params for writting output video
  
  cv::VideoWriter outputVideo;
  
  cv::Size sWH = cv::Size((int)cap.get(CAP_PROP_FRAME_WIDTH),
  
  (int)cap.get(CAP_PROP_FRAME_HEIGHT));
  
  if (!outputVideo.open(pathOutVideo, cv::VideoWriter::fourcc('M', 'P', '4', '2'), cap.get(CAP_PROP_FPS), sWH)) {
  
  std::cerr << "Failed to write video" << std::endl;
  
  return -1;
  
  }
  
  // gauss filter frame by frame
  
  for (int i = 0; i < frameLength; i++) {
  
  std::cout << "Process the " << i << " frame" << std::endl;
  
  cv::Mat inputImg, outputImg;
  
  cap.read(inputImg);
  
  if (!inputImg.empty()) {
  
  cv::GaussianBlur(inputImg, outputImg, cv::Size(25, 25), 0.0, 0.0);
  
  outputVideo.write(outputImg);
  
  }
  
  }
  
  // release cap && outputVideo
  
  cap.release();
  
  outputVideo.release();

【学习笔记】Learning OpenCV3——Ch8 working with video的更多相关文章

  1. 流媒体技术学习笔记之(四)解决问题video.js 播放m3u8格式的文件,根据官方的文档添加videojs-contrib-hls也不行的原因解决了

    源码地址:https://github.com/Tinywan/PHP_Experience 总结: 说明: 测试环境:本测试全部来自阿里云直播和OSS存储点播以及本地服务器直播和点播 播放器:Vid ...

  2. 我的Android进阶之旅------>Android中编解码学习笔记

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...

  3. Deep Learning(深度学习)学习笔记整理系列之(八)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  4. 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL

    周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...

  5. OpenCV学习笔记(一)——OpenCV3.1.0+VS2015开发环境配置

    摘要: 由于最近AR(增强现实)这个概念非常火爆,各种基于AR的应用及游戏逐渐面向大众,而在AR中最重要的两个技术就是跟踪识别和增强渲染,其中跟踪识别是通过OpenCV这个开源的计算机视觉库来实现的, ...

  6. 阅读《LEARNING HARD C#学习笔记》知识点总结与摘要系列文章索引

    从发表第一篇文章到最后一篇文章,时间间隔有整整一个月,虽只有5篇文章,但每一篇文章都是我吸收<LEARNING HARD C#学习笔记>这本书的内容要点及网上各位大牛们的经验,没有半点废话 ...

  7. 阅读《LEARNING HARD C#学习笔记》知识点总结与摘要三

    最近工作较忙,手上有几个项目等着我独立开发设计,所以平时工作日的时候没有太多时间,下班累了就不想动,也就周末有点时间,今天我花了一个下午的时间来继续总结与整理书中要点,在整理的过程中,发现了书中的一些 ...

  8. 阅读《LEARNING HARD C#学习笔记》知识点总结与摘要二

    今天继续分享我的阅读<LEARNING HARD C#学习笔记>知识点总结与摘要二,仍然是基础知识,但可温故而知新. 七.面向对象 三大基本特性: 封装:把客观事物封装成类,并隐藏类的内部 ...

  9. 阅读《LEARNING HARD C#学习笔记》知识点总结与摘要一

    本人有幸在Learning Hard举行的整点抢书活动<Learninghard C#学习笔记>回馈网友,免费送书5本中免费获得了一本<LEARNING HARD C#学习笔记> ...

随机推荐

  1. 其他乱七八糟的css

    white-space:normal; word-break:break-all;字母数字强制换行表格宽度失效给上table-layout:fixed(display: table-cell;此元素会 ...

  2. MySQL数据操作(DML)

    表结构准备: mysql> CREATE TABLE student( -> sid INT PRIMARY KEY AUTO_INCREMENT, ), -> age INT, ) ...

  3. ECSHOP和SHOPEX快递单号查询EMS插件V8.6专版

    发布ECSHOP说明: ECSHOP快递物流单号查询插件特色 本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅 ...

  4. apache使用.htaccess文件中RewriteRule重定向后,URL中的加号无法解析

    今天在使用.htaccess做伪静态的时候,发生一件怪事,URL里存在C++时会有问题,在处理C++这个词的时候,无论如何,$_GET都得不到++,只能得到C空格. 一开始我以为是没用urlencod ...

  5. Centos7 搭建 hadoop3.1.1 集群教程

    配置环境要求: Centos7 jdk 8 Vmware 14 pro hadoop 3.1.1 Hadoop下载 安装4台虚拟机,如图所示 克隆之后需要更改网卡选项,ip,mac地址,uuid 重启 ...

  6. 新手学习ARM,对片内ram、SDRAM、NOR FLASH和NAND FLASH启动这几个概念的理解

    片内的ram用来存储启动代码,在2440初始化sdram之前,代码就在片内ram中运行.片内ram装载的是norflash中的内容,即u-boot. uboot放在norflash里,nandflas ...

  7. C语言实例解析精粹学习笔记——34(用“结构”统计学生成绩)

    实例34: 设学生信息包括学号.姓名和五门功课的成绩,要求编写输入输出学生信息的函数.在输入学生信息后,以学生成绩的总分从高到低顺序输出学生信息. 思路: 程序引入一个结构数组依次存储输入的学生信息, ...

  8. (数据科学学习手札19)R中基本统计分析技巧总结

    在获取数据,并且完成数据的清洗之后,首要的事就是对整个数据集进行探索性的研究,这个过程中会利用到各种描述性统计量和推断性统计量来初探变量间和变量内部的基本关系,本篇笔者便基于R,对一些常用的数据探索方 ...

  9. python第一个程序-->hello world

    最近在网上看到一个小笑话,一个程序员的自我嘲讽:“我精通所以计算机语言的hello world!” 好了,废话不多说了,开始撸代码: 我本人用的是python3.6版本,各位可以通过官网下载自己喜欢的 ...

  10. WCF入门三[WCF宿主]

    一.概述 WCF程序必须在宿主上运行,也就是WCF服务必须寄宿在某一个windows的进程中,可以是IIS.控制台程序.窗体程序.WAS以及所有.net程序等程序进程中.在我用VS2013创建WCF服 ...