OpenCV:获取相机分辨率 C++

mar*_*z-s 5 c++ opencv resolutions

OpenCV 中是否有任何方法可以获取相机分辨率列表?

And*_*dov 4

对于 Windows,您可以使用以下代码枚举所有相机和分辨率:

\n\n
#include <dshow.h>\n#include <locale>\n#include <vector>\nusing namespace std;\n\n#define BLUE    0x0001\n#define GREEN   0x0002\n#define RED     0x0004\n#define GRAY    0x0007\n\nstatic void setcolor(unsigned int color)                                                                                                         \n{\n    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); \n    SetConsoleTextAttribute(hCon,color|FOREGROUND_INTENSITY);\n}\n\nvoid _FreeMediaType(AM_MEDIA_TYPE& mt)\n{\n    if (mt.cbFormat != 0)\n    {\n        CoTaskMemFree((PVOID)mt.pbFormat);\n        mt.cbFormat = 0;\n        mt.pbFormat = NULL;\n    }\n    if (mt.pUnk != NULL)\n    {\n        // pUnk should not be used.\n        mt.pUnk->Release();\n        mt.pUnk = NULL;\n    }\n}\n\n\nHRESULT CamCaps(IBaseFilter *pBaseFilter)\n{\n    HRESULT hr = 0;\n    vector<IPin*> pins;\n    IEnumPins *EnumPins;\n    pBaseFilter->EnumPins(&EnumPins);\n    pins.clear();\n    for(;;)\n    {\n        IPin *pin;\n        hr=EnumPins->Next(1,&pin,NULL);\n        if(hr!=S_OK){break;}\n        pins.push_back(pin);\n        pin->Release();\n    }   \n    EnumPins->Release();\n\n    printf("Device pins number: %d\\n",pins.size());\n\n    PIN_INFO pInfo; \n    for(int i=0;i<pins.size();i++)\n    {\n        pins[i]->QueryPinInfo(&pInfo);\n\n        setcolor(RED);\n\n        if(pInfo.dir==0)\n        {\n            wprintf(L"Pin name: %s (\xd0\x92\xd0\xb2\xd0\xbe\xd0\xb4)\\n",pInfo.achName);\n        }\n\n        if(pInfo.dir==1)\n        {\n            wprintf(L"Pin name: %s (\xd0\x92\xd1\x8b\xd1\x85\xd0\xbe\xd0\xb4)\\n",pInfo.achName);\n        }\n\n        IEnumMediaTypes *emt=NULL;\n        pins[i]->EnumMediaTypes(&emt);\n\n        AM_MEDIA_TYPE *pmt;\n\n        vector<SIZE> modes;\n        setcolor(GRAY);\n        wprintf(L"Avialable resolutions.\\n",pInfo.achName);\n        for(;;)\n        {   \n            hr=emt->Next(1,&pmt,NULL);\n            if(hr!=S_OK){break;}\n\n            if ( (pmt->formattype == FORMAT_VideoInfo) &&\n                //(pmt->subtype == MEDIASUBTYPE_RGB24) &&\n                (pmt->cbFormat >= sizeof(VIDEOINFOHEADER)) &&\n                (pmt->pbFormat != NULL) )\n            {\n                VIDEOINFOHEADER *pVIH = (VIDEOINFOHEADER*)pmt->pbFormat;\n                SIZE s;\n                // Get frame size\n                s.cy=pVIH->bmiHeader.biHeight;\n                s.cx=pVIH->bmiHeader.biWidth;\n                // \xd0\x91\xd0\xb8\xd1\x82\xd1\x80\xd0\xb5\xd0\xb9\xd1\x82\n                unsigned int bitrate=pVIH->dwBitRate;\n                modes.push_back(s);\n                // Bits per pixel\n                unsigned int bitcount=pVIH->bmiHeader.biBitCount;\n                REFERENCE_TIME t=pVIH->AvgTimePerFrame; // blocks (100ns) per frame\n                int FPS=floor(10000000.0/static_cast<double>(t));\n                printf("Size: x=%d\\ty=%d\\tFPS: %d\\t bitrate: %ld\\tbit/pixel:%ld\\n",s.cx,s.cy,FPS,bitrate,bitcount);\n            }\n            _FreeMediaType(*pmt);\n        }\n        //----------------------------------------------------\n        // \n        // \n        // \n        //----------------------------------------------------\n        modes.clear();\n        emt->Release();\n    }\n\n    pins.clear();\n\n    return S_OK;\n}\n\n/*\n* Do something with the filter. In this sample we just test the pan/tilt properties.\n*/\nvoid process_filter(IBaseFilter *pBaseFilter)\n{\n    CamCaps(pBaseFilter);\n}\n\n\n/*\n* Enumerate all video devices\n*\n* See also:\n*\n* Using the System Device Enumerator:\n*     http://msdn2.microsoft.com/en-us/library/ms787871.aspx\n*/\nint enum_devices()\n{\n    HRESULT hr;\n    setcolor(GRAY);\n    printf("Enumeraring videoinput devices ...\\n");\n\n    // Create the System Device Enumerator.\n    ICreateDevEnum *pSysDevEnum = NULL;\n    hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,\n        IID_ICreateDevEnum, (void **)&pSysDevEnum);\n    if(FAILED(hr))\n    {\n        fprintf(stderr, "Error. Can\'t create enumerator.\\n");\n        return hr;\n    }\n\n    // Obtain a class enumerator for the video input device category.\n    IEnumMoniker *pEnumCat = NULL;\n    hr = pSysDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pEnumCat, 0);\n\n    if(hr == S_OK) \n    {\n        // Enumerate the monikers.\n        IMoniker *pMoniker = NULL;\n        ULONG cFetched;\n        while(pEnumCat->Next(1, &pMoniker, &cFetched) == S_OK)\n        {\n            IPropertyBag *pPropBag;\n            hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, \n                (void **)&pPropBag);\n            if(SUCCEEDED(hr))\n            {\n                // To retrieve the filter\'s friendly name, do the following:\n                VARIANT varName;\n                VariantInit(&varName);\n                hr = pPropBag->Read(L"FriendlyName", &varName, 0);\n                if (SUCCEEDED(hr))\n                {\n                    // Display the name in your UI somehow.\n                    setcolor(GREEN);\n                    wprintf(L"------------------> %s <------------------\\n", varName.bstrVal);\n                }\n                VariantClear(&varName);\n\n                // To create an instance of the filter, do the following:\n                IBaseFilter *pFilter;\n                hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter,\n                    (void**)&pFilter);\n\n                process_filter(pFilter);\n\n                //Remember to release pFilter later.\n                pPropBag->Release();\n            }\n            pMoniker->Release();\n        }\n        pEnumCat->Release();\n    }\n    pSysDevEnum->Release();\n\n    return 0;\n}\n\n\nint wmain(int argc, wchar_t* argv[])\n{\n    setlocale(LC_ALL, "Russian");\n    int result;\n\n    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);\n\n    result = enum_devices();\n\n    CoUninitialize();\n    getchar();\n    return result;\n}\n
Run Code Online (Sandbox Code Playgroud)\n