1

问题:
只需调用cv2.VideoCapture()- 更改摄像机上的像素格式。

问题描述:
我执行了以下两个命令,之后我/dev/video2的相机的像素格式是' Y8I':

$ v4l2-ctl -d2 --set-fmt-video=width=424,height=240,pixelformat=2
$ v4l2-ctl -d2 --get-fmt-video | grep 'Pixel Format'
        Pixel Format      : 'Y8I '

如果我执行以下 MCVE Python 脚本:

#!/usr/bin/env python

import subprocess
import cv2

def query_current_format(video_device_index):
    command = "v4l2-ctl -d %d -V" % video_device_index
    output = subprocess.call(command, shell=True)

if __name__ == "__main__":
    #cap = cv2.VideoCapture(2)
    query_current_format(2)

我得到了预期的结果(即像素格式为 ' Y8I' 符合预期):

$ python show_current_format.SO.py
Format Video Capture:
        Width/Height      : 424/240
        Pixel Format      : 'Y8I '
        Field             : None
        Bytes per Line    : 848
        Size Image        : 203520
        Colorspace        : Default
        Transfer Function : Default
        YCbCr Encoding    : Default
        Quantization      : Default
        Flags             :


但是,如果我取消注释cv2.VideoCapture(2)命令,像素格式会发生变化

$ v4l2-ctl -d2 --set-fmt-video=width=424,height=240,pixelformat=2
$ v4l2-ctl -d2 --get-fmt-video | grep 'Pixel Format'
        Pixel Format      : 'Y8I '
$ cat show_current_format.SO.py
#!/usr/bin/env python

import subprocess
import cv2

def query_current_format(video_device_index):
    command = "v4l2-ctl -d %d -V" % video_device_index
    output = subprocess.call(command, shell=True)

if __name__ == "__main__":
    cap = cv2.VideoCapture(2)
    query_current_format(2)
$ python show_current_format.SO.py
Format Video Capture:
        Width/Height      : 640/480
        Pixel Format      : 'UYVY'
        Field             : None
        Bytes per Line    : 1280
        Size Image        : 614400
        Colorspace        : Default
        Transfer Function : Default
        YCbCr Encoding    : Default
        Quantization      : Default
        Flags             :

谁能解释为什么调用cv2.VideoCapture()会改变像素格式?


是这样的格式将是唯一可用的非专有格式吗?


环境:
操作系统:Ubuntu 16.04
摄像头:Intel(R) RealSense(TM) 410
驱动程序:uvcvideo
驱动程序版本:4.4.44

相机可用的像素格式:

$ v4l2-ctl --list-formats -d 2
ioctl: VIDIOC_ENUM_FMT
        Index       : 0
        Type        : Video Capture
        Pixel Format: ''
        Name        : 00000032-0000-0010-8000-00aa003

        Index       : 1
        Type        : Video Capture
        Pixel Format: 'UYVY'
        Name        : UYVY 4:2:2

        Index       : 2
        Type        : Video Capture
        Pixel Format: 'Y8I '
        Name        : Greyscale 8 L/R (Y8I)

        Index       : 3
        Type        : Video Capture
        Pixel Format: 'Y12I'
        Name        : Greyscale 12 L/R (Y12I)

相机数据:

$ v4l2-ctl --all -d 2
Driver Info (not using libv4l2):
        Driver name   : uvcvideo
        Card type     : Intel(R) RealSense(TM) 410
        Bus info      : usb-0000:00:14.0-2
        Driver version: 4.4.44
        Capabilities  : 0x84200001
                Video Capture
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps   : 0x04200001
                Video Capture
                Streaming
                Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
        Width/Height      : 424/240
        Pixel Format      : 'Y8I '
        Field             : None
        Bytes per Line    : 848
        Size Image        : 203520
        Colorspace        : Default
        Transfer Function : Default
        YCbCr Encoding    : Default
        Quantization      : Default
        Flags             :
Crop Capability Video Capture:
        Bounds      : Left 0, Top 0, Width 424, Height 240
        Default     : Left 0, Top 0, Width 424, Height 240
        Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 424, Height 240
Selection: crop_bounds, Left 0, Top 0, Width 424, Height 240
Streaming Parameters Video Capture:
        Capabilities     : timeperframe
        Frames per second: 90.000 (90/1)
        Read buffers     : 0
 white_balance_temperature_auto (bool)   : default=0 value=0
                           gain (int)    : min=16 max=248 step=1 default=16 value=16
      white_balance_temperature (int)    : min=2800 max=6500 step=1 default=2800 value=2800
                  exposure_auto (menu)   : min=0 max=3 default=1 value=1
              exposure_absolute (int)    : min=0 max=1000 step=1 default=10 value=10
         exposure_auto_priority (bool)   : default=0 value=0

编辑1:

$ v4l2-ctl -d1 --set-fmt-video=width=424,height=240,pixelformat=0
$ v4l2-ctl -d1 --get-fmt-video | grep 'Pixel Format'                   
Pixel Format      : 'Z16 '
$ cat show_current_format.SO.py
#!/usr/bin/env python

import subprocess
import cv2

def query_current_format(video_device_index):
    command = "v4l2-ctl -d %d -V" % video_device_index
    output = subprocess.call(command, shell=True)

if __name__ == "__main__":
    cap = cv2.VideoCapture(1)
    query_current_format(1)
openstack@prclnx04:~/python/opencv$ python show_current_format.SO.py
libv4l2: error set_fmt gave us a different result then try_fmt!
HIGHGUI ERROR: libv4l unable convert to requested pixfmt
HIGHGUI ERROR: libv4l unable to ioctl VIDIOCSPICT

Format Video Capture:
        Width/Height      : 424/240
        Pixel Format      : ''
        Field             : None
        Bytes per Line    : 848
        Size Image        : 203520
        Colorspace        : Default
        Transfer Function : Default
        YCbCr Encoding    : Default
        Quantization      : Default
        Flags             :
$ v4l2-ctl --list-formats -d 1
ioctl: VIDIOC_ENUM_FMT
        Index       : 0
        Type        : Video Capture
        Pixel Format: 'Z16 '
        Name        : Depth data 16-bit (Z16)

        Index       : 1
        Type        : Video Capture
        Pixel Format: ''
        Name        : 00000050-0000-0010-8000-00aa003

$ v4l2-ctl --all -d 1
Driver Info (not using libv4l2):
        Driver name   : uvcvideo
        Card type     : Intel(R) RealSense(TM) 410
        Bus info      : usb-0000:00:14.0-2
        Driver version: 4.4.44
        Capabilities  : 0x84200001
                Video Capture
                Streaming
                Extended Pix Format
                Device Capabilities
        Device Caps   : 0x04200001
                Video Capture
                Streaming
                Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
        Width/Height      : 424/240
        Pixel Format      : ''
        Field             : None
        Bytes per Line    : 848
        Size Image        : 203520
        Colorspace        : Default
        Transfer Function : Default
        YCbCr Encoding    : Default
        Quantization      : Default
        Flags             :
Crop Capability Video Capture:
        Bounds      : Left 0, Top 0, Width 424, Height 240
        Default     : Left 0, Top 0, Width 424, Height 240
        Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 424, Height 240
Selection: crop_bounds, Left 0, Top 0, Width 424, Height 240
Streaming Parameters Video Capture:
        Capabilities     : timeperframe
        Frames per second: 90.000 (90/1)
        Read buffers     : 0
 white_balance_temperature_auto (bool)   : default=0 value=0
                           gain (int)    : min=16 max=248 step=1 default=16 value=16
      white_balance_temperature (int)    : min=2800 max=6500 step=1 default=2800 value=2800
                  exposure_auto (menu)   : min=0 max=3 default=1 value=1
              exposure_absolute (int)    : min=0 max=1000 step=1 default=10 value=10
         exposure_auto_priority (bool)   : default=0 value=0
4

1 回答 1

1

似乎目前 Y8I 格式未在 OpenCV videoio 模块中实现。请参阅此处的源代码。以下源代码提示支持的格式:

static int autosetup_capture_mode_v4l2(CvCaptureCAM_V4L* capture) {
    //in case palette is already set and works, no need to setup.
    if(capture->palette != 0 and try_palette_v4l2(capture)){
        return 0;
    }
    __u32 try_order[] = {
            V4L2_PIX_FMT_BGR24,
            V4L2_PIX_FMT_YVU420,
            V4L2_PIX_FMT_YUV411P,
#ifdef HAVE_JPEG
            V4L2_PIX_FMT_MJPEG,
            V4L2_PIX_FMT_JPEG,
#endif
            V4L2_PIX_FMT_YUYV,
            V4L2_PIX_FMT_UYVY,
            V4L2_PIX_FMT_SN9C10X,
            V4L2_PIX_FMT_SBGGR8,
            V4L2_PIX_FMT_SGBRG8,
            V4L2_PIX_FMT_RGB24,
            V4L2_PIX_FMT_Y16
    };

    for (size_t i = 0; i < sizeof(try_order) / sizeof(__u32); i++) {
        capture->palette = try_order[i];
        if (try_palette_v4l2(capture)) {
            return 0;
        }
    }

    fprintf(stderr,
            "VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV\n");
    icvCloseCAM_V4L(capture);
    return -1;
}
于 2017-03-15T07:15:29.250 回答