OpenNi Blob跟踪从depthMap生成的二进制图像

时间:2013-11-27 12:15:58

标签: processing openni

我正在尝试从OpenNI中的depthMap()函数生成二进制图像,它提供了一个int类型的数组。有了那个图像,我想做blob-Tracking。 问题是我无法从depthMap生成清晰的二进制图像。在我的理解中,深度图像为距离传感器更近的所有物体产生了明亮的像素,而距离传感器越远,它们就越暗。所以我问(一维)数组中的每个像素是否超过我的最小值并且在我的最大阈值之下,以构成我想要获取数据的范围。 这是我的代码:

 // import library
import SimpleOpenNI.*;
import processing.opengl.*; // opengl
import blobDetection.*; // blobs

// declare SimpleOpenNI object
SimpleOpenNI context;
BlobDetection theBlobDetection;
BlobBall blobBalls;
PrintWriter output;

// threshold for binaryImage
int minThreshold, maxThreshold;
// Size of the kinect Image
int kinectWidth = 640;
int kinectHeight = 480;
//
float globalX, globalY;
// Colors
color bgColor = color(0, 0, 123);
color white = color(255,255,255);
color black = color(0,0,0);

// PImage to hold incoming imagery
int[] distanceArray;
PImage cam, forBlobDetect;

void setup() {
  output = createWriter("positions.txt");
  // init threshold
  minThreshold = 960;
  maxThreshold = 2500;
  // same as Kinect dimensions
  size(kinectWidth, kinectHeight);
  background(bgColor);
  // initialize SimpleOpenNI object
  context = new SimpleOpenNI(this);
  if (context.isInit() == false) {
    println("Can't init SimpleOpenNI, maybe the camera is not connected!"); 
    exit();
  } 
  else {
    // mirror the image to be more intuitive
    context.setMirror(true);
    context.enableDepth();
    // context.enableScene();
    distanceArray = context.depthMap();
    forBlobDetect = new PImage(width, height);
    theBlobDetection = new BlobDetection(forBlobDetect.width, forBlobDetect.height);
    theBlobDetection.setThreshold(0.2);
  }
}

void draw() {
  noStroke();
  // update the SimpleOpenNI object
  context.update();
  // put the image into a PImage
  cam = context.depthImage();
  // copy the image into the smaller blob image
  // forBlobDetect.copy(cam, 0, 0, cam.width, cam.height, 0, 0, forBlobDetect.width, forBlobDetect.height);
  // blur the blob image
  forBlobDetect.filter(BLUR, 2);
  //
  int pos = 0;
  int currentDepthValue = 0;
  distanceArray = context.depthMap();
  for(int x = 0; x < cam.width; x++) {
    for(int y = 0; y < cam.height; y++) {
      pos = y*cam.width+x;
      currentDepthValue = distanceArray[pos];
//      println(currentDepthValue);
      if((currentDepthValue > minThreshold) && (currentDepthValue < maxThreshold)) {
        forBlobDetect.pixels[pos] = black; 
      } else {
        forBlobDetect.pixels[pos] = white;
      }
    }
  }
//  for(int i=0; i < distanceArray.length; i++) {
//    currentDepthValue = distanceArray[i];
//    // println(currentDepthValue);
//    if(currentDepthValue > minThreshold) /*&& (currentDepthValue < maxThreshold)*/) {
//      forBlobDetect.pixels[pos] = white;
//    } else {
//      forBlobDetect.pixels[pos] = black;
//    }
//  }
  // detect the blobs
  theBlobDetection.computeBlobs(forBlobDetect.pixels); 
  // display the image
  image(cam, 0, 0);
  image(forBlobDetect, 0, 0, width/2, height/2);

  // image(context.sceneImage(), context.depthWidth(), 0);
}

1 个答案:

答案 0 :(得分:0)

我自己真的很愚蠢,因为我误解了11位阵列。 感谢“看到事情”的例子我解决了它。 https://github.com/atduskgreg/Making-Things-See-Examples/tree/master/ax02_depth_range_limit