您好,登錄后才能下訂單哦!
這篇文章主要介紹了C++ OpenCV攝像頭及視頻操作類VideoCapture怎么用,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
VideoCapture類
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中我們會發現獲取視頻幀可以有多種方法 :
// 方法一
capture.read(frame);
// 方法二
capture.grab();
// 方法三
capture.retrieve(frame);
// 方法四
capture >> frame;
8.VideoCapture::get
double VideoCapture::get(int propId);
功能:一個視頻有很多屬性,比如:幀率、總幀數、尺寸、格式等,VideoCapture的get方法可以獲取這些屬性。
參數:屬性的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)
功能:設置VideoCapture類的屬性,設置成功返回ture,失敗返回false。
參數:第一個是屬性ID,第二個是該屬性要設置的值。
屬性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怎么用”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。