处理 - 弃用的OpenKinect库

时间:2015-10-30 02:03:17

标签: processing

我正在尝试为this music video复制Kinect的项目,但代码严重过时了。

经过数周的搜索,我还没有发现任何相关信息。

如果有人向我指出以下代码中弃用的内容,我将非常感谢:

(我正在使用Processing 3

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

import java.io.*;

// Kinect Library object
Kinect kinect;

float a = 0;

// Size of kinect image
int w = 640;
int h = 480;

// writing state indicator
boolean write = false;

// treshold filter initial value
int fltValue = 950;


// "recording" object. each vector element holds a coordinate map vector
Vector <Object> recording = new Vector<Object>(); 


// We'll use a lookup table so that we don't have to repeat the math over and     over
float[] depthLookUp = new float[2048];

 void setup() {
 size(800,600,P3D);
 kinect = new Kinect(this);
 kinect.start();
 kinect.enableDepth(true);
 // We don't need the grayscale image in this example
 // so this makes it more efficient
 kinect.processDepthImage(false);

 // Lookup table for all possible depth values (0 - 2047)
 for (int i = 0; i < depthLookUp.length; i++) {
  depthLookUp[i] = rawDepthToMeters(i);
 }

}

void draw() {

background(0);
fill(255);
textMode(SCREEN);
text("Kinect FR: " + (int)kinect.getDepthFPS() + "\nProcessing FR: " +   (int)frameRate,10,16);

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

// We're just going to calculate and draw every 4th pixel (equivalent of 160x120)
int skip = 4;

// Translate and rotate
translate(width/2,height/2,-50);
rotateY(a);

//noStroke();
 //lights();


int index = 0;


PVector[] frame = new PVector[19200];


for(int x=0; x<w; x+=skip) {
  for(int y=0; y<h; y+=skip) {
    int offset = x+y*w;

  // Convert kinect data to world xyz coordinate
  int rawDepth = depth[offset];

  boolean flt = true;
  PVector v = depthToWorld(x,y,rawDepth);
  if (flt && rawDepth > fltValue)
  {
    v = depthToWorld(x,y,2047);
  }

  frame[index] = v;

  index++;   

  stroke(map(rawDepth,0,2048,0,256));
  pushMatrix();
  // Scale up by 200
  float factor = 400;
  translate(v.x*factor,v.y*factor,factor-v.z*factor);
  //sphere(1);
  point(0,0);

  //line (0,0,1,1);
  popMatrix();
  }
 }

if (write == true) {
recording.add(frame);    

}


// Rotate
//a += 0.015f;
}

// These functions come from:http://graphics.stanford.edu/~mdfisher/Kinect.html
 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 stop() {
kinect.quit();
super.stop();
}


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");
    PVector [] frame = (PVector []) e.nextElement();

    for (int j = 0; j < frame.length; j++) {
     output.println(j + ", " + frame[j].x + ", " + frame[j].y + ", " + frame[j].z );

    }
    output.flush(); // Write the remaining data
    output.close();
  }
  currentFile++;



  }
}
else if (key == '3') {
    println("Started Recording "+currentFile);
    recording.clear();

    write = true;
 }

}

1 个答案:

答案 0 :(得分:0)

如果代码有效,那么我不会太担心它。不推荐使用只是意味着可以使用更新的版本,而不是旧版本停止工作。

但是,如果代码不起作用,那么无论如何更新到更新的库可能是一个好主意。查看Processing主页的library部分,其中列出了几个Kinect库。

事实上,其中一个库是您正在使用的旧库的更新版本:Open Kinect for Processing

编辑:您提到的两个错误看起来都是由于缺少import语句。您需要同时导入VectorEnumeration才能使用它们:

import java.util.Vector;
import java.util.Enumeration;