In the tutorial session, Yee Siang assist me to point out and delete the coding part that having conflict that cause the bug.
The meeting only stay around 1hour as this is minor debug section.
Below is the 3 part of the coding after debug.
Part 1 - Average tracking point
"
import org.openkinect.*;
import org.openkinect.processing.*;
KinectTracker tracker;
Kinect kinect;
int ssizeW = 640;
int ssizeH = 480;
float heatmap[][][] = new float[2][ssizeW][ssizeH];
int index = 0;
Gradient g;
int depth[];
void setup() {
size(ssizeW, ssizeH);
kinect = new Kinect(this);
tracker = new KinectTracker();
g = new Gradient();
g.addColor(color(0, 0, 0));
g.addColor(color(102, 0, 102));
g.addColor(color(0, 144, 255));
g.addColor(color(0, 255, 207));
g.addColor(color(51, 204, 102));
g.addColor(color(111, 255, 0));
g.addColor(color(191, 255, 0));
g.addColor(color(255, 240, 0));
g.addColor(color(255, 153, 102));
g.addColor(color(204, 51, 0));
g.addColor(color(153, 0, 0));
//initialize heat map array value
for(int i = 0; i < ssizeW; ++i)
for(int j = 0; j < ssizeH; ++j)
heatmap[index][i][j] = 0.0;
}
void draw() {
background(255);
// Run the tracking analysis
tracker.track();
// Show the image
//tracker.display();
// Let's draw the raw location
PVector v1 = tracker.getPos();
{
player.play();
}
PImage img = kinect.getDepthImage();
depth = kinect.getRawDepth();
for(int x = 0; x < ssizeW; x+=16)
{
for(int y = 0; y < ssizeH; y+=16)
{
int offset = ssizeW-x-1+y*ssizeW;
int rawDepth = depth[offset];
if (rawDepth < tracker.getThreshold())
{
apply_heat(x, y, 10, 1);
}
else
{
apply_heat(x, y, 15, -.25);
}
}
}
{
}
update_heatmap();
render_heatmap();
fill(50,100,250,200);
noStroke();
ellipse(v1.x,v1.y,20,20);
int t = tracker.getThreshold();
fill(255);
text("threshold: " + t + " " + "framerate: " + (int)frameRate + " " + "UP increase threshold, DOWN decrease threshold",10,470);
}
void keyPressed() {
int t = tracker.getThreshold();
if (key == CODED) {
if (keyCode == UP) {
t+=5;
tracker.setThreshold(t);
}
else if (keyCode == DOWN) {
t-=5;
tracker.setThreshold(t);
}
}
}
void stop() {
tracker.quit();
super.stop();
}
void update_heatmap()
{
for(int i = 0; i < ssizeW; i++)
{
for(int j = 0; j < ssizeH; j++)
{
heatmap[index ^ 1][i][j] = calc_pixel(i, j);
}
}
index ^= 1;
}
void render_heatmap()
{
for(int i = 0; i < ssizeW; i++)
{
for(int j = 0; j < ssizeH; j++)
{
set(i, j, g.getGradient(heatmap[index][i][j]));
}
}
}
float calc_pixel(int i, int j)
{
float total = 0.0;
int count = 0;
for(int ii = -1; ii < 2; ii++)
{
for(int jj = -1; jj < 2; jj++)
{
if(i + ii < 0 || i + ii >= width || j + jj < 0 || j + jj >= height)
continue;
count++;
total += heatmap[index][i + ii][j + jj];
}
}
return total / count;
}
void apply_heat(int i, int j, int r, float delta)
{
for(int ii = -(r / 2); ii < (r / 2); ii++)
{
for(int jj = -(r / 2); jj < (r / 2); jj++)
{
if(i + ii < 0 || i + ii >= width || j + jj < 0 || j + jj >= height)
continue;
heatmap[index][i + ii][j + jj] += delta;
heatmap[index][i + ii][j + jj] = constrain(heatmap[index][i + ii][j + jj], 0.0, 20.0);
}
}
}
"
2nd Part - Kinect Tracker
"
class KinectTracker {
// Size of kinect image
int kw = 640;
int kh = 480;
int threshold = 745;
// Raw location
PVector loc;
// Interpolated location
PVector lerpedLoc;
// Depth data
int[] depth;
PImage display;
KinectTracker() {
kinect.start();
kinect.enableDepth(true);
kinect.processDepthImage(true);
display = createImage(kw,kh,PConstants.RGB);
loc = new PVector(0,0);
lerpedLoc = new PVector(0,0);
}
void track() {
depth = kinect.getRawDepth();
if (depth == null) return;
float sumX = 0;
float sumY = 0;
float count = 0;
for(int x = 0; x < kw; x++) {
for(int y = 0; y < kh; y++) {
int offset = kw-x-1+y*kw;
int rawDepth = depth[offset];
if (rawDepth < threshold) {
sumX += x;
sumY += y;
count++;
}
}
}
if (count != 0) {
loc = new PVector(sumX/count,sumY/count);
}
lerpedLoc.x = PApplet.lerp(lerpedLoc.x, loc.x, 0.3f);
lerpedLoc.y = PApplet.lerp(lerpedLoc.y, loc.y, 0.3f);
}
PVector getLerpedPos() {
return lerpedLoc;
}
PVector getPos() {
return loc;
}
void display() {
PImage img = kinect.getDepthImage();
if (depth == null || img == null) return;
display.loadPixels();
for(int x = 0; x < kw; x++) {
for(int y = 0; y < kh; y++) {
// mirroring image
int offset = kw-x-1+y*kw;
// Raw depth
int rawDepth = depth[offset];
int pix = x+y*display.width;
if (rawDepth < threshold) {
// A red color instead
display.pixels[pix] = color(150,50,50);
}
else {
display.pixels[pix] = img.pixels[offset];
}
}
}
display.updatePixels();
// Draw the image
image(display,0,0);
}
void quit() {
kinect.quit();
}
int getThreshold() {
return threshold;
}
void setThreshold(int t) {
threshold = t;
}
}
3rd Part- Gradient
"
class Gradient
{
ArrayList colors;
Gradient()
{
colors = new ArrayList();
}
void addColor(color c)
{
colors.add(c);
}
color getGradient(float value)
{
if(colors.size() == 0)
return #000000;
if(value <= 0.0)
return (color)(Integer) colors.get(0);
if(value >= colors.size() - 1)
return (color)(Integer) colors.get(colors.size() - 1);
int color_index = (int)value;
color c1 = (color)(Integer) colors.get(color_index);
color c2 = (color)(Integer) colors.get(color_index + 1);
return lerpColor(c1, c2, value - color_index);
}
}
"