#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Mapping depth to color with Open Kinect

edited February 2017 in Kinect

Hi, I'm an absolute beginner with Kinect & Processing and I'm stuck with something that I believe should be quite basic stuff. I'm trying to make an interactive video wall so that when a person approaches the wall, color changes. I'm using openKinect and Kinect V2 and in this example code I'm trying to change the color of the ellipse. I managed to map the x coordinates (rx) to color values but I haven't succeeded in mapping the depth values to the color. I'd really appreciate your help!

Here's my code (based on Shiffman's Closest point tracking):

``````import org.openkinect.processing.*;

// Kinect Library object
Kinect2 kinect2;

float minThresh = 480;
float maxThresh = 830;
PImage img;

void setup() {
size(512, 424);
kinect2 = new Kinect2(this);
kinect2.initDepth();
kinect2.initDevice();
img = createImage(kinect2.depthWidth, kinect2.depthHeight, RGB);
}

void draw() {
background(0);

PImage dImg = kinect2.getDepthImage();
//image(dImg, 0, 0);

// Get the raw depth as array of integers
int[] depth = kinect2.getRawDepth();

int record = 4500;
int rx = 0;
int ry = 0;

for (int x = 0; x < kinect2.depthWidth; x++) {
for (int y = 0; y < kinect2.depthHeight; y++) {
int offset = x + y * kinect2.depthWidth;
int d = depth[offset];

if (d > minThresh && d < maxThresh && x > 100 && y > 50) {
img.pixels[offset] = color(255, 0, 150);

if (d < record) {
record = d;
rx = x;
ry = y;
}

} else {
img.pixels[offset] = dImg.pixels[offset];
}
}
}
img.updatePixels();
image(img,0,0);

float r = map(record, 0, 4500, 0, 255);

fill(r);
ellipse(rx, ry, 60, 60);
}
``````

• edit post, highlight code, press ctrl-o to format the code.

• Good point, thanks @koogs!

• ctrl-t in the processing editor will indent it nicely.

I think I found the answer, the code is below. If anyone has ideas on how to make this better or how to get rid of the flickering, would love to hear your suggestions!

``````import org.openkinect.processing.*;

// Kinect Library object
Kinect2 kinect2;

float minThresh = 480;
float maxThresh = 830;
PImage img;

void setup() {
size(512, 424);
kinect2 = new Kinect2(this);
kinect2.initDepth();
kinect2.initDevice();
img = createImage(kinect2.depthWidth, kinect2.depthHeight, RGB);
}

void draw() {
background(0);

PImage dImg = kinect2.getDepthImage();
//image(dImg, 0, 0);

// Get the raw depth as array of integers
int[] depth = kinect2.getRawDepth();

//int record = 4500;
int record = kinect2.depthHeight;
int rx = 0;
int ry = 0;

for (int x = 0; x < kinect2.depthWidth; x++) {
for (int y = 0; y < kinect2.depthHeight; y++) {
int offset = x + y * kinect2.depthWidth;
int d = depth[offset];

if (d > minThresh && d < maxThresh && x > 100 && y > 50) {
img.pixels[offset] = color(255, 0, 150);

if (y < record) {
record = y;
rx = x;
ry = y;
}

} else {
img.pixels[offset] = dImg.pixels[offset];
}
}
}
img.updatePixels();
image(img,0,0);

int rdOffset= rx+ry*kinect2.depthWidth;
int rd= depth[rdOffset];

float r= map(rd,minThresh,maxThresh,0,255);
fill(r);
ellipse(rx, ry, 100, 100);
}
``````