OpenCV的VideoCapture是一个视频读取与解码的API接口,支持各种视频格式、网络视频流、摄像头读取。

针对一般摄像头的读取,opencv为了实现跨平台读取摄像头时是使用的摄像头索引,

 VideoCapture capture(int index);

一般而言电脑自带的摄像头id=0,但是也存在一些特殊情况,有些usb的摄像头接入笔记本后,usb摄像头的id会变位0,原有的笔记本id则变为1,所以为了程序的稳定性,最好还是使用图像采集设备的名称获取对应的id最后在使用opencv接口打开对应的设备(摄像头、视频采集卡...)。

 #include<opencv2/objdetect/objdetect.hpp>
#include<opencv2/highgui/highgui.hpp>
#include "windows.h"
#include "dshow.h"
#include <iostream> #pragma comment(lib, "strmiids.lib")
#pragma comment(lib, "quartz.lib") using namespace cv;
using namespace std; int listDevices(vector<string>& list) { //COM Library Initialization
//comInit(); ICreateDevEnum *pDevEnum = NULL;
IEnumMoniker *pEnum = NULL;
int deviceCounter = ;
CoInitialize(NULL); HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
CLSCTX_INPROC_SERVER, IID_ICreateDevEnum,
reinterpret_cast<void**>(&pDevEnum)); if (SUCCEEDED(hr))
{
// Create an enumerator for the video capture category.
hr = pDevEnum->CreateClassEnumerator(
CLSID_VideoInputDeviceCategory,
&pEnum, ); if (hr == S_OK) { printf("SETUP: Looking For Capture Devices\n");
IMoniker *pMoniker = NULL; while (pEnum->Next(, &pMoniker, NULL) == S_OK) { IPropertyBag *pPropBag;
hr = pMoniker->BindToStorage(, , IID_IPropertyBag,
(void**)(&pPropBag)); if (FAILED(hr)) {
pMoniker->Release();
continue; // Skip this one, maybe the next one will work.
} // Find the description or friendly name.
VARIANT varName;
VariantInit(&varName);
hr = pPropBag->Read(L"Description", &varName, ); if (FAILED(hr)) hr = pPropBag->Read(L"FriendlyName", &varName, ); if (SUCCEEDED(hr))
{ hr = pPropBag->Read(L"FriendlyName", &varName, ); int count = ;
char tmp[] = { };
//int maxLen = sizeof(deviceNames[0]) / sizeof(deviceNames[0][0]) - 2;
while (varName.bstrVal[count] != 0x00 && count < )
{
tmp[count] = (char)varName.bstrVal[count];
count++;
}
list.push_back(tmp);
//deviceNames[deviceCounter][count] = 0; //if (!silent) DebugPrintOut("SETUP: %i) %s\n", deviceCounter, deviceNames[deviceCounter]);
} pPropBag->Release();
pPropBag = NULL; pMoniker->Release();
pMoniker = NULL; deviceCounter++;
} pDevEnum->Release();
pDevEnum = NULL; pEnum->Release();
pEnum = NULL;
} //if (!silent) DebugPrintOut("SETUP: %i Device(s) found\n\n", deviceCounter);
} //comUnInit(); return deviceCounter;
} int main()
{
vector<string> list;
listDevices(list);
int capid0 = , capid1 = ;
cout << "dev_size = " << list.size() << endl;
for (int i = ; i<list.size(); i++)
{
if (list[i] == "3D Camera")
capid1 = i;
if (list[i] == "USB2.0 HD UVC WebCam")
capid0 = i;
cout << "device lists:" << list[i] <<'\t'<<"i="<<i<< endl; if (list[i]=="CY3014 USB, Analog 01 Capture")
{
cout<<"video..."<<'\n';
}
}
getchar();
return ;
}

涉及的配置:

opencv的环境不用说自己配置还有两个系统库,需要额外的添加 #pragma comment(lib, “strmiids.lib”) #pragma comment(lib, “quartz.lib”)

note:库存在64位和32位的区别,使用时需要和自己的项目位数相同(以上两个库一般系统都是自带的,不需要额外指定库路径,可以直接引用)

参考博客:

https://blog.csdn.net/wangxiuwen12/article/details/87371359

http://theoractice.github.io/2015/09/18/[Win]%20OpenCV%20管理多摄像头的最简洁方法/

https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=videocapture#reading-and-writing-images-and-video

opencv根据摄像头名称获取索引值的更多相关文章

  1. ResDrawableImgUtil【根据图片名称获取resID值或者Bitmap对象】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 根据图片名称获取项目的res/drawable-xxdhpi中相应资源的ID值以及bitmap值的封装类. 效果图 代码分析 根据图 ...

  2. javascript使用for循环批量注册的事件不能正确获取索引值的解决方法

    今天遇到一个问题,那就是当使用for循环批量注册事件处理函数,然后最后通过事件处理函数获取当前元素的索引值的时候会失败,先看一段代码实例: <script type="text/jav ...

  3. 关于Shareppoint客户端对象模型和Shareppoint根据内部名称获取字段值的随笔

    实际上,每个SharePoint字段实际上有两个名称,一个是“标题”(Title,有时候也把它叫做“显示名称”),一个是“内部名称”(Internal Name).平时用户在列表视图界面上看到的,都是 ...

  4. foreach获取索引值

    List<" }; foreach (string item in items) { int index = items.IndexOf(item); Console.WriteLin ...

  5. js读取cookie 根据cookie名称获取值、赋值

    借鉴:原作者https://blog.csdn.net/zouxuhang/article/details/80548417   //方法1   //存在问题:如果cookie中存在 aaaname= ...

  6. js读取cookie 根据cookie名称获取值的方法

    //方法1 //存在问题:如果cookie中存在 aaaname=aa;name=bb 获取name的值就会出现错误function getCookie(c_name){ if (document.c ...

  7. C#通过属性名称获取(读取)属性值的方法

    之前在开发一个程序,希望能够通过属性名称读取出属性值,但是由于那时候不熟悉反射,所以并没有找到合适的方法,做了不少的重复性工作啊! 然后今天我再上网找了找,被我找到了,跟大家分享一下. 其实原理并不复 ...

  8. 【转】JavaScript获取节点类型、节点名称和节点值

    DOM节点信息包括节点类型(nodeType).节点名称(nodeName)和节点值(nodeValue). 节点类型 DOM节点中,每个节点都拥有不同的类型.W3C规范中常用的 DOM节点类型有以下 ...

  9. ASP.NET中gridview获取当前行的索引值

    在用GridView控件时,我们经常会碰到获取当前行的索引,通过索引进行许多操作.例如,可以获得当前行某一个控件元素:设置某一元素的值等等.下面结合实例介绍几种获得GridView当前行索引值的方法. ...

随机推荐

  1. SQLServer常见查询问题

     http://bbs.csdn.net/topics/340078327 1.生成若干行记录 --自然数表1-1M CREATE TABLE Nums(n int NOT NULL PRIMAR ...

  2. iwap问题

    1. 2. . . ========================================================================================== ...

  3. LibreOJ #6217. 扑克牌

    二次联通门 : LibreOJ #6217. 扑克牌 /* LibreOJ #6217. 扑克牌 背包.... 回到家之后简直了...sb题想半天 */ #include <cstdio> ...

  4. NetworkX系列教程(4)-设置graph的信息

    小书匠Graph图论 要画出美观的graph,需要对graph里面的节点,边,节点的布局都要进行设置,具体可以看官方文档:Adding attributes to graphs, nodes, and ...

  5. 【概率论】5-1:分布介绍(Special Distribution Introduction)

    title: [概率论]5-1:分布介绍(Special Distribution Introduction) categories: - Mathematic - Probability keywo ...

  6. PHP全栈学习笔记26

    php 验证码 <?php /* *@Author: 达叔小生 **/ header("Content-type:image/png"); // 发送头部信息,生成png图片 ...

  7. Pyhton3异常处理

    例: while True:        try:            x = int(input("Please enter a number: "))            ...

  8. Tomcat的默认端口问题

    0x00 起因 今天看到一个226团队,进群的时候有一个问题问的就是:Tomcat的默认端口是多少? 当时我只想到了8080,等过了不久,有位管理员回复了我是三个默认端口....,马上去翻了下资料,才 ...

  9. [Ubuntu] 移植Ubuntu16.04根文件系统到嵌入式平台

    CPU:RK3288 1.通过 ubuntu cdimage 下载 ubuntu16.04 内核,以下两种方式都可以 在 windows 系统网页中下载 http://cdimage.ubuntu.c ...

  10. spring boot + vue 前后分离实现登录功能(一)

    使用webpack 打包初始化项目 vue init webpack book-vue 进入工程目录 cd hello-vue 安装 vue-router npm install vue-router ...