在做场景合并等情况下,需要用到两个或者两个以上的摄像头。虽然可以用一个简单的设置函数
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”)
测试运行的结果