OpenCV之VideoCapture

VideoCaptrue类对视频进行读取操作以及调用摄像头。

头文件:

#include <opencv2/video.hpp>

主要函数如下:

构造函数

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

参数:
filename – 打开的视频文件名。

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

基本功能

打开视频文件或者设备

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

打开一个视频文件或者打开一个捕获视频的设备(也就是摄像头)

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

判断打开是否成功

C++: bool VideoCapture::isOpened();

成功返回true,,否则false.

关闭视频文件或者摄像头


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
OpenCV VideoCapture is a class that provides an interface for capturing video from cameras or files. It is a part of the OpenCV library that is used for computer vision applications. VideoCapture can be used to capture live video from a webcam or to read video files. To use VideoCapture, you need to create an instance of the class and specify the device or file you want to capture video from. The device is identified by its index number, which starts from 0. For example, to capture video from the default camera, you can use the following code: ```python import cv2 cap = cv2.VideoCapture(0) ``` This creates a VideoCapture object and sets it to capture video from the first camera device available on the system. You can also specify a video file to read using the constructor. For example, to read a video file named "test.mp4", you can use the following code: ```python import cv2 cap = cv2.VideoCapture("test.mp4") ``` Once you have created a VideoCapture object, you can use the read() method to read frames from the video stream. The read() method returns a tuple containing a boolean value and a frame. The boolean value indicates whether the frame was successfully read or not, and the frame is a NumPy array containing the image data. ```python import cv2 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if ret: cv2.imshow("Frame", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() ``` This code captures video from the default camera and displays each frame in a window. The loop continues until the user presses the 'q' key to quit. Finally, the VideoCapture object is released and the display window is destroyed.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

视图猿人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值