VideoCapture类

目录

 成员函数:

open

grab

read 

release 

 实现摄像头实时显示


视频的读操作由VideoCapture类完成

class CV_EXPORTS_W VideoCapture

/** @brief Class for video capturing from video files, image sequences or cameras.

The class provides C++ API for capturing video from cameras or for reading video files and image sequences.

Here is how the class can be used:
@include samples/cpp/videocapture_basic.cpp

@note In @ref videoio_c "C API" the black-box structure `CvCapture` is used instead of %VideoCapture.
@note
-   (C++) A basic sample on using the %VideoCapture interface can be found at
    `OPENCV_SOURCE_CODE/samples/cpp/videocapture_starter.cpp`
-   (Python) A basic sample on using the %VideoCapture interface can be found at
    `OPENCV_SOURCE_CODE/samples/python/video.py`
-   (Python) A multi threaded video processing sample can be found at
    `OPENCV_SOURCE_CODE/samples/python/video_threaded.py`
-   (Python) %VideoCapture sample showcasing some features of the Video4Linux2 backend
    `OPENCV_SOURCE_CODE/samples/python/video_v4l2.py`
 */

 成员函数:

  • open

    CV_WRAP virtual bool open(int index, int apiPreference = CAP_ANY);

   /** @brief  Opens a camera for video capturing

    @overload

    Parameters are same as the constructor VideoCapture(int index, int apiPreference = CAP_ANY)
    @return `true` if the camera has been successfully opened.

    The method first calls VideoCapture::release to close the already opened file or camera.
    */

  • grab

    CV_WRAP virtual bool grab();

 /** @brief Grabs the next frame from video file or capturing device.

    @return `true` (non-zero) in the case of success.

    The method/function grabs the next frame from video file or camera and returns true (non-zero) in the case of success.

    @ref tutorial_kinect_openni
     */

  • read 

     CV_WRAP virtual bool read(OutputArray image);

    /** @brief Grabs, decodes and returns the next video frame.

    @param [out] image the video frame is returned here. If no frames has been grabbed the image will be empty.
    @return `false` if no frames has been grabbed

    The method/function combines VideoCapture::grab() and VideoCapture::retrieve() in one call. This is the
    most convenient method for reading video files or capturing data from decode and returns the just
    grabbed frame. If no frames has been grabbed (camera has been disconnected, or there are no more
    frames in video file), the method returns false and the function returns empty image (with %cv::Mat, test it with Mat::empty()).

    @note In @ref videoio_c "C API", functions cvRetrieveFrame() and cv.RetrieveFrame() return image stored inside the video
    capturing structure. It is not allowed to modify or release the image! You can copy the frame using
    cvCloneImage and then do whatever you want with the copy.
     */

  • release 

    CV_WRAP virtual void release();

/** @brief Closes video file or capturing device.

    The method is automatically called by subsequent VideoCapture::open and by VideoCapture
    destructor.

    The C function also deallocates memory and clears \*capture pointer.
     */

 实现摄像头实时显示

#include <iostream>
#include <opencv.hpp>
using namespace cv;
using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    VideoCapture cameraCap;
    cameraCap.open(0); // step1.open打开摄像头
    while(1){
        Mat srcImage;   // 定义Mat数据类型变量 存放读取的视频帧

        /*
        // method 1
        cameraCap >> srcImage;
        if(srcImage.empty()) break;
       */

        // method 2
        if(cameraCap.grab())    //  step2.grab获取摄像头
        {
            cameraCap.read(srcImage);   //step3.读取一帧数据,存入Mat变量
        }
        if(srcImage.data == nullptr) return -1;


        imshow("show", srcImage);    // step4.show
        if(waitKey(20) == 'q')   //延时20ms,获取用户是否按键的情况,如果按下q,会推出程序
            break;
    }

    cameraCap.release();     //释放摄像头资源
    destroyAllWindows();   //释放全部窗口
    return 0;
}

  • 11
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值