Python OpenCV 访问网络摄像头最大分辨率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19448078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:45:52  来源:igfitidea点击:

Python OpenCV access webcam maximum resolution

pythonopencvwebcamresolutionlogitech

提问by Ste

I am trying to acquire images from my webcam using a python code that imports OpenCV. The code is the following:

我正在尝试使用导入 OpenCV 的 Python 代码从我的网络摄像头获取图像。代码如下:

import sys
sys.path.append("C:\opencv\build\python\2.7")
import cv2
import cv2.cv as cv
import time

# Set resolution
cap = cv2.VideoCapture(0)
print "Frame default resolution: (" + str(cap.get(cv.CV_CAP_PROP_FRAME_WIDTH)) + "; " + str(cap.get(cv.CV_CAP_PROP_FRAME_HEIGHT)) + ")"
cap.set(cv.CV_CAP_PROP_FRAME_WIDTH, 800)
cap.set(cv.CV_CAP_PROP_FRAME_HEIGHT, 600)
print "Frame resolution set to: (" + str(cap.get(cv.CV_CAP_PROP_FRAME_WIDTH)) + "; " + str(cap.get(cv.CV_CAP_PROP_FRAME_HEIGHT)) + ")"

# Acquire frame
capture = cv.CreateCameraCapture(0)
img = cv.QueryFrame(capture)

The code works fine, except that the Camera default resolution is 640x480, and my code seems to be able to set only resolution values lower than that. For example, I can set the image size to 320x240, but I can't change it to 800x600. I have no error appearing: simply the resolution is set to the default one (640x480) as I try to set it to higher values.

该代码工作正常,除了相机默认分辨率为 640x480,而且我的代码似乎只能设置低于该值的分辨率值。例如,我可以将图像大小设置为 320x240,但我无法将其更改为 800x600。我没有出现错误:只是将分辨率设置为默认值 (640x480),因为我尝试将其设置为更高的值。

The camera I am using (no other webcam is connected to the computer) is the QuickCam V-UBK45: with the software provided by Logitech, I am able to take pictures at full resolution (1280x960) and at all intermediate ones (e.g. 800x600).

我使用的相机(没有其他网络摄像头连接到计算机)是 QuickCam V-UBK45:使用罗技提供的软件,我能够以全分辨率(1280x960)和所有中间分辨率(例如 800x600)拍照.

Therefore, those frame sizes are supported from the hardware, but my code can't access them.

因此,硬件支持这些帧大小,但我的代码无法访问它们。

Does anyone know what I can do?

有谁知道我能做什么?

回答by DBS06

I have the same problem as you, that I cant get a higher resolution in opencvdifferent "application". But with the Logitech Software I am able to record videos in 720p (camera C270 ). After some days of research, I came to the same explanation as @break that they restricted the resolution in the driver. I gave it up and will buy a different, non Logitech, one...

我和你有同样的问题,我无法在opencv不同的“应用程序”中获得更高的分辨率。但是使用罗技软件,我能够以 720p(相机 C270)录制视频。经过几天的研究,我得出了与@break 相同的解释,即他们限制了驱动程序中的分辨率。我放弃了,会买一个不同的,非罗技,一个......

回答by duggi

I used the different resolutions to set image resolution from List of common resolutionsby looping over

我使用不同的分辨率通过循环从常用分辨率列表中设置图像分辨率

def set_res(cap, x,y):
    cap.set(cv.CV_CAP_PROP_FRAME_WIDTH, int(x))
    cap.set(cv.CV_CAP_PROP_FRAME_HEIGHT, int(y))
    return str(cap.get(cv.CV_CAP_PROP_FRAME_WIDTH)),str(cap.get(cv.CV_CAP_PROP_FRAME_HEIGHT))

It seems that OpenCV or my camera allows only certain resolutions.

似乎 OpenCV 或我的相机只允许某些分辨率。

  • 160.0 x 120.0

  • 176.0 x 144.0

  • 320.0 x 240.0

  • 352.0 x 288.0

  • 640.0 x 480.0

  • 1024.0 x 768.0

  • 1280.0 x 1024.0

  • 160.0 x 120.0

  • 176.0 x 144.0

  • 320.0 x 240.0

  • 352.0 x 288.0

  • 640.0 x 480.0

  • 1024.0 x 768.0

  • 1280.0 x 1024.0

回答by ubehagelig

I got it to work, so this post is for others experiencing the same problem:

我让它工作了,所以这篇文章是为遇到同样问题的其他人准备的:

I am running on the Logitech C270 as well. For some reason it would only show 640x480 even though the webcam supports 1280x720. Same issue persists with the built-in webcam in my laptop.

我也在 Logitech C270 上运行。出于某种原因,即使网络摄像头支持 1280x720,它也只会显示 640x480。我的笔记本电脑中的内置网络摄像头仍然存在同样的问题。

If I set it to 800x600 in the code it shows 640x480. However, if I set it to 1024x768 it becomes 800x600. And if I set it to something silly like 2000x2000 it becomes 1280x720.

如果我在代码中将其设置为 800x600,它会显示 640x480。但是,如果我将其设置为 1024x768,它将变为 800x600。如果我将它设置为 2000x2000 之类的愚蠢内容,它就会变成 1280x720。

This is in C++ on OpenCV 3.0, but perhaps it applies to Python as well.

这是在 OpenCV 3.0 上的 C++ 中,但也许它也适用于 Python。

回答by Trent Cook TragicXxBonesxX

For cv2 just change to this.

对于 cv2 只需更改为此。

    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)

回答by JoeyGutMen

The problem as mentioned above is caused by the camera driver. I was able to fix it using Direct Show as a backend. I read (sorry, but I do not remember where) that almost all cameras provide a driver that allows their use from DirectShow. Therefore, I used DirectShow in Windows to interact with the cameras and I was able to configure the resolution as I wanted and also get the native aspect ratio of my camera (16: 9). You can try this code to see if this works for you.

上面提到的问题是由相机驱动程序引起的。我能够使用 Direct Show 作为后端来修复它。我读到(对不起,但我不记得在哪里)几乎所有相机都提供了一个驱动程序,允许它们从 DirectShow 使用。因此,我在 Windows 中使用 DirectShow 与相机进行交互,我能够根据需要配置分辨率并获得相机的原始纵横比 (16:9)。你可以试试这个代码,看看它是否适合你。

import cv2

cam = cv2.VideoCapture(0,cv2.CAP_DSHOW)

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

r, frame = cam.read()
...
print('Resolution: ' + str(frame.shape[0]) + ' x ' + str(frame.shape[1]))

In the OpenCV documentation, I found the following information for those who want to know more about OpenCV backends (OpenCV docs)

在 OpenCV 文档中,我为那些想了解更多 OpenCV 后端(OpenCV 文档)的人找到了以下信息

I hope this can help you!

我希望这可以帮助你!