void Kinect2Manager::UpdateBody(IBodyFrame* pBodyFrame) { #ifdef _USE_KINECT INT64 nTime = 0; HRESULT hr = pBodyFrame->get_RelativeTime(&nTime); IBody* ppBodies[BODY_COUNT] = { 0 }; if (SUCCEEDED(hr)) { hr = pBodyFrame->GetAndRefreshBodyData(_countof(ppBodies), ppBodies); } if (SUCCEEDED(hr)) { ProcessBody(nTime, _countof(ppBodies), ppBodies); } for (int i = 0; i < _countof(ppBodies); ++i) { SafeRelease(ppBodies[i]); } #else #endif }
void KinectPlugin::updateBody() { #ifndef HAVE_KINECT return; #else if (!_bodyFrameReader) { return; } IBodyFrame* pBodyFrame = NULL; HRESULT hr = _bodyFrameReader->AcquireLatestFrame(&pBodyFrame); if (SUCCEEDED(hr)) { INT64 nTime = 0; hr = pBodyFrame->get_RelativeTime(&nTime); IBody* bodies[BODY_COUNT] = {0}; if (SUCCEEDED(hr)) { hr = pBodyFrame->GetAndRefreshBodyData(_countof(bodies), bodies); } if (SUCCEEDED(hr)) { ProcessBody(nTime, BODY_COUNT, bodies); } for (int i = 0; i < _countof(bodies); ++i) { SafeRelease(bodies[i]); } } SafeRelease(pBodyFrame); #endif }
/// <summary> /// Main processing function /// </summary> void CBodyBasics::Update() { if (!m_pBodyFrameReader) { return; } IBodyFrame* pBodyFrame = NULL; HRESULT hr = m_pBodyFrameReader->AcquireLatestFrame(&pBodyFrame); if (SUCCEEDED(hr)) { INT64 nTime = 0; hr = pBodyFrame->get_RelativeTime(&nTime); IBody* ppBodies[BODY_COUNT] = {0}; if (SUCCEEDED(hr)) { hr = pBodyFrame->GetAndRefreshBodyData(_countof(ppBodies), ppBodies); } if (SUCCEEDED(hr)) { ProcessBody(nTime, BODY_COUNT, ppBodies); } for (int i = 0; i < _countof(ppBodies); ++i) { SafeRelease(ppBodies[i]); } } SafeRelease(pBodyFrame); /// Update Color Frame if (IsDrawColorBase) { UpdateColorBase(); } }
void testApp::update() { kinect.update(); if (!m_pBodyFrameReader) { return; } IBodyFrame* pBodyFrame = NULL; HRESULT hr = m_pBodyFrameReader->AcquireLatestFrame(&pBodyFrame); if (SUCCEEDED(hr)) { INT64 nTime = 0; hr = pBodyFrame->get_RelativeTime(&nTime); IBody* ppBodies[BODY_COUNT] = { 0 }; if (SUCCEEDED(hr)) { hr = pBodyFrame->GetAndRefreshBodyData(_countof(ppBodies), ppBodies); } if (SUCCEEDED(hr)) { ProcessBody(nTime, BODY_COUNT, ppBodies); } for (int i = 0; i < _countof(ppBodies); ++i) { SafeRelease(ppBodies[i]); } } SafeRelease(pBodyFrame); for (int y = 0; y < vectorField.getHeight(); y++) for (int x = 0; x< vectorField.getWidth(); x++) { int index = vectorField.getPixelIndex(x, y); float angle = ofNoise(x / (float)vectorField.getWidth()*4.0, y / (float)vectorField.getHeight()*4.0, ofGetElapsedTimef()*0.05)*TWO_PI*2.0; ofVec2f dir(cos(angle), sin(angle)); dir.normalize().scale(ofNoise(x / (float)vectorField.getWidth()*4.0, y / (float)vectorField.getHeight()*4.0, ofGetElapsedTimef()*0.05 + 10.0)); vectorField.setColor(x, y, ofColor_<float>(dir.x, dir.y, 0)); } for (int i = 0; i < BODY_COUNT; i++) { updateParticleSystem(&particleSystems[i], lastChestPositions[i], lastHandPositionLeft[i], lastHandPositionRight[i], leftHandStates[i], rightHandStates[i]); } //Check if a body left the system for a long period of time for (int i = 0; i < BODY_COUNT; i++) { if (lastKnownChestPosition[i] == lastChestPositions[i]) { bodyFreezeIterationToRemoveCount[i]++; } else { bodyFreezeIterationToRemoveCount[i] = 0; } lastKnownChestPosition[i] = lastChestPositions[i]; if (bodyFreezeIterationToRemoveCount[i] >= MAX_NUM_OF_ITERATIONS_TO_REMOVE_A_BODY ) { bodyFreezeIterationToRemoveCount[i] = 0; lastChestPositions[i].x = lastChestPositions[i].y = 0; } } }
void MyKinect2::Update() { // récupération de l'image en 2D if (!m_pColorFrameReader) { return; } IColorFrame* pColorFrame = NULL; HRESULT hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame); if (SUCCEEDED(hr)) { INT64 nTime = 0; IFrameDescription* pFrameDescription = NULL; int nWidth = 0; int nHeight = 0; ColorImageFormat imageFormat = ColorImageFormat_None; UINT nBufferSize = 0; hr = pColorFrame->get_RelativeTime(&nTime); if (SUCCEEDED(hr)) { hr = pColorFrame->get_FrameDescription(&pFrameDescription); } if (SUCCEEDED(hr)) { hr = pFrameDescription->get_Width(&nWidth); } if (SUCCEEDED(hr)) { hr = pFrameDescription->get_Height(&nHeight); } if (SUCCEEDED(hr)) { hr = pColorFrame->get_RawColorImageFormat(&imageFormat); } if (SUCCEEDED(hr) && (nWidth == cColorWidth) && (nHeight == cColorHeight)) { if (imageFormat == ColorImageFormat_Bgra) { hr = pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize, reinterpret_cast<BYTE**>(&webcam.data)); } else if (m_pColorRGBX) { nBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD); hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, reinterpret_cast<BYTE*>(webcam.data), ColorImageFormat_Bgra); } else { hr = E_FAIL; } } SafeRelease(pFrameDescription); } // récupération du squelette if (!m_pBodyFrameReader) { return; } IBodyFrame* pBodyFrame = NULL; hr = m_pBodyFrameReader->AcquireLatestFrame(&pBodyFrame); if (SUCCEEDED(hr)) { INT64 nTime = 0; hr = pBodyFrame->get_RelativeTime(&nTime); IBody* ppBodies[BODY_COUNT] = {0}; if (SUCCEEDED(hr)) { hr = pBodyFrame->GetAndRefreshBodyData(_countof(ppBodies), ppBodies); } if (SUCCEEDED(hr)) { ProcessBody(BODY_COUNT, ppBodies); // BODY_COUNT est un define de Kinect.h égal à 6... peut etre mettre 1 plus tard afin déviter des problemes lors de la récupération des positions de joints } for (int i = 0; i < _countof(ppBodies); ++i) { SafeRelease(ppBodies[i]); } } SafeRelease(pBodyFrame); SafeRelease(pColorFrame); }
/// Main processing function void CBodyBasics::Update() { clear = true; for ( int i = 0; i < BODY_COUNT; i ++ ) { bodyXY[i][0] = bodyXY[i][1] = -1; position[i][0] = position[i][1] = -1; angle[i] = -1; distance = -1; } //每次先清空skeletonImg skeletonImg.setTo(0); //如果丢失了kinect,则不继续操作 if (!m_pBodyFrameReader) { return; } IBodyFrame* pBodyFrame = NULL;//骨架信息 IDepthFrame* pDepthFrame = NULL;//深度信息 IBodyIndexFrame* pBodyIndexFrame = NULL;//背景二值图 //记录每次操作的成功与否 HRESULT hr = S_OK; //---------------------------------------获取背景二值图并显示--------------------------------- if (SUCCEEDED(hr)){ hr = m_pBodyIndexFrameReader->AcquireLatestFrame(&pBodyIndexFrame);//获得背景二值图信息 } if (SUCCEEDED(hr)){ BYTE *bodyIndexArray = new BYTE[cDepthHeight * cDepthWidth];//背景二值图是8为uchar,有人是黑色,没人是白色 pBodyIndexFrame->CopyFrameDataToArray(cDepthHeight * cDepthWidth, bodyIndexArray); //把背景二值图画到MAT里 uchar* skeletonData = (uchar*)skeletonImg.data; for (int j = 0; j < cDepthHeight * cDepthWidth; ++j){ *skeletonData = bodyIndexArray[j]; ++skeletonData; *skeletonData = bodyIndexArray[j]; ++skeletonData; *skeletonData = bodyIndexArray[j]; ++skeletonData; } delete[] bodyIndexArray; } SafeRelease(pBodyIndexFrame);//必须要释放,否则之后无法获得新的frame数据 //-----------------------------获取骨架并显示---------------------------- if (SUCCEEDED(hr)){ hr = m_pBodyFrameReader->AcquireLatestFrame(&pBodyFrame);//获取骨架信息 } if (SUCCEEDED(hr)) { IBody* ppBodies[BODY_COUNT] = { 0 };//每一个IBody可以追踪一个人,总共可以追踪六个人 if (SUCCEEDED(hr)) { //把kinect追踪到的人的信息,分别存到每一个IBody中 hr = pBodyFrame->GetAndRefreshBodyData(_countof(ppBodies), ppBodies); } if (SUCCEEDED(hr)) { //对每一个IBody,我们找到他的骨架信息,并且画出来 ProcessBody(BODY_COUNT, ppBodies); } for (int i = 0; i < _countof(ppBodies); ++i) { SafeRelease(ppBodies[i]);//释放所有 } } SafeRelease(pBodyFrame);//必须要释放,否则之后无法获得新的frame数据 //-----------------------获取深度数据并显示-------------------------- if (SUCCEEDED(hr)){ hr = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);//获得深度数据 } if (SUCCEEDED(hr)){ UINT16 *depthArray = new UINT16[cDepthHeight * cDepthWidth];//深度数据是16位unsigned int pDepthFrame->CopyFrameDataToArray(cDepthHeight * cDepthWidth, depthArray); //把深度数据画到MAT中 uchar* depthData = (uchar*)depthImg.data; for (int j = 0; j < cDepthHeight * cDepthWidth; ++j){ *depthData = depthArray[j]; ++depthData; } distance = depthArray[cDepthHeight*cDepthWidth/2 + cDepthWidth/2]; for ( int j = 0; j < BODY_COUNT; j ++ ) { if ( -1 == (bodyXY[j][0] | bodyXY[j][1]) ) { continue; } double r = depthArray[cDepthWidth*bodyXY[j][1] + bodyXY[j][0]]; position[j][0] = r * cos(angle[j]) / 1000.0; position[j][1] = r * sin(angle[j]) / 1000.0; } delete[] depthArray; } SafeRelease(pDepthFrame);//必须要释放,否则之后无法获得新的frame数据 imshow("depthImg", depthImg); cv::waitKey(5); }
/// <summary> /// Main processing function /// </summary> // この関数がループ処理される void CColorBasics::Update() { HRESULT hr = NULL; // (1) カラーフレーム(背景描画のみに使用) if (m_pColorFrameReader) { IColorFrame* pColorFrame = NULL; hr = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame); if (SUCCEEDED(hr)) { INT64 nTime = 0; IFrameDescription* pFrameDescription = NULL; int nWidth = 0; int nHeight = 0; ColorImageFormat imageFormat = ColorImageFormat_None; UINT nBufferSize = 0; RGBQUAD *pBuffer = NULL; hr = pColorFrame->get_RelativeTime(&nTime); if (SUCCEEDED(hr)) { hr = pColorFrame->get_FrameDescription(&pFrameDescription); } if (SUCCEEDED(hr)) { hr = pFrameDescription->get_Width(&nWidth); } if (SUCCEEDED(hr)) { hr = pFrameDescription->get_Height(&nHeight); } if (SUCCEEDED(hr)) { hr = pColorFrame->get_RawColorImageFormat(&imageFormat); } if (SUCCEEDED(hr)) { if (imageFormat == ColorImageFormat_Bgra) { hr = pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize, reinterpret_cast<BYTE**>(&pBuffer)); } else if (m_pColorRGBX) { pBuffer = m_pColorRGBX; nBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD); hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, reinterpret_cast<BYTE*>(pBuffer), ColorImageFormat_Bgra); } else { hr = E_FAIL; } } if (SUCCEEDED(hr)) { ProcessColor(nTime, pBuffer, nWidth, nHeight); } SafeRelease(pFrameDescription); } SafeRelease(pColorFrame); } // これ以降にBody処理を実装 TIMESPAN nBodyTime = 0; if (m_pBodyFrameReader) { IBodyFrame* pBodyFrame = NULL; hr = m_pBodyFrameReader->AcquireLatestFrame(&pBodyFrame); if (SUCCEEDED(hr)) { hr = pBodyFrame->get_RelativeTime(&nBodyTime); // ここに、UI描画処理 ボタン等 // ゲームのステータスによって描画を設定 if (!m_pGame){ m_pGame = new CSemaphoreGame(m_pDrawColor, m_pCoordinateMapper); } if (m_pGame){ m_pGame->Display(nBodyTime); } IBody* ppBodies[BODY_COUNT] = { 0 }; if (SUCCEEDED(hr)) { hr = pBodyFrame->GetAndRefreshBodyData(_countof(ppBodies), ppBodies); } if (SUCCEEDED(hr)) { // ここに、UI処理->ProcessBody()内でOK // ボディデータにてUI処理を行う // ステータスの変更 if (m_pGame){ m_pGame->Play(nBodyTime, BODY_COUNT, ppBodies); } ProcessBody(nBodyTime, BODY_COUNT, ppBodies); } for (int i = 0; i < _countof(ppBodies); ++i) { SafeRelease(ppBodies[i]); } } SafeRelease(pBodyFrame); } return; }