Reputation: 1373
I am trying to adapt Daniel Shiffman's code for a basic Kinect (v2) depth cloud using Processing, but there is always a pixel right in the middle of the screen that won't go anywhere, which is pretty annoying. This is an example of what I mean.
You can see it seems to be right at the front and doesnt move when anything in the field of view moves.
Here is the code that i used to generate the image above, (which is a very stripped down version of what i am trying to do)
// imports for openkinect
import org.openkinect.freenect2.*;
import org.openkinect.processing.*;
import java.nio.FloatBuffer;
// dots size is dot_size*skip
int dot_size = 2;
// step size when iterating through pixel array
int skip = 5;
// Kinect Library object
Kinect2 kinect2;
// Angle for rotation
float a = 3.1;
void setup() {
// Rendering in P3D
size(1500,1000,P3D);
// start the kinect
kinect2 = new Kinect2(this);
kinect2.initDepth();
kinect2.initDevice();
smooth(16);
// Black background
background(0);
}
void draw() {
background(0);
// Translate and rotate
pushMatrix();
translate(width/2, height/2,50);
rotateY(a);
// get current depth information
int[] depth = kinect2.getRawDepth();
// read depth pixels in kinect window bounds
for (int x = 0; x < kinect2.depthWidth; x+=skip) {
for (int y = 0; y < kinect2.depthHeight; y+=skip) {
// compute offset for 1D depth array
int offset = x + y * kinect2.depthWidth;
// get the depth
int d = depth[offset];
//calculte the x, y, z camera position based on the depth information
PVector point = depthToPointCloudPos(x, y, d);
// compute depth modifier for colours and such
float depth_modifier = map(point.z,1000,2048,255,0);
fill(depth_modifier);
pushMatrix();
translate(point.x,point.y,point.z);
circle(0,0,skip*dot_size);
popMatrix();
}
}
popMatrix();
}
//calculte the xyz camera position based on the depth data
PVector depthToPointCloudPos(int x, int y, float depthValue) {
PVector point = new PVector();
point.z = (depthValue);// / (1.0f); // Convert from mm to meters
point.x = (x - CameraParams.cx) * point.z / CameraParams.fx;
point.y = (y - CameraParams.cy) * point.z / CameraParams.fy;
return point;
}
Here is the contents of CameraParams.pde
to make it easy for someone to replicate:
//camera information based on the Kinect v2 hardware
static class CameraParams {
static float cx = 254.878f;
static float cy = 205.395f;
static float fx = 365.456f;
static float fy = 365.456f;
static float k1 = 0.0905474;
static float k2 = -0.26819;
static float k3 = 0.0950862;
static float p1 = 0.0;
static float p2 = 0.0;
}
Does anyone have any idea where this dot is coming from, and how I can get rid of it?
Upvotes: 6
Views: 176
Reputation: 1373
I managed to fix it, it was a bit of a hacky solution, but it worked.. the pixel seems to be showing up at exactly 0 depth, so I just added the code:
if (d == 0){
continue;
}
after it calculated the depth of the current pixel, and that worked fine. I thought I would answer my own question in case anyone else had similar problems.
I know its still lurking in the data though, so if anyone has any idea of the source of the problem, I would still like to know why it is occurring - I am pretty new to the Processing language, so I'm sure there is just something I'm missing.
But for now, at least it is "fixed"!
Upvotes: 4