在做場景合並等情況下,需要用到兩個或者兩個以上的攝像頭。雖然可以用一個簡單的設置函數
VideoCapture capture(int index);
去設置相應的設備ID,但是這個需要你知道設備攝像頭的ID號,雖然一般電腦自帶的攝像頭ID為0,不同的USB插口有不同的ID號,我還是遇到有些奇葩的電腦其攝像頭ID的值不是0;
所以還是覺得有必要去獲取一下攝像頭的ID號,以下的源代碼是借鑒別人的,主要是從videocapture的源代碼找到的。
1 #include <opencv2/imgcodecs.hpp>
2 #include <opencv2/highgui.hpp>
3 #include "windows.h"
4 #include "dshow.h"
5 #include <iostream>
6
7 #pragma comment(lib, "strmiids.lib")
8 #pragma comment(lib, "quartz.lib")
9
10 using namespace cv; 11 using namespace std; 12
13 int listDevices(vector<string>& list) { 14
15 //COM Library Initialization 16 //comInit(); 17
18 //if (!silent) DebugPrintOut("\nVIDEOINPUT SPY MODE!\n\n");
19
20
21 ICreateDevEnum *pDevEnum = NULL; 22 IEnumMoniker *pEnum = NULL; 23 int deviceCounter = 0; 24 CoInitialize(NULL); 25
26 HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, 27 CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, 28 reinterpret_cast<void**>(&pDevEnum)); 29
30
31 if (SUCCEEDED(hr)) 32 { 33 // Create an enumerator for the video capture category.
34 hr = pDevEnum->CreateClassEnumerator( 35 CLSID_VideoInputDeviceCategory, 36 &pEnum, 0); 37
38 if (hr == S_OK) { 39
40 printf("SETUP: Looking For Capture Devices\n"); 41 IMoniker *pMoniker = NULL; 42
43 while (pEnum->Next(1, &pMoniker, NULL) == S_OK) { 44
45 IPropertyBag *pPropBag; 46 hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, 47 (void**)(&pPropBag)); 48
49 if (FAILED(hr)) { 50 pMoniker->Release(); 51 continue; // Skip this one, maybe the next one will work.
52 } 53
54
55 // Find the description or friendly name.
56 VARIANT varName; 57 VariantInit(&varName); 58 hr = pPropBag->Read(L"Description", &varName, 0); 59
60 if (FAILED(hr)) hr = pPropBag->Read(L"FriendlyName", &varName, 0); 61
62 if (SUCCEEDED(hr)) 63 { 64
65 hr = pPropBag->Read(L"FriendlyName", &varName, 0); 66
67 int count = 0; 68 char tmp[255] = { 0 }; 69 //int maxLen = sizeof(deviceNames[0]) / sizeof(deviceNames[0][0]) - 2;
70 while (varName.bstrVal[count] != 0x00 && count < 255) 71 { 72 tmp[count] = (char)varName.bstrVal[count]; 73 count++; 74 } 75 list.push_back(tmp); 76 //deviceNames[deviceCounter][count] = 0; 77
78 //if (!silent) DebugPrintOut("SETUP: %i) %s\n", deviceCounter, deviceNames[deviceCounter]);
79 } 80
81 pPropBag->Release(); 82 pPropBag = NULL; 83
84 pMoniker->Release(); 85 pMoniker = NULL; 86
87 deviceCounter++; 88 } 89
90 pDevEnum->Release(); 91 pDevEnum = NULL; 92
93 pEnum->Release(); 94 pEnum = NULL; 95 } 96
97 //if (!silent) DebugPrintOut("SETUP: %i Device(s) found\n\n", deviceCounter);
98 } 99
100 //comUnInit();
101
102 return deviceCounter; 103 } 104
105 int main() 106 { 107 vector<string> list; 108 listDevices(list); 109 int capid0 = 0, capid1 = 0; 110 cout << "dev_size = " << list.size() << endl; 111 for (int i = 0; i<list.size(); i++) 112 { 113 if (list[i] == "3D Camera") 114 capid1 = i; 115 if (list[i] == "USB2.0 HD UVC WebCam") 116 capid0 = i; 117 cout << "device lists: " << list[i] <<" i = "<<i<< endl; 118 } 119 getchar(); 120 return 0; 121 }
opencv的環境不用說自己配置,還有兩個系統庫,需要額外的添加
#pragma comment(lib, “strmiids.lib”) #pragma comment(lib, “quartz.lib”)
測試運行的結果
