Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query maximum webcam resolution in OpenCV

I'm dealing with several types of cameras and I need to know the maximum resolution each one is capable.

Is there a way to query such property in OpenCV?

If not, is there any other way? The application will work under Windows (by the moment) and all the project is being developed using C++.

like image 738
cbuchart Avatar asked Aug 27 '13 06:08

cbuchart


3 Answers

A trick that's working for me:

Just set to a very high resolution (above the capabilities of any usual capture device), then get the current resolution. You will see that the device will automatically switch to his maximum value.

Code example in Python with OpenCV 3.0:

HIGH_VALUE = 10000
WIDTH = HIGH_VALUE
HEIGHT = HIGH_VALUE

self.__capture = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
self.__capture.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
self.__capture.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)

width = int(self.__capture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(self.__capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

Hope it helps.

like image 161
user2949634 Avatar answered Sep 21 '22 21:09

user2949634


FINAL SOLUTION

As the accepted answered by user2949634 was written in Python, I'm posting the equivalent implementation in C++ for completeness:

void query_maximum_resolution(cv::VideoCapture* camera, int& max_width, int& max_height)
{
  // Save current resolution
  const int current_width  = static_cast<int>(camera->get(CV_CAP_PROP_FRAME_WIDTH));
  const int current_height = static_cast<int>(camera->get(CV_CAP_PROP_FRAME_HEIGHT));

  // Get maximum resolution
  camera->set(CV_CAP_PROP_FRAME_WIDTH,  10000);
  camera->set(CV_CAP_PROP_FRAME_HEIGHT, 10000);
  max_width  = static_cast<int>(camera->get(CV_CAP_PROP_FRAME_WIDTH));
  max_height = static_cast<int>(camera->get(CV_CAP_PROP_FRAME_HEIGHT));

  // Restore resolution
  camera->set(CV_CAP_PROP_FRAME_WIDTH,  current_width);
  camera->set(CV_CAP_PROP_FRAME_HEIGHT, current_height);
}
like image 38
cbuchart Avatar answered Sep 19 '22 21:09

cbuchart


VideoCapture::get(int propId)

Passing in CV_CAP_PROP_FRAME_WIDTH and CV_CAP_PROP_FRAME_HEIGHT will get you the resolution.

For getting the maximum possible resolution, all the functionality for cv::VideoCapture is in that link. There does not seem to be a possible way to do that directly, probably because many cameras expect you to know the possible resolutions from the manual and to set some flags to toggle what you want. One thing you can try is to keep a list of all common resolutions, then try all of them for each camera with VideoCapture::set while checking the return value to see if it was successful. There aren't many resolutions to search, so this should be viable.

like image 21
Matteo Mannino Avatar answered Sep 17 '22 21:09

Matteo Mannino