日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OpenNI2下简单操作两个体感设备(Xtion与Kinect for Xbox 360)

發布時間:2025/5/22 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenNI2下简单操作两个体感设备(Xtion与Kinect for Xbox 360) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

主要內容:

  • 設備與驅動準備
  • 代碼演示
  • 總結

一、設備與驅動準備

  最近忙著寫論文,已好長時間沒瞎寫了,這兩天偶然看到一篇有關OpenNI2操作兩個體感設備的文章,自己復制粘貼運行下看了效果挺好的,所以我大膽的搬過來,和大家分享分享~~~

  設備準備:一個ASUS Xtion;一個Kinect for Xbox360;

  驅動準備:我是win7下運行的,只要在設備管理器中顯示如下效果,就沒問題;要是有出現一個顯示黃的,那就表示其中有一個顯示不了,我目前的做法是:反復拔了再插,插了再拔,直到顯示如下效果就OK;

二、代碼演示

  在之前都是用到一個體感設備進行開發,但也有人拿兩個把玩著,所以如何同時讓兩個都運行起來?

  在OpenNI2中提供了一個OpenNI::enumerateDevices函數原型是這樣的:

/**Fills up an array of @ref DeviceInfo objects with devices that are available.@param [in,out] deviceInfoList An array to be filled with devices.*/static void enumerateDevices(Array<DeviceInfo>* deviceInfoList){OniDeviceInfo* m_pDeviceInfos;int m_deviceInfoCount;oniGetDeviceList(&m_pDeviceInfos, &m_deviceInfoCount);deviceInfoList->_setData((DeviceInfo*)m_pDeviceInfos, m_deviceInfoCount, true);oniReleaseDeviceList(m_pDeviceInfos);}

英語學的不好,大概的意思是將可使用的divices設備信息保存到Array<DeviceInfo>* deviceInfoList中吧~通過看oniGetDeviceList函數:

/*** Get the list of currently connected device.* Each device is represented by its OniDeviceInfo.* pDevices will be allocated inside.*/ ONI_C_API OniStatus oniGetDeviceList(OniDeviceInfo** pDevices, int* pNumDevices);

  通過Array<DeviceInfo>* deviceInfoList,我們就可以得到DeviceInfo信息操作Device了,其中DeviceInfo包括設備Uri,供應商名,供應商ID,設備Id,設備名:

class DeviceInfo : private OniDeviceInfo { public:/** Returns the device URI. URI can be used by @ref Device::open to open a specific device. The URI string format is determined by the driver.*/const char* getUri() const { return uri; }/** Returns a the vendor name for this device. */const char* getVendor() const { return vendor; }/** Returns the device name for this device. */const char* getName() const { return name; }/** Returns the USB VID code for this device. */uint16_t getUsbVendorId() const { return usbVendorId; }/** Returns the USB PID code for this device. */uint16_t getUsbProductId() const { return usbProductId; }friend class Device;friend class OpenNI; };

  有了這些信息就可以看目前連接的設備的信息了,看代碼:

int main( int argc, char **argv ) { // 初始化OpenNI OpenNI::initialize();// 獲取設備信息 Array<DeviceInfo> aDeviceList; OpenNI::enumerateDevices( &aDeviceList ); // output information //vector<CDevice> vDevices; cout << "電腦上連接著 " << aDeviceList.getSize() << " 個體感設備." << endl; for( int i = 0; i < aDeviceList.getSize(); ++ i ) { cout << "設備 " << i << endl; const DeviceInfo& rDevInfo = aDeviceList[i]; cout << "設備名: " << rDevInfo.getName() << endl;cout << "設備Id: " << rDevInfo.getUsbProductId() << endl;cout << "供應商名: "<< rDevInfo.getVendor() << endl; cout << "供應商Id: " << rDevInfo.getUsbVendorId() << endl; cout << "設備URI: " << rDevInfo.getUri() << endl; }system("pause"); // 編譯運行之后可以 OpenNI::shutdown(); return 0; }

  顯示效果:

  接著在獲取Kinect和Xtion信息之后,就可以驅動他們運行了,通過設備Uri驅動,估計大家都懂了,然后獲得它們各自的深度信息流數據,再利用OpenCV進行處理,直接上代碼:

// MoreDevice.cpp : 定義控制臺應用程序的入口點。 // #include "stdafx.h"// 標準庫頭文件 #include <iostream> #include <string> #include <vector> // OpenCV頭文件 #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> // OpenNI頭文件 #include <OpenNI.h> // namespace using namespace std; using namespace openni;class CDevice{ public: const char* devicename; Device* pDevice; VideoStream* pDepthStream; CDevice( int idx, const char* uri, const char* deviceName) { devicename = deviceName;// 創建DevicepDevice = new Device(); // 打開指定Uri設備pDevice->open( uri ); // 創建深度數據量pDepthStream = new VideoStream(); pDepthStream->create( *pDevice, SENSOR_DEPTH ); // 創建OpenCV窗口 cv::namedWindow( deviceName, CV_WINDOW_AUTOSIZE );// 開始 pDepthStream->start(); } }; int main( int argc, char **argv ) { // 初始化OpenNI OpenNI::initialize();// 獲取設備信息 Array<DeviceInfo> aDeviceList; OpenNI::enumerateDevices( &aDeviceList ); // 將每個設備信息用CDevice類封裝 vector<CDevice> vDevices;cout << "電腦上連接著 " << aDeviceList.getSize() << " 個體感設備." << endl; for( int i = 0; i < aDeviceList.getSize(); ++ i ) { cout << "設備 " << i << endl; const DeviceInfo& rDevInfo = aDeviceList[i]; cout << "設備名: " << rDevInfo.getName() << endl;cout << "設備Id: " << rDevInfo.getUsbProductId() << endl;cout << "供應商名: "<< rDevInfo.getVendor() << endl; cout << "供應商Id: " << rDevInfo.getUsbVendorId() << endl; cout << "設備URI: " << rDevInfo.getUri() << endl; // 封裝類初始化,傳入設備名和設備Uri CDevice mDev(i, aDeviceList[i].getUri(), aDeviceList[i].getName()); vDevices.push_back( mDev ); } while( true ) { for( vector<CDevice>::iterator itDev = vDevices.begin(); itDev != vDevices.end(); ++ itDev ) { // 獲取深度圖像幀 VideoFrameRef vfFrame; itDev->pDepthStream->readFrame( &vfFrame ); // 轉換成 OpenCV 格式 const cv::Mat mImageDepth( vfFrame.getHeight(), vfFrame.getWidth(), CV_16UC1, const_cast<void*>( vfFrame.getData() ) ); // 從 [0,Max] 轉為 [0,255] cv::Mat mScaledDepth; mImageDepth.convertTo( mScaledDepth, CV_8U, 255.0 / itDev->pDepthStream->getMaxPixelValue() ); // 顯示圖像 cv::imshow( itDev->devicename, mScaledDepth ); vfFrame.release(); } // 退出鍵 if( cv::waitKey( 1 ) == 'q' ) break; } // 停止時的操作 for( vector<CDevice>::iterator itDev = vDevices.begin(); itDev != vDevices.end(); ++ itDev ) { itDev->pDepthStream->stop(); itDev->pDepthStream->destroy(); delete itDev->pDepthStream; itDev->pDevice->close(); delete itDev->pDevice; } OpenNI::shutdown();return 0; }

  顯示效果:

三、總結

  代碼挺簡單的,我自己碰到的問題主要是xtion和kinect驅動共存的問題,剩下的就好解決了。最后說明的是:根據自己的感覺寫代碼,沒做封裝、優化、重構,完全是面向過程,而且肯定還存在細節的問題,會在后面進一步優化的。??? 寫的粗糙,歡迎指正批評~~~

轉載于:https://www.cnblogs.com/yemeishu/archive/2013/03/13/2957403.html

總結

以上是生活随笔為你收集整理的OpenNI2下简单操作两个体感设备(Xtion与Kinect for Xbox 360)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。