Reputation: 565
Im working with Kinect for a research project, and i was until now only messing with skeleton tracking. Now i went into the depths of the Depth Stream and i want to know how to create that RGB color scale we see in some depth streams. Mine is in gray scale. There is a part of the Depth Event that i dont understand and i feel this is very important to understand how it works, and how i could change it to color, its the definition of the intesity variable.
private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e){
using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
{
if (depthFrame != null)
{
// Copy the pixel data from the image to a temporary array
depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);
// Get the min and max reliable depth for the current frame
int minDepth = depthFrame.MinDepth;
int maxDepth = depthFrame.MaxDepth;
// Convert the depth to RGB
int colorPixelIndex = 0;
for (int i = 0; i < this.depthPixels.Length; ++i)
{
// Get the depth for this pixel
short depth = depthPixels[i].Depth;
if (depth > 2000) depth = 0; //ive put this here just to test background elimination
byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
//WHAT IS THIS LINE ABOVE DOING?
// Write out blue byte
this.colorPixels[colorPixelIndex++] = intensity;
// Write out green byte
this.colorPixels[colorPixelIndex++] = intensity;
// Write out red byte
this.colorPixels[colorPixelIndex++] = intensity;
// We're outputting BGR, the last byte in the 32 bits is unused so skip it
// If we were outputting BGRA, we would write alpha here.
++colorPixelIndex;
}
// Write the pixel data into our bitmap
this.colorBitmap.WritePixels(
new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
this.colorPixels,
this.colorBitmap.PixelWidth * sizeof(int),
0);
}
}
}
Upvotes: 1
Views: 1908
Reputation: 2418
byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
//WHAT IS THIS LINE ABOVE DOING?
The line above is using the Ternary Operator
Basically its a one line if statement and is equivalent to:
byte intensity;
if (depth >= minDepth && depth <= maxDepth)
{
intensity = (byte)depth;
}
else
{
intensity = 0;
}
The trick to colouring the depth image is to multiply the intensity by a tint colour. For example:
Color tint = Color.FromArgb(0, 255, 0) // Green
// Write out blue byte
this.colorPixels[colorPixelIndex++] = intensity * tint.B;
// Write out green byte
this.colorPixels[colorPixelIndex++] = intensity * tint.G;
// Write out red byte
this.colorPixels[colorPixelIndex++] = intensity * tint.R;
Upvotes: 1