Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
I do need your help!!!
Thank!!
Posted

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.
 
Share this answer
 
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,
 
Share this answer
 
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 ?
 
Share this answer
 
Hi, YDaoust
I did follow as your opinion, i wrote code as follows
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!!
 
Share this answer
 
v2
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;
            }
        }
    }
}


)
 
Share this answer
 
v2
Thank YDaoust helped me.
I solved my problem.
Regards,
 
Share this answer
 
Comments
YDaoust 1-Jul-11 6:07am    
You could rate me then :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900