VideoCaptrue クラスはビデオを読み取り、カメラを呼び出します。
ヘッド ファイル:
#include <opencv2/video.hpp>
主な機能は次のとおりです。
コンストラクタ
C++: VideoCapture::VideoCapture();
C++: VideoCapture::VideoCapture(const string& filename);
C++: VideoCapture::VideoCapture(int device);
パラメータ:
filename – 開かれたビデオ ファイルの名前。
device – 開かれたビデオ キャプチャ デバイスの ID カメラが 1 つだけの場合は、デフォルトのカメラが開かれていることを示す 0 を入力します。
基本的なスキル
ビデオ ファイルまたはデバイスを開きます
C++: bool VideoCapture::open(const string& filename);
C++: bool VideoCapture::open(int device);
ビデオ ファイルを開くか、ビデオをキャプチャするデバイス (カメラなど) を開きます。
パラメータ:
filename – 開かれたビデオ ファイルの名前。
device – 開かれたビデオ キャプチャ デバイスの ID カメラが 1 つだけの場合は、デフォルトのカメラが開かれていることを示す 0 を入力します。
オープンが成功したかどうかを判断する
C++: bool VideoCapture::isOpened();
成功した場合は true を返し、それ以外の場合は false を返します。
ビデオ ファイルまたはカメラを閉じます
C++: void VideoCapture::release();
次のフレームを取得します
C++: bool VideoCapture::grab();//需与retrieve结合使用
C++: bool VideoCapture::retrieve(Mat& image, int channel=0);
C++: VideoCapture& VideoCapture::operator>>(Mat& image);
C++: bool VideoCapture::read(Mat& image);
ビデオのプロパティを取得する
C++: double VideoCapture::get(int propId);
属性がサポートされていない場合は、0 が返されます。
パラメータ: 属性の 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)
プロパティを設定する
bool VideoCapture::set(int propertyId, double value)
成功した場合は true を返し、それ以外の場合は false を返します
パラメータ: 1 つ目は属性 ID、2 つ目は属性に設定する値です。
属性 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)
例: 任意のフレームを取得する
1 例と初期化:
cv::VideoCapture capture.open("Old Film Effect.mp4");
2 フレームを読み取る必要がある位置を設定します。
capture.set(cv::CAP_PROP_POS_FRAMES,10);//设置读取第10帧
3 フレームの読み取り
Mat frame;
if (capture.read(frame))
imwrite("d:/a.bmp",frame);