2

処理用の Kinect ライブラリ (http://www.shiffman.net/2010/11/14/kinect-and-processing/) から例を実行していますが、次の行を指す NullPointerException が発生することがあります。

  int rawDepth = depth[offset];

深度配列は次の行で作成されます。

  int[] depth = kinect.getRawDepth();

NullPointerException が何であるか正確にはわかりません。多くのグーグル検索は実際には役に立ちませんでした。コードが 70% の確率でコンパイルされ、予想外にエラーが返されるのは奇妙に思えます。ハードウェア自体が影響している可能性はありますか?

役立つ場合は、例全体を次に示します。

// Daniel Shiffman
// Kinect Point Cloud example
// http://www.shiffman.net
// https://github.com/shiffman/libfreenect/tree/master/wrappers/java/processing

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

// Kinect Library object
Kinect kinect;

float a = 0;

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


// 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);

  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];
      PVector v = depthToWorld(x,y,rawDepth);

      stroke(255);
      pushMatrix();
      // Scale up by 200
      float factor = 200;
      translate(v.x*factor,v.y*factor,factor-v.z*factor);
      // Draw a point
      point(0,0);
      popMatrix();
    }
  }

  // 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();
}

エラーは次のとおりです。

processing.app.debug.RunnerException: NullPointerException
    at processing.app.Sketch.placeException(Sketch.java:1543)
    at processing.app.debug.Runner.findException(Runner.java:583)
    at processing.app.debug.Runner.reportException(Runner.java:558)
    at processing.app.debug.Runner.exception(Runner.java:498)
    at processing.app.debug.EventThread.exceptionEvent(EventThread.java:367)
    at processing.app.debug.EventThread.handleEvent(EventThread.java:255)
    at processing.app.debug.EventThread.run(EventThread.java:89)
Exception in thread "Animation Thread" java.lang.NullPointerException
    at org.openkinect.processing.Kinect.enableDepth(Kinect.java:70)
    at PointCloud.setup(PointCloud.java:48)
    at processing.core.PApplet.handleDraw(PApplet.java:1583)
    at processing.core.PApplet.run(PApplet.java:1503)
    at java.lang.Thread.run(Thread.java:637)
4

4 に答える 4

3

デプス配列の値がnullであるため、NullPointerExceptionが発生します。Kinectクラスのソースコードからわかるように、getRawDepth()メソッドによってnull値が返される可能性があります。その時点で画像が表示されていない可能性があります。

コードは次の場所にあります。

https://github.com/shiffman/libfreenect/blob/master/wrappers/java/processing/KinectProcessing/src/org/openkinect/processing/Kinect.java

コードは、深さ配列を処理する前に、深さ配列がnullかどうかを確認する必要があります。例えば...

int[] depth = kinect.getRawDepth();

if (depth == null) {
  // do something here where you handle there being no image
} else {
  // 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);

  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];
      PVector v = depthToWorld(x,y,rawDepth);

      stroke(255);
      pushMatrix();
      // Scale up by 200
      float factor = 200;
      translate(v.x*factor,v.y*factor,factor-v.z*factor);
      // Draw a point
      point(0,0);
      popMatrix();
    }
  }

  // Rotate
  a += 0.015f;
}
于 2011-01-03T04:54:14.180 に答える
1

オフセット>kinect.getRawDepth();の場合、nullポインタが発生します。

ここにはたくさんのコードがあります。すべてを見るつもりはありません。オフセットが<kinect.getRawDepth()であると仮定できるのはなぜですか?

編集

第二に、@Asaphのコメントはおそらく正しいでしょう。

于 2011-01-03T04:51:02.143 に答える
1

ヌルポインタ例外は、depth[offset]存在しないか、割り当てられていない場合に発生します。depth[offset]が未定義であり、それがnullpointer例外の原因である場合を確認してください。

kinect.getRawDepth();がより大きい場合を確認しoffsetます。

于 2011-01-03T04:59:35.277 に答える
1

例外がスローされたときの変数の状態を確認できるように、Java Debugger を使用することをお勧めします。ログステートメントを使用して、アプリケーションのさまざまな時点で変数の値を出力することを好む人もいます。

次に、値の 1 つに null 以外の値が設定されていないところまで問題を追跡できます。

于 2011-01-03T04:48:13.557 に答える