视频基本操作
视频读取
opencv中通过VideoCaptrue类对视频进行读取操作以及调用摄像头,下面是该类的API:
import cv2
video = cv2.VideoCapture(0) # 打开本地摄像头,如果是视频流,可将0替换为url
在C++中,CvCapture 是一个结构体,用来保存图像捕获所需要的信息。 opencv通过调用底层ffmpeg提供两种方式从外部捕获图像,打开摄像头或者解析视频流,直接再后面加0或者路径就可以了。而在python中也一样,VideoCapture()是用于从视频文件、图片序列、摄像头捕获视频的类,当我们创建好了上面的video对象后,就可以查看使用帮助和具体能调用的方法,使用help和dir:
$ help(video)
class VideoCapture(builtins.object)
......
| grab(...)
| grab() -> retval
| . @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.
| .
| . The primary use of the function is in multi-camera environments, especially when the cameras do not
| . have hardware synchronization. That is, you call VideoCapture::grab() for each camera and after that
| . call the slower method VideoCapture::retrieve() to decode and get frame from each camera. This way
| . the overhead on demosaicing or motion jpeg decompression etc. is eliminated and the retrieved frames
| . from different cameras will be closer in time.
| .
| . Also, when a connected camera is multi-head (for example, a stereo camera or a Kinect device), the
| . correct way of retrieving data from it is to call VideoCapture::grab() first and then call
| . VideoCapture::retrieve() one or more times with different values of the channel parameter.
| .
| . @ref tutorial_kinect_openni
|
| isOpened(...)
| isOpened() -> retval
| . @brief Returns true if video capturing has been initialized already.
| .
| . If the previous call to VideoCapture constructor or VideoCapture::open() succeeded, the method returns
| . true.
|
| open(...)
| open(filename[, apiPreference]) -> retval
| . @brief Opens a video file or a capturing device or an IP video stream for video capturing.
| .
| . @overload
| .
| . Parameters are same as the constructor VideoCapture(const String& filename, int apiPreference = CAP_ANY)
| . @return `true` if the file has been successfully opened
| .
| . The method first calls VideoCapture::release to close the already opened file or camera.
|
|
|
| open(index[, apiPreference]) -> retval
| . @brief Opens a camera for video capturing
| .
| . @overload
|