Camera resolution in OpenCV isn't accurate

Viewed 44

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

2 Answers

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)

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.

Related