3.2使用Python3视频捕获更改设置 [英] 3. 2 with Python 3 VideoCapture changes settings of
本文介绍了3.2使用Python3视频捕获更改设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是OpenCV 3.2和Python3以及SBC OXU4。我有一个真正的5MPx网络摄像头连接到SBC。我想用这台相机拍一张2592x1944分辨率的照片。如果我用奶酪,我可以用这个分辨率拍照。我可以用命令行程序streamer -t 4 -r 4 -s 2592x1944 -o b0.jpeg
保存图片,但当我用OpenCV3.2拍摄图片时,如下所示:
#!/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')
我将看到1920x1080的照片分辨率。 我还检查了V4L2-CTL
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)
活动摄像头设置
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
事实上,我看到我们的默认设置为MJPG 1920x1080。之后,我尝试使用v4l2-ctl --set-fmt-video=width=2592,height=1936,pixelformat=YUYV
和v4l2-ctl --all
,我看到参数设置为:
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
但当我开始我的脚本时,这个参数会重置,我拍摄1920x1080图像,然后在v4l2-ctl --all
上看到它的MJPG 1920x1080。据我所知,cap=cv2.VideoCapture(0)
在工作前将V4L2设备的设置重置为默认设置。如何将默认设置调到YUYV 2952x1944,以便拍摄图像?还有为什么cap.set(cv2.CAP_PROP_FRAME_WIDTH)
不起作用?因为OpenCV识别出了像MJPG 1920那样的最大分辨率?
推荐答案
OpenCV自动选择第一个可用的捕获后端(see here)。可能是因为它没有自动使用V4L2。
还可以在构建时同时设置-D WITH_V4L=ON
和-D WITH_LIBV4L=ON
。
(与问题无关)要设置要使用的像素格式,请设置捕获的CAP_PROP_FOURCC
属性:
capture = cv2.VideoCapture(cam_id, cv2.CAP_V4L2)
capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
这篇关于3.2使用Python3视频捕获更改设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文