LoginSignup
7
10

More than 5 years have passed since last update.

OpenCV の VideoCapture で生フレームを取得する方法

Posted at

背景

Linux 環境で Motion JPEG 出力のカメラを繋いで cv2.VideoCapture すると、JPEG がデコードされて出力が得られるのですが、生の JPEG のままの画像を取得する方法が知りたくて、調べました。

生フレームを取得する方法

RGB への変換オプションを無効にすると、生の画像が取得できます。

>>> import cv2
>>> cap = cv2.VideoCapture(0)
>>> cap.get(cv2.CAP_PROP_CONVERT_RGB)
1.0
>>> cap.set(cv2.CAP_PROP_CONVERT_RGB, False)
>>> cap.get(cv2.CAP_PROP_CONVERT_RGB)
0.0
>>> cap.read()

処理の裏側

opencv/modules/videoio/src/cap_v4l.cpp を見ると、

static IplImage* icvRetrieveFrameCAM_V4L( CvCaptureCAM_V4L* capture, int) {
    // 前略
    if(!capture->convert_rgb) {
        capture->frame.imageData = (char*)capture->buffers[capture->bufferIndex].start;
        return &capture->frame;
    }
    // 後略: フォーマットに応じたデコード処理

RGB への変換処理が無効な場合、デコードせずに生の値を返してくれるようです。

参考

7
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10