【OpenCV】使用调用摄像头:VideoCapture类的使用

以下内容翻译自OpenCV官方文档,仅翻译了C++相关内容。

VideoCapture是一个用来从视频文件、图片序列或摄像头来获取图像的类。以下是一个样例用法:

#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

opencv_source_code/samples/cpp/starter_video.cpp是一个基础的VideoCapture接口用法样例。

opencv_source_code/samples/cpp/video_dmtx.cpp是一个基础的视频处理样例

 

 

VideoCapture::VideoCapture

类的构造函数。

C++: VideoCapture::VideoCapture()

C++: VideoCapture::VideoCapture(const string& filename)

C++: VideoCapture::VideoCapture(int device)

 

VideoCapture::open

打开一个视频文件或摄像头

C++: bool VideoCapture::open(const string& filename)

C++: bool VideoCapture::open(int device)

该方法会首先调用VideoCapture::release()来关闭掉已经打开的文件或摄像头

 

VideoCapture::isOpened

如果视频捕获已经初始化完成,则返回true。

C++: bool VideoCapture::isOpened()

如果之前调用了构造函数或VideoCapture::open,则该方法会返回true。

 

VideoCapture::release

关闭视频文件或摄像头

VideoCapture::release()

该方法会被后来的VideoCapture::open()或者解构函数调用。

 

VideoCapture::grab

抓获视频文件或摄像头的下一帧

C++: bool VideoCapture::grab()

如果捕获成功,则返回true

本方法主要用在多相机的场合,尤其是当多相机没有硬件同步时。也就是,你可以对每一个相机分别调用VideoCapture::grab(),然后再对每一个相机调用更花时间的VideoCapture::retrieve()方法,来解码和得到图像。这样获取到的几帧图像的时间戳将更加接近。

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值