简体   繁体   English

即使请求的大小可用,也无法使用 opencv2 设置视频分辨率

[英]Unable to set video resolution using opencv2 even though requested size is available

I am only able to capture small images with my webcam using opencv2 even though I can see higher resoution using cheese and over the command line uvcdynctrl (see output at bottom).我只能使用我的网络摄像头使用 opencv2 捕获小图像,即使我可以使用 cheese 和命令行 uvcdynctrl 看到更高的分辨率(见底部的 output)。

Here is my python code which works on a raspberry pi, but not on my laptop.这是我的 python 代码,它适用于树莓派,但不适用于我的笔记本电脑。

import cv2
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(width, height)
### outputs 176.0 144.0

    (base) ~/code/CaptureQueen$ uvcdynctrl -f
Listing available frame formats for device video0:
Pixel format: MJPG (Motion-JPEG; MIME type: image/jpeg)
  Frame size: 160x120
    Frame rates: 30, 25, 20, 15, 10, 5
  Frame size: 176x144
    Frame rates: 30, 25, 20, 15, 10, 5
  Frame size: 320x240
    Frame rates: 30, 25, 20, 15, 10, 5
  Frame size: 352x288
    Frame rates: 30, 25, 20, 15, 10, 5
  Frame size: 640x480
    Frame rates: 30, 25, 20, 15, 10, 5
  Frame size: 800x600
    Frame rates: 15, 10, 5
Pixel format: YUYV (YUYV 4:2:2; MIME type: video/x-raw-yuv)
  Frame size: 160x120
    Frame rates: 15, 10, 5
  Frame size: 176x144
    Frame rates: 15, 10, 5

Thanks to @Rotem for suggesting the fix.感谢@Rotem 提出修复建议。 Code posted below for completness.为了完整起见,下面发布了代码。

import cv2
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG')
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(width, height)
### old output: 176.0 144.0
### new output: 640.0 480.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM