Friday, August 12, 2011

Moondust – A WPF Theme

Moondust

Introducing moondust, a metro inspired WPF theme. The theme is far from complete but does include a button, checkbox, radiobutton, sliders and scrollbars.

Moondust emulates metro’s bold monocrome outlines but includes the addition of subtle drop shadow and white mouse-over halo effect.

Moondust

Moondust

The source code to this theme is available via the following link. The download includes the sample application displayed in this post.

How to align Kinect’s depth image with the color image?

How to align kinect’s depth image with its color image

These two good looking gentlemen are demonstrating the blending of kinect's depth and video feeds. Because the depth and video sensors are different resolutions and offset on the device itself there a computation procedure needed to map data one to the other.

Thankfully Microsoft has provided comprehensive documentation such as the Skeletal Viewer Walkthrough and the Programming Guide for the Kinect for Windows SDK. This post is going to provide simple walkthrough to efficiently map depth sensor pixels, for each person, to the video sensor feed.

This exercise requires:

  1. Xbox 360 Kinect
  2. Microsoft Windows 7 (32 or 64bit)
  3. Microsoft Visual Studio 2010
  4. Kinect for Windows SDK beta

In Microsoft Visual Studio 2010, create a new WPF application and add a reference to the Microsoft.Research.Kinect assembly. In this exercise the name of the project (and default namespace) is KinectSample.

Add the following code to MainWindow.xaml:

<Window x:Class="KinectSample.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="Kinect Sample"
       Height="600"
       Width="800"
       >
    <Grid>
        <Image x:Name="ImageVideo" Stretch="UniformToFill" HorizontalAlignment="Center"
              VerticalAlignment="Center" />
        <Image x:Name="ImageDepth" Stretch="UniformToFill" HorizontalAlignment="Center"
              VerticalAlignment="Center" Opacity="0.5" />
    </Grid
>
</
Window
>

In MainWindows.xaml.cs add the following:

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using
Microsoft.Research.Kinect.Nui;

namespace
KinectSample {
   
public partial class MainWindow : Window
{
       
private Runtime _runtime = null
;

        public
MainWindow() {
            InitializeComponent();


            this.Loaded += new RoutedEventHandler(this
.KinectControl_Loaded);
        }
       
private void KinectControl_Loaded(object sender, RoutedEventArgs
args) {
           
this._runtime = new Runtime
();

           
try
{
               
this
._runtime.Initialize(
                   
RuntimeOptions
.UseDepthAndPlayerIndex |
                   
RuntimeOptions
.UseSkeletalTracking |
                   
RuntimeOptions
.UseColor
                );
            }
           
catch (InvalidOperationException
) {
               
MessageBox.Show("Runtime initialization failed. "
+
                               
"Please make sure Kinect device is plugged in."
);
               
return
;
            }

           
try
{
               
this
._runtime.VideoStream.Open(
                   
ImageStreamType
.Video, 2,
                   
ImageResolution
.Resolution640x480,
                   
ImageType
.Color);
               
this
._runtime.DepthStream.Open(
                   
ImageStreamType
.Depth, 2,
                   
ImageResolution
.Resolution320x240,
                   
ImageType
.DepthAndPlayerIndex);
            }
           
catch (InvalidOperationException
) {
               
MessageBox.Show("Failed to open stream. "
+
                 
"Please make sure to specify a supported image type and resolution."
);
               
return
;
            }

           
this
._runtime.VideoFrameReady += (s, e) => {
               
PlanarImage
planarImage = e.ImageFrame.Image;
               
this.ImageVideo.Source = BitmapSource
.Create(
                    planarImage.Width,
                    planarImage.Height,
                    96d,
                    96d,
                   
PixelFormats
.Bgr32,
                   
null
,
                    planarImage.Bits,
                    planarImage.Width * planarImage.BytesPerPixel
                );
            };
           
this
._runtime.DepthFrameReady += (s, e) => {
               
//
                PlanarImage
planarImage = e.ImageFrame.Image;
               
byte
[] depth = planarImage.Bits;
               
int
width = planarImage.Width;
               
int
height = planarImage.Height;
               
byte[] color = new byte
[width * height * 4];
               
ImageViewArea viewArea = new ImageViewArea
() {
                    CenterX = 0,
                    CenterY = 0,
                    Zoom =
ImageDigitalZoom
.Zoom1x
                };
               
ImageResolution resolution = this
._runtime.VideoStream.Resolution;

               
for (int
y = 0; y < height; y++) {
                   
for (int
x = 0; x < width; x++) {
                       
int
index = (y * width + x) * 2;
                       
int
player = depth[index] & 7;
                       
if (player == 0) { continue
; }
                       
short
depthValue =
                            (
short
)(depth[index] | (depth[index + 1] << 8));                       
                       
int
colorX;
                       
int
colorY;
                       
this
._runtime.NuiCamera.GetColorPixelCoordinatesFromDepthPixel(
                            resolution,
                            viewArea,
                            x,
                            y,
                            depthValue,
                           
out
colorX,
                           
out
colorY
                        );
                       
int
adjustedX = colorX / 2;
                       
int
adjustedY = colorY / 2;
                       
if (adjustedX < 0 || adjustedX > 319) { continue
; }
                       
if (adjustedY < 0 || adjustedY > 239) { continue
; }
                       
int
indexColor = (adjustedY * width + adjustedX) * 4;
                       
Color[] colors = new Color
[]{
                           
Colors.Red, Colors.Green, Colors.Blue, Colors
.White,
                           
Colors.Gold, Colors.Cyan, Colors
.Plum
                        };
                       
Color
col = colors[player - 1];
                        color[indexColor + 0] = (
byte
)col.B;
                        color[indexColor + 1] = (
byte
)col.R;
                        color[indexColor + 2] = (
byte
)col.G;
                        color[indexColor + 3] = (
byte
)col.A;
                    }
                }

               
this.ImageDepth.Source = BitmapSource
.Create(
                    width,
                    height,
                    96d,
                    96d,
                   
PixelFormats
.Bgra32,
                   
null,
                    color,
                    width * 4
                );
            };
        }
    }
}

You are done!

This sample will color code the first seven people identified by the kinect device. The most intensive part of this code is the GetColorPixelCoordinatesFromDepthPixel call that maps depth pixels to the video image. To improve performance only pixels identified by the sensor as being a person are mapped. All other depth pixels are ignored.

Thursday, August 11, 2011

Kinect Control for WPF

Yesterday, Esri’s Applications Prototype Lab released a sample for ArcGlobe that allows users to navigate in three dimensions using a Kinect sensor and simple hand gestures.

This post describes a sample utility library developed in conjunction with the ArcGlobe add-in called KinectControl. KinectControl is a WPF user control that can display raw kinect feeds but most importantly provide developers with the orientation, inclination and extension of both arms relative to the sensor. KinectControl was developed a generic library that can used to kinectize any application.

The following few screenshots demonstrate the capabilities of the KinectControl. By default, KinectControl displays the sensor’s video feed and the skeleton of the closest person to the sensor. The orange text at the bottom of the app is debug information from the test application.

Kinect Control for WPF

Occasionally a limb may appear red, this indicates that one or more of the limb’s joints cannot be “tracked” and its position is “inferred” or approximated by the sensor. This often happens when a joint is obscured from view, for example, if a user is pointing their hand and arm directly towards the sensor, the user’s shoulder cannot be seen by the sensor.

On the upper right hand corner of the KinectControl are three button that allow the user to toggle between three different views. The video and depth views are self explanatory but the third, blend, is a combination of the both.

Kinect Control for WPF

The blend view color codes each and every person identified by the kinect sensor with a different color as shown below. The kinect sensor can identify up to seven people.

Kinect Control for WPF

This white stick figure graphic in the upper left hand corner is used to alert the user whenever he or she has moved beyond the kinect’s field of view. For example, in the screenshot below, the user has moved too far to their left.

Kinect Control for WPF

In the bottom left hand corner are two buttons to control the inclination of the sensor. Each button click will move the sensor one degree up or down.

Kinect Control for WPF

And lastly, the test app that is included with the sample uses binding to display the left and right arm orientation and inclination on the screen.

Kinect Control for WPF

Please click the link below to download the KinectControl sample. To use this sample you must have a Kinect connected to Windows 7 computer with Kinect for Windows SDK installed.

Wednesday, August 10, 2011

Kinect for ArcGlobe

On June 16, 2011, Microsoft released the Kinect for Windows SDK. This SDK allows windows developers to support motion with an Xbox 360 Kinect device. The Applications Prototype Lab at Esri has just completed a prototype using a Kinect to navigate in ArcGlobe.

To fly forward, the user can raise their right hand. The display will navigate in the direct the right hand is pointing. We call this “superman navigation”. If the left hand is elevated, the display will pivot around a central location on the globe surface. And lastly, if both hands are raised, the screen will zoom in or out as the hands are both together or apart.

To use the add-in you must have the following:

  1. Kinect for Xbox 360 sensor,
  2. Windows 7 (32 or 64bit),
  3. .NET Framework 4.0,
  4. Kinect for Windows SDK beta.

The add-in (with source code) is available here.

This add-in was challenging in sense that translating traditional mouse navigation to motion was not easy. With mouse or touch input devices, users have immediate sensory feedback once they have clicked a mouse button or touched a screen. In a number of Xbox games, this issue has been overcome with a paused hover. That is, a user uses his or her hand to move the screen cursor over a button and waits for a few seconds as an activation animation completes. This is fine for buttons that occupy discrete areas of a screen but not for interaction throughout the screen,

The approach adopted, rightly or wrongly, by this add-in is that of a virtual screen that exists at arm’s length directly in front of the user. This virtual screen only extended +/- 25° from an arm point directly ahead at the real screen. This technique provides an approximate motion to screen mapping with screen contact only at full arm extension and only within a narrow 50°x50° area in front of the user.

Ultimately a better approach could be to rely purely on touch-like gestures such as left swipe, right swipe and pinching. There has been exciting work in this area by Deltakosh on the Kinect Toolbox project but I hope that the final release of the Kinect SDK includes gesture recognition.

Thursday, July 28, 2011

How to create a drop target for layers in ArcMap?

This post as originally published 3/16/2007 on Microsoft Live Spaces.

How to create a drop target for layers in ArcMap?

Drag and drop is a convenient and intuitive method of exchanging data between windows (and applications). ESRI applications like ArcCatalog and ArcMap have drop and drop capabilities, for example, you can drag layer into the Buffer geoprocessing tool window (see above).

Using the sample code below you can add this capability to your .NET application, that is, you can make your window/control a drop target for layers dragged from the ArcMap table of contents. This code can be used in a window hosted by an ESRI application such as an ArcMap dockable window or as a standalone application.

To enable this feature, a utility class called EsriDataObject is provided below to assist with the deserialization of dropped objects from ArcMap.

The first code sample demonstrates how to use EsriDataObject in a .NET Form. The code will display the concatenate list of layer names that were dropped on the form. The code assumes there is Form called form1 and a TextBox called textBox1.

How to use "EsriDataObject" in a .NET Form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;

namespace MyNamespace {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            this.textBox1.AllowDrop = true;
            this.textBox1.DragEnter +=
new DragEventHandler(this
.textBox1_DragEnter); this.textBox1.DragOver +=
new DragEventHandler(this
.textBox1_DragOver); this.textBox1.DragDrop +=
new DragEventHandler(this
.textBox1_DragDrop); } private void textBox1_DragEnter(object sender, DragEventArgs e) { e.Effect = EsriDataObject.IsValid(e.Data) ?
DragDropEffects.All : DragDropEffects
.None; } private void textBox1_DragOver(object sender, DragEventArgs e) { e.Effect = EsriDataObject.IsValid(e.Data) ?
DragDropEffects.All : DragDropEffects
.None; } private void textBox1_DragDrop(object sender, DragEventArgs e) { EsriDataObject esriDataObject =
EsriDataObject
.ConvertToEsriDataObject(e.Data); foreach (ILayer layer in esriDataObject.LayerCollection) { this.textBox1.Text += layer.Name + " "; } } } }

Sample 2: Source code to "EsriDataObject".

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.esriSystem;

namespace MyNamespace {
    public class EsriDataObject {
        private const string DATAOBJECT_ESRILAYERS = "ESRI Layers";
        private const string INTERFACE_ILAYER =
"{34C20002-4D3C-11D0-92D8-00805F7C28B0}"
; private const string INTERFACE_ITABLEPROPERTY =
"{4657D951-5FFB-11D3-9F6C-00C04F6BC886}"; private readonly List<ILayer> _lc = new List<ILayer>(); private readonly List<ITableProperty> _tpc = List<ITableProperty>(); // // PROPERTIES // public List<ILayer> LayerCollection { get { return this._lc; } } public List<ITableProperty> TablePropertyCollection { get { return this._tpc; } } // // STATIC METHODS // public static bool IsValid(IDataObject dataObject) { return dataObject.GetDataPresent(DATAOBJECT_ESRILAYERS); } public static EsriDataObject ConvertToEsriDataObject(
IDataObject
dataObject) { // EsriDataObject esriDataObject = new EsriDataObject(); // Exit if dropped object is invalid if (EsriDataObject.IsValid(dataObject)) { // Get Byte Array from DataObject object esriLayers = dataObject.GetData(DATAOBJECT_ESRILAYERS); MemoryStream memoryStream = (MemoryStream)esriLayers; byte[] bytes = memoryStream.ToArray(); // Load Byte Array into a Stream (ESRI Wrapper of IStream) IMemoryBlobStreamVariant memoryBlobStreamVariant =
new MemoryBlobStreamClass
(); memoryBlobStreamVariant.ImportFromVariant(bytes); IMemoryBlobStream2 memoryBlobStream =
(
IMemoryBlobStream2
)memoryBlobStreamVariant; IStream stream = (IStream)memoryBlobStream; // Load Stream into an ESRI ObjectStream IObjectStream objectStream = new ObjectStreamClass(); objectStream.Stream = stream; // Get Number of Layers in Dropped Object byte pv; uint cb = sizeof(int); uint pcbRead; objectStream.RemoteRead(out pv, cb, out pcbRead); int count = Convert.ToInt32(pv); // Define Guids Guid guidLayer = new Guid(INTERFACE_ILAYER); Guid guidTable = new Guid(INTERFACE_ITABLEPROPERTY); // Get Dropped Layers for (int i = 0; i < count; i++) { object o = objectStream.LoadObject(ref guidLayer, null); ILayer layer = (ILayer)o; esriDataObject.LayerCollection.Add(layer); } // Get Dropped TableProperties for (int i = 0; i < count; i++) { object o = objectStream.LoadObject(ref guidTable, null); if (o == null) { continue; } ITableProperty tableProperty = (ITableProperty)o; esriDataObject.TablePropertyCollection.Add(tableProperty); } } return esriDataObject; } } }

Additional information:

Dragging and dropping objects from ArcCatalog is a lot easier than ArcMap. In ArcMap, a dragged object is packaged as an "ESRI Layer" object in the DataObject. The code above describes how to unpack an "ESRI Layer" object. In ArcCatalog, a dragged object is packaged as an "ESRI Names" object in the DataObject. Fortunately, there are ArcObjects available to pack and unpack this object.

To create an "ESRI Names" object use the following:
INameFactory.PackageNames Method

To unpackage an "ESRI Names" object use:
INameFactory.UnpackageNames Method

Lastly, here is an online example showing how to unpackage an "ESRI Names" object:
esriSystem NameFactory Example