首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么调用cv2. v4l ()会改变v4l摄像机的像素格式?

为什么调用cv2. v4l ()会改变v4l摄像机的像素格式?
EN

Stack Overflow用户
提问于 2017-03-14 15:36:49
回答 1查看 5.3K关注 0票数 1

问题:

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

问题描述:

我执行以下两项建议,在此之后,/dev/video2摄像机的像素格式为“Y8I”:

代码语言:javascript
复制
$ 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脚本:

代码语言:javascript
复制
#!/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‘相同):

代码语言:javascript
复制
$ 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)命令,像素格式就会改变。

代码语言:javascript
复制
$ 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()会改变像素格式吗?

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

Environment:

操作系统: Ubuntu 16.04

相机:英特尔(R) RealSense(TM) 410

司机: uvcvideo

驱动程序版本: 4.4.44

相机的可用像素格式:

代码语言:javascript
复制
$ 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)

相机的数据:

代码语言:javascript
复制
$ 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:

代码语言:javascript
复制
$ 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
EN

回答 1

Stack Overflow用户

发布于 2017-03-15 07:15:29

看来目前Y8I格式还没有在OpenCV视频模块中实现。见这里的源代码。下面的源代码提示了支持的格式:

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42790290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档