Skip to content
Permalink
Branch: master
Find file Copy path
1 contributor

Users who have contributed to this file

177 lines (136 sloc) 3.96 KB
// press G to record and K to stop
// press X to exit
//jasonhendrik 2018,
// TO DO LIST
// actually dont export points which we choose not to see (background-delete)
// change skip-resolution during program
//
import org.openkinect.freenect.*;
import org.openkinect.processing.*;
import peasy.PeasyCam;
Kinect kinect;
PeasyCam cam;
PrintWriter output;
int myFPS = 25;
float[] depthLookUp = new float[2048];
float fov = PI/6.0;
//float cameraZ = (height/1.0) / tan(fov/1.0);
float cameraZ;
float factor = 200;
int skip = 4;
boolean recording = false;
int deleteBackground = 102;
void setup() {
size(1280, 800, P3D);
frameRate(myFPS);
kinect = new Kinect(this);
kinect.initDepth();
delay(100);
for (int i = 0; i < depthLookUp.length; i++) {
depthLookUp[i] = rawDepthToMeters(i);
}
recording = false;
cam = new PeasyCam(this, 400);
}
void draw() {
//fov = mouseX * 0.1;
background(0);
int[] depth = kinect.getRawDepth();
cameraZ = deleteBackground * 2;
if (!recording)
{
perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*10.0);
translate(width/2, height/2, -50);
for (int x = 0; x < kinect.width; x += skip) {
for (int y = 0; y < kinect.height; y += skip) {
int offset = x + y * kinect.width;
int rawDepth = depth[offset];
PVector v = depthToWorld(x, y, rawDepth);
stroke(255);
pushMatrix();
translate(v.x*factor, v.y*factor, factor-v.z*factor);
point(0, 0);
popMatrix();
}
}
}
if (recording)
{
perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*10.0);
translate(width/2, height/2, -50);
output = createWriter("frame" + (frameCount -1) + ".txt");
int vertexCounter = 0;
for (int x = 0; x < kinect.width; x += skip) {
for (int y = 0; y < kinect.height; y += skip) {
int offset = x + y * kinect.width;
int rawDepth = depth[offset];
PVector v = depthToWorld(x, y, rawDepth);
stroke(255);
pushMatrix();
translate(v.x*factor, v.y*factor, factor-v.z*factor);
point(0, 0);
popMatrix();
output.println( vertexCounter + "," + v.x + "," + v.y + "," + v.z +","+ rawDepth ); // Write the coordinate to the file
vertexCounter += 1;
}
}
output.flush();
output.close();
}
// println("recording is " + recording);
textSize(12);
fill(255);
if (!recording) {
text("recording is " + recording, -190, 160);
} else if (recording) {
fill(255, 0, 0);
text("recording is " + recording, -190, 160);
}
fill(255);
text("FPS is at " + myFPS + " and f/"+myFPS+" is " + frameCount % myFPS, -190, 175);
text("Backgroud-Delete = " + cameraZ, -190, 190);
}
void outputFrames(int vertexCounter, PVector v, int rawDepth) {
output.println( vertexCounter + "," + v.x + "," + v.y + "," + v.z +","+ rawDepth );
output.flush();
output.close();
}
float rawDepthToMeters(int depthValue) {
if (depthValue < 2047) {
return (float)(1.0 / ((double)(depthValue) * -0.0030711016 + 3.3309495161));
}
return 0.0f;
}
PVector depthToWorld(int x, int y, int depthValue) {
final double fx_d = 1.0 / 5.9421434211923247e+02;
final double fy_d = 1.0 / 5.9104053696870778e+02;
final double cx_d = 3.3930780975300314e+02;
final double cy_d = 2.4273913761751615e+02;
PVector result = new PVector();
double depth = depthLookUp[depthValue];//rawDepthToMeters(depthValue);
result.x = (float)((x - cx_d) * depth * fx_d);
result.y = (float)((y - cy_d) * depth * fy_d);
result.z = (float)(depth);
return result;
}
void keyPressed() {
if (key == 'x' || key == 'X') {
exit(); // Stops the program
}
if (key == 'g' || key == 'G') {
recording = true;
}
if (key == 'k' || key == 'K') {
recording = false;
}
if (key == CODED) {
if (keyCode == UP) {
deleteBackground += 1;
}
}
if (key == CODED) {
if (keyCode == DOWN) {
deleteBackground -= 1;
}
}
}
You can’t perform that action at this time.