void UpdateUserHistogramImage()
{
int numOfPoints = 0;
Array.Clear(usersHistogramMap, 0, usersHistogramMap.Length);
// Calculate cumulative histogram for depth
for (int i = 0; i < usersMapSize; i++)
{
// Only calculate for depth that contains users
if (sensorData.bodyIndexImage[i] != 255)
{
ushort depth = sensorData.depthImage[i];
if (depth > 5000)
depth = 5000;
usersHistogramMap[depth]++;
numOfPoints++;
}
}
if (numOfPoints > 0)
{
for (int i = 1; i < usersHistogramMap.Length; i++)
{
usersHistogramMap[i] += usersHistogramMap[i - 1];
}
for (int i = 0; i < usersHistogramMap.Length; i++)
{
usersHistogramMap[i] = 1.0f - (usersHistogramMap[i] / numOfPoints);
}
}
List<int> alTrackedIndexes = new List<int>(dictUserIdToIndex.Values);
byte btSelBI = sensorData.selectedBodyIndex;
Color32 clrClear = Color.clear;
// Create the actual users texture based on label map and depth histogram
for (int i = 0; i < usersMapSize; i++)
{
ushort userMap = sensorData.bodyIndexImage[i];
ushort userDepth = sensorData.depthImage[i];
if (userDepth > 5000)
userDepth = 5000;
ushort nowUserPixel = userMap != 255 ? (ushort)((userMap << 13) | userDepth) : userDepth;
ushort wasUserPixel = usersPrevState[i];
// draw only the changed pixels
if (nowUserPixel != wasUserPixel)
{
usersPrevState[i] = nowUserPixel;
bool bUserTracked = btSelBI != 255 ? btSelBI == (byte)userMap :
(bLimitedUsers ? alTrackedIndexes.Contains(userMap) : userMap != 255);
if (!bUserTracked)
{
usersHistogramImage[i] = clrClear;
}
else
{
if (sensorData.colorImage != null)
{
Vector2 vColorPos = Vector2.zero;
if (sensorData.depth2ColorCoords != null)
{
vColorPos = sensorData.depth2ColorCoords[i];
}
else
{
Vector2 vDepthPos = Vector2.zero;
vDepthPos.x = i % sensorData.depthImageWidth;
vDepthPos.y = i / sensorData.depthImageWidth;
vColorPos = KinectInterop.MapDepthPointToColorCoords(sensorData, vDepthPos, userDepth);
}
if (!float.IsInfinity(vColorPos.x) && !float.IsInfinity(vColorPos.y))
{
int cx = (int)vColorPos.x;
int cy = (int)vColorPos.y;
int colorIndex = cx + cy * sensorData.colorImageWidth;
if (colorIndex >= 0 && colorIndex < usersClrSize)
{
int ci = colorIndex << 2;
Color32 colorPixel = new Color32(sensorData.colorImage[ci], sensorData.colorImage[ci + 1], sensorData.colorImage[ci + 2], 255);
usersHistogramImage[i] = colorPixel;
}
}
}
else
{
// Create a blending color based on the depth histogram
float histDepth = usersHistogramMap[userDepth];
Color c = new Color(histDepth, histDepth, histDepth, 0.9f);
switch (userMap % 4)
{
case 0:
usersHistogramImage[i] = Color.red * c;
break;
case 1:
usersHistogramImage[i] = Color.green * c;
break;
case 2:
usersHistogramImage[i] = Color.blue * c;
break;
case 3:
usersHistogramImage[i] = Color.magenta * c;
break;
}
}
}
}
}
}