Query maximum webcam resolution in OpenCV

时光怂恿深爱的人放手 提交于 2020-01-13 17:08:08

问题


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


回答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.




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



回答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.




回答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.




回答5:


This kind of hardware capabilities can be queried from USB devices if the camera is UVC compliant. It depends on the driver / firmware of the device. See for example these Microsoft requirements to guess what kind of support you can expect on Windows platforms.



来源:https://stackoverflow.com/questions/18458422/query-maximum-webcam-resolution-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!