private static byte NearestColor (byte[] point, ProcessorState state)
{
// In place rewriting of the array
//if (nearest_cache.ContainsKey(point))
Tuple<byte, byte, byte> t = new Tuple<byte, byte, byte>(point[0], point[1], point[2]);
if (state.nearest_cache_.ContainsKey(t))
{
//Console.WriteLine("Actually matching.");
Array.Copy(state.label_color_[state.nearest_cache_[t]], point, 3);
return state.nearest_cache_[t]; // should return the label
}
//int minIdx = 0;
double minDistance = 1000000;
byte minColorLabel = state.kBackgroundLabel;
lock (state.centroid_colors_)
{
for (int idx = 0; idx < state.centroid_colors_.Count; idx++)
{
double distance = Util.EuclideanDistance(point, state.centroid_colors_[idx]);
if (distance < minDistance)
{
minColorLabel = state.centroid_labels_[idx];
minDistance = distance;
}
}
}
state.nearest_cache_.Add(new Tuple<byte, byte, byte>(point[0], point[1], point[2]),
minColorLabel);
//Console.WriteLine(nearest_cache.Count());
Array.Copy(state.label_color_[minColorLabel], point, 3);
return minColorLabel;
}