Maximum OpenCV Python Webcam Resolution

I am trying to get images from my webcam using Python code that imports OpenCV. The code is as follows:

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 default resolution for the camera is 640x480, and my code seems to only be able to set resolution values โ€‹โ€‹below this. For example, I can set the image size to 320x240, but I cannot change it to 800x600. I have no error: just the resolution is set to the default value (640x480) as I try to set it to higher values.

The camera I use (no other webcam is connected to the computer) is QuickCam V-UBK45: with the software provided by Logitech, I can shoot in full resolution (1280x960) and in all intermediate ones (for example, 800x600).

Therefore, the sizes of these frames are supported from the hardware, but my code cannot access them.

Does anyone know what I can do?

+9
source share
5 answers

I used various permissions to set the image resolution from the General Permissions List by cycling

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

and this is what i got.

It seems that cv or my camera only allow certain permissions.

  • 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

+8
source

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

I am also running Logitech C270. For some reason, it will only show 640x480, even if the webcam supports 1280x720. The same problem persists with the built-in webcam on my laptop.

If I set it to 800x600 in code, it will display 640x480. However, if I set it to 1024x768, it becomes 800x600. And if I install it on something stupid, like 2000x2000, it will become 1280x720.

This is in C ++ in OpenCV 3.0, but perhaps this also applies to Python.

+5
source

For cv2, just change to this.

  cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600) 
+2
source

I have the same problem as yours that I cannot get higher resolution in opencv another "application". But with Logitech software, I can record video in 720p format (C270 camera). After several days of research, I came to the same explanation as @break that they limited the resolution in the driver. I threw it and bought another, not Logitech, one ...

0
source

The problem mentioned above is caused by the camera driver. I managed to fix it using Direct Show as a backend. I read (sorry, but I donโ€™t remember where) that almost all cameras provide a driver that allows them to be used from DirectShow. Therefore, I used DirectShow on Windows to interact with cameras, and I was able to adjust the resolution the way I wanted, and also get the original aspect ratio of my camera (16: 9). You can try this code to see if it 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 learn more about OpenCV backends ( OpenCV docs )

I hope this helps you!

0
source

Source: https://habr.com/ru/post/956231/


All Articles