Abdallah Barghouti
Abdallah Barghouti

Reputation: 165

Camera resolution in OpenCV isn't accurate

I found this code online to get the camera resolution that OpenCV detects:

import cv2
cap = cv2.VideoCapture(0)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print(width, height)

I got 640.0 480.0 although my computer's camera resolution is 720p (1280 720), why is that?
My laptop is Lenovo Legion Y530

Upvotes: 3

Views: 2526

Answers (3)

matanox
matanox

Reputation: 13686

Well, you probably worked it out by now.

You can use pyrav4l2 (or directly v4l2) for enumerating the streaming capabilities (format, resolution and fps) supported by each camera number that's exposed by the camera, to make sure that you are attempting your desired resolution against the camera number which supports it.

As mentioned, some physical cameras expose more than one device number, whereby each such device number may typically support different combinations of resolutions and other properties, hence you want to choose the right one for you (or you can brute-force iterating all numbers, till you hit the one which satisfies your desired resolution; this pragmatic approach saves your time from integrating with pyrav4l2 or using v4l2).

For example, in the original code posted, what you get is the resolution which OpenCV gets for camera device number 0, by default. You can use .set, as others have mentioned, to try overriding the default. Or you can open a different device number which supports a higher resolution, in case your camera exposes more than one device number, which is often the case.

Upvotes: 0

Yunus Temurlenk
Yunus Temurlenk

Reputation: 4352

First of all, here is the list which includes the properties you are trying to use. It says:

Reading / writing properties involves many layers. Some unexpected result might happens along this chain. Effective behaviour depends from device hardware, driver and API Backend.

There are 2 things to consider in your case.

  1. It seems your camera has 2 sub-streams: 1st is 1280x720 2nd is 640x480. it seems 0th index you are giving to the VideoCapture takes the 640x480 stream. According to my experiences, if there is no other connected camera device to the computer, in most of the times other sub-stream is assigned to the index -1, 1 or 2 like VideoCapture(-1)
  2. According to the quote I shared above: you can not expect OpenCV VideoCapture properties to be working fine for all cameras. It depends on the backends. If OpenCV backends supports your camera's backend then they will work othwerwise it will not.

If you still can not achieve to get the desired sub-stream, I suggest you to plug-unplug the camera and trying different indexes again. If the camera has its own SDK, using that will be the best way.

Upvotes: 3

Nguyễn Anh Bình
Nguyễn Anh Bình

Reputation: 198

Open CV allows you to customize the video capture parameters. Have you given it a try yet?

import cv2

cap = cv2.VideoCapture(0)
# set resolution to 1280x720
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

Upvotes: 7

Related Questions