Question

I am trying to generate a binary image from the depthMap()-function in OpenNI, which provides an array of type int. With that image I want to do blob-Tracking. Problem is that I am not able to generate a clear binary image from the depthMap. In my understanding the depth image generates a bright pixel for everything that is closer to the sensor and the farer away from the sensor the darker they get. So I ask every Pixel in the (one-dimensional) Array if it is over my min and under my max-Threshold to make up a range from that I want the get the data. Here is my code:

 // 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);
}
Was it helpful?

Solution

Really stupid mistake by myself because I missunderstood the 11-bit Array. Thanks to the "Making things see" examples I solved it. https://github.com/atduskgreg/Making-Things-See-Examples/tree/master/ax02_depth_range_limit

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top