MATLAB Examples

Preview color and depth streams from the Kinect for Windows v2 simultaneously

This example shows how to preview color and depth streams using the Kinect for Windows v2.

Contents

Requirements to run this example

1. MATLAB and Image Acquisition Toolbox.

2. A Kinect for Windows v2 sensor.

3. Minimum PC configuration: Windows 8 64-bit.

Identifying Kinect for Windows v2

The imaqhwinfo('kinect') command provides information on the Kinect v2's color and depth devices. Since the Kinect has 2 sensors, a color and a depth sensor, it is enumerated as two devices.

kinectDeviceInfo = imaqhwinfo('kinect')

% 1. Only one Kinect for Windows v2 can be used per PC.
% 2. You can use the Kinect v1 and Kinect v2 together in the same MATLAB session. The devices will be enumerated as [1],[2] (Kinect v1) and [3],[4](Kinect v2) devices.
kinectDeviceInfo = 

       AdaptorDllName: 'C:\MATLAB\SupportPackages\R2016a\toolbox\imaq\supportpackages\kinectruntime\adaptor\mwkinectimaq.dll'
    AdaptorDllVersion: '5.0 (R2016b Prerelease)'
          AdaptorName: 'kinect'
            DeviceIDs: {[1]  [2]  [3]  [4]}
           DeviceInfo: [1x4 struct]

Obtaining Kinect v2 Device information

Information on a specific device can be obtained by simply indexing into the device information structure array.

Get Kinect color device information.

colorDevice = kinectDeviceInfo.DeviceInfo(1)
colorDevice = 

             DefaultFormat: 'RGB_640x480'
       DeviceFileSupported: 0
                DeviceName: 'Kinect Color Sensor'
                  DeviceID: 1
     VideoInputConstructor: 'videoinput('kinect', 1)'
    VideoDeviceConstructor: 'imaq.VideoDevice('kinect', 1)'
          SupportedFormats: {1x7 cell}

Get Kinect depth device information.

depthDevice = kinectDeviceInfo.DeviceInfo(2)
depthDevice = 

             DefaultFormat: 'Depth_640x480'
       DeviceFileSupported: 0
                DeviceName: 'Kinect Depth Sensor'
                  DeviceID: 2
     VideoInputConstructor: 'videoinput('kinect', 2)'
    VideoDeviceConstructor: 'imaq.VideoDevice('kinect', 2)'
          SupportedFormats: {'Depth_320x240'  'Depth_640x480'  'Depth_80x60'}

Create color and depth videoinput objects

A videoinput object represents the connection between MATLAB® and an image acquisition device.

Create a color videoinput object.

colorVid = videoinput('kinect',1)
Summary of Video Input Object Using 'Kinect Color Sensor'.

   Acquisition Source(s):  Color Source is available.

  Acquisition Parameters:  'Color Source' is the current selected source.
                           10 frames per trigger using the selected source.
                           'RGB_640x480' video data to be logged upon START.
                           Grabbing first of every 1 frame(s).
                           Log data to 'memory' on trigger.

      Trigger Parameters:  1 'immediate' trigger(s) on START.

                  Status:  Waiting for START.
                           0 frames acquired since starting.
                           0 frames available for GETDATA.

Create a depth videoinput object.

depthVid = videoinput('kinect',2)
Summary of Video Input Object Using 'Kinect Depth Sensor'.

   Acquisition Source(s):  Depth Source is available.

  Acquisition Parameters:  'Depth Source' is the current selected source.
                           10 frames per trigger using the selected source.
                           'Depth_640x480' video data to be logged upon START.
                           Grabbing first of every 1 frame(s).
                           Log data to 'memory' on trigger.

      Trigger Parameters:  1 'immediate' trigger(s) on START.

                  Status:  Waiting for START.
                           0 frames acquired since starting.
                           0 frames available for GETDATA.

Preview both color and depth objects

Preview both videoinput objects.

preview([colorVid depthVid]);

Clear videoinput objects

delete(colorVid);
delete(depthVid);