Can't seem to get the loop right. Help desperately needed...
in
Contributed Library Questions
•
11 months ago
Hello World.
I'm fairly new to Processing, but have been coding different other languages on and off for some years. Currently I'm working on a Kinect project, and have the code doing what I want apart from a couple of weird outcomes. The following code "works", but instead of outputting a series of frames, all the files contain the same point cloud. I have tested the output loops to see if that loop is working, and it is. The problem seems to be somewhere else. The deadline is getting closer and I have already spent way to much time trying to figure this out. Any suggestions? Pretty please?
- /* --------------------------------------------------------------------------
- * SimpleOpenNI DepthMap3d Test
- * --------------------------------------------------------------------------
- * Processing Wrapper for the OpenNI/Kinect library
- * http://code.google.com/p/simple-openni
- * --------------------------------------------------------------------------
- * prog: Max Rheiner / Interaction Design / zhdk / http://iad.zhdk.ch/
- * date: 02/16/2011 (m/d/y)
- * ----------------------------------------------------------------------------
- */
- import SimpleOpenNI.*;
- SimpleOpenNI context;
- float zoomF =0.3f;
- float rotX = radians(180); // by default rotate the hole scene 180deg around the x-axis,
- // the data from openni comes upside down
- float rotY = radians(0);
- // Size of kinect image
- int w = 640;
- int h = 480;
- float a = 0;
- // treshold filter initial value
- int fltValue = 950;
- // writing state indicator
- boolean write = true;
- // "recording" object. each vector element holds a coordinate map vector
- Vector <Object> recording = new Vector<Object>();
- void setup()
- {
- frameRate(25);
- size(640,480,P3D); // strange, get drawing error in the cameraFrustum if i use P3D, in opengl there is no problem
- //context = new SimpleOpenNI(this,SimpleOpenNI.RUN_MODE_SINGLE_THREADED);
- context = new SimpleOpenNI(this);
- // disable mirror
- context.setMirror(false);
- // enable depthMap generation
- if(context.enableDepth() == false)
- {
- println("Can't open the depthMap, maybe the camera is not connected!");
- exit();
- return;
- }
- stroke(255,255,255);
- smooth();
- perspective(radians(45),
- float(width)/float(height),
- 10,150000);
- }
- void draw()
- {
- // update the cam
- context.update();
- background(0,0,0);
- //textMode(SCREEN);
- //text("Kinect FR: " + " noAvail " + "\nProcessing FR: " + (int)frameRate,10,16);
- translate(width/2, height/2, -50);
- rotateX(rotX);
- rotateY(rotY);
- scale(zoomF);
- int[] depthMap = context.depthMap();
- int skip = 4; // to speed up the drawing, draw every third point
- int offset = 0;
- PVector realWorldPoint;
- int index = 0;
- PVector[] frame = new PVector[19200];
- translate(0,0,-1000); // set the rotation center of the scene 1000 infront of the camera
- stroke(255);
- PVector[] realWorldMap = context.depthMapRealWorld();
- for(int x=0; x<w; x+=skip)
- {
- for(int y=0; y<h; y+=skip)
- {
- offset = x + y * w;
- // draw the projected point
- // realWorldPoint = context.depthMapRealWorld()[index];
- realWorldPoint = realWorldMap[offset];
- //print(realWorldPoint);
- point(realWorldPoint.x,realWorldPoint.y,realWorldPoint.z); // make realworld z negative, in the 3d drawing coordsystem +z points in the direction of the eye
- frame[index] = realWorldPoint;
- //print(realWorldPoint);
- index++;
- //println("x: " + x + " y: " + y);
- }
- }
- print(frame[10000]);
- if (write == true) {
- recording.add(frame);
- }
- //print(recording.length);
- // draw the kinect cam
- context.drawCamFrustum();
- }
- int currentFile = 0;
- void saveFile() {
- }
- void keyPressed() { // Press a key to save the data
- if (key == '1')
- {
- fltValue += 50;
- println("fltValue: " + fltValue);
- }
- else if (key == '2')
- {
- fltValue -= 50;
- println("fltValue: " + fltValue);
- }
- else if (key=='4'){
- if (write == true) {
- write = false;
- println( "recorded " + recording.size() + " frames.");
- // saveFile();
- // save
- Enumeration e = recording.elements();
- println("Stopped Recording " + currentFile);
- int i = 0;
- while (e.hasMoreElements()) {
- // Create one directory
- boolean success = (new File("out"+currentFile)).mkdir();
- PrintWriter output = createWriter("out"+currentFile+"/frame" + i++ +".txt");
- //output.println("This is frame " + i);
- PVector [] frameA = (PVector []) e.nextElement();
- //int fL = int(frame.length);
- //print(fL);
- //for (int j = 0; j < frame.length; j++) {
- for (int j = 0; j < frameA.length; j++) {
- //print(j + ", " + frameA[j].x + ", " + frameA[j].y + ", " + frameA[j].z + " ||| ");
- output.println("frame " + i + " | " +j + ", " + frameA[j].x + ", " + frameA[j].y + ", " + frameA[j].z );
- }
- output.flush(); // Write the remaining data
- output.close();
- }
- currentFile++;
- }
- }
- else if (key == '3') {
- println("Started Recording "+currentFile);
- recording.clear();
- write = true;
- }
- }
- frame 116 | 0, -1483.2522, 1112.4392, 2669.0
- frame 116 | 1, -1494.9226, 1102.5055, 2690.0
- frame 116 | 2, -1518.8192, 1101.1439, 2733.0
- frame 116 | 3, -1543.2715, 1099.5809, 2777.0
- frame 116 | 4, -1594.3989, 1116.0792, 2869.0
- frame 116 | 5, -1607.7365, 1105.3188, 2893.0
- frame 116 | 6, -1648.8607, 1112.981, 2967.0
- frame 116 | 7, -1707.7686, 1131.3966, 3073.0
- frame 116 | 8, -1707.7686, 1110.0496, 3073.0
- frame 116 | 9, -1723.329, 1098.6222, 3101.0
- frame 116 | 10, -1770.5664, 1106.604, 3186.0
- frame 116 | 11, -1821.1381, 1115.447, 3277.0
- frame 116 | 12, -1856.7051, 1114.0231, 3341.0
- frame 116 | 13, -1892.8278, 1112.0363, 3406.0
- frame 116 | 14, -1892.8278, 1088.376, 3406.0
- frame 116 | 15, -1911.7227, 1075.344, 3440.0
- frame 116 | 16, -1991.1925, 1095.1559, 3583.0
- frame 116 | 17, -2012.3103, 1081.6168, 3621.0
- frame 116 | 18, -2033.4282, 1067.5498, 3659.0
Øyvind
1