Reputation: 165
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
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
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.
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)
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
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