Turn Kinect into 3D scanner Explained Full tutorial with Code.

David Von Beethoven

The Kinect is here. Thousands around the world have been hacking the game cam. One of it’s most exciting applications the device offers is it’s ability to display depth information on the screen. By projecting infrared dots on a surface the camera can read depth along the Z access as well as X,Y coordinates.

What you will need:

Microsoft Xbox Kinect Camera.

FREE Software:
If you don’t already have   <> Processing <———->meshLab<———->.  DOWNLOAD ALL You will have to download additional Kinect Library for Processing and place it in Library folder. Thanks Dan Shiffman for creating a library. There are deeper  explanation on how to use the camera with Processing on his website. (there are tutorials on how and where to put the library files on the Processing website, for those with more questions. )

Next copy the code into the sketch.

Code for 3D Scan

Processing Code to Export Point Cloud

import peasy.*;
import shiffman.kinect.*;
import superCAD.*;

boolean record = false;

Simple Kinect point-cloud demo v. 0.2

Henry Palonen <h@yty.net>

Using Daniel Shiffman’s great processing-library for Kinect:


Based on Kyle McDonalds Structure Light scanner:


Using also SuperCAD for outputting the .obj – files: http://labelle.spacekit.ca/supercad/

17.11.2010 – 0.1 – First version, simple point-cloud working
18.11.2010 – 0.2 – Output to .obj for importing to Blender, gray-color for distance and small lines as output

float zscale = 3;
float zskew = 100;

int inputWidth = 640;
int inputHeight = 480;

PeasyCam cam;

float[][] gray = new float[inputHeight][inputWidth];

PImage depth;

static final int gray(color value) {
return max((value >> 16) & 0xff, (value >> 8 ) & 0xff, value & 0xff);

void setup() {
size(inputWidth, inputHeight, P3D);
cam = new PeasyCam(this, width);
depth = createImage(640,480,RGB);

int counter = 0;

void draw () {

if (record == true) {
beginRaw(“superCAD.ObjFile”, “Rhazes_” + counter +   “.obj”); // Start recording to the file

depth.pixels = NativeKinect.getDepthMap();


for (int y = 0; y < inputHeight; y++) {
for (int x = 0; x < inputWidth; x++) {
// FIXME: this loses Z-resolution about tenfold …
//       -> should grab the real distance instead…
color argb = depth.pixels[y*width+x];
gray[y][x] = gray(argb);

// Kyle McDonald’s original source used here
translate(-inputWidth / 2, -inputHeight / 2);
int step = 2;
for (int y = step; y < inputHeight; y += step) {
float planephase = 0.5 – (y – (inputHeight / 2)) / zskew;
for (int x = step; x < inputWidth; x += step)
//point(x, y, (gray[y][x] – planephase) * zscale);
line(x, y, (gray[y][x] – planephase) * zscale, x+1, y, (gray[y][x] – planephase) * zscale);


if (record == true) {
record = false; // Stop recording to the file


void keyPressed() {
if (key == ‘R’ || key == ‘r’) {
// Press R to save the file
record = true;
counter++;  //Thanks to Dr. Rhazes Spell for putting the counter in adding functionality.

4. Aim camera at Subject.
5. Press R button to save image as .obj.
6.Once you are happy with image. Find the saved .obj files in the sketch folder.

Open saved .obj file in meshlab 3D modeling software.
Once in meshlab you will see  your image as a “Point Cloud”. To make the image usable for manipulating the model in
Maya, Blender or other 3D software you will have to apply several filters.

When you open your .obj in MeshLab it should look something like this.

Under the Filters tab in the navigation menu you will see a Sampling option. In Sampling choose the Poisson -Disk Sampling.

Choose the your sample points in the prompted window. I have no idea the right formula. I have been picking between 60000 and 100000 sample points(sometimes the system crashes.) Remember to check on the Base Mesh Subsampling box or you will get an error. Once it works you can see a new layer. To get to this menu you can find the Layer button as the 5th icon to the right along the tool bar at the top of the screen. This will bring up your Layers view found on screen right.  Make sure the Poison-disk Samples is highlighted.


Make sure Poisson-disk Samples layer is highlighted

Next go to Filters —–>Normals, Curvatures and Orientation —> Compute Normals for Point Set. A menu box will appear .

Put a number more than 15 in the
Number of neighbors box. There is no hard fast rule so you will have to experiment to find what works for you.

Next go to Filters —>  Points—>Surface Reconstruction Poisson. This will create a new layer

called Poisson Mesh. From there you can add surfaces to the Model. Finally save the model as a .OBJ. to be opened in Maya or Blender. MeshLab allows you to save in a number of other formats The file is ready to be opened in Maya or Blender. Meshlab allows you to save in a multitude of formats that are perfect for other 3D software such as 3D Max.While I haven’t tried it these files are supposedly great for 3D printing. It will take much experimentation to clean up the models. Have fun—-That’s an order.

About these ads

Comments are closed.


Get every new post delivered to your Inbox.

%d bloggers like this: