踩过的坑——OpenNI读取多个xtion的输入

最近项目需要将单个相机拓展到多个相机阵列,使用的相机是xtion,因此首先要做的一步就是将多个相机的输入都能读取到。

我之前是没有用过OpenNI的,OpenNI是一个开源的对kinect, xtion等设备进行操作的驱动c++库。仔细观察了一下原来的代码中,获取设备是通过openni::ANY_DEVICE这么一个常量来获取的。经过查阅后发现,这个常量非常会使得系统连接所有的设备,如果恰好计算机只连接了一台xtion或者kinect,那么这个就非常有用。

但是现在的问题是我需要连接多个,不过这个也不是什么难事,OpenNI提供了一个OpenNI::enumerateDevices()来获取所有可用的设备列表,存取到Array<DeviceInfo>类型的数组中,然后我们可用通过调用DeviceInfo::getUri()来获取设备的uri,传入Device::Open即可打开对应的设备。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
bool initOpenNI(std::vector<std::string > &devicesURI)
{
openni::Status rc = openni::OpenNI::initialize();
if(rc != openni::STATUS_OK)
return false;

openni::Array<openni::DeviceInfo> aDeviceList;
openni::OpenNI::enumerateDevices( &aDeviceList );
std::cout<<"Now we have detected "<<aDeviceList.getSize()<<" devices! "<<std::endl;

for( int i = 0; i < aDeviceList.getSize(); ++ i )
{
std::cout << "device " << i <<":"<< std::endl;
const openni::DeviceInfo& rDevInfo = aDeviceList[i];
std::string name = rDevInfo.getName() ;
std::cout << "Name: " << name << std::endl;
std::cout << "UsbProductID: " << rDevInfo.getUsbProductId() << std::endl;
std::cout << "Vendor: "<< rDevInfo.getVendor() << std::endl;
std::cout << "UsbVendorId: " << rDevInfo.getUsbVendorId() << std::endl;
std::cout << "URI: " << rDevInfo.getUri() << std::endl;
if(name == "PS1080")
devicesURI.push_back(rDevInfo.getUri());
}
return true;
}

上面的例子中,因为我使用的相机全是xtion,而xtion在设备信息中显示的名称是PS1080,因此就用这个,来得到相机对应的uri。