Skip to content

Commit 28f439e

Browse files
committed
Kinect V2 to MATLAB
Import Kinect V2 (New Sensor) Skeleton Joints Points Cloud to MATLAB for Further Analysis
1 parent 471e815 commit 28f439e

13 files changed

+775
-0
lines changed

KinectV2_to_MATLAB.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.31101.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KinectV2_to_MATLAB", "KinectV2_to_MATLAB\KinectV2_to_MATLAB.csproj", "{D44C81A9-2776-4511-847F-1D905C626928}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{D44C81A9-2776-4511-847F-1D905C626928}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D44C81A9-2776-4511-847F-1D905C626928}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D44C81A9-2776-4511-847F-1D905C626928}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D44C81A9-2776-4511-847F-1D905C626928}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

KinectV2_to_MATLAB/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>

KinectV2_to_MATLAB/App.xaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Application x:Class="KinectV2_to_MATLAB.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
StartupUri="MainWindow.xaml">
5+
<Application.Resources>
6+
7+
</Application.Resources>
8+
</Application>

KinectV2_to_MATLAB/App.xaml.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace KinectV2_to_MATLAB
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}

KinectV2_to_MATLAB/Extension.cs

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using Microsoft.Kinect;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Windows.Controls;
7+
using System.Windows.Media;
8+
using System.Windows.Media.Imaging;
9+
using System.Windows.Shapes;
10+
11+
namespace KinectV2_to_MATLAB
12+
{
13+
public static class Extension
14+
// to use extension, the class must be static.
15+
{
16+
#region Camera
17+
18+
public static ImageSource ToBitmap(this ColorFrame frame)
19+
{
20+
int width = frame.FrameDescription.Width;
21+
int height = frame.FrameDescription.Height;
22+
PixelFormat format = PixelFormats.Bgr32;
23+
24+
byte[] pixels = new byte[width * height * ((format.BitsPerPixel + 7) / 8)];
25+
26+
if (frame.RawColorImageFormat == ColorImageFormat.Bgra)
27+
{
28+
frame.CopyRawFrameDataToArray(pixels);
29+
}
30+
else
31+
{
32+
frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra);
33+
}
34+
35+
int stride = width * format.BitsPerPixel / 8;
36+
37+
return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride);
38+
}
39+
40+
#endregion
41+
#region Coordinates
42+
43+
#region scaling
44+
private static float Scale(double maxPixel, double maxSkeleton, float position)
45+
{
46+
float value = (float)((((maxPixel / maxSkeleton) / 2) * position) + (maxPixel / 2));
47+
48+
if (value > maxPixel)
49+
{
50+
return (float)maxPixel;
51+
}
52+
53+
if (value < 0)
54+
{
55+
return 0;
56+
}
57+
58+
return value;
59+
}
60+
61+
public static Joint ScaleTo(this Joint joint, double width, double height, float skeletonMaxX, float skeletonMaxY)
62+
{
63+
joint.Position = new CameraSpacePoint
64+
{
65+
X = Scale(width, skeletonMaxX, joint.Position.X),
66+
Y = Scale(height, skeletonMaxY, -joint.Position.Y),
67+
Z = joint.Position.Z
68+
};
69+
70+
return joint;
71+
}
72+
73+
public static Joint ScaleTo(this Joint joint, double width, double height)
74+
{
75+
return ScaleTo(joint, width, height, 1.0f, 1.0f);
76+
}
77+
78+
#endregion
79+
80+
#region drawing
81+
// drawing joints
82+
public static void DrawPoint(this Canvas canvas, Joint joint)
83+
{
84+
if (joint.TrackingState == TrackingState.NotTracked) return;
85+
86+
joint = joint.ScaleTo(canvas.ActualWidth, canvas.ActualHeight);
87+
88+
Ellipse ellipse = new Ellipse
89+
{
90+
Width = 20,
91+
Height = 20,
92+
Fill = new SolidColorBrush(Colors.LightBlue)
93+
};
94+
95+
Canvas.SetLeft(ellipse, joint.Position.X - ellipse.Width / 2);
96+
Canvas.SetTop(ellipse, joint.Position.Y - ellipse.Height / 2);
97+
98+
canvas.Children.Add(ellipse);
99+
}
100+
// drawing lines by two points coordinates
101+
public static void DrawLine(this Canvas canvas, Joint first, Joint second)
102+
{
103+
if (first.TrackingState == TrackingState.NotTracked || second.TrackingState == TrackingState.NotTracked) return;
104+
105+
first = first.ScaleTo(canvas.ActualWidth, canvas.ActualHeight);
106+
second = second.ScaleTo(canvas.ActualWidth, canvas.ActualHeight);
107+
108+
Line line = new Line
109+
{
110+
X1 = first.Position.X,
111+
Y1 = first.Position.Y,
112+
X2 = second.Position.X,
113+
Y2 = second.Position.Y,
114+
StrokeThickness = 8,
115+
Stroke = new SolidColorBrush(Colors.LightBlue)
116+
};
117+
118+
canvas.Children.Add(line);
119+
}
120+
121+
// drawing skeleton by using DrawLine method
122+
public static void DrawSkeleton(this Canvas canvas, Body body)
123+
{
124+
if (body == null) return;
125+
126+
foreach (Joint joint in body.Joints.Values)
127+
{
128+
canvas.DrawPoint(joint);
129+
}
130+
131+
canvas.DrawLine(body.Joints[JointType.Head], body.Joints[JointType.Neck]);
132+
canvas.DrawLine(body.Joints[JointType.Neck], body.Joints[JointType.SpineShoulder]);
133+
canvas.DrawLine(body.Joints[JointType.SpineShoulder], body.Joints[JointType.ShoulderLeft]);
134+
canvas.DrawLine(body.Joints[JointType.SpineShoulder], body.Joints[JointType.ShoulderRight]);
135+
canvas.DrawLine(body.Joints[JointType.SpineShoulder], body.Joints[JointType.SpineMid]);
136+
canvas.DrawLine(body.Joints[JointType.ShoulderLeft], body.Joints[JointType.ElbowLeft]);
137+
canvas.DrawLine(body.Joints[JointType.ShoulderRight], body.Joints[JointType.ElbowRight]);
138+
canvas.DrawLine(body.Joints[JointType.ElbowLeft], body.Joints[JointType.WristLeft]);
139+
canvas.DrawLine(body.Joints[JointType.ElbowRight], body.Joints[JointType.WristRight]);
140+
canvas.DrawLine(body.Joints[JointType.WristLeft], body.Joints[JointType.HandLeft]);
141+
canvas.DrawLine(body.Joints[JointType.WristRight], body.Joints[JointType.HandRight]);
142+
canvas.DrawLine(body.Joints[JointType.HandLeft], body.Joints[JointType.HandTipLeft]);
143+
canvas.DrawLine(body.Joints[JointType.HandRight], body.Joints[JointType.HandTipRight]);
144+
canvas.DrawLine(body.Joints[JointType.HandTipLeft], body.Joints[JointType.ThumbLeft]);
145+
canvas.DrawLine(body.Joints[JointType.HandTipRight], body.Joints[JointType.ThumbRight]);
146+
canvas.DrawLine(body.Joints[JointType.SpineMid], body.Joints[JointType.SpineBase]);
147+
canvas.DrawLine(body.Joints[JointType.SpineBase], body.Joints[JointType.HipLeft]);
148+
canvas.DrawLine(body.Joints[JointType.SpineBase], body.Joints[JointType.HipRight]);
149+
canvas.DrawLine(body.Joints[JointType.HipLeft], body.Joints[JointType.KneeLeft]);
150+
canvas.DrawLine(body.Joints[JointType.HipRight], body.Joints[JointType.KneeRight]);
151+
canvas.DrawLine(body.Joints[JointType.KneeLeft], body.Joints[JointType.AnkleLeft]);
152+
canvas.DrawLine(body.Joints[JointType.KneeRight], body.Joints[JointType.AnkleRight]);
153+
canvas.DrawLine(body.Joints[JointType.AnkleLeft], body.Joints[JointType.FootLeft]);
154+
canvas.DrawLine(body.Joints[JointType.AnkleRight], body.Joints[JointType.FootRight]);
155+
}
156+
#endregion
157+
158+
#endregion
159+
}
160+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{D44C81A9-2776-4511-847F-1D905C626928}</ProjectGuid>
8+
<OutputType>WinExe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>KinectV2_to_MATLAB</RootNamespace>
11+
<AssemblyName>KinectV2_to_MATLAB</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
15+
<WarningLevel>4</WarningLevel>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
18+
<PlatformTarget>AnyCPU</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28+
<PlatformTarget>AnyCPU</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="Microsoft.Kinect, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
38+
<SpecificVersion>False</SpecificVersion>
39+
<HintPath>..\..\..\..\..\..\..\Program Files\Microsoft SDKs\Kinect\v2.0_1409\Assemblies\Microsoft.Kinect.dll</HintPath>
40+
</Reference>
41+
<Reference Include="System" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Core" />
46+
<Reference Include="System.Xml.Linq" />
47+
<Reference Include="System.Data.DataSetExtensions" />
48+
<Reference Include="System.Xaml">
49+
<RequiredTargetFramework>4.0</RequiredTargetFramework>
50+
</Reference>
51+
<Reference Include="WindowsBase" />
52+
<Reference Include="PresentationCore" />
53+
<Reference Include="PresentationFramework" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<ApplicationDefinition Include="App.xaml">
57+
<Generator>MSBuild:Compile</Generator>
58+
<SubType>Designer</SubType>
59+
</ApplicationDefinition>
60+
<Page Include="MainWindow.xaml">
61+
<Generator>MSBuild:Compile</Generator>
62+
<SubType>Designer</SubType>
63+
</Page>
64+
<Compile Include="App.xaml.cs">
65+
<DependentUpon>App.xaml</DependentUpon>
66+
<SubType>Code</SubType>
67+
</Compile>
68+
<Compile Include="Extension.cs" />
69+
<Compile Include="MainWindow.xaml.cs">
70+
<DependentUpon>MainWindow.xaml</DependentUpon>
71+
<SubType>Code</SubType>
72+
</Compile>
73+
</ItemGroup>
74+
<ItemGroup>
75+
<Compile Include="Properties\AssemblyInfo.cs">
76+
<SubType>Code</SubType>
77+
</Compile>
78+
<Compile Include="Properties\Resources.Designer.cs">
79+
<AutoGen>True</AutoGen>
80+
<DesignTime>True</DesignTime>
81+
<DependentUpon>Resources.resx</DependentUpon>
82+
</Compile>
83+
<Compile Include="Properties\Settings.Designer.cs">
84+
<AutoGen>True</AutoGen>
85+
<DependentUpon>Settings.settings</DependentUpon>
86+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
87+
</Compile>
88+
<EmbeddedResource Include="Properties\Resources.resx">
89+
<Generator>ResXFileCodeGenerator</Generator>
90+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
91+
</EmbeddedResource>
92+
<None Include="Properties\Settings.settings">
93+
<Generator>SettingsSingleFileGenerator</Generator>
94+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
95+
</None>
96+
<AppDesigner Include="Properties\" />
97+
</ItemGroup>
98+
<ItemGroup>
99+
<None Include="App.config" />
100+
</ItemGroup>
101+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
102+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
103+
Other similar extension points exist, see Microsoft.Common.targets.
104+
<Target Name="BeforeBuild">
105+
</Target>
106+
<Target Name="AfterBuild">
107+
</Target>
108+
-->
109+
</Project>

KinectV2_to_MATLAB/MainWindow.xaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Window x:Class="KinectV2_to_MATLAB.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
Title="Preview" Height="768" Width="1024">
5+
<Grid>
6+
<Image Name="camera" />
7+
<Canvas Name="canvas" />
8+
</Grid>
9+
</Window>

0 commit comments

Comments
 (0)