環境配置
由於官網關閉,沒有找到OpenNI2 SDK官方下載地址,下面的鏈接提供了各個平臺下OpenNI2的下載。注意安裝的版本最重要的不是要和平臺對應,例如x64系統安裝x64 SDK,而是要和所編寫的程序對應。我剛開始安裝了x64
SDK,建立工程後總是出現鏈接錯誤,檢查了好久才發現工程是32位的,而添加的庫文件是64位的。
建立工程時的環境的配置可以參考OpenCV的配置。
讀取VideoMode
OpenNI2 文檔中提到,在使用setVideoMode()設置數據流格式時(如分辨率、幀率),推薦是用getSupportVidoModes()函數讀取硬件所支持的格式組合:
Changes the current video mode of this stream. Recommended practice is to use Device::getSensorInfo(), and then SensorInfo::getSupportedVideoModes() to obtain a list of valid video mode settings for this stream. Then, pass a valid VideoMode to setVideoMode
to ensure correct operation.
查看源碼可以發現,getSupportVidoModes()返回Array<VideoMode>類型對象的引用,而Array<T>並沒有實現拷貝構造函數,而且還對其進行了"隱藏":
template<class T>
class Array
{
public:
Array() : m_data(NULL), m_count(0), m_owner(false) {}
Array(const T* data, int count) : m_owner(false) { _setData(data, count); }
~Array()
{
clear();
}
int getSize() const { return m_count; }
const T& operator[](int index) const {return m_data[index];}
void _setData(const T* data, int count, bool isOwner = false)
{
clear();
m_count = count;
m_owner = isOwner;
if (!isOwner)
{
m_data = data;
}
else
{
m_data = new T[count];
memcpy((void*)m_data, data, count*sizeof(T));
}
}
private:
Array(const Array<T>&);
Array<T>& operator=(const Array<T>&);
void clear()
{
if (m_owner && m_data != NULL)
delete []m_data;
m_owner = false;
m_data = NULL;
m_count = 0;
}
const T* m_data;
int m_count;
bool m_owner;
};
可能是爲了方便移植,OpenNI自己實現Array<T>而不是使用STL的容器。要接收getSupportVidoModes()返回的值,有兩種方法,一是實現Array<T>的拷貝構造函數,然後重新編譯OpenNI.lib;二是使用Array<T>的成員函數_setData()。使用_setData()的方法如下:
Status rc = OpenNI::initialize();
if (rc != STATUS_OK)
{
printf("Initialize failed\n%s\n", OpenNI::getExtendedError());
return -1;
}
Device device;
rc = device.open(ANY_DEVICE);
if (rc != STATUS_OK)
{
printf("Couldn't open device\n%s\n", OpenNI::getExtendedError());
return -1;
}
VideoStream depth;
if (device.getSensorInfo(SENSOR_DEPTH) != NULL)
{
rc = depth.create(device, SENSOR_DEPTH);
if (rc != STATUS_OK)
{
printf("Couldn't create depth stream\n%s\n", OpenNI::getExtendedError());
return -1;
}
}
const SensorInfo& sensorInfo = depth.getSensorInfo();
Array<VideoMode> modes;
modes._setData(&(sensorInfo.getSupportedVideoModes())[0],sensorInfo.getSupportedVideoModes().getSize(),true);
連接Kinect XBOX360時,打印的結果爲: