OpenCV——视频基础操作

 opencv中通过VideoCaptrue类对视频进行读取操作以及调用摄像头,下面是该类的API。

1.VideoCapture类的构造函数:

VideoCapture::VideoCapture();
VideoCapture::VideoCapture(const string& filename);
VideoCapture::VideoCapture(int device);

功能:创建一个VideoCapture类的实例,如果传入对应的参数,可以直接打开视频文件或者要调用的摄像头。

参数:
filename – 打开的视频文件名。
device – 打开的视频捕获设备id ,如果只有一个摄像头可以填0,表示打开默认的摄像头。 

2.VideoCapture::open

bool VideoCapture::open(const string& filename);
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

bool VideoCapture::isOpened();

功能:判断视频读取或者摄像头调用是否成功,成功则返回true。

4.VideoCapture::release

void VideoCapture::release();

功能:关闭视频文件或者摄像头


5.VideoCapture::grab

bool VideoCapture::grab();

功能:从视频文件或捕获设备中抓取下一个帧,假如调用成功返回true。(细节请参考opencv文档说明)

6.VideoCapture::retrieve

bool VideoCapture::retrieve(Mat& image, int channel=0);

功能:解码并且返回刚刚抓取的视频帧,假如没有视频帧被捕获(相机没有连接或者视频文件中没有更多的帧)将返回false。


7.VideoCapture::read

VideoCapture& VideoCapture::operator>>(Mat& image);
bool VideoCapture::read(Mat& image);

功能:该函数结合VideoCapture::grab()和VideoCapture::retrieve()其中之一被调用,用于捕获、解码和返回下一个视频帧这是一个最方便的函数对于读取视频文件或者捕获数据从解码和返回刚刚捕获的帧,假如没有视频帧被捕获(相机没有连接或者视频文件中没有更多的帧)将返回false。

    从上面的API中我们会发现获取视频帧可以有多种方法 :

// 方法一 
capture.read(frame); 
 
// 方法二 
capture.grab(); 
 
// 方法三
capture.retrieve(frame); 
 
// 方法四
capture >> frame;


8.VideoCapture::get

double VideoCapture::get(int propId);

功能:一个视频有很多属性,比如:帧率、总帧数、尺寸、格式等,VideoCapture的get方法可以获取这些属性。

参数:属性的ID。

属性的ID可以是下面的之一:

// 如果查询的视频属性是VideoCapture类不支持的,将会返回0。

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)

9.VideoCapture::set

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)

下面是一个示例:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(int argc,char* argv[])
{
    using namespace std;
    using namespace cv;
    VideoCapture capture(argv[1]);
    if(!capture.isOpened())
 
    {
        cout <<"video not open."<< endl;
        return 1;
    }
    //获取当前视频帧率
    double rate = capture.get(CV_CAP_PROP_FPS);

    //当前视频帧
    Mat frame;

    //每一帧之间的延时
    //与视频的帧率相对应
    int delay = 1000/rate;
    bool stop(false);
    while(!stop)
    {
        if(!capture.read(frame))
        {
            cout<<"no video frame"<<endl;
            break;
         }
 
        //此处为添加对视频的每一帧的操作方法
        int frame_num = capture.get(CV_CAP_PROP_POS_FRAMES);
        cout <<"Frame Num : "<<frame_num<< endl;
        if(frame_num==20)
        {
            capture.set(CV_CAP_PROP_POS_FRAMES,10);
        }
        imshow("video",frame);
        //引入延时
        //也可通过按键停止
        if(waitKey(delay)>0)
        stop = true;
    }
    //关闭视频,手动调用析构函数(非必须)
    capture.release();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
opencv——基于python语言实现》pdf 是一本介绍了使用Python语言实现OpenCV库的编程技术的电子书。 OpenCV 是一个广泛应用于计算机视觉领域的开源库,它提供了许多用于图像处理和计算机视觉的函数和算法。 该电子书首先介绍了OpenCV和Python的基本概念和背景知识,如图像处理的基本操作和图像编程的基本原理。它还详细介绍了OpenCV库中的各种图像处理算法和函数,并提供了许多示例代码和案例研究,以帮助读者理解和应用这些算法和函数。 该电子书还涵盖了如何使用Python与OpenCV进行人脸检测、目标跟踪、图像识别和图像分割等常见任务。它还介绍了如何利用OpenCV和Python进行视频处理和实时图像处理,并展示了如何与摄像头和外部设备进行交互。 这本书对于希望学习和应用OpenCV和Python进行图像处理和计算机视觉的人来说是非常有价值的。它详细讲解了OpenCV库中的各种函数和算法的使用方法,并提供了丰富的示例和案例研究。读者可以通过学习该书,深入了解OpenCV和Python的使用,并掌握图像处理和计算机视觉的基本原理和技术,从而能够应用于实际项目和应用中。 总之,《opencv——基于python语言实现》pdf 是一本适合初学者和有一定基础的人学习和应用OpenCV和Python进行图像处理和计算机视觉的电子书。通过阅读该书,读者可以全面了解OpenCV库的使用方法和基本原理,并掌握使用Python语言进行图像处理和计算机视觉任务的技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DDsoup

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值