Getting Started with Kinect and Processing

So, you want to use the Kinect in Processing. Great. This page will serve to document the current state of my Processing Kinect library, with some tips and info.

Before you proceed, however, you may want to instead consider using the wonderful SimpleOpenNI library and Greg Borenstein’s Making Things See book! OpenNI has lots of features (skeleton tracking, gesture recognition, etc.) that are not available in this library.

I’m ready to get started right now

What hardware do I need?

First you need a “stand-alone” kinect. You do not need to buy an Xbox. If you get the stand-alone kinect listed below, it will come with a USB adapter and power supply.

Standalone Kinect Sensor

If you have a kinect that came with an XBox, it will not include the USB adapter. You’ll need to purchase this separately:

Kinect Sensor Power Supply

Um, what is Processing?

I’m going to assume you are familiar with Processing, but just in case you are not, I suggest checking out: processing.org (Processing is an open source programming language and environment for people who want to create images, animations, and interactions. Initially developed to serve as a software sketchbook and to teach fundamentals of computer programming within a visual context, Processing also has evolved into a tool for generating finished professional work. Today, there are tens of thousands of students, artists, designers, researchers, and hobbyists who use Processing for learning, prototyping, and production.)

What if I don’t want to use Processing?

If you are comfortable with C++ I suggest you consider using openFrameworks or Cinder with the Kinect. At the time of this writing, these environments have some features I haven’t implemented yet and you also get a C++ speed advantage when processing the depth data, etc.:

ofxKinect
Kinect CinderBlock

More resources from: The OpenKinect Project

I’ve got Processing, how do I download and install the library?

By default Processing will create a “sketchbook” folder in your Documents directory, i.e. on my machine it’s:

/Users/daniel/Documents/Processing/

If there isn’t already, create a folder called “libraries” there, i.e.

/Users/daniel/Documents/Processing/libraries/

Then go and download openkinect.zip and extract it in the libraries folder, i.e. you should now see

/Users/daniel/Documents/Processing/libraries/openkinect/
/Users/daniel/Documents/Processing/libraries/openkinect/library/
/Users/daniel/Documents/Processing/libraries/openkinect/examples/
etc.

Restart Processing, open up one of the examples in the examples folder and you are good to go!

More about installing libraries:

http://wiki.processing.org/w/How_to_Install_a_Contributed_Library
http://www.learningprocessing.com/tutorials/libraries/

At the moment, my library only works with Mac OS X (intel, 10.5 or 10.6 should both be ok). Hopefully this will be remedied soon enough. However, if you are interested in working with Kinect on windows, I recommend SimpleOpenNI.

What code do I write?

To get started using the library, you need to include the proper import statements at the top of your code:

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

As well as a reference to a “Kinect” object, i.e.

// Kinect Library object
Kinect kinect;

Then in setup() you can initialize that kinect object:

  kinect = new Kinect(this);
  kinect.start();

Once you’ve done this you can begin to access data from the kinect sensor.

Currently, the library makes data available to you in four ways:

  1. RGB image from the kinect camera as a PImage.
  2. Grayscale image from the IR camera as a PImage
  3. Grayscale image with each pixel’s brightness mapped to depth (brighter = closer).
  4. Raw depth data (11 bit numbers between 0 and 2048) as an int[] array

Let’s look at these one at a time. If you want to use the Kinect just like a regular old webcam, you can request that the RGB image is captured:

  kinect.enableRGB(true);

Then you simply ask for the image as a PImage!

  PImage img = kinect.getVideoImage();
  image(img,0,0);

Alternatively, you can enable the IR image:

  kinect.enableIR(true);

Currently, you cannot have both the RGB image and the IR image. They are both passed back via getVideoImage() so whichever one was most recently enabled is the one you will get.

Now, if you want the depth image, you can:

  kinect.enableDepth(true);

and request the grayscale image:

  PImage img = kinect.getDepthImage();
  image(img,0,0);

As well as the raw depth data:

  int[] depth = kinect.getRawDepth();

If you are looking at the raw depth data only, you can turn off the library’s behind the scenes depth image processing to make it slightly more efficient:

  kinect.processDepthImage(false);

Finally, you can also adjust the camera angle with the tilt() function, i.e.:

float deg = 15;
kinect.tilt(deg);

So, there you have it, here are all the useful functions you might need to use the Processing kinect library:

  1. enableRGB(boolean) — turn on or off the RGB camera image
  2. enableIR(boolean) — turn on or off the IR camera image
  3. enableDepth(boolean) — turn on or off the depth tracking
  4. processDepthImage(boolean) — turn on or off the depth image processing
  5. PImage getVideoImage() — grab the RGB or IR video image
  6. PImage getDepthImage() — grab the grayscale depth map image
  7. int[] getRawDepth() — grab the raw depth data
  8. tilt(float) — adjust the camera angle (between 0 and 30 degrees)

Where’s the javadoc?

Stay tuned, I’ll get something up soon. All the source is here:

https://github.com/shiffman/libfreenect/tree/master/wrappers/java/processing

So now what?

So far, I only have three basic examples:

Display RGB, IR, and Depth Images


Code:RGBDepthTest

(NOTE: KNOWN BUG IN IR IMAGE RIGHT NOW)

This example does nothing but use all of the above listed functions to display the data from the kinect sensor.

Point Cloud


Code: PointCloud

Here, we’re doing something a bit fancier. Number one, we’re using the 3D capabilities of Processing to draw points in space. You’ll want to familiarize yourself with translate(), rotate(), pushMatrix(), popMatrix(). This tutorial is also a good place to start. In addition, the example uses a PVector to describe a point in 3D space. More here: PVector tutorial.

The real work of this example, however, doesn’t come from me at all. The raw depth values from the kinect are not directly proportional to physical depth. Rather, they scale with the inverse of the depth according to this formula:

depthInMeters = 1.0 / (rawDepth * -0.0030711016 + 3.3309495161);

Rather than do this calculation all the time, we can precompute all of these values in a lookup table since there are only 2048 depth values.

float[] depthLookUp = new float[2048];
for (int i = 0; i < depthLookUp.length; i++) {
  depthLookUp[i] = rawDepthToMeters(i);
}
 
float rawDepthToMeters(int depthValue) {
  if (depthValue < 2047) {
    return (float)(1.0 / ((double)(depthValue) * -0.0030711016 + 3.3309495161));
  }
  return 0.0f;
}

Thanks to Matthew Fisher for the above formula. (Note: for the results to be more accurate, you would need to calibrate your specific kinect device, but the formula is close enough for me so I’m sticking with it for now. More about calibration in a moment.)

Finally, we can draw some points based on the depth values in meters:

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

Average Point Tracking

The real magic of the kinect lies in its computer vision capabilities. With depth information, you can do all sorts of fun things like say: "the background is anything beyond 5 feet. Ignore it!" Without depth, background removal involves all sorts of painstaking pixel comparisons. As a quick demonstration of this idea, here is a very basic example that compute the average xy location of any pixels in front of a given depth threshold.

Source: AveragePointTracking

In this example, we declare two variables to add up all the appropriate x's and y's and one variable to keep track of how many there are.

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

Then, whenever we find a given point that complies with our threshold, we add the x and y to the sum:

  if (rawDepth < threshold) {
    sumX += x;
    sumY += y;
    count++;
  }

When we're done, we calculate the average and draw a point!

if (count != 0) {
  float avgX = sumX/count;
  float avgY = sumY/count;
  fill(255,0,0);
  ellipse(avgX,avgY,16,16);
}

Why don't the RGB images and depth values correspond properly?

Unfortunately, b/c the RGB camera and the IR camera are not physically located in the same spot, we have a stereo vision problem. Pixel XY in one image is not the same XY in an image from a camera an inch to the right. I'm hoping to stretch my brain to try to understand this better and work out some examples that calibrate the data in Processing. Stay tuned!

If you are interested in more (and software that will do this very job!) check out Nicolas Burrus' amazing work:

Theory on depth/color calibration and registration
version 0.3 of RGBDemo

What's missing?

Lots! Open a github issue if you want to add an item to my to do list!

https://github.com/shiffman/libfreenect/issues

FAQ

1. What are there shadows in the depth image?

Kinect Shadow diagram

2. What is the range of depth that the kinect can see?

~0.7–6 meters or 2.3–20 feet. Note you will get black pixels (or raw depth value of 2048) at both elements that are too far away and too close.

  • emusicman11

    I don’t understand why you put down the code to write, when its already in the openkinect.zip? Also getting message: No library found for org.openkinect No library found for org.openkinect.processing Libraries must be installed in a folder named ‘libraries’ inside the ‘sketchbook’ folder.

    Also don’t know what Iam supposed to do with:

    Source code: https://github.com/shiffman/libfreenect/tree/master/wrappers/java/processing

    Open Kinect: openkinect.org

  • Gerry Straathof

    The source code is for experienced programmers to have access to it if they would like to alter or improve the code.

    Openkinect is a multiplatform library for several implementations of the kinect. It was mostly reverse engineered before microsoft released thier sdk. You can ignore it.

    The libraries is a folder within the sketch folder. You can add other third party libraries to it.

    You can set the location of the sketch folder in the preferences.

    It sounds like you should familiarize yourself with processing before delving into using the kinect library. Check out processing.org

    This is the second long response to your question. I am not sure why they are not passing moderation.

  • Gerry Straathof

    Sorry. Ignore the link to openkinect.org…

  • Gerry Straathof

    It is a folder called sketch or sketches. You can set it in preferences. Check out the documentation at http://processing.org

  • tim b.

    Everything was running on my old MBP 10.6 – now I got a new MBP with OSX 10.8 and nothing is working anymore. Is the lib already updated to OSX 10.8? Has somebody else trouble with kinect and Mountain Lion?

  • shiffman

    Did you check the Processing preferences are set for 32-bit mode?

  • Gerry Straathof

    There are several possibilities, one of which is different versions of the shiffman kinect library you are using. I’ve had things change myself because of those changes, rendering much of what I had working in an older version broken until I can retool it to use the same formats as i uses now (used to be calculations for x/y/z in real space, now uses openGL RealWorldCoordinates)

    Are the working versions of the kinect library the same on both machines? Or did you download the latest kinect for your new macbook but use the old program of yours from your old machine, which relied on the older kinect library?

  • Daniell Algar

    I see that this is an old post but, could you update that big for-loop? The syntax has gotten way of, thanks!

  • tim b.

    I switched back to 32-bit but still the same, Kinect not found. I tried to install a couple of drivers from different sites (MacPort, OpenNI, OpenKinect driver etc.) and now I’m confused. Last one I tried was: http://creativec0d1ng.blogspot.com/2012/08/installing-openni-on-osx-lionmountain.html

    What is the easiest way to see if the kinect is installed correctly?

    And is there a proper description which for a setup on 10.8 which works?

  • tim b.

    Not sure what the problem exactly is, but where can I see if at least the kinect is installed correctly?

  • Charlie

    Hi All I’m running Win 7 64 bit machine. I installed the SimpleOpenNI. When I try to run an example file i get an error

    Can’t load SimpleOpenNI library (SimpleOpenNI64) : java.lang.UnsatisfiedLinkError?: C:UserscportelliDocumentsProcessinglibrariesSimpleOpenNIlibrarySimpleOpenNI64.dll: Can’t find dependent libraries Verify if you installed SimpleOpenNI correctly. http://code.google.com/p/simple-openni/wiki/Installation

    Any help would be much appreciated. thanks, C?

  • Sumit

    hi sir,sir i am doing project called 3D point cloud generation using kinect using openni. i am facing problems while recording depth and rgb through openni,sir please help me out..on monday we have our project review

  • tim a

    Can anyone tell me, what is occlusion?

  • Gerry Straathof

    Occlusion is when something prevents you from seeing something behind/under it. Your finger covering an icon on a touch screen is due to occlusion. In the case on the kinect, especially with the point cloud, occlusion refers to the fact items close to the kinect cast a ‘shadow’ preventing from seeing items behind them.

  • Luca Caridà

    In AveragePointTracking I have a nullpointerexception ,here:

    PImage display;

    KinectTracker() {

    kinect.start();

    kinect.enableDepth(true);

    *****
    Exception in thread “Animation Thread” java.lang.NullPointerException

    at org.openkinect.processing.Kinect.enableDepth(Kinect.java:82)

    at AveragePointTracking$KinectTracker.(AveragePointTracking.java:111)

    at AveragePointTracking.setup(AveragePointTracking.java:40)

    at processing.core.PApplet.handleDraw(PApplet.java:1608)

    at processing.core.PApplet.run(PApplet.java:1530)

    at java.lang.Thread.run(Thread.java:680)

    ***

    Processing 1.5.1 (the same with 2.x), Snow leopard, Kinect for XBOX

    Any idea about the fix?
    Thank you guys,
    L.

  • tim b.

    Did anyone got the kinect running under OSX Mountain Lion (10.8.*)???
    Please help, struggling since months… :(

  • nosarious

    I have had it working under mountain lion very well. One of the first things to note is that the mac can only work with an XBox kinect, not the microsoft kinect which is designed for windows (you NEED the one with XBox on it work with a mac)

    If you have this one and it isn’t working (power plugged in, it is connected to the mac through USB and you still can’t use it with processing) you may need to give more information on the problem (error codes, etc)

    “not working” describes nothing towards solving the problem.

  • tim b.

    The point is I had I running under 10.6 but since I got a new MBP 10.8 nothing is working anymore. I’m not even able to get the kinect running. I tried a couple of instructions to install. Which can you recommend? I think I have a microsoft kinect, but is was running before…?!

  • Gerry Straathof

    Check and make sure you are using the same version of processing as before. there have been some changes with the latest. I think I have two or three versions on my system right now, and at least one of them (latest? beta?) was quirky.

  • Seongwon

    Hi everyone. I am having problems playing examples.
    It says

    SimpleOpenNI Version 0.27
    Exception in thread “Animation Thread” java.lang.RuntimeException: Waited 5000ms for: [count 2, qsz 0, owner ] –
    at jogamp.common.util.locks.RecursiveLockImpl01Unfairish.lock(RecursiveLockImpl01Unfairish.java:197)
    at com.jogamp.newt.opengl.GLWindow.display(GLWindow.java:541)
    at processing.opengl.PGL.requestDraw(PGL.java:1197)
    at processing.opengl.PGraphicsOpenGL.requestDraw(PGraphicsOpenGL.java:1550)
    at processing.core.PApplet.run(PApplet.java:2140)
    at java.lang.Thread.run(Thread.java:680)

    All the examples cannot play. I am using video data inside of data folder.
    What is wrong??

  • Gerry Straathof

    you may get more responses on a site that is dedicated to SimpleOpenNi. This is a site kinda/sorta dedicated to the Daniel Shiffman library for the kinect.

    Sites I would suggest are the processing forum, stackoverflow,or the google forum set up for simpleopenni: https://groups.google.com/forum/#!forum/simple-openni-discuss

    You may also want to provide more information to get responses that are useful, such as operating system, version of kinect, specific examples, etc. I’m not trying to shut you down, just trying to help you get the answers you need. Elsewhere.

  • Tong Tom

    Dear all

    Hello. I am a visual artist.

    I would like to get the typography from sand dump using Kinect and Processing just like the video below.
    http://www.youtube.com/watch?v=j9JXtTj0mzE&feature=player_embedded

    Which library (e.g. blob detection) should I use?

    Tom Tong

  • Sensebellum

    I have been getting a problem with the IR feed. I have this banding type issue, where white lines scroll down the middle at a constant rate.

    Is there anyway around this?

  • Gerry Straathof

    There is a link on the youtube video page to the project page where they discuss it in more detail. Perhaps you should start there. They include various code and other information.

    http://idav.ucdavis.edu/~okreylos/ResDev/SARndbox/

    Now, if you are asking “what libraries can I use to mimic this in processing/openframeworks/other-programming-languages” I am not sure if anyone can help you there. Explore how they built this system, try to understand how they are using the kinect-point-cloud information to generate the terrain variables and imagery and then decide on how you want this to work with your own project.

    Personally, I think you should be able to get this kind of visualization with any of the kinect libraries out there. There is less question about the way the kinect works and more questions about how the visualization works.

  • Gerry Straathof

    sorry. missed this response. I would suggest trying the simpleopenni library to see if it is seeing the kinect. I believe it uses a different underlying method of connecting. This will at least let you know if the kinect is working, I think. There may be an free app or two on the mac store which will do this as well. It removes the variables in processing and could allow you to confirm there is communication between your laptop and kinect.

  • Peter Fonda

    I’ve been experiencing this same problem. I’m attempting to re-write the Moullinex – Catalina code for 2.0 because 1.5 keeps blowing up in my face (though it ran fine last Friday).

  • Kevin

    Hi, I’m trying to pick up Kinect to see if it’s viable with a project at school I’m working on. I did some googling and the base SDK seems to be in C#? Taking into account the libraries going on around the Internet, what do you think are the advantages and disadvantages of using the Kinect with Processing in comparison to C#? Sorry if you’ve had to answer this question many times already, thanks!

  • nosarious

    do you know c#? do you want your program that uses the kinect to only work on windows? If both of those were yes, then use the microsoft sdk.

    the other libraries were reverse engineered by people in the community. they may not have all the features that are present in the machine. They do, however let people learn what the device does using their favorite system without having to enter into a different language or platform.

    You should ask your question (or search for answers) on a site like stackoverflow. This is a string of comments on a site that is about one particular lib ray. you may not get the best (or any) answers that will help you decide where to start.