Click here to Skip to main content
14,969,930 members
Articles / Multimedia / GDI+
Posted 5 Jul 2010


77 bookmarked

Computer Vision Applications with C# - Fuzzy C-means Clustering

Rate me:
Please Sign up or sign in to vote.
4.88/5 (24 votes)
9 Mar 2011GPL34 min read
This code performs a fuzzy C-means clustering and segmentation of color images, and can be used for feature extraction.



Image segmentation, the partitioning of an image into homogeneous regions based on a set of characteristics, is a key element in image analysis and computer vision. Clustering is one of the methods available for this purpose. Clustering is a process which can be used for classifying pixels based on similarity according to the pixel's color or gray-level intensity.

The K-means algorithm has been used for a fast and crisp "hard" segmentation. The Fuzzy Set theory has improved this process by allowing the concept of partial membership, in which an image pixel can belong to multiple clusters. This "soft" clustering allows for a more precise computation of the cluster membership, and has been used successfully for image clustering and for the unsupervised segmentation of medical, geological, and satellite images.


The fuzzy C-means (FCM) algorithm follows the same principles as the K-means algorithm in that it compares the RGB value of every pixel with the value of the cluster center. The main difference is that instead of making a hard decision about which cluster the pixel should belong to, it assigns a value between 0 and 1 describing "how much this pixel belongs to that cluster" for each cluster. Fuzzy rule states that the sum of the membership value of a pixel to all clusters must be 1. The higher the membership value, the more likely that pixel is to belong to that cluster. The FCM clustering is obtained by minimizing an objective function shown in equation (1):

1.png (1)


  • J is the objective function
  • n is the number of pixels in the image E
  • c is the number of clusters
  • µ is the fuzzy membership value from table
  • m is a fuzziness factor (a value > 1)
  • pi is the ith pixel in E
  • vk is the centroid of the kth cluster
  • |pi – vk| is the Euclidean distance between pi and vk defined by equation (2):

2.png (2)

The calculation of the centroid of the kth cluster is achieved using equation (3):

3.png (3)

The fuzzy membership table is calculated using the original equation (4):

4.png (4)

This algorithm has been extended for clustering of color images in the RGB color space. Hence, the computation given in equation (2) to compute the Euclidean distance between the values pi and vk is modified to incorporate RGB colors, and is shown in equation (5):

1.png (5)



As mentioned earlier, this is an iterative process. The pseudo-code is as follows:

  • Step 1: Set the number of clusters, the fuzzy parameter (a constant > 1), and the stopping condition
  • Step 2: Initialize the fuzzy partition matrix
  • Step 3: Set the loop counter k = 0
  • Step 4: Calculate the cluster centroids, calculate the objective value J
  • Step 5: For each pixel, for each cluster, compute the membership values in the matrix
  • Step 6: If the value of J between consecutive iterations is less than the stopping condition, then stop; otherwise, set k=k+1 and go to step 4
  • Step 7: Defuzzification and segmentation

Using the Code

The GUI is pretty straightforward, but the calculations can be very intensive. For that reason, the processing is done in a worker thread, which complicates the interaction with the GUI.

First, use the "File" menu to load an image. Beware, large images will tale a long time!

The options available to be changed from the GUI are the number of clusters, the maximum number of iterations (so that the program doesn't just keep running forever), and the precision. The algorithm will stop before the maximum number of iterations if the precision is achieved.


Clicking the "Fuzzy C-means Clustering" button will start the computation. The program will start by creating a ClusterPoint object for every pixel in the image:

List<ClusterPoint> points = new List<ClusterPoint>();
for (int row = 0; row < originalImage.Width; ++row)
    for (int col = 0; col < originalImage.Height; ++col)
        Color c2 = originalImage.GetPixel(row, col);
        points.Add(new ClusterPoint(row, col, c2));

Then, create the requested number of ClusterCentroid cluster objects:

List<ClusterCentroid> centroids = new List<ClusterCentroid>();

//Create random points to use a the cluster centroids
Random random = new Random();
for (int i = 0; i < numClusters; i++)
    int randomNumber1 = random.Next(sourceImage.Width);
    int randomNumber2 = random.Next(sourceImage.Height);
    centroids.Add(new ClusterCentroid(randomNumber1, randomNumber2, 
                  filteredImage.GetPixel(randomNumber1, randomNumber2))); 

The cluster centers (or centroids) are selected randomly for the first pass, and will be adjusted by the algorithm.

Finally, create the FCM object and start the iterations:

FCM alg = new FCM(points, centroids, 2, filteredImage, (int)numericUpDown2.Value);
alg.J = alg.CalculateObjectiveFunction();
double Jnew = alg.CalculateObjectiveFunction();
Console.WriteLine("Run method i={0} accuracy = {1} delta={2}", 
                  k, alg.J, Math.Abs(alg.J - Jnew));
backgroundWorker.ReportProgress((100 * k) / maxIterations, "Iteration " + k);
if (Math.Abs(alg.J - Jnew) < accuracy) break;

Progress is displayed on the status bar:


Once the iterations are done, the algorithm will perform a defuzzification process to assign the pixel to the cluster for which it has the highest membership value. For each cluster, the program will then save a segmented image containing the pixels from the original image that are contained in that cluster.

As an example, the clustering of the following test image with 2 clusters:


will result in the following clustered image:


By using the cluster information and the pixels from the original image, the following regions can be extracted:

printscreen.png printscreen.png

Because this algorithm is very computationally intensive, it has a tendency to "lock" the GUI and not refresh the status and the working image. For that reason, I had to use a worker thread.

Points of Interest

This is my first post, and my first C# program, so I am sure that it leaves a lot of room for improvement. But overall, I hope that you will find this project fun and interesting!



This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Christophe Gauge
United States United States
Graduate student
Department of Information and Computer Sciences
University of Hawai'i at Manoa

Comments and Discussions

QuestionInstantiation of the image variable Pin
Member 1261841225-Jul-16 13:37
MemberMember 1261841225-Jul-16 13:37 
Questionhow with cluster point is image block Pin
Leo Phạm23-Jan-15 4:46
professionalLeo Phạm23-Jan-15 4:46 
QuestionEps? Pin
imparente24-Apr-13 1:09
Memberimparente24-Apr-13 1:09 
Questionfuzzy c means Pin
arc_15720-Mar-13 19:28
Memberarc_15720-Mar-13 19:28 
Question5 all the way Pin
khairul78612-Jan-12 4:40
Memberkhairul78612-Jan-12 4:40 
GeneralMy vote of 5 Pin
khairul78612-Jan-12 4:39
Memberkhairul78612-Jan-12 4:39 
GeneralMy vote of 4 Pin
gorgias9915-Mar-11 0:54
Membergorgias9915-Mar-11 0:54 
QuestionCan I suggest one of my articles? Pin
Paulo Zemek9-Mar-11 8:40
mvaPaulo Zemek9-Mar-11 8:40 
AnswerRe: Can I suggest one of my articles? Pin
Member 177385512-Apr-11 12:39
MemberMember 177385512-Apr-11 12:39 
GeneralMy vote of 1 Pin
ghh12512-Dec-10 0:26
Memberghh12512-Dec-10 0:26 
GeneralRe: My vote of 1 Pin
Nicolas Dorier9-Mar-11 21:51
professionalNicolas Dorier9-Mar-11 21:51 
GeneralRe: My vote of 1 Pin
sanosay4-Apr-12 12:43
Membersanosay4-Apr-12 12:43 
AnswerRe: My vote of 1 Pin
Claude He1-Oct-14 5:39
MemberClaude He1-Oct-14 5:39 
GeneralRe: My vote of 1 Pin
Member 80589164-Mar-17 19:09
MemberMember 80589164-Mar-17 19:09 
GeneralMaybe a small problem Pin
likuan26-Oct-10 21:51
Memberlikuan26-Oct-10 21:51 
AnswerRe: Maybe a small problem Pin
Christophe Gauge2-Feb-11 8:40
MemberChristophe Gauge2-Feb-11 8:40 
GeneralMy vote of 5 Pin
Joel Ivory Johnson19-Jul-10 4:38
mvaJoel Ivory Johnson19-Jul-10 4:38 
GeneralSo cool! Pin
hackcat19-Jul-10 3:23
Memberhackcat19-Jul-10 3:23 
GeneralI like this a lot Pin
Yves12-Jul-10 13:57
MemberYves12-Jul-10 13:57 
GeneralGreat start Pin
Alphons van der Heijden10-Jul-10 12:54
professionalAlphons van der Heijden10-Jul-10 12:54 
GeneralFew tips for you..... Pin
Md. Marufuzzaman4-Jul-10 19:14
professionalMd. Marufuzzaman4-Jul-10 19:14 
GeneralRe: Few tips for you..... Pin
Christophe Gauge5-Jul-10 5:58
MemberChristophe Gauge5-Jul-10 5:58 
GeneralRe: Few tips for you..... Pin
Md. Marufuzzaman5-Jul-10 6:31
professionalMd. Marufuzzaman5-Jul-10 6:31 
GeneralMy vote of 1 Pin
gaurav_verma_mca4-Jul-10 18:25
Membergaurav_verma_mca4-Jul-10 18:25 
GeneralRe: My vote of 1 Pin
Laserson12-Mar-11 9:45
MemberLaserson12-Mar-11 9:45 

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.