Skip to content

Commit

Permalink
Preview window finished
Browse files Browse the repository at this point in the history
Co-Authored-By: Abdurrahman Akın Aktaş <[email protected]>
  • Loading branch information
AnilOsmanTur and AbdurrahmanAkinAktas committed Sep 4, 2018
1 parent 00dc0b5 commit ba4fd57
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 255 deletions.
24 changes: 23 additions & 1 deletion KinectRecorder/DepthHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ public void Read(ref WriteableBitmap depthPreview)
this.depthPreviewPixels = depthReader.ReadBytes(Width * Height * 2);


int stride = 2, j, k;
for (int i = 0; i < Width * Height; ++i)
{
k = stride * i;
// Get the depth for this pixel
ushort depth = (ushort)((ushort)depthPreviewPixels[k] + (ushort)(depthPreviewPixels[k + 1] << 8));

// To convert to a byte, we're mapping the depth value to the byte range.
// Values outside the reliable depth range are mapped to 0 (black).

if(depth != 0)
{
depth = (ushort)(((depth - 500) / (float)4000) * (ushort.MaxValue - 1));

this.depthPreviewPixels[k] = (byte)(depth);
this.depthPreviewPixels[k + 1] = (byte)(depth >> 8);
}

}



depthPreview.WritePixels(
new Int32Rect(0, 0, (int)(depthPreview.Width),(int)(depthPreview.Height)),
this.depthPreviewPixels,
Expand Down Expand Up @@ -223,7 +245,7 @@ public void DepthFrameArrival(DepthFrame df, ref bool frameProcessed, double fps
this.depthBinaryBuffer.Enqueue((byte[])(depthPixelBuffer.Clone()));
this.frameCount++;
}
if(garbageCount > 1000)
if(garbageCount > 500)
{
System.GC.Collect();
garbageCount = 0;
Expand Down
89 changes: 61 additions & 28 deletions KinectRecorder/InfraredHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ class InfraredHandler

private String infraredVideoPath;
private VideoFileWriter infraredWriter = new VideoFileWriter();
private VideoFileReader infraredReader = new VideoFileReader();
//private VideoFileReader infraredReader = new VideoFileReader();
private int bitRate = 1200000;


private BinaryReader infraredReader;
private BinaryWriter binaryWriter;
private int readBinaryCount = 0;
private int savedBinaryCount = 0;

public UInt32 frameCount = 0;
Expand All @@ -53,12 +56,13 @@ class InfraredHandler

private bool infraredRecording = false;
public byte[] infraredPixels = null;
public byte[] infraredPixelBuffer = null;
public bool show;

public byte[] infraredPreviewPixels = null;

static InfraredHandler instance = new InfraredHandler();

public static InfraredHandler Instance
{
get { return instance; }
Expand All @@ -72,24 +76,15 @@ public void InfraredHandlerSet(FrameDescription fd)

// to show on screen
infraredPixels = new byte[Width * Height * 2];
infraredPixelBuffer = new byte[Width * Height * 2];

// to save to a video helper buffer
//infraredPixelBuffer = new byte[Width * Height * 3];

infraredPreviewPixels = new byte[Width * Height * 2];

}
public void openReader()
{
infraredReader.Open(infraredVideoPath);
readerFrameCount = infraredReader.FrameCount;
}

public void closeReader()
{
infraredReader.Close();
readerFrameCount = 0;
}
public void SetShowState(bool state)
{
show = state;
Expand All @@ -104,32 +99,67 @@ public uint getBPP() // bytes per pixel
{
return this.infraredFrameDescription.BytesPerPixel;
}
public void Read(ref WriteableBitmap infraredPreview)
{
Bitmap img = infraredReader.ReadVideoFrame();

int k = 0;
for (int i = 0; i < img.Height; i++)
public void openReader(string path)
{
try
{
for (int j = 0; j < img.Width; j++)
{
infraredReader = new BinaryReader(new FileStream(path, FileMode.Open));
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot open file.");
return;
}

System.Drawing.Color c = img.GetPixel(j, i);
readerFrameCount = savedBinaryCount - 1;
}

//depth = (int)((float) depth / 4000 * ushort.MaxValue);
this.infraredPreviewPixels[k++] = (byte)c.R;
this.infraredPreviewPixels[k++] = (byte)c.G;
public void closeReader()
{
infraredReader.Close();
readerFrameCount = 0;
}

}
public void startReading()
{
readBinaryCount = 0;
}

public void Read(ref WriteableBitmap infraredPreview)
{
openReader(infraredVideoPath + "/" + readBinaryCount);

this.infraredPreviewPixels = infraredReader.ReadBytes(Width * Height * 2);

int pixelIndex = 0;
for (int i = 0; i < (Width * Height); ++i)
{
// since we are displaying the image as a normalized grey scale image, we need to convert from
// the ushort data (as provided by the InfraredFrame) to a value from [InfraredOutputValueMinimum, InfraredOutputValueMaximum]
//backBuffer[i] = Math.Min(InfraredOutputValueMaximum, (((float)frameData[i] / InfraredSourceValueMaximum * InfraredSourceScale) * (1.0f - InfraredOutputValueMinimum)) + InfraredOutputValueMinimum);
ushort ir = (ushort)((ushort)infraredPreviewPixels[pixelIndex] + (ushort)(infraredPreviewPixels[pixelIndex + 1] << 8));
float intensityRatio = (float)ir / InfraredSourceValueMaximum;
intensityRatio /= InfraredSceneValueAverage * InfraredSceneSD;
intensityRatio = Math.Min(InfraredOutputValueMaximum, intensityRatio);
intensityRatio = Math.Max(InfraredOutputValueMinimum, intensityRatio);

ushort intensity = (ushort)(intensityRatio * ushort.MaxValue);
this.infraredPreviewPixels[pixelIndex++] = (byte)(intensity); // Red
this.infraredPreviewPixels[pixelIndex++] = (byte)(intensity >> 8); // Green
}


infraredPreview.WritePixels(
new Int32Rect(0, 0, img.Width, img.Height),
new Int32Rect(0, 0, (int)(infraredPreview.Width), (int)(infraredPreview.Height)),
this.infraredPreviewPixels,
infraredPreview.PixelWidth * 2,
0);

readBinaryCount++;
closeReader();
}

public void Write()
{
while (true)
Expand Down Expand Up @@ -202,12 +232,12 @@ public void InfraredFrameArrival(InfraredFrame df, double fps, ref bool processe
processed = true;
if (infraredRecording)
{
this.infraredBinaryBuffer.Enqueue((byte[])(infraredPixels.Clone()));
this.infraredBinaryBuffer.Enqueue((byte[])(infraredPixelBuffer.Clone()));
this.frameCount++;
if (fps < 16.0)
{
Console.WriteLine("fps drop yaşandı");
this.infraredBinaryBuffer.Enqueue((byte[])(infraredPixels.Clone()));
this.infraredBinaryBuffer.Enqueue((byte[])(infraredPixelBuffer.Clone()));
this.frameCount++;
}

Expand Down Expand Up @@ -255,7 +285,10 @@ private unsafe void ProcessInfraredFrameData(IntPtr infraredFrameData, uint infr

ushort intensity = (ushort)(intensityRatio * ushort.MaxValue);
infraredPixels[pixelIndex++] = (byte) (intensity); // Red
infraredPixels[pixelIndex++] = (byte) (intensity >> 8); // Green
infraredPixels[pixelIndex++] = (byte) (intensity >> 8); // Green

infraredPixelBuffer[pixelIndex - 2] = (byte) (ir);
infraredPixelBuffer[pixelIndex - 1] = (byte) (ir >> 8);
}


Expand Down
4 changes: 2 additions & 2 deletions KinectRecorder/PreviewWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
<Image x:Name="bodyIndex_preview" Stretch="UniformToFill" />
</Viewbox>
<Viewbox RenderTransformOrigin="0.5,0.5" Margin="10" Grid.Row="3" Grid.Column="1" >
<Image x:Name="skeletal_preview" Source="{Binding ImageSourceSkeletal}" Stretch="UniformToFill" />
<Image x:Name="skeletal_preview" Stretch="UniformToFill" />
</Viewbox>
<TextBlock Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontFamily="Segoe UI" FontSize="18" Height="24" Width="95" Margin="152.2,0,164.8,309.4" TextAlignment="Center" Grid.Column="1" Grid.RowSpan="2"><Run Text="Depth"/></TextBlock>
<TextBlock Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Bottom" FontFamily="Segoe UI" FontSize="18" Height="24" Width="95" Margin="140,0,153,0.8" TextAlignment="Center" Grid.Column="1"><Run Text="Depth"/></TextBlock>
<Viewbox RenderTransformOrigin="0.5,0.5" Margin="10,10.2,9.2,10.4" Grid.Row="1" Grid.Column="2" >
<Image x:Name="infrared_preview" Stretch="UniformToFill" />
</Viewbox>
Expand Down
39 changes: 15 additions & 24 deletions KinectRecorder/PreviewWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public partial class PreviewWindow : Window
private WriteableBitmap infraredPreviewBitmap;
private ImageSource bodyIndexPreviewBitmap;
private DrawingImage skeletalImage = null;

private ColorHandler ch;
private DepthHandler dh;
private InfraredHandler ih;
Expand All @@ -38,7 +38,7 @@ public partial class PreviewWindow : Window
public PreviewWindow()
{
InitializeComponent();

ch = ColorHandler.Instance;
ch.openReader();

Expand All @@ -47,15 +47,15 @@ public PreviewWindow()

dh = DepthHandler.Instance;
dh.startReading();

// ih = InfraredHandler.Instance;
// ih.openReader();

//sh = SkeletonHandler.Instance;
//sh.openReader();
ih = InfraredHandler.Instance;
ih.startReading();

sh = SkeletonHandler.Instance;
sh.openReader();

depthPreviewBitmap = new WriteableBitmap(dh.Width, dh.Height, 96.0, 96.0, PixelFormats.Gray16, null);
//infraredPreviewBitmap = new WriteableBitmap(ih.Width, ih.Height, 96.0, 96.0, PixelFormats.Gray16, null);
infraredPreviewBitmap = new WriteableBitmap(ih.Width, ih.Height, 96.0, 96.0, PixelFormats.Gray16, null);

ComponentDispatcher.ThreadIdle += new System.EventHandler(ComponentDispatcher_ThreadIdle);
}
Expand All @@ -65,39 +65,30 @@ private void OK_Click(object sender, RoutedEventArgs e)
this.DialogResult = true;
ch.closeReader();
bh.closeReader();
// dh.closeReader();
// ih.closeReader();
sh.closeReader();
}


void ComponentDispatcher_ThreadIdle(object sender, EventArgs e)
{
if (count < ch.readerFrameCount)
{
if (count < ch.readerFrameCount)
{
color_preview.Source = ch.Read();

bodyIndex_preview.Source = bh.Read();

dh.Read(ref depthPreviewBitmap);
depth_preview.Source = depthPreviewBitmap;

// ih.Read(ref infraredPreviewBitmap);
// infrared_preview.Source = infraredPreviewBitmap;
ih.Read(ref infraredPreviewBitmap);
infrared_preview.Source = infraredPreviewBitmap;

sh.Read();
skeletal_preview.Source = sh.getPreviewImageSource();


count++;
}
}

public ImageSource ImageSourceSkeletal
{
get
{
return this.skeletalImage;
//return null;
}
}

}
}
61 changes: 61 additions & 0 deletions KinectRecorder/SkeletonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,5 +493,66 @@ public SkeletonData( int id, double[] a)
ThumbRight_color_X = a[i++];
ThumbRight_color_Y = a[i++];
}

public double[] getDepthValues()
{
double[] depthValues = new double[50];

depthValues[0] = SpineBase_depth_X;
depthValues[1] = SpineBase_depth_Y;
depthValues[2] = SpineMid_depth_X;
depthValues[3] = SpineMid_depth_Y;
depthValues[4] = Neck_depth_X;
depthValues[5] = Neck_depth_Y;
depthValues[6] = Head_depth_X;
depthValues[7] = Head_depth_Y;
depthValues[8] = ShoulderLeft_depth_X;
depthValues[9] = ShoulderLeft_depth_Y;
depthValues[10] = ElbowLeft_depth_X;
depthValues[11] = ElbowLeft_depth_Y;
depthValues[12] = WristLeft_depth_X;
depthValues[13] = WristLeft_depth_Y;
depthValues[14] = HandLeft_depth_X;
depthValues[15] = HandLeft_depth_Y;
depthValues[16] = ShoulderRight_depth_X;
depthValues[17] = ShoulderRight_depth_Y;
depthValues[18] = ElbowRight_depth_X;
depthValues[19] = ElbowRight_depth_Y;
depthValues[20] = WristRight_depth_X;
depthValues[21] = WristRight_depth_Y;
depthValues[22] = HandRight_depth_X;
depthValues[23] = HandRight_depth_Y;
depthValues[24] = HipLeft_depth_X;
depthValues[25] = HipLeft_depth_Y;
depthValues[26] = KneeLeft_depth_X;
depthValues[27] = KneeLeft_depth_Y;
depthValues[28] = AnkleLeft_depth_X;
depthValues[29] = AnkleLeft_depth_Y;
depthValues[30] = FootLeft_depth_X;
depthValues[31] = FootLeft_depth_Y;
depthValues[32] = HipRight_depth_X;
depthValues[33] = HipRight_depth_Y;
depthValues[34] = KneeRight_depth_X;
depthValues[35] = KneeRight_depth_Y;
depthValues[36] = AnkleRight_depth_X;
depthValues[37] = AnkleRight_depth_Y;
depthValues[38] = FootRight_depth_X;
depthValues[39] = FootRight_depth_Y;
depthValues[40] = SpineShoulder_depth_X;
depthValues[41] = SpineShoulder_depth_Y;
depthValues[42] = HandTipLeft_depth_X;
depthValues[43] = HandTipLeft_depth_Y;
depthValues[44] = ThumbLeft_depth_X;
depthValues[45] = ThumbLeft_depth_Y;
depthValues[46] = HandTipRight_depth_X;
depthValues[47] = HandTipRight_depth_Y;
depthValues[48] = ThumbRight_depth_X;
depthValues[49] = ThumbRight_depth_Y;

return depthValues;

}

}

}
Loading

0 comments on commit ba4fd57

Please sign in to comment.