OpenCV VideoCapture類


OpenCV中通過VideoCaptrue類對視頻進行讀取操作以及調用攝像頭,下面是該類的API。

1.VideoCapture類的構造函數:

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

功能:創建一個VideoCapture類的實例,如果傳入對應的參數,可以直接打開視頻文件或者要調用的攝像頭。

參數:filename – 打開的視頻文件名。

device – 打開的視頻捕獲設備id ,如果只有一個攝像頭可以填0,表示打開默認的攝像頭。 

2.VideoCapture::open

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

功能:打開一個視頻文件或者打開一個捕獲視頻的設備(也就是攝像頭)

參數: filename – 打開的視頻文件名。

device – 打開的視頻捕獲設備id ,如果只有一個攝像頭可以填0,表示打開默認的攝像頭。

通過對VideoCapture類的構造函數和open函數分析,可以發現opencv讀入視頻的方法一般有如下兩種。比如讀取當前目錄下名為"dog.avi"的視頻文件,那么這兩種寫法分別如下。

(1)先實例化再初始化:

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

(2)在實例化的同時進行初始化:

VideoCapture("dog.avi");

3.VideoCapture::isOpened

bool VideoCapture::isOpened();

功能:判斷視頻讀取或者攝像頭調用是否成功,成功則返回true。

4.VideoCapture::release

void VideoCapture::release();

功能:關閉視頻文件或者攝像頭。

5.VideoCapture::grab

bool VideoCapture::grab();

功能:從視頻文件或捕獲設備中抓取下一個幀,假如調用成功返回true。(細節請參考opencv文檔說明)

6.VideoCapture::retrieve

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

功能:解碼並且返回剛剛抓取的視頻幀,假如沒有視頻幀被捕獲(相機沒有連接或者視頻文件中沒有更多的幀)將返回false。

7.VideoCapture::read

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

功能:該函數結合VideoCapture::grab()和VideoCapture::retrieve()其中之一被調用,用於捕獲、解碼和返回下一個視頻幀這是一個最方便的函數對於讀取視頻文件或者捕獲數據從解碼和返回剛剛捕獲的幀,假如沒有視頻幀被捕獲(相機沒有連接或者視頻文件中沒有更多的幀)將返回false。

從上面的API中我們會發現獲取視頻幀可以有多種方法 :

 1 // 方法一 
 2 capture.read(frame);  3 
 4 // 方法二 
 5 capture.grab();  6 
 7 // 方法三
 8 capture.retrieve(frame);  9 
10 // 方法四
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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM