opencv学习—VideoCapture 类基础知识「建议收藏」

opencv学习—VideoCapture 类基础知识「建议收藏」以下是对两位大神的博客进行简单整理得到:http://blog.csdn.net/weicao1990/article/details/53379881http://blog.csdn.net/guduruyu/article/details/68486063便于为需要的同学解惑,便于自己以后复习!      在opencv中关于视频的读操作是通过

大家好,又见面了,我是你们的朋友全栈君。

以下是对两位大神的博客进行简单整理得到:http://blog.csdn.net/weicao1990/article/details/53379881

http://blog.csdn.net/guduruyu/article/details/68486063

便于为需要的同学解惑,便于自己以后复习!       


    在opencv中关于视频的读操作是通过VideoCapture类来完成的;关于视频的写操作是通过VideoWriter类来实现的。

<一>—VideoCapture—视频的获取操作

       VideoCapture既支持从视频文件(.avi , .mpg格式)读取,也支持直接从摄像机(比如电脑自带摄像头)中读取。要想获取视频需要先创建一个VideoCapture对象,VideoCapture对象的创建方式有以下三种: cop 

【方式一】是从文件(.MPG或.AVI格式)中读取视频,对象创建以后,OpenCV将会打开文件并做好准备读取它,如果打开成功,我们将可以开始读取视频的帧,并且cv::VideoCapture的成员函数isOpened()将会返回true(建议在打开视频或摄像头时都使用该成员函数判断是否打开成功)。

方法:  cv::VideoCapture capture(const string& filename);  // 从视频文件读取 例程:  cv::VideoCapture capture("C:/Users/DADA/DATA/gogo.avi");  // 从视频文件读取 

【方式二】是从摄像机中读取视频,这种情况下,我们会给出一个标识符,用于表示我们想要访问的摄像机,及其与操作系统的握手方式。对于摄像机而言,这个标志符就是一个标志数字——如果只有1个摄像机,那么就是0,如果系统中有多个摄像机,那么只要将其向上增加即可。标识符另外一部分是摄像机域(camera domain),用于表示摄像机的类型,这个域值可以是下面任一预定义常量。

  1. [cpp] view plain
  2. cv::VideoCapture capture(int device );  //视频捕捉设备 id —笔记本电脑的用0表示 

opencv学习---VideoCapture 类基础知识「建议收藏」

      以这种方式创建视频捕获对象时,我们所传递的标识符是域索引和摄像机索引的和。例如:

[cpp] 
view plain
 copy

  1. cv::VideoCapture capture(cv::CAP_IEEE1394 + 1);  

       这个例子中VideoCapture将尝试打开第2个(编号从0开始)1394摄像机。多数情况下,由于我们只有一个摄像机,因此没必要指定摄像机的域,此时使用cv::CAP_ANY是一种高效的方式(也即是0,所以不用特意指定)。

【方式三】先创建一个捕获对象,然后通过成员函数open()来设定打开的信息,操作如下。

[cpp] 
view plain
 copy

  1. cv::VideoCapture VideoCapture;  这里的第二个VideoCapture是一个对象名
  2. VideoCapture.open( “C:/Users/DADA/DATA/gogo.avi );  

将视频帧读取到cv::Mat矩阵中,有两种方式:一种是read()操作;另一种是 “>>”操作。

[cpp] 
view plain
 copy

  1. cv::Mat frame;  
  2. cap.read(frame); //读取方式一  
  3. cap >> frame; //读取方式二  

下面是读取视频并显示的示例代码:

[cpp] 
view plain
 copy

  1. // 示例代码1
  2. #include <opencv2/opencv.hpp> 
  3. #include <iostream>  
  4.   
  5.  mian()  
  6. {  
  7.     cv::VideoCapture capture(C:/Users/DADA/DATA/gogo.avi);  
  8.   
  9.     if (!capture.isOpened())  
  10.     {  
  11.         std::cout << “Read video Failed !” << std::endl;  
  12.         return;  
  13.     }  
  14.   
  15.     cv::Mat frame;  
  16.     cv::namedWindow(“video test”);  
  17.   
  18.     int frame_num = capture.get(cv::CAP_PROP_FRAME_COUNT);  
  19.     std::cout << “total frame number is: “ << frame_num << std::endl;  
  20.   
  21.     for (int i = 0; i < frame_num – 1; ++i)  
  22.     {  
  23.         capture >> frame;  
  24.         //capture.read(frame); 第二种方式  
  25.         imshow(“video test”, frame);  
  26.         if (cv::waitKey(30) == ‘q’)  
  27.         {  
  28.             break;  
  29.         }  
  30.     }  
  31.   
  32.     cv::destroyWindow(“video test”);  
  33.     capture.release();  
  34.     return 0;  
  35. }  

  1. 示例代码2
  1. #include <iostream>
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. int main(int argc,char* argv[])
  5. {
  6.     cv::VideoCapture capture(argv[1]);
  7.     if(!capture.isOpened())
  8.     {
  9.         std::cout<<“video not open.”<<std::endl;
  10.         return 1;
  11.     }
  12.     //获取当前视频帧率
  13.     double rate = capture.get(CV_CAP_PROP_FPS);
  14.     //当前视频帧
  15.     cv::Mat frame;
  16.     //每一帧之间的延时
  17.     //与视频的帧率相对应
  18.     int delay = 1000/rate;
  19.     bool stop(false);
  20.     while(!stop)
  21.     {
  22.         if(!capture.read(frame))
  23.         {

  24.             std::cout<<“no video frame”<<std::endl;
  25.             break;
  26.          }
  27.         //此处为添加对视频的每一帧的操作方法
  28.         int frame_num = capture.get(CV_CAP_PROP_POS_FRAMES);
  29.         std::cout<<“Frame Num : “<<frame_num<<std::endl;
  30.         if(frame_num==20)
  31.         {

  32.             capture.set(CV_CAP_PROP_POS_FRAMES,10);
  33.         }
  34.         cv::imshow(“video”,frame);
  35.         //引入延时
  36.         //也可通过按键停止
  37.         if(cv::waitKey(delay)>0)
  38.         stop = true;
  39.     }
  40.     //关闭视频,手动调用析构函数(非必须)
  41.     capture.release();
  42.     return 0;
  43. }

    上面的代码,我们使用了cv::VideoCapture的成员函数get()并设定标识cv::CAP_PROP_FRAME_COUNT获取了读取视频的帧总数。同样,我们可以指定其他标识,来获取读取视频或摄像头的其他属性。另外,我们也可以使用成员函数set(),设定相应属性的值。cv::VideoCapture中提供的属性标识如下图所示。

opencv学习---VideoCapture 类基础知识「建议收藏」

下面是该类的API。
1.VideoCapture类的构造函数:
C++: VideoCapture::VideoCapture()
C++: VideoCapture::VideoCapture(const string& filename)
C++: VideoCapture::VideoCapture(int device)
功能:创建一个VideoCapture类的实例,如果传入对应的参数,可以直接打开视频文件或者要调用的摄像头。
参数:
filename – 打开的视频文件名。
device – 打开的视频捕获设备id ,如果只有一个摄像头可以填0,表示打开默认的摄像头。 
2.VideoCapture::open
功能:打开一个视频文件或者打开一个捕获视频的设备(也就是摄像头)
C++: bool VideoCapture::open(const string& filename)
C++: bool VideoCapture::open(int device)
参数: 
filename – 打开的视频文件名。
device – 打开的视频捕获设备id ,如果只有一个摄像头可以填0,表示打开默认的摄像头。
    通过对VideoCapture类的构造函数和open函数分析,可以发现opencv读入视频的方法一般有如下两种。比如读取当前目录下名为”dog.avi”的视频文件,那么这两种写法分别如下。
(1)先实例化再初始化:
VideoCapture capture;
capture.open(“dog.avi”);
(2)在实例化的同时进行初始化:
VideoCapture(“dog.avi”);
3.VideoCapture::isOpened
C++: bool VideoCapture::isOpened()
功能:判断视频读取或者摄像头调用是否成功,成功则返回true。
4.VideoCapture::release
C++: void VideoCapture::release()
功能:关闭视频文件或者摄像头。
5.VideoCapture::grab
C++: bool VideoCapture::grab()
功能:从视频文件或捕获设备中抓取下一个帧,假如调用成功返回true。(细节请参考opencv文档说明)
6.VideoCapture::retrieve
C++: bool VideoCapture::retrieve(Mat& image, int channel=0)
功能:解码并且返回刚刚抓取的视频帧,假如没有视频帧被捕获(相机没有连接或者视频文件中没有更多的帧)将返回false。
7.VideoCapture::read
C++: VideoCapture& VideoCapture::operator>>(Mat& image)
C++: bool VideoCapture::read(Mat& image)
功能:该函数结合VideoCapture::grab()和VideoCapture::retrieve()其中之一被调用,用于捕获、解码和返回下一个视频帧这是一个最方便的函数对于读取视频文件或者捕获数据从解码和返回刚刚捕获的帧,假如没有视频帧被捕获(相机没有连接或者视频文件中没有更多的帧)将返回false。
    从上面的API中我们会发现获取视频帧可以有多种方法 :
// 方法一 
capture.read(frame); 
// 方法二 
capture.grab(); 
// 方法三
capture.retrieve(frame); 
// 方法四
capture >> frame;
8.VideoCapture::get
C++: double VideoCapture::get(int propId)
功能:一个视频有很多属性,比如:帧率、总帧数、尺寸、格式等,VideoCapture的get方法可以获取这些属性。
参数:属性的ID。
属性的ID可以是下面的之一:
CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 – start of the film, 1 – end of the film.
CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
CV_CAP_PROP_FPS Frame rate.
CV_CAP_PROP_FOURCC 4-character code of codec.
CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
CV_CAP_PROP_HUE Hue of the image (only for cameras).
CV_CAP_PROP_GAIN Gain of the image (only for cameras).
CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
CV_CAP_PROP_WHITE_BALANCE Currently not supported
CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
Note: 如果查询的视频属性是VideoCapture类不支持的,将会返回0。
9.VideoCapture::set
C++: bool VideoCapture::set(int propertyId, double value)
功能:设置VideoCapture类的属性,设置成功返回ture,失败返回false。
参数:第一个是属性ID,第二个是该属性要设置的值。
属性ID如下:
CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 – start of the film, 1 – end of the film.
CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
CV_CAP_PROP_FPS Frame rate.
CV_CAP_PROP_FOURCC 4-character code of codec.
CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
CV_CAP_PROP_HUE Hue of the image (only for cameras).
CV_CAP_PROP_GAIN Gain of the image (only for cameras).
CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
CV_CAP_PROP_WHITE_BALANCE Currently unsupported
CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/124988.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • SLAM 综述_综述翻译

    SLAM 综述_综述翻译SLAM概述参考资料分享来自本人博客:https://blog.csdn.net/Darlingqiang/article/details/78840931SLAM一般处理流程包括track和map两部分。所谓的track是用来估计相机的位姿,也叫front-end。而map部分(back-end)则是深度的构建,通过前面的跟踪模块估计得到相机的位姿,采用三角法(triangulation…

    2022年10月24日
  • vue的实现原理怎么回答_变压吸附的原理

    vue的实现原理怎么回答_变压吸附的原理一、Vue对比其他框架原理Vue相对于React,Angular更加综合一点。AngularJS则使用了“脏值检测”。React则采用避免直接操作DOM的虚拟dom树。而Vue则采用的是&#160

  • 软件开发项目管理经验总结

    软件开发项目管理经验总结这是我从事软件外包工作以来的项目管理经验的总结,编写文章的目的是为了回顾和总结自己的一些想法,如果其中有不足的地方大家可以一起讨论交流。项目经理的职责关于项目经理的工作职责有很多种说法,我自己是这样理解的作为一名项目经理第一目标就是合理利用公司资源组织设计、开发、测试等各种资源完成项目的高质量交付,并保证项目的盈利。这是衡量一个项目失败或者成功的唯…

  • 2060s

    2060s2060s老版本三星核心:175显存:2030功耗:125算力:43+镁光核心:-400显存:1050功耗:125算力:37+升级系统后三星核心:1035显存:2030镁光核心:1035显存:1050

  • 【iOS】UIViewController生命周期

    【iOS】UIViewController生命周期

  • Apache tez_apache ii

    Apache tez_apache ii转发自这位大佬博客:https://www.cnblogs.com/rongfengliang/p/6991020.html你可能听说过ApacheTez,它是一个针对Hadoop数据处理应用程序的新分布式执行框架。但是它到底是什么呢?它的工作原理是什么?哪些人应该使用它,为什么?如果你有这些疑问,那么可以看一下BikasSaha和ArunMurthy提供的呈现“ApacheTez:加…

    2022年10月24日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号