OpenKinect和Processing - 无法显示Z坐标

时间:2013-04-26 13:22:52

标签: coordinates kinect processing openkinect

我有Daniel Shiffman的代码(下图)。我想读出Z坐标。我根本不确定如何做到这一点,所以任何帮助都会非常感激。

AveragePointTracking.pde

// Daniel Shiffman
// Tracking the average location beyond a given depth threshold
// Thanks to Dan O'Sullivan
// http://www.shiffman.net
// https://github.com/shiffman/libfreenect/tree/master/wrappers/java/processing

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

// Showing how we can farm all the kinect stuff out to a separate class
KinectTracker tracker;
// Kinect Library object
Kinect kinect;

void setup() {
  size(640,600);
  kinect = new Kinect(this);
  tracker = new KinectTracker();
}

void draw() {
  background(255);

  // Run the tracking analysis
  tracker.track();
  // Show the image
  tracker.display();

  // Let's draw the raw location
  PVector v1 = tracker.getPos();
  fill(50,100,250,200);
  noStroke();
  ellipse(v1.x,v1.y,10,10);

  // Let's draw the "lerped" location
  //PVector v2 = tracker.getLerpedPos();
  //fill(100,250,50,200);
  //noStroke();
  //ellipse(v2.x,v2.y,20,20);

  // Display some info
  int t = tracker.getThreshold();
  fill(0);
  text("Location-X: " + v1.x,10,500);
  text("Location-Y: " + v1.y,10,530);
  text("Location-Z: ",10,560);
  text("threshold: " + t,10,590);
}

void stop() {
  tracker.quit();
  super.stop();
}

KinectTracker.pde

class KinectTracker {

  // Size of kinect image
  int kw = 640;
  int kh = 480;
  int threshold = 500;

  // Raw location
  PVector loc;

  // Interpolated location
  PVector lerpedLoc;

  // Depth data
  int[] depth;


  PImage display;

  KinectTracker() {
    kinect.start();
    kinect.enableDepth(true);

    // We could skip processing the grayscale image for efficiency
    // but this example is just demonstrating everything
    kinect.processDepthImage(true);

    display = createImage(kw,kh,PConstants.RGB);

    loc = new PVector(0,0);
    lerpedLoc = new PVector(0,0);
  }

  void track() {

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

    // Being overly cautious here
    if (depth == null) return;

    float sumX = 0;
    float sumY = 0;
    float count = 0;

    for(int x = 0; x < kw; x++) {
      for(int y = 0; y < kh; y++) {
        // Mirroring the image
        int offset = kw-x-1+y*kw;
        // Grabbing the raw depth
        int rawDepth = depth[offset];

        // Testing against threshold
        if (rawDepth < threshold) {
          sumX += x;
          sumY += y;
          count++;
        }
      }
    }
    // As long as we found something
    if (count != 0) {
      loc = new PVector(sumX/count,sumY/count);
    }

    // Interpolating the location, doing it arbitrarily for now
    lerpedLoc.x = PApplet.lerp(lerpedLoc.x, loc.x, 0.3f);
    lerpedLoc.y = PApplet.lerp(lerpedLoc.y, loc.y, 0.3f);
  }

  PVector getLerpedPos() {
    return lerpedLoc;
  }

  PVector getPos() {
    return loc;
  }

  void display() {
    PImage img = kinect.getDepthImage();

    // Being overly cautious here
    if (depth == null || img == null) return;

    // Going to rewrite the depth image to show which pixels are in threshold
    // A lot of this is redundant, but this is just for demonstration purposes
    display.loadPixels();
    for(int x = 0; x < kw; x++) {
      for(int y = 0; y < kh; y++) {
        // mirroring image
        int offset = kw-x-1+y*kw;
        // Raw depth
        int rawDepth = depth[offset];

        int pix = x+y*display.width;
        if (rawDepth < threshold) {
          // A red color instead
          display.pixels[pix] = color(245,100,100);
        } 
        else {
          display.pixels[pix] = img.pixels[offset];
        }
      }
    }
    display.updatePixels();

    // Draw the image
    image(display,0,0);
  }

  void quit() {
    kinect.quit();
  }

  int getThreshold() {
    return threshold;
  }

  void setThreshold(int t) {
    threshold =  t;
  }
}

3 个答案:

答案 0 :(得分:0)

有两种方法可以做到这一点......

Daniel的代码现在访问坐标的方式是使用二维向量(即带有X和Y)。 您可以将其更改为三维向量(因此它也存储Z坐标),OpenKinect库应该以与X和Y相同的方式返回Z坐标... 我认为 ;-)(必须检查他的来源)。但是这将返回每个像素的Z坐标,然后你必须循环,这很麻烦,而且计算成本很高......

现在,Daniel在这个示例中实际执行此操作的方式是找到特定XY位置的深度并将其返回给您如果超过某个阈值...这是您在KinectTracker中看到的rawDepth整数...所以它测试它是否小于阈值(您可以更改),如果是,它会将这些像素着色并将它们写入图像缓冲区......然后您就可以了例如,要求该图像的XY坐标,或将其传递给blob检测程序,等等......

答案 1 :(得分:0)

主要有两个步骤:

  1. 获取深度(KinectTracker已在track()方法中执行)
  2. 使用偏移量获取当前像素的深度,以根据2D位置(x,y)在1D深度数组中找到位置(这也是在track()方法中完成的:{{1} })
  3. 请注意,坐标是镜像的,通常对索引的计算如下:

    int offset = kw-x-1+y*kw;

    ,如get() reference notes

    中所述

    所以理论上你需要的就是在track()方法结束时这样的东西:

    index = y*width+x
    
    像这样:

    lerpedLoc.z = depth[kw-((int)lerpedLoc.x)-1+((int)lerpedLoc.y)*kw];
    

    我现在无法使用kinect进行测试,但这应该可行。我不确定你是否会获得正确像素或镜像像素的深度。唯一的另一种选择是:

    void track() {
    
        // Get the raw depth as array of integers
        depth = kinect.getRawDepth();
    
        // Being overly cautious here
        if (depth == null) return;
    
        float sumX = 0;
        float sumY = 0;
        float count = 0;
    
        for(int x = 0; x < kw; x++) {
          for(int y = 0; y < kh; y++) {
            // Mirroring the image
            int offset = kw-x-1+y*kw;
            // Grabbing the raw depth
            int rawDepth = depth[offset];
    
            // Testing against threshold
            if (rawDepth < threshold) {
              sumX += x;
              sumY += y;
              count++;
            }
          }
        }
        // As long as we found something
        if (count != 0) {
          loc = new PVector(sumX/count,sumY/count);
        }
    
        // Interpolating the location, doing it arbitrarily for now
        lerpedLoc.x = PApplet.lerp(lerpedLoc.x, loc.x, 0.3f);
        lerpedLoc.y = PApplet.lerp(lerpedLoc.y, loc.y, 0.3f);
        lerpedLoc.z = depth[kw-((int)lerpedLoc.x)-1+((int)lerpedLoc.y)*kw];
      }
    

答案 2 :(得分:0)

在void track()的末尾添加它:

lerpedLoc.z = depth[kw-((int)lerpedLoc.x)-1+((int)lerpedLoc.y)*kw];

然后我将void draw()中的最后一个块更改为此以读出Z值:

// Display some info
int t = tracker.getThreshold();
fill(0);
text("Location-X: " + v1.x,10,500);
text("Location-Y: " + v1.y,10,530);
text("Location-Z: " + v2.z,10,560);  // <<Adding this worked!
text("threshold: " + t,10,590);