Click here to Skip to main content
14,739,187 members
Articles » Multimedia » GDI+ » Applications
Posted 11 Aug 2009

Tagged as


82 bookmarked

Computer Vision Applications with C# - Part IV

Rate me:
Please Sign up or sign in to vote.
4.87/5 (15 votes)
11 Aug 2009CPOL
KMeans Clustering
Image 1


This article introduces Pattern Recognition in computer vision. I am going to discuss a clustering algorithm called KMeans Clustering. Just a reminder, that Pattern Recognition is largely based on statistical techniques and so one should not be surprised to find, let’s say data mining algorithms, used in computer vision. In fact, there is an implementation of KMeans in SQL Analysis Services.

So what is KMeans? It is an unsupervised learning technique. It is unsupervised because when it starts running, it decides when to stop based on some criteria. And it is a learning technique because it learns about data. The aim of KMeans is to discover clusters of data within data. So if you run KMeans on some data, then you would end with “K” clusters of data. These clusters are formed based on some common attribute of data. We have to specify “K” upfront and give the algorithm an estimate of the centroids of these “K” clusters. Just note that the ability to provide these two parameters is not always possible and this is a drawback of this algorithm – and in this case one needs to use a variation of KMeans instead of this basic version.

Image Segmentation

So what does KMeans Clustering have to do with images? Well, it can find clusters of different colours for you resulting in image segmentation. The data in image is the pixel colours. We need to provide the algorithm the number of clusters. This is relatively easy as we can guess the number of distinct colours by looking at the image. This does not have to be exact but depending on the number of clusters, we may or may not get good segmentation. In the above image, you can see that I have used 3 clusters which means the image is segmented in 3 distinct colours. Okay now based on the number of clusters, the algorithm is going to group similar colours together till it cannot find any colours to move around the clusters. So let’s have a look at the algorithm next.

KMeans Algorithm

Here is how KMeans Algorithm learns data clusters:

  1. Set number of clusters.
  2. Set or randomly set the centroids of these clusters.
  3. For all data:
    1. Find the distance between centroids of the clusters and the data.
    2. Move the data to the cluster that has the least distance to it.
  4. Calculate the new centroids of the clusters now that its data may have moved.
  5. If the new centroids are different from old centroids, then go to step 3, otherwise stop.

And this is pretty much it! So let’s understand this: we know what steps 1 & 2 mean. For step 2, I get the top X colours from the image and use its values as centroid. In other words, if I said I want 2 clusters, then if top 2 colours (colours with most pixels) from the image are RGB:100,200,130 & 40,70,140, then these values become my starting 2 centroids.

Step 3a is where we would read each pixel in the image and find its distance from the 2 centroids. Now what is that distance? I have used Euclidean Distance but you could use any. But remember that type of distance used will affect the segmentation results. This is because using this distance, we measure how similar the current pixel colour is to the cluster centroid. To emphasis this point, I have used RGB and HSV colour models. Using the same number of clusters for RGB & HSV will produce different segmentation results.

Step 3b moves pixels to closest clusters. So if for instance, pixel 110,215,110 will be closest based on Euclidean Distance to first cluster centroid and pixel 50,80,160 will be closest to the second.

Step 4 is where we find new centroids for the clusters. In the current example, the new centroids are: 100+110/2,200+215/2,130+110/2 and 40+50/2,70+80/2,140+160/2. Yes the new centroids are found using average.

Step 5 is convergence check. If the new centroids are the same as the old centroids, then that means the data within the centroids have not moved and the clusters have stabilized!

Using the Code

The code is pretty straight forward and should be easy to use:

  • Step 1: Load an image – “Load Image” button.
  • Step 2: Set number of clusters.
  • Step 3: Select a colour model.
  • Step 4: Click “Start KMeans” button to run the algorithm.

Change the number of clusters and colour models to see the effects on segmentation.

Points of Interest

The values of the final colours in segmented image come from the cluster centroids and may not necessarily reflect the colours in the original image. All you will see are “K” different colours in the segmented image.


  • 11th August, 2009: Version 1.0


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


About the Author

Australia Australia
I have been in the IT industry since April 1996. My main expertise is in Microsoft space.

Coming from engineering background, any application of programming to engineering and related fields easily excites me. I like to use OO and design patterns and find them very useful.

I have been an avid reader of CodeProject. I decided it was time to make a commitment to make my contribution to the community - so here I am.

My Website:

Comments and Discussions

Questionclustering into 2 colour, ground (terrain) and any other besides ground Pin
Member 1128785920-Jun-15 19:00
MemberMember 1128785920-Jun-15 19:00 
Questionask?? Pin
amin erfandy22-Sep-11 14:52
Memberamin erfandy22-Sep-11 14:52 
GeneralClustering techniques Pin
Trevor Misfeldt7-Oct-09 9:10
MemberTrevor Misfeldt7-Oct-09 9:10 
GeneralRe: Clustering techniques Pin
Arif_Khan7-Oct-09 14:37
MemberArif_Khan7-Oct-09 14:37 
GeneralPatent issues Pin
3Drti20-Aug-09 10:22
Member3Drti20-Aug-09 10:22 
GeneralRe: Patent issues Pin
Dmitry N. Medvedev20-Aug-09 12:17
MemberDmitry N. Medvedev20-Aug-09 12:17 
GeneralRe: Patent issues [modified] Pin
Arif_Khan20-Aug-09 14:25
MemberArif_Khan20-Aug-09 14:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.