您好,登錄后才能下訂單哦!
這篇文章主要介紹了C++ OpenCV攝像頭及視頻操作類VideoCapture怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
VideoCapture類
1.VideoCapture類的構(gòu)造函數(shù):
VideoCapture::VideoCapture();
VideoCapture::VideoCapture(const string& filename);
VideoCapture::VideoCapture(int device);
功能:創(chuàng)建一個VideoCapture類的實例,如果傳入對應(yīng)的參數(shù),可以直接打開視頻文件或者要調(diào)用的攝像頭。
參數(shù):
filename – 打開的視頻文件名。
device – 打開的視頻捕獲設(shè)備id ,如果只有一個攝像頭可以填0,表示打開默認的攝像頭。
2.VideoCapture::open
bool VideoCapture::open(const string& filename);
bool VideoCapture::open(int device);
功能:打開一個視頻文件或者打開一個捕獲視頻的設(shè)備(也就是攝像頭)
參數(shù):
filename – 打開的視頻文件名。
device – 打開的視頻捕獲設(shè)備id ,如果只有一個攝像頭可以填0,表示打開默認的攝像頭。
通過對VideoCapture類的構(gòu)造函數(shù)和open函數(shù)分析,可以發(fā)現(xiàn)opencv讀入視頻的方法一般有如下兩種。比如讀取當前目錄下名為"dog.avi"的視頻文件,那么這兩種寫法分別如下。
(1)先實例化再初始化:
VideoCapture capture;
capture.open("dog.avi");
(2)在實例化的同時進行初始化:
VideoCapture("dog.avi");
3.VideoCapture::isOpened
bool VideoCapture::isOpened();
功能:判斷視頻讀取或者攝像頭調(diào)用是否成功,成功則返回true。
4.VideoCapture::release
void VideoCapture::release();
功能:關(guān)閉視頻文件或者攝像頭。
5.VideoCapture::grab
bool VideoCapture::grab();
功能:從視頻文件或捕獲設(shè)備中抓取下一個幀,假如調(diào)用成功返回true。(細節(jié)請參考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);
功能:該函數(shù)結(jié)合VideoCapture::grab()和VideoCapture::retrieve()其中之一被調(diào)用,用于捕獲、解碼和返回下一個視頻幀這是一個最方便的函數(shù)對于讀取視頻文件或者捕獲數(shù)據(jù)從解碼和返回剛剛捕獲的幀,假如沒有視頻幀被捕獲(相機沒有連接或者視頻文件中沒有更多的幀)將返回false。
從上面的API中我們會發(fā)現(xiàn)獲取視頻幀可以有多種方法 :
// 方法一
capture.read(frame);
// 方法二
capture.grab();
// 方法三
capture.retrieve(frame);
// 方法四
capture >> frame;
8.VideoCapture::get
double VideoCapture::get(int propId);
功能:一個視頻有很多屬性,比如:幀率、總幀數(shù)、尺寸、格式等,VideoCapture的get方法可以獲取這些屬性。
參數(shù):屬性的ID。
屬性的ID可以是下面的之一:
CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
CV_CAP_PROP_FPS Frame rate.
CV_CAP_PROP_FOURCC 4-character code of codec.
CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
CV_CAP_PROP_HUE Hue of the image (only for cameras).
CV_CAP_PROP_GAIN Gain of the image (only for cameras).
CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
CV_CAP_PROP_WHITE_BALANCE Currently not supported
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)
功能:設(shè)置VideoCapture類的屬性,設(shè)置成功返回ture,失敗返回false。
參數(shù):第一個是屬性ID,第二個是該屬性要設(shè)置的值。
屬性ID如下:
CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
CV_CAP_PROP_FPS Frame rate.
CV_CAP_PROP_FOURCC 4-character code of codec.
CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
CV_CAP_PROP_HUE Hue of the image (only for cameras).
CV_CAP_PROP_GAIN Gain of the image (only for cameras).
CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
CV_CAP_PROP_WHITE_BALANCE Currently unsupported
CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
感謝你能夠認真閱讀完這篇文章,希望小編分享的“C++ OpenCV攝像頭及視頻操作類VideoCapture怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。