Louis DUFOUR 1 year ago
commit e9de34e3ed

@ -0,0 +1,97 @@
//using Microsoft.Kinect;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using System.Windows.Media.Imaging;
//using System.Windows.Media;
//using System.Windows;
//using System.Windows.Shapes;
//namespace Lib
//{
// class ColorAndBodyImageStream : KinectStream
// {
// private BodyFrameReader reader;
// public BodyFrameReader Reader
// {
// get { return reader; }
// set { reader = value; }
// }
// private Body[] bodies;
// public Body[] Bodies
// {
// get { return bodies; }
// private set { bodies = value; }
// }
// public ColorAndBodyImageStream(KinectManager kinectManager) : base(kinectManager)
// {
// var frameDescription = KinectManager.Sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
// this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
// reader = KinectManager.Sensor.BodyFrameSource.OpenReader();
// reader.FrameArrived += Reader_BodyFrameArrived;
// // Initialiser le tableau des corps
// this.bodies = new Body[KinectManager.Sensor.BodyFrameSource.BodyCount];
// }
// private void DrawSkeleton(Body body)
// {
// foreach (JointType jointType in body.Joints.Keys)
// {
// Joint joint = body.Joints[jointType];
// if (joint.TrackingState == TrackingState.Tracked)
// {
// // Convertir les coordonnées du joint en coordonnées de l'écran
// Point point = new Point();
// ColorSpacePoint colorPoint = KinectManager.Sensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position);
// point.X = float.IsInfinity(colorPoint.X) ? 0 : colorPoint.X;
// point.Y = float.IsInfinity(colorPoint.Y) ? 0 : colorPoint.Y;
// // Dessiner le joint
// DrawJoint(point);
// }
// }
// // Dessinez les os ici si nécessaire
// }
// private void DrawJoint(Point point)
// {
// Ellipse ellipse = new Ellipse
// {
// Width = 10,
// Height = 10,
// Fill = new SolidColorBrush(Colors.Red)
// };
// Canvas.SetLeft(ellipse, point.X - ellipse.Width / 2);
// Canvas.SetTop(ellipse, point.Y - ellipse.Height / 2);
// skeletonCanvas.Children.Add(ellipse);
// }
// private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e)
// {
// using (var bodyFrame = e.FrameReference.AcquireFrame())
// {
// if (bodyFrame != null)
// {
// bodyFrame.GetAndRefreshBodyData(this.bodies);
// skeletonCanvas.Children.Clear(); // Nettoyer le canvas avant de dessiner
// foreach (var body in this.bodies)
// {
// if (body.IsTracked)
// {
// // Dessiner le squelette
// DrawSkeleton(body);
// }
// }
// }
// }
// }
// }
//}

@ -18,17 +18,26 @@ namespace Lib
reader = value;
}
}
public ColorImageStream() : base()
public ColorImageStream(KinectManager kinectManager) : base(kinectManager)
{
var frameDescription = KinectManager.Sensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
reader = KinectManager.Sensor.ColorFrameSource.OpenReader();
reader.FrameArrived += Reader_FrameArrived;
reader.FrameArrived += Reader_ColorFrameArrived;
}
private void Reader_FrameArrived(object sender, ColorFrameArrivedEventArgs e)
public override void Stop()
{
using (var colorFrame = e.FrameReference.AcquireFrame())
if (reader != null)
{
reader.Dispose();
reader = null;
}
}
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
{
if (colorFrame != null)
{

@ -0,0 +1,90 @@
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;
namespace Lib
{
class DepthImageStream : KinectStream
{
private DepthFrameReader reader;
public DepthFrameReader Reader
{
get
{
return reader;
}
set
{
reader = value;
}
}
private byte[] depthPixels;
public byte[] DepthPixels {
get {
return depthPixels;
}
set
{
depthPixels = value;
}
}
public DepthImageStream(KinectManager kinectManager) : base(kinectManager)
{
var frameDescription = KinectManager.Sensor.DepthFrameSource.FrameDescription;
this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null);
reader = KinectManager.Sensor.DepthFrameSource.OpenReader();
reader.FrameArrived += Reader_DepthFrameArrived;
DepthPixels = new byte[frameDescription.Width * frameDescription.Height];
}
private void Reader_DepthFrameArrived(object sender, DepthFrameArrivedEventArgs e)
{
using (DepthFrame depthFrame = e.FrameReference.AcquireFrame())
{
if (depthFrame != null)
{
FrameDescription depthFrameDescription = depthFrame.FrameDescription;
// Créez un tableau pour stocker les données de profondeur
ushort[] depthData = new ushort[depthFrameDescription.LengthInPixels];
depthFrame.CopyFrameDataToArray(depthData);
// Traitez les données de profondeur
ProcessDepthFrameData(depthData, depthFrameDescription.LengthInPixels, depthFrame.DepthMinReliableDistance, depthFrame.DepthMaxReliableDistance);
// Mettez à jour le bitmap de profondeur
this.Bitmap.WritePixels(
new Int32Rect(0, 0, depthFrameDescription.Width, depthFrameDescription.Height),
this.depthPixels,
depthFrameDescription.Width,
0);
}
}
}
public override void Stop()
{
if (reader != null)
{
reader.Dispose();
reader = null;
}
}
private void ProcessDepthFrameData(ushort[] depthData, uint depthFrameDataSize, ushort minDepth, ushort maxDepth)
{
// Convertir les données de profondeur en niveaux de gris
for (int i = 0; i < depthFrameDataSize; ++i)
{
ushort depth = depthData[i];
DepthPixels[i] = (byte)(depth >= minDepth && depth <= maxDepth ? (depth % 256) : 0);
}
}
}
}

@ -0,0 +1,92 @@
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;
namespace Lib
{
class InfraredImageStream : KinectStream
{
private InfraredFrameReader reader;
private byte[] infraredPixels;
public byte[] InfraredPixels
{
get
{
return infraredPixels;
}
private set
{
infraredPixels = value;
}
}
public InfraredFrameReader Reader
{
get
{
return reader;
}
private set
{
reader = value;
}
}
public InfraredImageStream(KinectManager kinectManager) : base(kinectManager)
{
var frameDescription = KinectManager.Sensor.InfraredFrameSource.FrameDescription;
this.Bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null);
reader = KinectManager.Sensor.InfraredFrameSource.OpenReader();
reader.FrameArrived += Reader_InfraredFrameArrived;
infraredPixels = new byte[frameDescription.Width * frameDescription.Height];
}
public override void Stop()
{
if (reader != null)
{
reader.Dispose();
reader = null;
}
}
private void ProcessInfraredFrameData(ushort[] frameData, uint frameDataSize)
{
// Convertir les données infrarouges en niveaux de gris
for (int i = 0; i < frameDataSize; ++i)
{
// Convertir la valeur infrarouge en une intensité lumineuse
byte intensity = (byte)(frameData[i] >> 8);
InfraredPixels[i] = intensity;
}
}
private void Reader_InfraredFrameArrived(object sender, InfraredFrameArrivedEventArgs e)
{
using (InfraredFrame infraredFrame = e.FrameReference.AcquireFrame())
{
if (infraredFrame != null)
{
FrameDescription infraredFrameDescription = infraredFrame.FrameDescription;
// Créez un tableau pour stocker les données infrarouges
ushort[] infraredData = new ushort[infraredFrameDescription.LengthInPixels];
infraredFrame.CopyFrameDataToArray(infraredData);
// Traitez les données infrarouges
ProcessInfraredFrameData(infraredData, infraredFrameDescription.LengthInPixels);
// Mettez à jour le bitmap infrarouge
this.Bitmap.WritePixels(
new Int32Rect(0, 0, infraredFrameDescription.Width, infraredFrameDescription.Height),
this.infraredPixels,
infraredFrameDescription.Width,
0);
}
}
}
}
}

@ -7,10 +7,6 @@ namespace Lib
public class KinectManager : INotifyPropertyChanged
{
private KinectSensor sensor;
private BodyFrameReader bodyFrameReader;
private Body[] bodies = null;
public KinectSensor Sensor {
get {
return sensor;
@ -31,96 +27,77 @@ namespace Lib
public KinectManager()
{
sensor = KinectSensor.GetDefault();
if (sensor != null)
if (Sensor == null)
{
bodyFrameReader = sensor.BodyFrameSource.OpenReader();
bodyFrameReader.FrameArrived += BodyFrameReader_FrameArrived;
StatusText = "Kinect n'est pas connecté";
}
}
private void BodyFrameReader_FrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
using (var frame = e.FrameReference.AcquireFrame())
else if (!Sensor.IsOpen)
{
if (frame != null)
{
if (bodies == null)
{
bodies = new Body[frame.BodyCount];
}
frame.GetAndRefreshBodyData(bodies);
}
StatusText = "Kinect n'est pas ouvert";
}
}
public Body GetNextBody()
{
if (bodies == null) return null;
foreach (var body in bodies)
else if (Sensor.IsAvailable)
{
if (body.IsTracked)
{
return body;
}
StatusText = "Kinect est disponible";
}
else
{
StatusText = "Kinect n'est pas disponible";
}
return null;
}
public bool StartSensor()
public void StartSensor()
{
if (sensor != null) {
if (sensor != null && !sensor.IsOpen) {
sensor.Open();
return true;
}
else
{
return false;
else {
sensor = KinectSensor.GetDefault();
sensor.Open();
}
}
public bool StopSensor()
public void StopSensor()
{
if (sensor != null)
{
sensor.Close();
return true;
}
else
{
return false;
sensor = null;
}
}
public bool Status
{
get { return Sensor != null && Sensor.IsAvailable; }
}
private string _statusText;
public string StatusText
{
get
{
if (Sensor == null)
get { return _statusText; }
set {
_statusText = value;
if(this.PropertyChanged != null)
{
return "Kinect n'est pas connecté";
}
else if (!Sensor.IsOpen)
{
return "Kinect n'est pas ouvert";
}
else if (Sensor.IsAvailable)
{
return "Kinect est disponible";
}
else
{
return "Kinect n'est pas disponible";
this.PropertyChanged(this, new PropertyChangedEventArgs("StatusText"));
}
}
}
private void KinectSensor_IsAvailableChanged(object sender, IsAvailableChangedEventArgs args)
{
// Vous pouvez ajouter ici une logique supplémentaire si nécessaire
}
}
}
{
if (Sensor == null)
{
StatusText = "Kinect n'est pas connecté";
}
else if (!Sensor.IsOpen)
{
StatusText = "Kinect n'est pas ouvert";
}
else if (Sensor.IsAvailable)
{
StatusText = "Kinect est disponible";
}
else
{
StatusText = "Kinect n'est pas disponible";
}
}
}
}

@ -22,7 +22,7 @@ namespace Lib
if (bitmap != value)
{
bitmap = value;
OnPropertyChanged(nameof(bitmap));
OnPropertyChanged(nameof(Bitmap));
}
}
}
@ -33,9 +33,13 @@ namespace Lib
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public KinectStream()
public KinectStream(KinectManager kinectManager)
{
KinectManager = new KinectManager();
KinectManager = kinectManager;
if (kinectManager.Sensor == null)
{
KinectManager.StartSensor();
}
}
public virtual void Start()
@ -43,9 +47,7 @@ namespace Lib
KinectManager.StartSensor();
}
public virtual void Stop()
{
KinectManager.StopSensor();
}
public abstract void Stop();
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lib
{
public enum KinectStreams
{
None,
Color,
Depth,
IR
}
}

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
@ -9,9 +8,44 @@ namespace Lib
{
public class KinectStreamsFactory
{
private Dictionary<KinectStream, Func<KinectStream>> streamFactory;
public KinectStreamsFactory() {
private KinectManager _kinectManager;
public KinectManager KinectManager {
get { return _kinectManager; }
private set { _kinectManager = value; }
}
private Dictionary<KinectStreams, Func<KinectStream>> _streamFactory;
public Dictionary<KinectStreams, Func<KinectStream>> StreamFactory
{
get { return _streamFactory; }
private set { _streamFactory = value; }
}
public KinectStreamsFactory(KinectManager kinect)
{
_kinectManager = kinect;
// Initialisation de la fabrique avec les fonctions de création pour chaque type de flux.
StreamFactory = new Dictionary<KinectStreams, Func<KinectStream>>
{
{ KinectStreams.Color, () => new ColorImageStream(KinectManager) },
{ KinectStreams.Depth, () => new DepthImageStream(KinectManager) },
{ KinectStreams.IR, () => new InfraredImageStream(KinectManager) }
};
}
public KinectStream this[KinectStreams stream]
{
get
{
if (StreamFactory.ContainsKey(stream))
{
return StreamFactory[stream]();
}
else
{
throw new ArgumentException("Invalid stream type.");
}
}
}
}
}

@ -46,9 +46,14 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="ColorAndBodyImageStream.cs" />
<Compile Include="ColorImageStream.cs" />
<Compile Include="DepthImageStream.cs" />
<Compile Include="InfraredImageStream.cs" />
<Compile Include="KinectManager.cs" />
<Compile Include="KinectStream.cs" />
<Compile Include="KinectStreams.cs" />
<Compile Include="KinectStreamsFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

@ -15,20 +15,20 @@
<StackPanel Grid.Row="0" Orientation="Horizontal" VerticalAlignment="Top">
<Ellipse Width="20" Height="20" Fill="Green" />
<TextBlock Text="Statut" VerticalAlignment="Center" Margin="5,0,0,0"/>
<TextBlock Text="{Binding CurrentKinectStream.KinectManager.StatusText}" VerticalAlignment="Center" Margin="5,0,0,0"/>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Top">
<Button Content="Filtre 1" Margin="5"/>
<Button Content="Filtre 2" Margin="5"/>
<Button Content="Filtre 3" Margin="5"/>
<Button Content="Filtre 1" Margin="5" Click="ToColorImageStream"/>
<Button Content="Filtre 2" Margin="5" Click="ToDepthImageStream"/>
<Button Content="Filtre 3" Margin="5" Click="ToInfraredImageStream"/>
<Button Content="Filtre 4" Margin="5"/>
<Button Content="Filtre 5" Margin="5"/>
</StackPanel>
<Viewbox Grid.Row="2" Stretch="Uniform">
<Grid>
<Image Source="{Binding ColorBitmap}" />
<Image Source="{Binding CurrentKinectStream.Bitmap}" />
<Canvas x:Name="skeletonCanvas" />
</Grid>
</Viewbox>

@ -2,6 +2,7 @@
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
@ -21,344 +22,79 @@ namespace WpfApp
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
public partial class MainWindow : Window,INotifyPropertyChanged
{
private KinectSensor kinectSensor = null;
// Attribut captor color
private ColorFrameReader colorFrameReader = null;
private WriteableBitmap colorBitmap = null;
// Attribut captor Body
private BodyFrameReader bodyFrameReader = null;
private Body[] bodies = null;
// Attribut captor Depth
private DepthFrameReader depthFrameReader = null;
private WriteableBitmap depthBitmap = null;
private byte[] depthPixels;
// Attribut captor InfraRouge
private InfraredFrameReader infraredFrameReader = null;
private byte[] infraredPixels;
private WriteableBitmap infraredBitmap;
// Propriété publique pour le binding
public WriteableBitmap ColorBitmap
private KinectManager kinectManager;
public KinectManager KinectManager
{
get { return this.colorBitmap; }
get { return kinectManager; }
set { kinectManager = value; }
}
public WriteableBitmap DepthBitmap
private KinectStream _currentKinectStream;
public KinectStream CurrentKinectStream
{
get { return this.depthBitmap; }
get { return _currentKinectStream; }
set {
_currentKinectStream = value;
OnPropertyChanged(nameof(CurrentKinectStream));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public WriteableBitmap InfraredBitmap
public void OnPropertyChanged(string propertyName)
{
get { return this.infraredBitmap; }
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private KinectStreamsFactory _factory;
public KinectStreamsFactory Factory
{
get { return _factory; }
set { _factory = value; }
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
KinectManager = new KinectManager();
Factory = new KinectStreamsFactory(KinectManager);
CurrentKinectStream = Factory[KinectStreams.Color];
Debug.WriteLine(CurrentKinectStream.KinectManager.StatusText);
CurrentKinectStream.Start();
Debug.WriteLine(CurrentKinectStream.KinectManager.StatusText);
// Initialiser la Kinect
this.kinectSensor = KinectSensor.GetDefault();
// Capteur couleur
// Ouvrir le lecteur de flux de couleur
this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader();
// Frame description pour les images de couleur
FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
// Créer le bitmap pour afficher l'image
this.colorBitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
// Gérer l'événement FrameArrived pour le flux de couleur
this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived;
// Initialisation du BodyFrameReader
this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader();
this.bodyFrameReader.FrameArrived += this.Reader_BodyFrameArrived;
// Initialiser le tableau des corps
this.bodies = new Body[this.kinectSensor.BodyFrameSource.BodyCount];
/* Capteur profondeur
// Initialisation du DepthFrameReader
this.depthFrameReader = this.kinectSensor.DepthFrameSource.OpenReader();
this.depthFrameReader.FrameArrived += this.Reader_DepthFrameArrived;
FrameDescription depthFrameDescription = this.kinectSensor.DepthFrameSource.FrameDescription;
// Initialisez depthPixels pour stocker les données de chaque pixel
this.depthPixels = new byte[depthFrameDescription.Width * depthFrameDescription.Height];
// Initialisez depthBitmap pour afficher les données de profondeur
this.depthBitmap = new WriteableBitmap(depthFrameDescription.Width, depthFrameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null);
// Capteur infra
// Initialisation du InfraredFrameReader
this.infraredFrameReader = this.kinectSensor.InfraredFrameSource.OpenReader();
this.infraredFrameReader.FrameArrived += this.Reader_InfraredFrameArrived;
FrameDescription infraredFrameDescription = this.kinectSensor.InfraredFrameSource.FrameDescription;
// Initialisez infraredPixels pour stocker les données de chaque pixel
this.infraredPixels = new byte[infraredFrameDescription.Width * infraredFrameDescription.Height];
// Initialisez infraredBitmap pour afficher les données infrarouges
this.infraredBitmap = new WriteableBitmap(infraredFrameDescription.Width, infraredFrameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null);
*/
// Ouvrir la Kinect
this.kinectSensor.Open();
}
private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
{
if (colorFrame != null)
{
FrameDescription colorFrameDescription = colorFrame.FrameDescription;
using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
{
this.colorBitmap.Lock();
// Vérifier si la taille de l'image a changé
if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight))
{
colorFrame.CopyConvertedFrameDataToIntPtr(
this.colorBitmap.BackBuffer,
(uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4),
ColorImageFormat.Bgra);
this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight));
}
this.colorBitmap.Unlock();
}
}
}
}
// Assurez-vous de fermer correctement le lecteur et le capteur Kinect lors de la fermeture de la fenêtre
private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.colorFrameReader != null)
{
this.colorFrameReader.Dispose();
this.colorFrameReader = null;
}
if (this.kinectSensor != null)
{
this.kinectSensor.Close();
this.kinectSensor = null;
}
}
private void Reader_BodyFrameArrived(object sender, BodyFrameArrivedEventArgs e)
{
using (var bodyFrame = e.FrameReference.AcquireFrame())
{
if (bodyFrame != null)
{
bodyFrame.GetAndRefreshBodyData(this.bodies);
skeletonCanvas.Children.Clear(); // Nettoyer le canvas avant de dessiner
foreach (var body in this.bodies)
{
if (body.IsTracked)
{
// Dessiner le squelette
DrawSkeleton(body);
}
}
}
}
}
private void DrawSkeleton(Body body)
{
// Tête et cou
DrawBone(body, JointType.Head, JointType.Neck);
DrawBone(body, JointType.Neck, JointType.SpineShoulder);
// Torse
DrawBone(body, JointType.SpineShoulder, JointType.SpineMid);
DrawBone(body, JointType.SpineMid, JointType.SpineBase);
DrawBone(body, JointType.SpineShoulder, JointType.ShoulderRight);
DrawBone(body, JointType.SpineShoulder, JointType.ShoulderLeft);
DrawBone(body, JointType.SpineBase, JointType.HipRight);
DrawBone(body, JointType.SpineBase, JointType.HipLeft);
// Bras droit
DrawBone(body, JointType.ShoulderRight, JointType.ElbowRight);
DrawBone(body, JointType.ElbowRight, JointType.WristRight);
DrawBone(body, JointType.WristRight, JointType.HandRight);
DrawBone(body, JointType.HandRight, JointType.HandTipRight);
DrawBone(body, JointType.WristRight, JointType.ThumbRight);
// Bras gauche
DrawBone(body, JointType.ShoulderLeft, JointType.ElbowLeft);
DrawBone(body, JointType.ElbowLeft, JointType.WristLeft);
DrawBone(body, JointType.WristLeft, JointType.HandLeft);
DrawBone(body, JointType.HandLeft, JointType.HandTipLeft);
DrawBone(body, JointType.WristLeft, JointType.ThumbLeft);
// Jambe droite
DrawBone(body, JointType.HipRight, JointType.KneeRight);
DrawBone(body, JointType.KneeRight, JointType.AnkleRight);
DrawBone(body, JointType.AnkleRight, JointType.FootRight);
// Jambe gauche
DrawBone(body, JointType.HipLeft, JointType.KneeLeft);
DrawBone(body, JointType.KneeLeft, JointType.AnkleLeft);
DrawBone(body, JointType.AnkleLeft, JointType.FootLeft);
// Dessinez les joints
foreach (JointType jointType in body.Joints.Keys)
{
Joint joint = body.Joints[jointType];
if (joint.TrackingState == TrackingState.Tracked)
{
DrawJoint(MapJointToScreen(joint));
}
}
}
private void DrawJoint(Point point)
{
Ellipse ellipse = new Ellipse
{
Width = 10,
Height = 10,
Fill = new SolidColorBrush(Colors.Red)
};
Canvas.SetLeft(ellipse, point.X - ellipse.Width / 2);
Canvas.SetTop(ellipse, point.Y - ellipse.Height / 2);
skeletonCanvas.Children.Add(ellipse);
}
private void DrawBone(Body body, JointType jointType0, JointType jointType1)
{
Joint joint0 = body.Joints[jointType0];
Joint joint1 = body.Joints[jointType1];
// Ne dessinez que si les deux joints sont suivis
if (joint0.TrackingState == TrackingState.Tracked && joint1.TrackingState == TrackingState.Tracked)
{
Line bone = new Line
{
Stroke = new SolidColorBrush(Colors.LightBlue),
StrokeThickness = 4,
X1 = MapJointToScreen(joint0).X,
Y1 = MapJointToScreen(joint0).Y,
X2 = MapJointToScreen(joint1).X,
Y2 = MapJointToScreen(joint1).Y
};
skeletonCanvas.Children.Add(bone);
}
}
private Point MapJointToScreen(Joint joint)
{
ColorSpacePoint colorPoint = this.kinectSensor.CoordinateMapper.MapCameraPointToColorSpace(joint.Position);
// Gestion des coordonnées infinies
float x = float.IsInfinity(colorPoint.X) ? 0 : colorPoint.X;
float y = float.IsInfinity(colorPoint.Y) ? 0 : colorPoint.Y;
return new Point(x, y);
Debug.WriteLine(CurrentKinectStream.KinectManager.StatusText);
CurrentKinectStream.Stop();
}
private void Reader_DepthFrameArrived(object sender, DepthFrameArrivedEventArgs e)
private void ToColorImageStream(object sender, RoutedEventArgs e)
{
using (DepthFrame depthFrame = e.FrameReference.AcquireFrame())
{
if (depthFrame != null)
{
FrameDescription depthFrameDescription = depthFrame.FrameDescription;
// Créez un tableau pour stocker les données de profondeur
ushort[] depthData = new ushort[depthFrameDescription.LengthInPixels];
depthFrame.CopyFrameDataToArray(depthData);
// Traitez les données de profondeur
ProcessDepthFrameData(depthData, depthFrameDescription.LengthInPixels, depthFrame.DepthMinReliableDistance, depthFrame.DepthMaxReliableDistance);
// Mettez à jour le bitmap de profondeur
this.depthBitmap.WritePixels(
new Int32Rect(0, 0, depthFrameDescription.Width, depthFrameDescription.Height),
this.depthPixels,
depthFrameDescription.Width,
0);
}
}
CurrentKinectStream.Stop();
CurrentKinectStream = Factory[KinectStreams.Color];
Debug.WriteLine(CurrentKinectStream.GetType().Name);
CurrentKinectStream.Start();
}
private void ProcessDepthFrameData(ushort[] depthData, uint depthFrameDataSize, ushort minDepth, ushort maxDepth)
private void ToDepthImageStream(object sender, RoutedEventArgs e)
{
// Convertir les données de profondeur en niveaux de gris
for (int i = 0; i < depthFrameDataSize; ++i)
{
ushort depth = depthData[i];
this.depthPixels[i] = (byte)(depth >= minDepth && depth <= maxDepth ? (depth % 256) : 0);
}
CurrentKinectStream.Stop();
CurrentKinectStream = Factory[KinectStreams.Depth];
Debug.WriteLine(CurrentKinectStream.GetType().Name);
CurrentKinectStream.Start();
}
private void ProcessInfraredFrameData(ushort[] frameData, uint frameDataSize)
private void ToInfraredImageStream(object sender, RoutedEventArgs e)
{
// Convertir les données infrarouges en niveaux de gris
for (int i = 0; i < frameDataSize; ++i)
{
// Convertir la valeur infrarouge en une intensité lumineuse
byte intensity = (byte)(frameData[i] >> 8);
this.infraredPixels[i] = intensity;
}
}
private void Reader_InfraredFrameArrived(object sender, InfraredFrameArrivedEventArgs e)
{
using (InfraredFrame infraredFrame = e.FrameReference.AcquireFrame())
{
if (infraredFrame != null)
{
FrameDescription infraredFrameDescription = infraredFrame.FrameDescription;
// Créez un tableau pour stocker les données infrarouges
ushort[] infraredData = new ushort[infraredFrameDescription.LengthInPixels];
infraredFrame.CopyFrameDataToArray(infraredData);
// Traitez les données infrarouges
ProcessInfraredFrameData(infraredData, infraredFrameDescription.LengthInPixels);
// Mettez à jour le bitmap infrarouge
this.infraredBitmap.WritePixels(
new Int32Rect(0, 0, infraredFrameDescription.Width, infraredFrameDescription.Height),
this.infraredPixels,
infraredFrameDescription.Width,
0);
}
}
CurrentKinectStream.Stop();
CurrentKinectStream = Factory[KinectStreams.IR];
Debug.WriteLine(CurrentKinectStream.GetType().Name);
CurrentKinectStream.Start();
}
}
}
}
Loading…
Cancel
Save