boardrider
boardrider

Reputation: 6185

Why calling cv2.VideoCapture() changes the pixel format of a v4l camera?

The problem:
Just calling cv2.VideoCapture() - changes the pixel format on a video camera.

Problem description:
I execute the following two commends, after which my /dev/video2 camera's pixel format is '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 '

and if I execute the following MCVE Python script:

#!/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)

I get the expected result (namely, the pixel format is 'Y8I ' as expected):

$ 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             :


However, if I un-comment the cv2.VideoCapture(2) command, the pixel format changes:

$ 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             :

Can anyone explain why calling cv2.VideoCapture() changes the pixel format?


Is that so the format would be the only available non-proprietary format?


Environment:
OS: Ubuntu 16.04
Camera: Intel(R) RealSense(TM) 410
Driver: uvcvideo
Driver version: 4.4.44

Camera's available pixel formats:

$ 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)

Camera's data:

$ 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

Edit 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

Upvotes: 1

Views: 6349

Answers (1)

Alper Kucukkomurler
Alper Kucukkomurler

Reputation: 1794

Seems like currently Y8I format is not implemented in OpenCV videoio module. See the source code here. The following source piece hints at supported formats:

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

Upvotes: 1

Related Questions