import org.openkinect.processing.*;
Kinect2 kinect;
int[] depthMapRef;
int[] depthMap;
int Threshold = 30;
int depthlength;
// Raw location
PVector loc = new PVector(0, 0);
// Interpolated location
PVector lerpedLoc = new PVector(0, 0);
// Depth data
int[] depth;
void setup() {
size(640, 520);
depthlength = 640*480;
kinect = new Kinect2(this);
kinect.initDepth();
kinect.initDevice();
depthMapRef = new int[depthlength];
}
void draw() {
float sumX = 0;
float sumZ = 0;
float sumY = 0;
float count = 0;
fill(255, 255, 255, 1);
rect(0, 0, width, height);
PImage img = kinect.getDepthImage();
depthMap = kinect.getRawDepth();
if (depthMapRef == null) {
arrayCopy(depthMap, depthMapRef);
}
img.loadPixels();
for (int x = 0; x < kinect.depthWidth; x++) {
for (int y = 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() {
background(255);
arrayCopy(depthMap, depthMapRef);
}
|