File Exchange

image thumbnail

Kinect Microsoft SDK

version 1.3 (18.2 KB) by

Video, Audio, Depth Stream and Skeleton tracking

11 Ratings



View License

This zip-file contains c++ wrapper functions for the Microsoft Kinect, Microsoft SDK Beta2 library.

To compile the code to mex-files use the Microsoft Visual Studio​ (Express), and install Beta2 library:
- KinectSDK32.msi
- KinectSDK64.msi

Execute compile_cpp_files. Now the mex-files are ready to use.

- Example : Will show the depth and image stream from the Kinect.

- ExampleSkeleton : Will show Skeleton tracking

- ExampleMicrophone: Will audio recording

See also, the OpenNI library wrapper functions (Which is almost bug free and has more functionality):

Note this is "version 0b"!, and contains the following bug:
There is a problem with stopping the Kinect Process. You have to manually kill Matlab.exe in the taskmanager to stop the process.
If some one has a solution please let me know...

Comments and Ratings (33)


i've worked on win 7 32bit matlab 2013b 32bit


All works fine but..
there seem to be a bug in
This function do not release kinect and if I call later "clear all" command, matlab crashes.
please keep me informed if you resolve this issue
thank you

Sanjay Saini

Can anyone give solution of my problem i am running this code with MATLAB 2013b in 64bit. I have install KinectSDK64.msi properly in my system while i am executing compile_cpp_files it showing error like: Error: File: compile_cpp_files.m Line: 1 Column: 28
Unexpected MATLAB expression.

My Path is ('C:\Program Files (x86)\Microsoft Research KinectSDK\')


Adam (view profile)

I am running Kinect SDK Beta 2 with an xbox Kinect, Visual Studio 2010 and Matlab R2012b. I was getting some errors in mxMSCreateContext.cpp but fixed by including lib and inc files in same directory.

Now I am getting this:
mxMSDepth.cpp(36) : error C2065: 'INuiFrameTexture' : undeclared identifier
mxMSDepth.cpp(36) : error C2065: 'pTexture' : undeclared identifier
mxMSDepth.cpp(37) : error C2065: 'NUI_LOCKED_RECT' : undeclared identifier
mxMSDepth.cpp(37) : error C2146: syntax error : missing ';' before identifier 'LockedRect'
mxMSDepth.cpp(37) : error C2065: 'LockedRect' : undeclared identifier
mxMSDepth.cpp(38) : error C2065: 'pTexture' : undeclared identifier
mxMSDepth.cpp(38) : error C2227: left of '->LockRect' must point to class/struct/union/generic type
type is ''unknown-type''
mxMSDepth.cpp(38) : error C2065: 'LockedRect' : undeclared identifier
mxMSDepth.cpp(39) : error C2065: 'LockedRect' : undeclared identifier
mxMSDepth.cpp(39) : error C2228: left of '.Pitch' must have class/struct/union
type is ''unknown-type''
mxMSDepth.cpp(40) : error C2065: 'LockedRect' : undeclared identifier
mxMSDepth.cpp(40) : error C2228: left of '.pBits' must have class/struct/union
type is ''unknown-type''

C:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'mxMSDepth.cpp' failed.

nuiCapture can export Kinect data to Matlab

I have a problem please help me, I using Matlab 2012a Windows7 x64 compile and test program to found

Invalid MEX-file 'C:\MATLAB\Kinect_Microsoft_version0c\Mex\mxMSCreateContext.mexw64': The
specified module could not be found.

Error in Example (line 5)
KinectHandles = mxMSCreateContext([2 1]);



kinect (view profile)

I found that do the follow change will solve the kinect hang issue:
KinectHandles=mxMSCreateContext([2 1]);


Pankaj (view profile)

Huanzhong, it would be helpful if you could explain the function call changes you made to reflect newest library.


Rod (view profile)

Huanzhong, could you elaborate on the changes that need to be made to the cpp files? I have a set up identical to yours.


I have made it work in Windows 7 machine with Matlab 2010a or later using 2010 c++ compiler (I only did the color, depth, and skeleton, not the audio). This is using the newest kinect sdk. Besides pointing to the correct sdk path in the compile_cpp_files.m file, you need to update all the function calls in all the .cpp files to reflect the newest library. Also, I have noticed a slight problem in the mxMSSkeleton.cpp program where the following 2 lines:

Pos[j+r+Jdimsc[0]*4] = (double) ( fx * depthwidth + 0.5f );
Pos[j+r+Jdimsc[0]*5] = (double) ( fy * depthheight + 0.5f );

should actually be:
Pos[j+r+Jdimsc[0]*4] = (double) ( fx * depthwidth)/320.0;
Pos[j+r+Jdimsc[0]*5] = (double) ( fy * depthheight)/240.0;

in order for the skeleton to show up correctly in Matlab figures assuming you have called the create context program with [2 1] parameter.

As far as stopping Kinect and restarting it, I also have encountered trouble. One thing that might help would be to clear your workspace after mxMSDeleteContext was called so the KinectHandles don't exist anymore. And maybe give a little bit time for Kinect to shutdown. Then run a new one, that works sometimes. However, still not stable.

If you need the working codes, let me know. Maybe I can email you.

And thank you Dirk-Jan Kroon very much for all your codes.


nosyan (view profile)

Thank you for good codes.

I have a problem to stop or restart the Kinect.
I've changed this code for new Kinect for Windows SDK.
In this code, after I restarted the Kinect process, MATLAB freezed.

mxMSCreateContext -> mxMSDeleteContext -> mxMSCreateContext (Freeze!).

I guess that mxMSDeleteContext has a bug.
Do somebody know about it?


Isacc (view profile)

I have a problem to stop the Kinect process. In the file "Example.m" at the end lines says that with "mxMSDeleteContext(KinectHandles" you can stop the process. But I don't know how do it. Please, somebody can help me? or give me an example?.


Jeff (view profile)

Petter, additional clearification on the modification required to make this work with the v1.0 Kinect SDK would be greatly appriciated. Thanks.

Alexander Wallar

Some great code. Does it work on MATLAB 2010a?


Petter, which SDK did you use? Can you detail the changes required in the source files?


Petter (view profile)

Since the SDK changed, the source files have to be modified. But it seems to be working well.

Also, the stopping problem is annoying.


I get the following error (with Matlab R2011b and Kinect SDK64)

Warning: MEX could not find the library "MSRKinectNUI" specified with -l option.
MEX looked for a file with one of the names:
MEX looked for the library in the following directories:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\LIB\amd64
C:\Program Files\Microsoft SDKs\Windows\v7.1\lib\x64
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\LIB
C:\Program Files (x86)\Microsoft Research KinectSDK\\lib\amd64

Contents of C:\Users\rafi\AppData\Local\Temp\mex_JN1ss6\mex_tmp.rsp:

--> link /out:"mxMSCreateContext.mexw64" /dll /export:mexFunction /LIBPATH:"C:\ProgramFiles\MATLAB\R2011b\extern\lib\win64\microsoft" libmx.lib libmex.lib libmat.lib /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /manifest /incremental:NO /implib:"C:\Users\rafi\AppData\Local\Temp\mex_JN1ss6\templib.x" /MAP:"" @C:\Users\rafi\AppData\Local\Temp\mex_JN1ss6\mex_tmp.rsp

Creating library C:\Users\rafi\AppData\Local\Temp\mex_JN1ss6\templib.x and object C:\Users\rafi\AppData\Local\Temp\mex_JN1ss6\templib.exp
mxMSCreateContext.obj : error LNK2019: unresolved external symbol __imp_MSR_NuiCreateInstanceByIndex referenced in function mexFunction
mxMSCreateContext.mexw64 : fatal error LNK1120: 1 unresolved externals

C:\PROGRA~4\MATLAB\R2011B\BIN\MEX.PL: Error: Link of 'mxMSCreateContext.mexw64' failed.

Error using mex (line 206)
Unable to complete successfully.

Error in compile_cpp_files (line 47)
mex('-v',['-L' MicrosoftSDKPathLib],'-lMSRKinectNUI',['-I' MicrosoftSDKPathInclude],Filename);



Can you please tell me how to generate real world coordinates from this system?


upn (view profile)

??? Error using ==> mex at 222
Unable to complete successfully.

Error in ==> compile_cpp_files at 45
MicrosoftSDKPathInclude],Filename); with SDK have solution please


This version does not seem to work with the Kinect SDK I was able to find a first problem due to the new name of the environment varable: KINECTSDK10_DIR. However I still get an error message when running compile_cpp: mxMSAudioClose.cpp(24) : fatal error C1083: Cannot open include file: 'MSRKinectAudio.h': No such file or directory.
If no fix is available, can anyone provide the "old" Kinect SDK with which this works? Thanks


Can anyone tell how to control tilt angle of the kinect camera



Tim (view profile)

Anyone tested yet with the new version 1.0.3 SDK released 1 February 2012 ?

Muhammad Amir

??? Error using ==> mex at 208
Unable to complete successfully.

Error in ==> compile_cpp_files at 47
mex('-v',['-L' MicrosoftSDKPathLib],'-lMSRKinectNUI',['-I' MicrosoftSDKPathInclude],Filename);

This is error I get during compile. How to solve this??


Jnk (view profile)

Nice. I would recomend to support more than one Kinect device. It is trivial to do it, just replace call to :
hr = MSR_NuiCreateInstanceByIndex((int)0, &m_pNuiInstance);

to a different number, which could be passed as a paramter of mxMSCreateContext function.


Itay (view profile)

Great work!
Would it be possible to add acquisition of the 4 voice channels, instead of just 1?


Chi (view profile)

This works great, but only with the default [imageres depthres] values in line 5 of the Example.m file. Any other value (say [2 2] instead of the default [2 1]) makes the corresponding initialization fail.

That is, I'd like to get 640x480 depth data (and reduce the video stream to 320x240 if bandwidth becomes an issue). But it gives me a 'Depth Sensor Initialization Failed' error (Line 105 of mxMSCreateContext.cpp). Any ideas how to fix this?


Tim (view profile)

New version 0c works nicely too. Beta2 Kinect SDK now has native 64bit but my Visual Studio C++ Express is 32bit only. But I installed the 64bit SDK under Win7 Pro 64bit and the code compiled and ran OK again under 32bit Matlab. Hopefully MS will fix the thread lock bug eventually.

Dirk-Jan Kroon

Dirk-Jan Kroon (view profile)


The x,y coordinates are aligned with the depth image, which is twice as small as the video-stream. Thus this code : x=(Pos(1:20,5)-0.5)*2; transfers the coordinates from depth to photo image.
But this is only true if the depth image and photo image are aligned, and I don't know if that is always true.


Tim (view profile)

Great code.
I have the same question ... what is the basis of adjusting the XY coords before plotting them over the video ?:


lalit (view profile)


lalit (view profile)

This version worked smooth on 64 bit Windows with 32 bit Matlab. Great job!
Though within the code I would like to know the logic behind adjusting the x,y co-ordinates.



Support Beta2 SDK


Added Audio. Cleaned c-code and comments.

MATLAB Release
MATLAB 7.13 (R2011b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video