python - 为什么调用 cv2.VideoCapture() 会改变 v4l 相机的像素格式?

标签 python opencv video-capture v4l2 pixelformat

问题:
只需调用 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

最佳答案

似乎目前 Y8I 格式没有在 OpenCV videoio 模块中实现。查看source code here .以下源代码提示支持的格式:

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;
}

关于python - 为什么调用 cv2.VideoCapture() 会改变 v4l 相机的像素格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42790290/

相关文章:

python - Pandas lambda 多个参数返回

python - 为 pandas 数据框中逗号分隔字符串中的每个项目添加 +1

c++ - 如何使用opencv框架将点[4]添加到轮廓

c++ - OpenCV C++ 中的 createBackgroundSubtractorKNN 参数是什么?

php - 使用 PHP 创建视频文件

video - VLC 输出到 DirectShow 源过滤器

python - 将数据插入 Pandas DataFrame 中,无需索引或列开销(因此无需连接或追加)

python - 离散傅立叶变换中的移位定理

opencv - 对 "cv::optflow::createOptFlow_DualTVL1()"的 undefined reference

python - 如何从图像采集卡中抓取帧?