OpenCV中通过VideoCaptrue类对视频进行读取操作以及调用摄像头,下面是该类的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(实际上只是简单的ms,距离开始的毫秒数).

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)

至此,视频捕获类VideoCapture的API介绍完了,下面是一个应用示例:

#include <iostream>

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

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

{

    cv::VideoCapture capture(argv[1]);
    if(!capture.isOpened())

{
        std::cout<<"video not open."<<std::endl;
        return 1;
    }
    //获取当前视频帧率
    double rate = capture.get(CV_CAP_PROP_FPS);
    //当前视频帧
    cv::Mat frame;
    //每一帧之间的延时
    //与视频的帧率相对应
    int delay = 1000/rate;
    bool stop(false);
    while(!stop)

{
        if(!capture.read(frame))

{
            std::cout<<"no video frame"<<std::endl;
            break;
         }

        //此处为添加对视频的每一帧的操作方法

        int frame_num = capture.get(CV_CAP_PROP_POS_FRAMES);
        std::cout<<"Frame Num : "<<frame_num<<std::endl;
        if(frame_num==20)

{
            capture.set(CV_CAP_PROP_POS_FRAMES,10);
        }

        cv::imshow("video",frame);
        //引入延时
        //也可通过按键停止
        if(cv::waitKey(delay)>0)
        stop = true;
    }

//关闭视频,手动调用析构函数(非必须)
    capture.release();
    return 0;

}


【计算机视觉】OpenCV读取视频获取时间戳等信息(PS:经测试并不是时间戳,与FFMPEG时间戳不一样)的更多相关文章

  1. opencv 读取视频内容写入图片帧

    现在主要把自己平时用到的opencv功能记录到博客,一方面方便自己有时间来回顾,另一方便提供给大家一个参考. opencv 读取视频内容,把视频帧每一帧写成图片,存入电脑中.这个步骤是许多数据处理的基 ...

  2. Opencv读取视频

    CvCapture 是一个结构体,用来保存图像捕获所需要的信息. opencv提供两种方式从外部捕获图像 一种是从摄像头中, 一种是通过解码视频得到图像. 两种方式都必须从第一帧开始一帧一帧的按顺序获 ...

  3. Opencv读取视频一闪而过情况分析

    在参加一个软件比赛需要用opencv对视频的处理,也碰到了一些问题. 最常见的就是视频一闪而过了,在网上查了好久都没解决, 最后重装在配置环境变量时发现的. 现在我来终结一下估计是比较全的了. 先说明 ...

  4. C/C++ OpenCV读取视频与调用摄像头

    原文:http://blog.csdn.net/qq78442761/article/details/54173104 OpenCV通过VideoCapture类,来对视频进行读取,调用摄像头 读取视 ...

  5. python+opencv读取视频,调用摄像头

    引用 import cv2 import numpy 创建摄像头对象 cap = cv2.VideoCapture("videoTest/test1.mp4") #参数为视频文件目 ...

  6. Opencv读取并获取视频属性

    opencv中通过VideoCaptrue类对视频进行读取操作以及调用摄像头.常用的操作如下: 1.常用构造函数 1.VideoCapture类的构造函数:C++: VideoCapture::Vid ...

  7. OpenCV 读取视频 多种方式

    OpenCV中常见的视频方式是while循环读取,可是,当遇到嵌套循环呢 1.常见的while循环 ,没有嵌套循环 cv::VideoCapture capture("d:/test/dem ...

  8. Linux OpenCV读取视频失败,cvCreateFileCapture失败的解决

    背景: 近期想在嵌入式平台上开发QT+Opencv,无料PC机上编写的OpenCV程序老是打不开视频. 開始提示:OpenCV Error: Bad argument (Array should be ...

  9. C++ 调用 opencv 读取视频文件列表并处理

    //g++ trans_video.cpp -o trans_video `pkg-config opencv --libs --cflags` -L/usr/lib/x86_64-linux-gnu ...

随机推荐

  1. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part4

    Load Data from Test Fixtures in Cypress When creating integration tests with Cypress, we’ll often wa ...

  2. 第四章 初始CSS

    一.引入样式 1.行内样式表 <h1 style="color: red;font-size: 18px;">10-30</h1> 2.内部样式表(在hea ...

  3. [USACO]骑马修栅栏 Riding the Fences

    题目链接 题目简述:欧拉回路,字典序最小.没什么好说的. 解题思路:插入边的时候,使用multiset来保证遍历出出答案的字典序最小. 算法模板:for(枚举边) 删边(无向图删两次) 遍历到那个点 ...

  4. 永远不会被卡的Dinic

    78,79行是精髓 61,148,149行是当前弧优化 #include <cstring> #include <cstdio> #include <queue> ...

  5. maven 聚合工程 > 坑总结

    maven聚合工程子项目无法构建:父工程未 install tomcat可以运行,maven 无法编译报错,检查是否添加红色语句: <dependency> <groupId> ...

  6. P1095 守望者的逃离——DP?贪心?

    https://www.luogu.org/problem/P1095 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大 ...

  7. ERROR: node with name "rabbit" already running on "localhost"

    rabbitmqctl start_app启动没有这个问题

  8. 【模板】分治FFT

    蒟蒻写题解实在不易 前置 方法一:\(Cdq+NTT\) 方法二:多项式求逆 NTT总结:多项式求逆总结 方法一 \(Cdq+NTT\): \[f_i=\sum\limits_{j=1}^i f_{i ...

  9. Hadoop(4)MapReduce 任务的推测(speculative)执行

    Straggle(掉队者)是指那些跑的很慢但最终会成功完成的任务.一个掉队的Map任务会阻止Reduce任务开始执行. Hadoop不能自动纠正掉队任务,但是可以识别那些跑的比较慢的任务,然后它会产生 ...

  10. 怎么新开一个组件并且配置路由?vue-cli

    首先要明白: 路由就是url路径,如果一个组件被引入到了另外一个组件,这个页面就包含这个组件了,所以这个被包含的组件不要去路由哪里配置了 第一步: 先写上想要添加的组件 2.组件的内容 3.路由的配置 ...