Hi everyone,
I need a little guidance on a small project we’re doing at my University. What I’m trying to do, is recording and playing a point cloud created with Kinect. I’m using as a base the ofxKinect example, and I’ve created two functions saveKinectDepth() and playKinectDepth() which is almost a copy/paste of drawPointCloud(). The first function generates for each frame a .dat file that contains the Z measurements, while the latter just reads the files and generates the mesh.
At the moment this solution works but I have few problems:
- the play function it’s not fast, it plays at 10fps (even with an SSD)
- the image played back is scaled down and it shouldn’t be a problem with the easy cam
Here are the two functions, you know for sure a better way to do it, thanks.
void testApp::saveKinectDepth() {
// create a filename according to the curent frame count
string fileName("KinectData/frame" + ofToString(frameCount) + ".dat");
// create and open a new .dat file
ofFile data;
data.open(ofToDataPath(fileName), ofFile::WriteOnly, false);
data.create();
// loop through the array and save data to the .dat file
int step = 2;
for(int y = 0; y < 480; y += step) {
for(int x = 0; x < 640; x += step) {
data << kinect.getDistanceAt(x, y) << "\n";
}
}
// close the file
data.close();
frameCount++;
}
void testApp::playKinectDepth()
{
// create a filename according to the curent frame count
string fileName("KinectData/frame" + ofToString(frameCount) + ".dat");
// open a .dat file
ofFile data;
data.open(ofToDataPath(fileName), ofFile::ReadOnly, false);
// if the file doesn't exist restart the loop through files
if (!data.exists()) {
frameCount = 0;
fileName = "KinectData/frame0.dat";
}
vector<string> zData = ofSplitString(ofBufferFromFile(fileName), "\n");
ofMesh mesh;
mesh.setMode(OF_PRIMITIVE_POINTS);
// loop through the zData array and add points to the mesh
int count = 0;
int step = 2;
for(int y = 0; y < 480; y += step) {
for(int x = 0; x < 640; x += step) {
float z = ofToFloat(zData[count]);
// draw a point on the mesh only when there is a Z value
if (z != 0) {
mesh.addColor(0);
mesh.addVertex(ofVec3f(x, y, z));
}
count++;
}
}
glPointSize(2);
ofPushMatrix();
ofScale(1.1, -1.1, -1.1); // the projected points are 'upside down' and 'backwards'
ofTranslate(0, 0, -1000); // center the points a bit
glEnable(GL_DEPTH_TEST);
mesh.drawVertices();
glDisable(GL_DEPTH_TEST);
ofPopMatrix();
// close the file
data.close();
frameCount++;
}