OpenCV VideoCapture类

OpenCV the reading operation of the video camera via the VideoCaptrue and invoking the class, the class is the following API.

1.VideoCapture class constructor:

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

Function: VideoCapture create an instance of the class, if the corresponding parameter passing, you can open the file directly or video camera to call.

Parameters: filename - open the video file name.

device - open the video capture device id, if only a camera can fill 0, indicating open the default camera. 

2.VideoCapture::open

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

Feature: open a video file or open a captured video device (ie camera)

Parameters: filename - open the video file name.

device - open the video capture device id, if only a camera can fill 0, indicating open the default camera.

By constructor and open function VideoCapture class analysis, a method can be found in general video opencv read the following two. For example, reading a video file named "dog.avi" in the current directory, both written are as follows.

(1) First example of reinitialization:

VideoCapture capture;
capture.open("dog.avi");

(2) In the example of the initialized at the same time:

VideoCapture("dog.avi");

3.VideoCapture::isOpened

bool VideoCapture::isOpened();

Function: read or video camera to determine whether the call is successful, returns true if successful.

4.VideoCapture::release

void VideoCapture::release();

Function: Close the file or video camera.

5.VideoCapture::grab

bool VideoCapture::grab();

Function: The next frame grab from a video capture device or file returns true if the call succeeds. (For details, see opencv documentation)

6.VideoCapture::retrieve

bool VideoCapture::retrieve(Mat& image, int channel=0);

Function: returns the decoded video frames and captured immediately, if no video frames are captured (not connected to the camera or video files are no more frames) returns false.

7.VideoCapture::read

VideoCapture& VideoCapture::operator>>(Mat& image);
bool VideoCapture::read(Mat& image);

Function: This function combined VideoCapture :: grab () and VideoCapture :: retrieve () one of which is called, for capturing, decoding and returns the next video frame that is most convenient function for a video file, or read data capture from the decoded frame freshly caught and returned, if no video frames are captured (not connected to the camera or video files are no more frames) returns false.

From the above, we find that API to obtain video frame can have a number of ways:

1  // method 
2  capture.read (Frame); 
 . 3  
. 4  // Method II 
. 5  capture.grab (); 
 . 6  
. 7  // Method three 
. 8  capture.retrieve (Frame); 
 . 9  
10  // FOUR 
. 11 Capture >> frame;

8.VideoCapture::get

double VideoCapture::get(int propId);

功能:一个视频有很多属性,比如:帧率、总帧数、尺寸、格式等,VideoCapture的get方法可以获取这些属性。

参数:属性的ID。

属性的ID可以是下面的之一:

 1 CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
 2 CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
 3 CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
 4 CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
 5 CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
 6 CV_CAP_PROP_FPS Frame rate.
 7 CV_CAP_PROP_FOURCC 4-character code of codec.
 8 CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
 9 CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
10 CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
11 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
12 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
13 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
14 CV_CAP_PROP_HUE Hue of the image (only for cameras).
15 CV_CAP_PROP_GAIN Gain of the image (only for cameras).
16 CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
17 CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
18 CV_CAP_PROP_WHITE_BALANCE Currently not supported
19 CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

Note: 如果查询的视频属性是VideoCapture类不支持的,将会返回0。

9.VideoCapture::set

bool VideoCapture::set(int propertyId, double value)

功能:设置VideoCapture类的属性,设置成功返回ture,失败返回false。

参数:第一个是属性ID,第二个是该属性要设置的值。

属性ID如下:

 1 CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
 2 CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
 3 CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
 4 CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
 5 CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
 6 CV_CAP_PROP_FPS Frame rate.
 7 CV_CAP_PROP_FOURCC 4-character code of codec.
 8 CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
 9 CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
10 CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
11 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
12 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
13 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
14 CV_CAP_PROP_HUE Hue of the image (only for cameras).
15 CV_CAP_PROP_GAIN Gain of the image (only for cameras).
16 CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
17 CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
18 CV_CAP_PROP_WHITE_BALANCE Currently unsupported
19 CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)

至此,视频捕获类VideoCapture的API介绍完了,下面是一个应用示例:

 1 #include <iostream>
 2 
 3 #include <opencv2/core/core.hpp>
 4 #include <opencv2/highgui/highgui.hpp>
 5 
 6 int main(int argc,char* argv[])
 7 {
 8     cv::VideoCapture capture(argv[1]);
 9     if(!capture.isOpened())
10 
11     {
12         std::cout<<"video not open."<<std::endl;
13         return 1;
14     }
15     //获取当前视频帧率
16     double rate = capture.get(CV_CAP_PROP_FPS);
17     //当前视频帧
18     cv::Mat frame;
19     //每一帧之间的延时
20     //与视频的帧率相对应
21     int delay = 1000/rate;
22     bool stop(false);
23     while(!stop)
24     {
25         if(!capture.read(frame))
26         {
27             std::cout<<"no video frame"<<std::endl;
28             break;
29          }
30 
31         //此处为添加对视频的每一帧的操作方法
32         int frame_num = capture.get(CV_CAP_PROP_POS_FRAMES);
33         std::cout<<"Frame Num : "<<frame_num<<std::endl;
34         if(frame_num==20)
35         {
36             capture.set(CV_CAP_PROP_POS_FRAMES,10);
37         }
38 
39         cv::imshow("video",frame);
40         //引入延时
41         //也可通过按键停止
42         if(cv::waitKey(delay)>0)
43         stop = true;
44     }
45 
46 
47     //关闭视频,手动调用析构函数(非必须)
48     capture.release();
49     return 0;
50 }

 

Guess you like

Origin www.cnblogs.com/ybqjymy/p/12515938.html