Kinect Depth Threshold

edited April 2017 in Kinect

Hi all I am working on a project using Processing, Kinect and OpenNI.

I want to create a Kinect Depth Image to create a distance threshold. Ie. Pixels within 1m are white, any beyond that are black. I am new to Processing and I am struggling a bit to make the code work.

I found out one of the previous post asked similar problems ( but the code wasn't working when I run it/modify it.

Does anyone has experience on this and can give me a hand? Thanks a lot!


  • You could start describing what errors you get in the code that you tried before. What changes did you make, or what did you try to debug it? I will suggest exploring more previous posts related to your device:


  • Thanks for the advice, Kf!

    Basically what I did is use the existing DepthMap3D example from OpenNI. The example gives me a visualization of 3D pointcloud. I then modify it with the method I learnt from Shiffman's tutorial on how to create Depth Threshold.

    Shiffman's tutorial used on a different library (OpenKinect) which couldn't work with my kinect. So what I tried to do is copy Shiffman's code into the OpenNI example, then changed some of the code to match the OpenNI library. I'm guessing it's the process when I change the codes create errors.


    Below is my code:

    import SimpleOpenNI.*;
    SimpleOpenNI context;
    PImage img;
    // SIZES
    int canvasWidth  = 512;
    int canvasHeight = 424;
    int kinectWidth  = 512;
    int kinectHeight = 424;
    void setup()
      context = new SimpleOpenNI(this);
      if(context.isInit() == false)
         println("Can't init SimpleOpenNI, maybe the camera is not connected!"); 
      // disable mirror
      // enable depthMap generation 
    img  = createImage(kinectWidth, kinectHeight, RGB); 
    void draw()
      int[]   depthMap = context.depthMap();
      // draw pointcloud
      for(int y=0;y < context.depthHeight();y++)
        for(int x=0;x < context.depthWidth();x++)
          int offset =  x + y * context.depthWidth();
          int d = depthMap[offset];
          if (d > 300 && d < 1500) {
          img.pixels[offset] = color(255, 0, 150);
          } else {
            img.pixels[offset] = color(0);
      image (img,0,0);
  • In the processing IDE (not here), you can press ctrl+T to autoformat your code.

    For the record, what OS are you suing and Processing version?

    I checked your code and I don't see anything wrong with it. It is similar to what I have seen in other kinect sketches. Mind I do not have any of these units, so I have never tested one. checking other posts, it seems you need to call kinect update. Check:



  • edited April 2017

    I am currently using window 7, Processing 3.2.1 and kinect for windows Model 1517. When you say kinect update, Do u mean "update driver".? I just checked and my kinect is up to date. The error that processing gave me when i ran the code is"ArrayIndexOutBounds".


    Do you know what does that mean and how could that be fixed?

    I tried to run the code from the link you provided but it wasn't running properly on mine. It was shown as blank grey image on the pop up screen.

    Thanks in advances!

  • Answer ✓

    When you say kinect update, Do u mean "update driver".? I just checked and my kinect is up to date

    No... if you study a previous example in one of the links, they call something like kinectObj.update() right before you access your depth data. The error in your previous post tells me you are not loading your data before you try to access it.


Sign In or Register to comment.