How to calculate distance between usermap and an object on Kinect2 ?

edited September 2015 in Kinect

Hello there, I wrote a simple program I needed 6 months ago with K1: calculate "usermap" > draw particles in the scene > if the distance between the silhouette [userMapX & userMapY] and a particle is less than 10px, particles color is green, else particles color is red. Now I'm trying to achieve the same thing with Kinect 2 and the lib KinectPV2 on Processing 3 but I feel a little bit stuck here. I wrote a code to test it, it's suppose to change the color of a square if the distance between the usermap and the square is less than 20 (see variable f). If you test it, you will see that the square changes color depending on the way you move with the kinect, but it doesn't match with the body tracked.. Any advice or help with this ?

Charles -


    import KinectPV2.*;
    KinectPV2 kinect;
    int f = 20;
    float dist;
    int c;
    boolean detect;
    
    void setup() {   
      size(512, 424);
      frameRate(30);
      kinect = new KinectPV2(this);   
      kinect.enableBodyTrackImg(true);
      kinect.enableDepthImg(false);
      kinect.init();
    }  
    
    void draw() {   
    background(255);    
    image(kinect.getBodyTrackImage(), 0, 0);   
    image(kinect.getDepthImage(), 0, 0);
    int [] rawData = kinect.getRawBodyTrack();
    
    //For 1/8 of my pixels
    for (int i = 0; i < rawData.length; i +=2){
    
      //if pixel is part of the user
      if (rawData[i] != 255){
         float x = (i%width)+1;
         float y = int(i/height);
        
        //draw body with blue lines
        stroke(0,0,255);
        line(x,y,x,y);
    
        //calculate distance between usermapx, usermapy, and rectangle position
        dist = dist(x,f,y,f);
        //if collision found, rectangle is green 
        if (dist < = f) {
         detect=true;
         //else, red
        } else{
          detect=false;
        }
      }
    
    }
    if (detect == false){
      c = color(255,0,0);
    }
    
    if (detect == true){
     c =  color(0,255,0);
    }
    
    pushMatrix();
    fill(c);
    rect(f,f,50,50);
    popMatrix();
    } 
Sign In or Register to comment.