15,964,052 members
See more:
Hi all,
I set up Accord.Net Framework on Microsoft Visual Studio 2008, after that
i saw in a its samples about K-means as a image segmentation.
For example,when i choose a number of cluster is 3, the result will get 3 region with different color intensity.
Right now, I want to localize these clusters with borders.For example,i have 3 cluster.Thus, 1 cluster with red border, 1 cluster with yellow border, 1 cluster with green border.
Thank!!
Posted

## Solution 1

Dientu,

A way to achieve this is to create a separate image where you assign every pixel the cluster color (1, 2, or 3). Then scan this image and for every pixel look at the immediate neighbors (there are 8 or 4 of them if you consider corners or not). If at least one neighbor is of a different color, then the given pixel is a border pixel and you can paint it in your original image.

This will give you the internal borders of the regions. You can also do without the separate image, by recomputing the pixel indexes on the fly.

## Solution 2

Thank you Ydaoust
I'm Vietnamese and my programming skill is fairly bad because my major's electronic. I also understood your opinion, but it's difficult for me to write code like your suggestion.So,I'm glad if you can give me a sample project about this problem.
Regards,

## Solution 3

I am sorry, I don't use Accord.NET. All I could give you is pseudocode.

Are you able to perform the most basic operations such as getting or setting a pixel in the image at given X, Y coordinates ?

## Solution 4

Hi, YDaoust
C#
```for (int x = 1; x < image.Width-1; x++)
{
for (int y = 1; y < image.Height-1; y++)
{
if (image.GetPixel(x, y) != image.GetPixel(x + 1, y))
image.SetPixel(x + 1, y, Color.Red);
if (image.GetPixel(x, y) != image.GetPixel(x - 1, y))
image.SetPixel(x - 1, y, Color.Red);
if (image.GetPixel(x, y) != image.GetPixel(x, y + 1))
image.SetPixel(x, y + 1, Color.Red);
if (image.GetPixel(x, y) != image.GetPixel(x, y - 1))
image.SetPixel(x, y - 1, Color.Red);
}
}
pictureBox3.Image = image;```

When I run my program, the result didn't change
I think the color of a pixel is very small,
We can't see by eyes
Thank!!

v2

## Solution 5

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))
{
// This is a border pixel, color it after its cluster index
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;
}
}
}
}```

)

v2

## Solution 6

Thank YDaoust helped me.
I solved my problem.
Regards,