Kinect SDK1 - A 3D Point Cloud
Written by Mike James   
Friday, 27 April 2012
Article Index
Kinect SDK1 - A 3D Point Cloud
Lights, Camera, Action
Adding the Kinect

Adding the Kinect

 

 

Now we come to the easy part of the project - adding the Kinect. All we need is a basic depth map and this is well described in earlier chapters.

You need to include a reference to

Microsoft.Kinect.dll.

and to avoid having to type fully qualified names add:

using Microsoft.Kinect;

to the start of the code.

Add

KinectSensor sensor;

to the global variables. We go through the usual initialization procedure to get the Kinect ready to supply a depth map:

sensor = KinectSensor.KinectSensors[0];
sensor.DepthStream.Enable(
 DepthImageFormat.Resolution320x240Fps30);
sensor.DepthFrameReady+=DepthFrameReady;
sensor.Start();

Of course our next task is to write the event handler. This has to retrieve the depth data from the Kinect:

void DepthFrameReady(object sender,
         DepthImageFrameReadyEventArgs e)
{
 DepthImageFrame imageFrame =
                e.OpenDepthImageFrame();
 if (imageFrame != null)
 {
  short[] pixelData = new  
       short[imageFrame.PixelDataLength];
  imageFrame.CopyPixelDataTo(pixelData);

Now we have the depth data as an array of short integers. All we have to do now is move each pixel in turn to the new z position by updating its translation transform:

int temp = 0;
int i = 0;
for (int y = 0; y < 240; y +=s)
 for (int x = 0; x < 320; x +=s)
 {
  temp = ((ushort)pixelData[x+y*320])>>3;
  ((TranslateTransform3D)
    points[i].Transform).OffsetZ = temp;
  i++;
 }
}

That's all you have to do. Now when you run the program you will see triangles positioned according to the depth returned by the Kinect. It takes some time to get used to reading the image, but you should be able to see shapes and the 3D effect should be clear. Try modifying the angle of view and the distance the camera is from the point cloud. Notice also that the field of view is left right reversed - this is a consequence of turning the camera upside down!

 

pointcloud1

 A person standing in a doorway

The results are more impressive when you view a moving 3D image rather than a static screenshot, but there are a few things you need to understand about the image. The first is that areas that have no points are either to far away or too close. Each point in the depth field is positioned at a particular depth and because of the perspective projection the this makes it appear to leave a sort of shadow if itself in the background as triangles are x,y displaced in the image.

You can also get some interesting results by placing the view camera at an angle to the z direction. which is the direction the Kinect is pointing in. This allows you to look sideways at the depth image.

 

You can download the code for this program from the CodeBin (note you have to register first).

Further Reading

Practical Windows Kinect in C#
Chapter List

  1. Introduction to Kinect
  2. Getting started with Microsoft Kinect SDK 1
  3. Using the Depth Sensor
  4. The Player Index
  5. Depth and Video Space
  6. Skeletons
  7. The Full Skeleton
  8. A 3D Point Cloud

blog comments powered by Disqus

 

To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter, Linkedin or Facebook or sign up for our weekly newsletter.



Last Updated ( Friday, 27 April 2012 )
 
 

   
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.