just do it

与其苟延残喘,不如纵情燃烧

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);
	}
}
posted @ 2019-05-06 15:13  elong1995  阅读(10496)  评论(0编辑  收藏  举报