视频捕获的类,可以从图像序列和摄像头两种方式捕获。该类提供了C++的接口
被保护的属性:
Ptr< CvCapture > cap
Ptr< IVideoCapture > icap
构造函数:
cv::VideoCapture::VideoCapture ( )
当结束视频捕获时,不需要手动调用解构函数。
cv::VideoCapture::VideoCapture ( const String & filename )
参数filename可以是视频文件名(eg. video.avi)或者是图像序列(eg. img_%02d.jpg, which will read samples like img_00.jpg, img_01.jpg, img_02.jpg, ...)
cv::VideoCapture::VideoCapture ( const String & filename,
int apiPreference
)
参数filename同上,apiPreference:当多种capture方式都可以使用时,指定一种方式(e.g. CAP_FFMPEG or CAP_IMAGES )
cv::VideoCapture::VideoCapture ( int index )
参数index= camera_id + domain_offset (CAP_*). id of the video capturing device to open。
如果是单摄像头,那么index=0。
Advanced Usage: to open Camera 1 using the MS Media Foundation API: index = 1 + CAP_MSMF
解构函数:
virtual cv::VideoCapture::~VideoCapture ( )
成员函数:
virtual double cv::VideoCapture::get ( int propId ) const
propId Property identifier. It can be one of the following:•CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
•CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
•CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
•CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
•CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
•CAP_PROP_FPS Frame rate.
•CAP_PROP_FOURCC 4-character code of codec.
•CAP_PROP_FRAME_COUNT Number of frames in the video file.
•CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
•CAP_PROP_MODE Backend-specific value indicating the current capture mode.
•CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
•CAP_PROP_CONTRAST Contrast of the image (only for cameras).
•CAP_PROP_SATURATION Saturation of the image (only for cameras).
•CAP_PROP_HUE Hue of the image (only for cameras).
•CAP_PROP_GAIN Gain of the image (only for cameras).
•CAP_PROP_EXPOSURE Exposure (only for cameras).
•CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
•CAP_PROP_WHITE_BALANCE Currently not supported
•CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
当查询的propId不在上述范围内时,返回0
virtual bool cv::VideoCapture::grab ( )
从视频文件或者摄像头获取下一帧。
返回值是bool
主要用在多摄像头的情况下,尤其在摄像头没有硬件同步的情况下。先对每个摄像头使用grab(),然后使用慢一点的retrieve()方法获取每个摄像头的帧。
https://github.com/Itseez/opencv/tree/master/samples/cpp/openni_capture.cpp
virtual bool cv::VideoCapture::isOpened ( ) const
判断capture是否初始化完毕。
If the previous call to VideoCapture constructor or VideoCapture::open succeeded, the method returns true.
virtual bool cv::VideoCapture::open ( const String & filename )
打开视频文件或者序列,和带同样参数的构造函数中的参数意义一致。
The methods first call VideoCapture::release to close the already opened file or camera.
virtual bool cv::VideoCapture::open ( int index )
index和带同样参数的构造函数中的参数意义一致。
virtual bool cv::VideoCapture::open ( const String & filename,
int apiPreference
)
两个参数和带同样参数的构造函数中的参数意义一致。
The methods first call VideoCapture::release to close the already opened file or camera.
virtual VideoCapture& cv::VideoCapture::operator>> ( Mat & image )
读入下一帧:eg. myCapture>>myFrame;imshow("window",myFrame);
virtual VideoCapture& cv::VideoCapture::operator>> ( UMat & image )
virtual bool cv::VideoCapture::read ( OutputArray image )
这个方法是grab和retrieve方法的合并。这个方法先调用了grab方法,再调用了retrieve方法,最后返回了帧。
virtual void cv::VideoCapture::release ( )
关闭视频文件或者摄像头。
The methods are automatically called by subsequent VideoCapture::open and by VideoCapture destructor.
virtual bool cv::VideoCapture::retrieve ( OutputArray image,
int flag = 0
)
Decodes and returns the grabbed video frame.
If no frames has been grabbed (camera has been disconnected, or there are no more frames in video file), the methods return false and the functions return NULL pointer.
virtual bool cv::VideoCapture::set ( int propId,
double value
)
参数propId与get方法中的propId一致。
参数value就是要设置的值。
成员数据:
Ptr<CvCapture> cv::VideoCapture::cap
Ptr<IVideoCapture> cv::VideoCapture::icap
示例程序:
#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
VideoCapture cap,cap1;
cap.open("video.avi");
cap1.open(0);
if (cap.isOpened() == true&&cap1.isOpened()==true)
cout << "ready" << endl;
while (1)
{
Mat frame,frame1;
cap.read(frame);
cap1 >> frame1;
cout << cap.get(CAP_PROP_POS_FRAMES)<<endl;
if (frame.empty() == true) break;
imshow("capture", frame);
imshow("capture1", frame1);
waitKey(30);
}
cap.release();
return 0;
}