当前位置:编程问答 > 网站相关 >

kinect 学习笔记二(深度图像的利用--抠取用户躯体)

今天主要把深度数据和骨骼还有视频数据同步起来。算是上一次的三个的组合吧。期间遇到点白痴的问题整治了一下午:带ID的数据的ID是否准确。而且差点把自己的质疑给发到博客上来,竟然是自己代码的错误。伤心伤心。。。
 
直接把代码贴出来吧,也许有人觉得贴代码没水平,嗨,给自己留个小版本说不定以后还用得着,而且自信自己代码写的还算是规范,方便后来人嘛。再有一点,那个getTheContour函数画蛇添足了,实际直接利用深度数据的ID就可以抠出任务的区域。
 
先上实验结果:这是实时图像

\

 
然后是深度图像、骨骼图像和抠出的人体区域

\

\

\
 
 
这是代码,希望可以帮助到大家,当然,如果有错误欢迎指正:
 
#include <iostream> 
#include <fstream> 
#include  "math.h" 
#include "Windows.h"     
#include "MSR_NuiApi.h"     
#include "cv.h"     
#include "highgui.h"  
 
using namespace std; 
 
bool tracked[NUI_SKELETON_COUNT]={FALSE}; 
CvPoint skeletonPoint[NUI_SKELETON_COUNT][NUI_SKELETON_POSITION_COUNT]={cvPoint(0,0)}; 
CvPoint colorPoint[NUI_SKELETON_COUNT][NUI_SKELETON_POSITION_COUNT]={cvPoint(0,0)}; 
 
void getColorImage(HANDLE &colorEvent, HANDLE &colorStreamHandle, IplImage *colorImage); 
void getDepthImage(HANDLE &depthEvent, HANDLE &depthStreamHandle, IplImage *depthImage); 
void getSkeletonImage(HANDLE &skeletonEvent, IplImage *skeletonImage, IplImage *colorImage, IplImage *depthImage); 
void drawSkeleton(IplImage *image, CvPoint pointSet[], int witchone); 
void getTheContour(IplImage *image, int whichone, IplImage *mask);//得到各个人物的轮廓 
 
int main() 

    IplImage *colorImage = cvCreateImage(cvSize(640, 480), 8, 3); 
    IplImage *depthImage = cvCreateImage(cvSize(320, 240), 8, 3); 
    IplImage *skeletonImage = cvCreateImage(cvSize(320, 240), 8, 3); 
    IplImage *mask = cvCreateImage(cvSize(320, 240), 8, 3); 
 
    HANDLE colorEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); 
    HANDLE depthEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); 
    HANDLE skeletonEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); 
 
    HANDLE colorStreamHandle = NULL; 
    HANDLE depthStreamHandle = NULL; 
 
    HRESULT hr = NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON);   
    if( hr != S_OK )   
    {   
        cout<<"NuiInitialize failed"<<endl;   
        return hr;   
    } 
 
    hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480, NULL, 4, colorEvent, &colorStreamHandle); 
    if( hr != S_OK )   
    {   
        cout<<"Open the color Stream failed"<<endl; 
        NuiShutdown(); 
        return hr;   
    } 
    hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_320x240, NULL, 2, depthEvent, &depthStreamHandle); 
    if( hr != S_OK )   
    {   
        cout<<"Open the depth Stream failed"<<endl; 
        NuiShutdown(); 
        return hr;   
    } 
    hr = NuiSkeletonTrackingEnable( skeletonEvent, 0 );//打开骨骼跟踪事件   
    if( hr != S_OK )   
    {   
        cout << "NuiSkeletonTrackingEnable failed" << endl;   
        NuiShutdown(); 
        return hr;   
    } 
  
    //HANDLE hEvents[3]; 
    //int nEventIdx; 
    //hEvents[0] = colorEvent; 
    //hEvents[1] = depthEvent; 
    //hEvents[2] = skeletonEvent; 
    int a=0; 
    while (1) 
    { 
        //刚开始想用WaitForMultipleObjects,但是怎么调都是值显示视频,深度很少显示,骨骼直接不显示。 
        //自己理解不深入,只能临时用WaitForSingleObject这样了。 
        //nEventIdx = WaitForMultipleObjects(sizeof(hEvents)/sizeof(hEvents[0]), hEvents, FALSE, 10); 
        ////cout << nEventIdx << endl; 
 
        //switch(nEventIdx) 
        //{ 
        //case 0: 
        //  //if(WaitForSingleObject(colorEvent, 0)==0) 
        //  getColorImage(colorEvent, colorStreamHandle, colorImage); 
        //  break; 
        //case 1: 
        //  //if(WaitForSingleObject(depthEvent, 0)==0) 
        //  getDepthImage(depthEvent, depthStreamHandle, depthImage); 
        //  break; 
        //case 2: 
        //  //if(WaitForSingleObject(skeletonEvent, 0)==0) 
        //  getSkeletonImage(skeletonEvent,
补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,