Query maximum webcam resolution in OpenCV

13,549

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

Searching on same topic myself : As previously answered there is no direct property, but you could find supported resolutions trying to figure out accepted camera resolutions :

  1. trying all possible common resolutions
  2. probing minimum resolution and increasing width/height

Here C++ OpenCV 2.4.8/Windows tested code sample

trying common resolutions solution :

const CvSize CommonResolutions[] = {
  cvSize(120, 90),
  cvSize(352, 240),
  cvSize(352, 288),
  // and so on
  cvSize(8192, 4608)
};

vector<CvSize> getSupportedResolutions(VideoCapture camera)
{
  vector<CvSize> supportedVideoResolutions;
  int nbTests = sizeof(CommonResolutions) / sizeof(CommonResolutions[0]);

  for (int i = 0; i < nbTests; i++) {
    CvSize test = CommonResolutions[i];

    // try to set resolution
    camera.set(CV_CAP_PROP_FRAME_WIDTH, test.width);
    camera.set(CV_CAP_PROP_FRAME_HEIGHT, test.height);

    double width = camera.get(CV_CAP_PROP_FRAME_WIDTH);
    double height = camera.get(CV_CAP_PROP_FRAME_HEIGHT);

    if (test.width == width && test.height == height) {
      supportedVideoResolutions.push_back(test);
    }
  }

  return supportedVideoResolutions;
}

Probing solution based on width increment :

vector<CvSize> getSupportedResolutionsProbing(VideoCapture camera)
{
    vector<CvSize> supportedVideoResolutions;

    int step = 100;
    double minimumWidth = 16; // Microvision
    double maximumWidth = 1920 + step; // 1080

    CvSize currentSize = cvSize(minimumWidth, 1);
    CvSize previousSize = currentSize;

    while (1) {
        camera.set(CV_CAP_PROP_FRAME_WIDTH, currentSize.width);
        camera.set(CV_CAP_PROP_FRAME_HEIGHT, currentSize.height);

        CvSize cameraResolution = cvSize(
            camera.get(CV_CAP_PROP_FRAME_WIDTH),
            camera.get(CV_CAP_PROP_FRAME_HEIGHT));


        if (cameraResolution.width == previousSize.width 
                 && cameraResolution.height == previousSize.height)
        {
            supportedVideoResolutions.push_back(cameraResolution);
            currentSize = previousSize = cameraResolution;
        }
        currentSize.width += step;
        if (currentSize.width > maximumWidth)
        {
            break;
        }
    }

    return supportedVideoResolutions;
}

I hope this will be helpful for futur users.

Solution 5

For Ubuntu: install

sudo apt install v4l-utils

And then, run:

v4l2-ctl -d /dev/video0 --list-formats-ext
Share:
13,549
cbuchart
Author by

cbuchart

Senior C++ Software Engineer @ Wallbox Invited Professor @ TECNUN Author of HeaderFiles.com (in Spanish) std::vector&lt;std::string&gt; interests = { "C++", "algorithm performance", "computational geometry", "user interfaces design", "memory management", };

Updated on June 04, 2022

Comments

  • cbuchart
    cbuchart almost 2 years

    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++.

  • cbuchart
    cbuchart over 10 years
    Thank you. This is as far the only way of do it I've also found using OpenCV. Is there another way to determine it? maybe using DShow?
  • Matteo Mannino
    Matteo Mannino over 10 years
    I'm not sure about DShow, but it seems promising. Also, if you know the protocol, then maybe. For example for IEEE 1394 (firewire), look into libdc1394 structures. OpenCV uses it as a backend for IEEE1394 cameras. I would look at DShow first, though, possibly asking another question directed towards DShow users.
  • cbuchart
    cbuchart about 7 years
    Worked perfectly, at least with the webcams I have access to. Thanks and sorry for the late answer.
  • Everyone
    Everyone about 6 years
    Long but useful. +1 for the effort put in this.
  • Rahat Zaman
    Rahat Zaman over 3 years
    The order of the width and height setting mattered for me. When I set height followed by width, the resolution is set to 800x640. And when I set width followed by height, the resolution is 1024x768.
  • Rahat Zaman
    Rahat Zaman over 3 years
    The order of the width and height setting mattered for me. When I set height followed by width, the resolution is set to 800x640. And when I set width followed by height, the resolution is 1024x768.