windows下qt5 kinect 2.0开发与环境配置
生活随笔
收集整理的這篇文章主要介紹了
windows下qt5 kinect 2.0开发与环境配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上一次挑戰杯我們用kinect1.8做了一個體感3d試衣系統,那時就想用qt來開發了,由于那時候很多東西都不懂,怎么也不知道怎樣去配置環境,只能在vs2013中開發,結合qt開發界面。直到出了kinect2.0后才配置成功完全用qt 來開發Kinect!而且完美運行。注意一點就是使用的qt必須是
qt forwindows msvc的才能正常編譯。
kinect 2.0開發系統要求:win8.1以上
kinect 1.8開發系統要求:win7以上
工程文件的配置
kinectBasicBuildingExplorer.pro
實現kinect功能代碼,本代碼功能是實現了右手當作鼠標來操控程序,當手握緊相當于鼠標按下左鍵,當手控制鼠標懸停在某個按鈕的時候,相當于鼠標的點擊事件。
kinectsensor.h
#ifndef KINECTSENSOR_H #define KINECTSENSOR_H// Windows Header Files必須用到這兩個頭文件才能使用Kienct api,而且要在Kinect.h之前引用 #include <windows.h> #include <Shlobj.h>// Kinect Header files #include <Kinect.h>#include <QObject> #include <QString> #include <QMouseEvent> #include <QApplication> #include <QWidget> #include <QPoint> #include <QDebug> #include <QWindow> #include <QCursor> #include <QBitmap>class KinectSensor : public QObject {Q_OBJECT public:explicit KinectSensor(QObject *parent = 0){m_pKinectSensor=NULL;m_pCoordinateMapper=NULL;m_pBodyFrameReader=NULL;currentPBody=NULL;InitializeDefaultSensor();// bitmap=new QBitmap("qrc:/imgSource/cursor.png");cursor=new QCursor(Qt::CrossCursor);cursor->setShape(Qt::DragMoveCursor);}/// <summary>/// Main processing function/// </summary>Q_INVOKABLE void updatebody();/// <summary>/// Initializes the default Kinect sensor/// </summary>/// <returns>S_OK on success, otherwise failure code</returns>Q_INVOKABLE HRESULT InitializeDefaultSensor();/// <summary>/// Handle new body data/// <param name="nTime">timestamp of frame</param>/// <param name="nBodyCount">body data count</param>/// <param name="ppBodies">body data in frame</param>/// </summary> // Q_INVOKABLE void ProcessBody(INT64 nTime, int nBodyCount, IBody** ppBodies);Q_INVOKABLE bool getCurrentBody(IBody **ppbodies);Q_INVOKABLE void getBodyJoint();Q_INVOKABLE void mapJointsToXYCoord();Q_INVOKABLE QString getLeftHandState();Q_INVOKABLE QString getRightHandState();Q_INVOKABLE float getLeftHandx(){return leftHandPoint.X;}Q_INVOKABLE float getLeftHandy(){return leftHandPoint.Y;}Q_INVOKABLE float getRightHandx(){return rightHandPoint.X;}Q_INVOKABLE float getRightHandy(){return rightHandPoint.Y;}Q_INVOKABLE void setWinPos(int x,int y){winx=x;winy=y;}Q_INVOKABLE void refreshMousePos(int mousex,int mousey){cursor->setPos(mousex,mousey);}Q_INVOKABLE void sendMouseLeftPressEvent(){QPoint pos;pos.setX(cursor->pos().x());pos.setY(cursor->pos().y());QMouseEvent *mevent=new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);QApplication::sendEvent(QApplication::focusWindow(),mevent);delete mevent;}Q_INVOKABLE void sendMouseLeftReleaseEvent(){QPoint pos;pos.setX(cursor->pos().x());pos.setY(cursor->pos().y());QMouseEvent *mevent=new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);QApplication::sendEvent(QApplication::focusWindow(),mevent);delete mevent;}Q_INVOKABLE void sendMouseDragEvent(){QPoint pos;pos.setX(cursor->pos().x());pos.setY(cursor->pos().y());QMouseEvent *mevent=new QMouseEvent(QEvent::DragMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);QApplication::sendEvent(QApplication::focusWindow(),mevent);delete mevent;}Q_INVOKABLE void sentMouseRightPressEvent(){}Q_INVOKABLE void sentMouseRightReleaseEvent(){} // Q_INVOKABLE void sentMouseMoveEvent(){ // QApplication::sendEvent(QApplication::focusObject(),mouseMoveEvent); // }Q_INVOKABLE void setnMouseLeftClickEvent(){QPoint pos;pos.setX(cursor->pos().x());pos.setY(cursor->pos().y());QMouseEvent *mevent=new QMouseEvent(QEvent::MouseButtonDblClick, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);QApplication::sendEvent(QApplication::focusWindow(),mevent);delete mevent; // QApplication::sendEvent(QApplication::focusObject(),mouseLeftClickEvent);}Q_INVOKABLE double getGuestureWidth(){return (topRightJoint.Position.X-topLeftJoint.Position.X);}Q_INVOKABLE double getCurrentMousePositionx(){return (rightHandJoint.Position.X-topRightJoint.Position.X)/(((topRightJoint.Position.X-topLeftJoint.Position.X)));}Q_INVOKABLE double getCurrentMousePositiony(){return (rightHandJoint.Position.Y-((bottomRightJoint.Position.Y+topRightJoint.Position.Y)/2+topRightJoint.Position.Y)/2)/(neckJoint.Position.Y-((bottomRightJoint.Position.Y+topRightJoint.Position.Y)/2+topRightJoint.Position.Y)/2);}Q_INVOKABLE bool hasTrackingBody(){if(currentPBody){return true;}else{return false;}}signals:public slots:private:// Current KinectIKinectSensor* m_pKinectSensor;ICoordinateMapper* m_pCoordinateMapper;IBody *currentPBody;//hand stateHandState leftHandState=HandState_Unknown;HandState rightHandState=HandState_Unknown;//hand joint (get both hands's position)Joint leftHandJoint;Joint rightHandJoint;Joint topRightJoint;Joint bottomRightJoint;Joint topLeftJoint;Joint headJoint;Joint neckJoint;//hand color positionColorSpacePoint leftHandPoint;ColorSpacePoint rightHandPoint;// Body readerIBodyFrameReader* m_pBodyFrameReader;QCursor *cursor;int winx;int winy;}; #endif // KINECTSENSOR_Hkinectsensor.cpp
#include "kinectsensor.h" #include <QDebug>// Safe release for interfaces template<class Interface> inline void SafeRelease(Interface *& pInterfaceToRelease) {if (pInterfaceToRelease != NULL){pInterfaceToRelease->Release();pInterfaceToRelease = NULL;} }//KinectSensor::KinectSensor(QObject *parent) : QObject(parent) //{//} void KinectSensor::updatebody(){if (!m_pBodyFrameReader){return;}IBodyFrame* pBodyFrame = NULL;HRESULT hr = m_pBodyFrameReader->AcquireLatestFrame(&pBodyFrame);if (SUCCEEDED(hr)){INT64 nTime = 0;hr = pBodyFrame->get_RelativeTime(&nTime);//body positionIBody* ppBodies[BODY_COUNT]={0};if (SUCCEEDED(hr)){hr = pBodyFrame->GetAndRefreshBodyData(_countof(ppBodies), ppBodies);}if (SUCCEEDED(hr)){ // ProcessBody(nTime, BODY_COUNT, ppBodies);getCurrentBody(ppBodies);// qDebug()<<"updating";getBodyJoint();mapJointsToXYCoord();currentPBody->get_HandLeftState(&leftHandState);currentPBody->get_HandRightState(&rightHandState);}for (int i = 0; i < _countof(ppBodies); ++i){SafeRelease(ppBodies[i]);}}SafeRelease(pBodyFrame);}HRESULT KinectSensor::InitializeDefaultSensor(){HRESULT hr;hr = GetDefaultKinectSensor(&m_pKinectSensor);if (FAILED(hr)){return hr;}if (m_pKinectSensor){// Initialize the Kinect and get coordinate mapper and the body readerIBodyFrameSource* pBodyFrameSource = NULL;hr = m_pKinectSensor->Open();if (SUCCEEDED(hr)){hr = m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper);}if (SUCCEEDED(hr)){hr = m_pKinectSensor->get_BodyFrameSource(&pBodyFrameSource);}if (SUCCEEDED(hr)){hr = pBodyFrameSource->OpenReader(&m_pBodyFrameReader);}SafeRelease(pBodyFrameSource);}if (!m_pKinectSensor || FAILED(hr)){ // SetStatusMessage(L"No ready Kinect found!", 10000, true);qDebug()<<"No ready Kinect found!";return E_FAIL;}return hr;}//void KinectSensor::ProcessBody(INT64 nTime, int nBodyCount, IBody** ppBodies){ // if (m_hWnd){ // HRESULT hr = EnsureDirect2DResources();// if (SUCCEEDED(hr) && m_pRenderTarget && m_pCoordinateMapper) // { // m_pRenderTarget->BeginDraw(); // m_pRenderTarget->Clear();// RECT rct; // GetClientRect(GetDlgItem(m_hWnd, IDC_VIDEOVIEW), &rct); // int width = rct.right; // int height = rct.bottom;// for (int i = 0; i < nBodyCount; ++i) // { // IBody* pBody = ppBodies[i]; // if (pBody) // { // BOOLEAN bTracked = false; // hr = pBody->get_IsTracked(&bTracked);// if (SUCCEEDED(hr) && bTracked) // { // Joint joints[JointType_Count]; // D2D1_POINT_2F jointPoints[JointType_Count]; // HandState leftHandState = HandState_Unknown; // HandState rightHandState = HandState_Unknown;// pBody->get_HandLeftState(&leftHandState); // pBody->get_HandRightState(&rightHandState);// hr = pBody->GetJoints(_countof(joints), joints); // if (SUCCEEDED(hr)) // { // for (int j = 0; j < _countof(joints); ++j) // { // jointPoints[j] = BodyToScreen(joints[j].Position, width, height); // }// DrawBody(joints, jointPoints);// DrawHand(leftHandState, jointPoints[JointType_HandLeft]); // DrawHand(rightHandState, jointPoints[JointType_HandRight]); // } // } // } // }// hr = m_pRenderTarget->EndDraw();// // Device lost, need to recreate the render target // // We'll dispose it now and retry drawing // if (D2DERR_RECREATE_TARGET == hr) // { // hr = S_OK; // DiscardDirect2DResources(); // } // }// if (!m_nStartTime) // { // m_nStartTime = nTime; // }// double fps = 0.0;// LARGE_INTEGER qpcNow = {0}; // if (m_fFreq) // { // if (QueryPerformanceCounter(&qpcNow)) // { // if (m_nLastCounter) // { // m_nFramesSinceUpdate++; // fps = m_fFreq * m_nFramesSinceUpdate / double(qpcNow.QuadPart - m_nLastCounter); // } // } // }// WCHAR szStatusMessage[64]; // StringCchPrintf(szStatusMessage, _countof(szStatusMessage), L" FPS = %0.2f Time = %I64d", fps, (nTime - m_nStartTime));// if (SetStatusMessage(szStatusMessage, 1000, false)) // { // m_nLastCounter = qpcNow.QuadPart; // m_nFramesSinceUpdate = 0; // } // }//}bool KinectSensor::getCurrentBody(IBody **ppbodies){for(int a=0;a<BODY_COUNT;a++){currentPBody=ppbodies[a];if(currentPBody){HRESULT hr;BOOLEAN btracked;hr=currentPBody->get_IsTracked(&btracked);if(SUCCEEDED(hr)&&btracked){ // qDebug()<<"succeed!";return true;}}}return false; }void KinectSensor::getBodyJoint(){if(currentPBody){HRESULT hr;Joint joints[JointType_Count];hr=currentPBody->GetJoints(_countof(joints), joints);if(SUCCEEDED(hr)){leftHandJoint=joints[JointType_HandLeft];rightHandJoint=joints[JointType_HandRight];topRightJoint=joints[JointType_ShoulderRight];bottomRightJoint=joints[JointType_HipRight];topLeftJoint=joints[JointType_ShoulderLeft];headJoint=joints[JointType_Head];neckJoint=joints[JointType_Neck];}} }void KinectSensor::mapJointsToXYCoord(){m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper);if(m_pCoordinateMapper){m_pCoordinateMapper->MapCameraPointToColorSpace(leftHandJoint.Position,&leftHandPoint);m_pCoordinateMapper->MapCameraPointToColorSpace(rightHandJoint.Position,&rightHandPoint);} }QString KinectSensor::getLeftHandState(){if(leftHandState==HandState_Unknown){return "HandState_Unknown";}else if(leftHandState==HandState_NotTracked){return "HandState_NotTracked";}else if(leftHandState==HandState_Open){return "HandState_Open";}else if(leftHandState==HandState_Closed){return "HandState_Closed";}else if(leftHandState==HandState_Lasso){return "HandState_Lasso";}else {return "I don't know hell";}}QString KinectSensor::getRightHandState(){if(rightHandState==HandState_Unknown){return "HandState_Unknown";}else if(rightHandState==HandState_NotTracked){return "HandState_NotTracked";}else if(rightHandState==HandState_Open){return "HandState_Open";}else if(rightHandState==HandState_Closed){return "HandState_Closed";}else if(rightHandState==HandState_Lasso){return "HandState_Lasso";}else {return "I don't know hell";} }kinect 相關資源網站:kinect for windows
總結
以上是生活随笔為你收集整理的windows下qt5 kinect 2.0开发与环境配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql错误用法insert into
- 下一篇: composer 报 zlib_deco