OpenCV 3.2 with Python 3 VideoCapture changes settings of v4l2

有些话、适合烂在心里 提交于 2019-12-24 08:48:54

问题


I worked with OpenCV 3.2 with Python3 and SBC OXU4. I have a true 5MPx web-camera connected to SBC. I want to take from this camera 2592x1944 resolution picture. If I use Cheese I can take picture with this resolution. I can save pictures with command line program streamer -t 4 -r 4 -s 2592x1944 -o b0.jpeg But when I take picture with OpenCV3.2 like this:

#!/usr/bin/env python3
 import cv2
 import os
 import time
 capture1 = cv2.VideoCapture(2)
 if capture1.isOpened():
   capture1.set(3, 2592)
   capture1.set(4, 1944)
   s, img = capture1.read()
   if not img.all():
    cv2.imwrite('test1.jpg', img)
    print('image done!')
else:
 print('cant open camera')

I will see 1920x1080 photo resolution. Also I have inspected v4l2-ctls

 v4l2-ctl --list-formats-ext
 ioctl: VIDIOC_ENUM_FMT
Index       : 0
Type        : Video Capture
Pixel Format: 'YUYV'
Name        : YUYV 4:2:2
    Size: Discrete 2592x1944
        Interval: Discrete 0.267s (3.750 fps)
    Size: Discrete 1920x1080
        Interval: Discrete 0.133s (7.500 fps)
    Size: Discrete 1280x960
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 1280x720
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 640x480
        Interval: Discrete 0.017s (60.000 fps)
        Interval: Discrete 0.033s (30.000 fps)
    Size: Discrete 320x240
        Interval: Discrete 0.008s (120.000 fps)
        Interval: Discrete 0.011s (90.000 fps)
        Interval: Discrete 0.017s (60.000 fps)
        Interval: Discrete 0.033s (30.000 fps)

Index       : 1
Type        : Video Capture
Pixel Format: 'MJPG' (compressed)
Name        : Motion-JPEG
    Size: Discrete 1920x1080
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 1280x720
        Interval: Discrete 0.022s (45.000 fps)
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)
    Size: Discrete 640x480
        Interval: Discrete 0.033s (30.000 fps)
        Interval: Discrete 0.067s (15.000 fps)

Active camera settings

v4l2-ctl --all
Driver Info (not using libv4l2):
Driver name   : uvcvideo
Card type     : oCam
Bus info      : usb-12110000.usb-1
Driver version: 4.14.37
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      : 1920/1080
Pixel Format      : 'MJPG'
Field             : None
Bytes per Line    : 0
Size Image        : 10077696
Colorspace        : Default
Transfer Function : Default (maps to Rec. 709)
YCbCr/HSV Encoding: Default (maps to ITU-R 601)
Quantization      : Default (maps to Full Range)
Flags             : 
Crop Capability Video Capture:
Bounds      : Left 0, Top 0, Width 1920, Height 1080
Default     : Left 0, Top 0, Width 1920, Height 1080
Pixel Aspect: 1/1
Selection: crop_default, Left 0, Top 0, Width 1920, Height 1080
Selection: crop_bounds, Left 0, Top 0, Width 1920, Height 1080
Streaming Parameters Video Capture:
Capabilities     : timeperframe
Frames per second: 30.000 (30/1)
Read buffers     : 0

In fact I see that we have default settings as MJPG 1920X1080. After that I tried to use v4l2-ctl --set-fmt-video=width=2592,height=1936,pixelformat=YUYV and with v4l2-ctl --all I saw that params are sets to:

Driver name   : uvcvideo
Card type     : oCam
Bus info      : usb-12110000.usb-1
Driver version: 4.14.37
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      : 2592/1944
Pixel Format      : 'YUYV'
Field             : None
Bytes per Line    : 5184
Size Image        : 10077696
Colorspace        : Default
Transfer Function : Default (maps to Rec. 709)
YCbCr/HSV Encoding: Default (maps to ITU-R 601)
Quantization      : Default (maps to Limited Range)
Flags    

But when I start my script this params resets and I take 1920x1080 image and see on v4l2-ctl --all that its MJPG 1920x1080. As I understood cap=cv2.VideoCapture(0) resets settings of v4l2 device to default before working. How can I tune default settings to YUYV 2952x1944 with which I can take an image? Also why cap.set(cv2.CAP_PROP_FRAME_WIDTH) doesn't work? Because OpenCV identified max resolution like MJPG 1920?


回答1:


OpenCV automatically selects the first available capture backend (see here). It can be that it is not using V4L2 automatically.

Also set both -D WITH_V4L=ON and -D WITH_LIBV4L=ON when building.

(Unrelated to the question) In order to set the pixel format to be used set the CAP_PROP_FOURCC property of the capture:

capture = cv2.VideoCapture(cam_id, cv2.CAP_V4L2)
capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))


来源:https://stackoverflow.com/questions/50470593/opencv-3-2-with-python-3-videocapture-changes-settings-of-v4l2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!