Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleDepth Threshold
collapsetrue
import org.openkinect.processing.*;

Kinect2 kinect;

int lowerThreshold = 0;
int upperThreshold = 750;

void setup() {
  size(512, 424);

  kinect = new Kinect2(this);
  kinect.initDepth();
  kinect.initDevice();
}

void draw() {
  PImage img = kinect.getDepthImage();
  int[] depthMap = kinect.getRawDepth();

  loadPixels();

  for (int x = 0; x < kinect.depthWidth; x++) {
    for (int y = 0; y < kinect.depthHeight; y++) {
      int loc = x+ y * kinect.depthWidth;
      int rawDepth = depthMap[loc];
      
      if (rawDepth > lowerThreshold && rawDepth < upperThreshold) {
        pixels[loc] = color(150, 50, 50);
      } else {
        pixels[loc] = img.pixels[loc];
      }
    }
  }

  updatePixels();
}


Code Block
titleDepth ThresholdBackground Removal and mass centre
collapsetrue
import org.openkinect.processing.*;

Kinect2 kinect;
int[] depthMapRef; 
int[] depthMap;
int Threshold = 50030;
int depthlength;
void setup() {
  size(512, 424);
  depthlength = 512*424;
  kinect 

// Raw location
PVector loc = new PVector(0, 0);
// Interpolated location
PVector lerpedLoc = new Kinect2(thisPVector(0, 0);

 kinect.initDepth()// Depth data
int[] depth;

void kinect.initDevicesetup(); }{
 void drawsize(640, 520);
{
  depthlength = 640*480;
 PImage imgkinect = kinect.getDepthImage(new Kinect2(this);
  depthMap = kinect.getRawDepthinitDepth();
  loadPixelskinect.initDevice();
  if (depthMapRef == nulldepthMapRef = new int[depthlength];
}


void draw() {

  float depthMapRefsumX = depthMap0;
  }
float sumZ = 0;
 for (intfloat xsumY = 0;
x < kinect.depthWidth; x++) {
float count = 0;

  for fill(int255, y255, =255, 01); y < kinect.depthHeight; y++) {
      int loc = x+ y * kinect.depthWidth
  rect(0, 0, width, height);
  PImage img = kinect.getDepthImage();
  depthMap = kinect.getRawDepth();
  
   intif difference(depthMapRef == abs(depthMap[loc]-depthMapRef[loc]);
null) {
     if arrayCopy( difference  > Threshold) {
 depthMap, depthMapRef);
  }
  
   pixels[loc] = color(150, 50, 50img.loadPixels();
  for (int x  } else {= 0; x < kinect.depthWidth; x++) {
    for (int y =  pixels[loc] = img.pixels[loc];
      }
    }
  }

  updatePixels(0; y < kinect.depthHeight; y++) {
      int loc = x+ y * kinect.depthWidth;
      int difference = abs(depthMap[loc]-depthMapRef[loc]);
      if ( difference  > Threshold) {
        sumX += x;
        sumY += y;
        sumZ += depthMap[loc];
        count++;
        img.pixels[loc] = color(150, 50, 50);
      } else {
        img.pixels[loc] = img.pixels[loc];
      }
    }
  }


  img.updatePixels();
  image(img, 0, 0);
  // find the average location of the "body mass" 
  if (count != 0) {
    loc = new PVector(sumX/count, sumY/count, sumZ/count);
  }
  // Interpolating the location with lerp for a smoother animation
  lerpedLoc.x = PApplet.lerp(lerpedLoc.x, loc.x, 0.3f);
  lerpedLoc.y = PApplet.lerp(lerpedLoc.y, loc.y, 0.3f);
  lerpedLoc.z = PApplet.lerp(lerpedLoc.z, loc.z, 0.3f);
  fill(255);
  text("depth:" +lerpedLoc.z+"", lerpedLoc.x+5, lerpedLoc.y);
  ellipse(lerpedLoc.x, lerpedLoc.y, 5, 5);
}



void mousePressed() {
  depthMapRef = depthMapbackground(255);
  arrayCopy(depthMap, depthMapRef);
}