Based on the Dragonboard 410c kinect application series - get the human skeleton map

After "Dragonboard 410c based kinect application series - get depth map", today we get the bone map of the human body.

First, the core code:
//------------------------------------------------ ------------------------------
//
// Copyright (c) Microsoft CorporaTIon. All rights reserved.
//
//------------------------------------------------ ------------------------------
#include "stdafx.h"
#include
#include "SkeletonBasics.h"
#include "resource.h"
staTIc const float g_JointThickness = 3.0f ;
staTIc const float g_TrackedBoneThickness = 6.0f ;
staTIc const float g_InferredBoneThickness = 1.0f ;
///
/// Entry point for the application
///
/// Handle to the application instance
/// Always 0
/// Command line arguments
/// Whether to display minimized, maximized, or normally
/// Status
Int APIENTRY wWinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance , LPWSTR lpCmdLine , int nCmdShow )
{
CSkeletonBasics application ;
Application . Run ( hInstance , nCmdShow );
}
///
/// Constructor
///
CSkeletonBasics :: CSkeletonBasics () :
m_pD2DFactory ( NULL ),
m_hNextSkeletonEvent ( INVALID_HANDLE_VALUE ),
m_pSkeletonStreamHandle ( INVALID_HANDLE_VALUE ),
m_bSeatedMode ( false ),
m_pRenderTarget ( NULL ),
m_pBrushJointTracked ( NULL ),
m_pBrushJointInferred ( NULL ),
m_pBrushBoneTracked ( NULL ),
m_pBrushBoneInferred ( NULL ),
m_pNuiSensor ( NULL )
{
ZeroMemory ( m_Points , sizeof ( m_Points ));
}
///
/// Destructor
///
CSkeletonBasics ::~ CSkeletonBasics ()
{
If ( m_pNuiSensor )
{
m_pNuiSensor -> NuiShutdown ();
}
If ( m_hNextSkeletonEvent && ( m_hNextSkeletonEvent != INVALID_HANDLE_VALUE ))
{
CloseHandle ( m_hNextSkeletonEvent );
}
// clean up Direct2D objects
DiscardDirect2DResources ();
// clean up Direct2D
SafeRelease ( m_pD2DFactory );
SafeRelease ( m_pNuiSensor );
}
 
///
///Creates the main window and begins processing
///
/// Handle to the application instance
/// Whether to display minimized, maximized, or normally
Int CSkeletonBasics :: Run ( HINSTANCE hInstance , int nCmdShow )
{
MSG msg = { 0 };
WNDCLASS wc = { 0 };
// Dialog custom window class
Wc . style = CS_HREDRAW | CS_VREDRAW ;
Wc . cbWndExtra = DLGWINDOWEXTRA ;
Wc . hInstance = hInstance ;
Wc . hCursor = LoadCursorW ( NULL , IDC_ARROW );
Wc . hIcon = LoadIconW ( hInstance , MAKEINTRESOURCE ( IDI_APP ));
Wc . lpfnWndProc = DefDlgProcW ;
Wc . lpszClassName = L"SkeletonBasicsAppDlgWndClass" ;
If ( ! RegisterClassW ( & wc ))
{
Return 0 ;
}
// Create main application window
HWND hWndApp = CreateDialogParamW (
hInstance ,
MAKEINTRESOURCE ( IDD_APP ),
NULL ,
( DLGPROC ) CSkeletonBasics :: MessageRouter ,
Reinterpret_cast < LPARAM > ( this ));
// Show window
ShowWindow ( hWndApp , nCmdShow );
Const int eventCount = 1 ;
HANDLE hEvents [ eventCount ];
// Main message loop
While ( WM_QUIT != msg . message )
{
hEvents [ 0 ] = m_hNextSkeletonEvent ;
// Check to see if we have either a message (by passing in QS_ALLEVENTS)
// Or a Kinect event (hEvents)
// Update() will check for Kinect events individually, in case more than one are signalled
MsgWaitForMultipleObjects ( eventCount , hEvents , FALSE , INFINITE , QS_ALLINPUT );
// Explicitly check the Kinect frame event since MsgWaitForMultipleObjects
// can return for other reasons even though it is signaled.
Update ();
While ( PeekMessageW ( & msg , NULL , 0 , 0 , PM_REMOVE ))
{
// If a dialog message will be taken care of by the dialog proc
If (( hWndApp != NULL ) && IsDialogMessageW ( hWndApp , & msg ))
{
Continue ;
}
TranslateMessage ( & msg );
DispatchMessageW ( & msg );
}
}
Return static_cast < int > ( msg . wParam );
}
///
/// Main processing function
///
Void CSkeletonBasics :: Update ()
{
If ( NULL == m_pNuiSensor )
{
Return ;
}
// Wait for 0ms, just quickly test if it is time to process a skeleton
If ( WAIT_OBJECT_0 == WaitForSingleObject ( m_hNextSkeletonEvent , 0 ) )
{
ProcessSkeleton ();
}
}
///
/// Handles window messages, passes most to the class instance to handle
///
/// Window message is for
/// Message
/// Message data
/// Additional message data
/// Result of message processing
LRESULT CALLBACK CSkeletonBasics :: MessageRouter ( HWND hWnd , UINT uMsg , WPARAM wParam , LPARAM lParam )
{
CSkeletonBasics * pThis = NULL ;
If ( WM_INITDIALOG == uMsg )
{
pThis = reinterpret_cast < CSkeletonBasics *> ( lParam );
SetWindowLongPtr ( hWnd , GWLP_USERDATA , reinterpret_cast < LONG_PTR > ( pThis ));
}
Else
{
pThis = reinterpret_cast < CSkeletonBasics *> ( :: GetWindowLongPtr ( hWnd , GWLP_USERDATA ));
}
If ( pThis )
{
Return pThis -> DlgProc ( hWnd , uMsg , wParam , lParam );
}
Return 0 ;
}
///
/// Handle windows messages for the class instance
///
/// Window message is for
/// Message
/// Message data
/// Additional message data
/// Result of message processing
LRESULT CALLBACK CSkeletonBasics :: DlgProc ( HWND hWnd , UINT message , WPARAM wParam , LPARAM lParam )
{
Switch ( message )
{
case WM_INITDIALOG:
{
// Bind application window handle
m_hWnd = hWnd ;
// Init Direct2D
D2D1CreateFactory ( D2D1_FACTORY_TYPE_SINGLE_THREADED , & m_pD2DFactory );
// Look for a connected Kinect, and create it if found
CreateFirstConnected ();
}
Break ;
// If the titlebar X is clicked, destroy app
case WM_CLOSE:
DestroyWindow ( hWnd );
Break ;
case WM_DESTROY:
// Quit the main message pump
PostQuitMessage ( 0 );
Break ;
// Handle button press
Case WM_COMMAND :
// If it was for the near mode control and a clicked event, change near mode
If ( IDC_CHECK_SEATED == LOWORD ( wParam ) && BN_CLICKED == HIWORD ( wParam ))
{
// Toggle out internal state for near mode
m_bSeatedMode = ! m_bSeatedMode ;
 
If ( NULL != m_pNuiSensor )
{
// Set near mode for sensor based on our internal state
m_pNuiSensor -> NuiSkeletonTrackingEnable ( m_hNextSkeletonEvent , m_bSeatedMode ? NUI_SKELETON_TRACKING_FLAG_ENABLE_SEATED_SUPPORT : 0 );
}
}
Break ;
}
Return FALSE ;
}
///
/// Create the first connected Kinect found
///
/// Indicate success or failure
HRESULT CSkeletonBasics :: CreateFirstConnected ()
{
INuiSensor * pNuiSensor ;
Int iSensorCount = 0 ;
HRESULT hr = NuiGetSensorCount ( & iSensorCount );
If ( FAILED ( hr ))
{
Return hr ;
}
// Look at each Kinect sensor
For ( int i = 0 ; i < iSensorCount ; ++ i )
{
// Create the sensor so we can check status, if we can't create it, move on to the next
Hr = NuiCreateSensorByIndex ( i , & pNuiSensor );
If ( FAILED ( hr ))
{
Continue ;
}
 
// Get the status of the sensor, and if connected, then we can initialize it
Hr = pNuiSensor -> NuiStatus ();
If ( S_OK == hr )
{
m_pNuiSensor = pNuiSensor ;
Break ;
}
// This sensor wasn't OK, so release it since we're not using it
pNuiSensor -> Release ();
}
If ( NULL != m_pNuiSensor )
{
// Initialize the Kinect and specify that we'll be using skeleton
hr = m_pNuiSensor -> NuiInitialize (NUI_INITIALIZE_FLAG_USES_SKELETON );
If ( SUCCEEDED ( hr ))
{
// Create an event that will be signaled when skeleton data is available
m_hNextSkeletonEvent = CreateEventW ( NULL , TRUE , FALSE , NULL );
// Open a skeleton stream to receive skeleton data
Hr = m_pNuiSensor -> NuiSkeletonTrackingEnable ( m_hNextSkeletonEvent , 0 );
}
}
If ( NULL == m_pNuiSensor || FAILED ( hr ))
{
SetStatusMessage ( L"No ready Kinect found!" );
Return E_FAIL ;
}
Return hr ;
}
 
///
/// Handle new skeleton data
///
Void CSkeletonBasics :: ProcessSkeleton ()
{
NUI_SKELETON_FRAME skeletonFrame = { 0 };
HRESULT hr = m_pNuiSensor -> NuiSkeletonGetNextFrame ( 0 , & skeletonFrame );
If ( FAILED ( hr ) )
{
Return ;
}
// smooth out the skeleton data
Lithium Batteries

Lithium Batteries 72V,Lithium Batteries 200A Rechargeable,Customized Rechargeable Lithium Battery,Lithium Ion Battery

Shaoxing Honyo International Trading Co., Ltd , https://www.honyopower.com

Posted on