computer vision applications with c# - fuzzy c-means clustering - codeproject
TRANSCRIPT
9,832,570 members (24,218 online) 552 Sign out
1 hiepltsdsgthh
home quick answers discussions features community help Search for articles, questions, tips
Articles » Multimedia » GDI+ » Applications
Article
Browse Code
Stats
Revisions (6)
Alternatives
Comments &Discussions (22)
Add your ownalternative version
About Article
This code performs a fuzzyC-means clustering andsegmentation of colorimages, and can be used forfeature extraction.
Type Article
Licence GPL3
First Posted 5 Jul 2010
Views 39,419
Downloads 3,868
Bookmarked 58 times
C# .NET GDI+ Dev
Advanced Application , +
Next
Computer Vision Applications with C# - Fuzzy C-means
ClusteringBy Christophe Gauge, 9 Mar 2011
Is your email address OK? You are signed up for our newsletters but your email address is either
unconfirmed, or has not been reconfirmed in a long time. Please click here to have a confirmation email
sent so we can confirm your email address and start sending you newsletters again. Alternatively, you
can update your subscriptions.
Download source code - 56.6 KB
Introduction
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.
Algorithm
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)
Where:
J is the objective function
4.71 (12 votes)
articles
Top News
10 Pieces of Really Bad
Advice (for Computer
Scientists)
Get the Insider News free eachmorning.
Related Videos
Welcome toCodeProject.TV
Working with Forms
Related Articles
Matrix Multiplication in C#
Creating animations withDundas Chart for ASP.NET
Smarter Data Labels withDundas Chart SmartLabels
Understanding Chart Areas withDundas Chart for .NET
Add "Select All" to parameterlists in SQL Reporting
Using screensavers inside theWindows Media Player
Making Sense of GeographicData with Dundas Map andAJAX
Handling connectionnotification between a desktopmachine and Windows CEbased devices
SmartLink
Create data-driven applicationswith the Hera ApplicationFramework
Towards the self-documentingdatabase: extended properties
Accessibility audit vs.accessibility testing
Digital Signatures and PDFDocuments
Color Scale Filter
WMP Power Hour APP
Merge Landscape and PortraitPDFs using ASP.NET
How to conduct an SMS survey
using a cell phone connectedSMS gateway and MS Access
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)
The calculation of the centroid of the kth cluster is achieved using equation (3):
(3)
The fuzzy membership table is calculated using the original equation (4):
(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):
(5)
Pseudo-Code
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.
Using Barcodes in Documents –Best Practices
How to Retrieve EMC CenteraCluster/Pool Capabilities
"Hey! Is That My Car? How toSharpen a QuickBird SatelliteImage Using DotImage"
Integrate your SharePointenvironment into the openstandards-based WebSpherePortal platform using the VisualStudio IDE
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:
Collapse | Copy Code
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:
Collapse | Copy Code
List<ClusterCentroid> centroids = new List<ClusterCentroid>();
//Create random points to use a the cluster centroidsRandom 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:
Collapse | Copy Code
FCM alg = new FCM(points, centroids, 2, filteredImage, (int)numericUpDown2.Value);k++;alg.J = alg.CalculateObjectiveFunction();alg.CalculateClusterCentroids();alg.Step();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:
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!
References
Java Image Processing Cookbook, the Fuzzy C-means algorithm by Rafael Santos.
Modified Fuzzy C-means Clustering Algorithm with Spatial Distance to Cluster Center of Gravity, 2010 IEEE
International Symposium on Multimedia, Taichung, Taiwan, December 13-December 15 2010.
License
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
Member
Graduate student
Department of Computer and Information Science
Gannon University, Erie, PA
Article Top
0 TweetTweet 3 Rate this: Poor Excellent Vote
Add a Comment or Question Search this forum Go
Comments and Discussions
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
Profile popups Spacing Relaxed Noise Very Low Layout Normal Per page 25 Update
First Prev Next
imparente 24 Apr '13 - 6:09
arc_157 21 Mar '13 - 0:28
khairul786 12 Jan '12 - 9:40
khairul786 12 Jan '12 - 9:39
gorgias99 15 Mar '11 - 5:54
Paulo Zemek 9 Mar '11 - 13:40
Member 1773855 12 Apr '11 - 17:39
ghh125 12 Dec '10 - 5:26
Nicolas Dorier 10 Mar '11 - 2:51
sanosay 4 Apr '12 - 17:43
likuan 27 Oct '10 - 2:51
Christophe Gauge 2 Feb '11 - 13:40
Joel Ivory Johnson 19 Jul '10 - 9:38
hackcat 19 Jul '10 - 8:23
Yves 12 Jul '10 - 18:57
Alphons van der Heijden 10 Jul '10 - 17:54
Md. Marufuzzaman 5 Jul '10 - 0:14
gaugec 5 Jul '10 - 10:58
Md. Marufuzzaman 5 Jul '10 - 11:31
gaurav_verma_mca 4 Jul '10 - 23:25
Laserson 12 Mar '11 - 14:45
Sandeep Mewara 4 Jul '10 - 23:18
Last Visit: 31 Dec '99 - 23:00 Last Update: 27 Apr '13 - 18:36 Refresh 1
Like 10
Eps?
fuzzy c means
5 all the way
My vote of 5
My vote of 4
Can I suggest one of my articles?
Re: Can I suggest one of my articles?
My vote of 1
Re: My vote of 1
Re: My vote of 1
Maybe a small problem
Re: Maybe a small problem
My vote of 5
So cool!
I like this a lot
Great start
Few tips for you.....
Re: Few tips for you.....
Re: Few tips for you.....
My vote of 1
Re: My vote of 1
Needs improvement...
Permalink | Advertise | Privacy | Mobile Web03 | 2.6.130426.1 | Last Updated 9 Mar 2011
Article Copyright 2010 by Christophe GaugeEverything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid
Last Visit: 31 Dec '99 - 23:00 Last Update: 27 Apr '13 - 18:36 Refresh 1
General News Suggestion Question Bug Answer Joke Rant Admin