資源簡介
Kinect 2.0 骨骼顯示及畫出骨骼結(jié)合opencv 顯示。
代碼片段和文件信息
#include?“myKinect.h“
#include?
///?Initializes?the?default?Kinect?sensor
HRESULT?CBodyBasics::InitializeDefaultSensor()
{
//用于判斷每次讀取操作的成功與否
HRESULT?hr;
//搜索kinect
hr?=?GetDefaultKinectSensor(&m_pKinectSensor);
if?(FAILED(hr)){
return?hr;
}
//找到kinect設(shè)備
if?(m_pKinectSensor)
{
//?Initialize?the?Kinect?and?get?coordinate?mapper?and?the?body?reader
IBodyframeSource*?pBodyframeSource?=?NULL;//讀取骨架
IDepthframeSource*?pDepthframeSource?=?NULL;//讀取深度信息
IBodyIndexframeSource*?pBodyIndexframeSource?=?NULL;//讀取背景二值圖
//打開kinect
hr?=?m_pKinectSensor->Open();
//coordinatemapper
if?(SUCCEEDED(hr))
{
hr?=?m_pKinectSensor->get_CoordinateMapper(&m_pCoordinateMapper);
}
//bodyframe
if?(SUCCEEDED(hr))
{
hr?=?m_pKinectSensor->get_BodyframeSource(&pBodyframeSource);
}
if?(SUCCEEDED(hr))
{
hr?=?pBodyframeSource->OpenReader(&m_pBodyframeReader);
}
//depth?frame
if?(SUCCEEDED(hr)){
hr?=?m_pKinectSensor->get_DepthframeSource(&pDepthframeSource);
}
if?(SUCCEEDED(hr)){
hr?=?pDepthframeSource->OpenReader(&m_pDepthframeReader);
}
//body?index?frame
if?(SUCCEEDED(hr)){
hr?=?m_pKinectSensor->get_BodyIndexframeSource(&pBodyIndexframeSource);
}
if?(SUCCEEDED(hr)){
hr?=?pBodyIndexframeSource->OpenReader(&m_pBodyIndexframeReader);
}
SafeRelease(pBodyframeSource);
SafeRelease(pDepthframeSource);
SafeRelease(pBodyIndexframeSource);
}
if?(!m_pKinectSensor?||?FAILED(hr))
{
std::cout?<“Kinect?initialization?failed!“?< return?E_FAIL;
}
//skeletonImg用于畫骨架、背景二值圖的MAT
skeletonImg.create(cDepthHeight?cDepthWidth?CV_8UC3);
skeletonImg.setTo(0);
//depthImg用于畫深度信息的MAT
depthImg.create(cDepthHeight?cDepthWidth?CV_8UC1);
depthImg.setTo(0);
return?hr;
}
///?Main?processing?function
void?CBodyBasics::Update()
{
//每次先清空skeletonImg
skeletonImg.setTo(0);
//如果丟失了kinect,則不繼續(xù)操作
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? *skeletonData?=?bodyIndexArray[j];?++skeletonData;
*skeletonData?=?bodyIndexArray[j];?++skeletonData;
*skeletonData?=?bodyIndexArray[j];?++skeletonData;
}
delete[]?bodyIndexArray;
}
SafeRelease(pBodyIndexframe);//必須要釋放,否則之后無法獲得新的frame數(shù)據(jù)
//-----------------------獲取深度數(shù)據(jù)并顯示--------------------------
if?(SUCCEEDED(hr)){
hr?=?m_pDepthframeReader->AcquireLat
- 上一篇:pic18系列單片機C語言程序例程
- 下一篇:基于C++的三幀差法
評論
共有 條評論