Python OpenCV access webcam maximum resolution

大兔子大兔子 提交于 2020-04-29 10:49:33

问题


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

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.

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

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

Does anyone know what I can do?


回答1:


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.

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)

I hope this can help you!




回答2:


I used the different resolutions to set image resolution from List of common resolutions by 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.

  • 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




回答3:


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.

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.

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




回答4:


For cv2 just change to this.

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



回答5:


I have the same problem as you, that I cant get a higher resolution in opencv different "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...



来源:https://stackoverflow.com/questions/19448078/python-opencv-access-webcam-maximum-resolution

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