OpenCV--VideoCapture类

读取本地视频&打开摄像头


OpenCV中VideoCapture中有三个构造函数

  • VideoCapture();
  • VideoCapture(const String& filename, int apiPreference = CAP_ANY);
  • VideoCapture(int index, int apiPreference = CAP_ANY);

1.VideoCapture();

默认构造函数

2.VideoCapture(const String& filename, int apiPreference = CAP_ANY);

简单打开一个视频文件 或者一个捕捉设备 或者IP视频流(具体怎么读可以参考https://www.cnblogs.com/arkenstone/p/7194226.html

filename可以是

  • 视频文件名
  • 图片名
  • 视频流的URL

3.VideoCapture(int index, int apiPreference = CAP_ANY);

打开摄像头用来捕捉视频
index默认自带摄像头0,其他的外接摄像头一般是1.

具体定义如下

class CV_EXPORTS_W VideoCapture
{
public:
        CV_WRAP VideoCapture();
        CV_WRAP VideoCapture(const String& filename, int apiPreference = CAP_ANY);
        CV_WRAP VideoCapture(int index, int apiPreference = CAP_ANY);
        virtual ~VideoCapture();
    
    CV_WRAP virtual bool open(const String& filename, int apiPreference = CAP_ANY);

    CV_WRAP virtual bool open(int index, int apiPreference = CAP_ANY);
    
    CV_WRAP virtual bool isOpened() const;
    
    CV_WRAP virtual void release();
    
    CV_WRAP virtual bool grab();
    
    CV_WRAP virtual bool retrieve(OutputArray image, int flag = 0);
    
    virtual VideoCapture& operator >> (CV_OUT Mat& image);

    virtual VideoCapture& operator >> (CV_OUT UMat& image);

    CV_WRAP virtual bool read(OutputArray image);

    CV_WRAP virtual bool set(int propId, double value);

    CV_WRAP virtual double get(int propId) const;

    CV_WRAP String getBackendName() const;

protected:
    Ptr<CvCapture> cap;
    Ptr<IVideoCapture> icap;
}

eg. 读取本地视频

void VideoRead()
{
    VideoCapture capture("1.mp4");
    /*
    VideoCapture capture;
    captrue.open("1.mp4");
    */
        while (1)
    {
        //frame存储每一帧图像
        Mat frame;
        //读取当前帧
        capture >> frame;
        //播放完退出
        if (frame.empty()) {
            printf("播放完成\n");
            break;
        }
        imshow("读取视频",frame);
        //延时30ms
        waitKey(30);
    }
}

转载于:https://www.cnblogs.com/elong1995/p/10819739.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值