You are getting closer to the solution. Anyway, two things to improve:
- use two distinct images: one with the original data, let it be "image", one for the classification only, i.e. the result of K-means clustering, let it be "cluster"; this image will be grayscale;
- the way to detect border pixels is not exactly as you wrote.
The complete procedure is as follows:
- perform K-means clustering
- in "cluster", assign every pixel a value 1, 2 or 3, depending on the nearest class center for the corresponding pixel in "image"
- then scan "cluster" and detect the border pixels as follows:
for (int x = 1; x < image.Width-1; x++)
{
for (int y = 1; y < image.Height-1; y++)
{
if (cluster.GetPixel(X, Y) != cluster.GetPixel(X - 1, Y) ||
cluster.GetPixel(X, Y) != cluster.GetPixel(X + 1, Y) ||
cluster.GetPixel(X, Y) != cluster.GetPixel(X, Y - 1) ||
cluster.GetPixel(X, Y) != cluster.GetPixel(X, Y + 1))
{
switch (cluster.GetPixel(X, Y))
{
case 1: image.SetPixel(X, Y, Color.Red); break;
case 2: image.SetPixel(X, Y, Color.Green); break;
case 3: image.SetPixel(X, Y, Color.Blue); break;
}
}
}
}
)