Image color clustering and segmentation algorithm _ kmean

Source: Internet
Author: User

/*************************************** *********************************** <Br/> ** <br/> * KMEANS * <br/> ** <br/> ******************** **************************************** * ****************/</p> <p> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> # include <conio. h> <br/> # include <math. h> </p> <p> // function prototypes </p> <p> // DEFINES <br/> # define SUCCESS 1 <br/> # define F AILURE 0 <br/> # define TRUE 1 <br/> # define FALSE 0 <br/> # define MAXVECTDIM 20 <br/> # define MAXPATTERN 20 <br/> # define MAXCLUSTER 10 </p> <p> // ****** Defined structures & classes ***** <br/> struct aCluster <br/> {<br/> double Center [MAXVECTDIM]; <br/> int Member [MAXPATTERN]; // Index of Vectors belonging to this cluster <br/> int NumMembers; <br/> }; </p> <p> struct aVector {<br/> double Center [MAXVECTDIM]; <B R/> int Size; <br/>}; </p> <p> # include <map> <br/> using namespace std; </p> <p> class System <br/> {<br/> private: <br/> double Pattern [MAXPATTERN] [MAXVECTDIM + 1]; <br/> // map <TPoint, RGBQUAD> m_mapPattern; <br/> aCluster Cluster [MAXCLUSTER]; <br/> int NumPatterns; // Number of patterns <br/> int SizeVector; // Number of dimensions in vector <br/> int NumClusters; // Number of clusters <br/> void DistributeSamp Les (); // Step 2 of K-means algorithm <br/> int CalcNewClustCenters (); // Step 3 of K-means algorithm <br/> double EucNorm (int, int); // Calc Euclidean norm vector <br/> int FindClosestCluster (int ); // ret indx of clust closest to pattern <br/> // whose index is arg <br/> public: <br/> system (); <br/> int LoadPatterns (); // Get pattern data to be clustered <br/> void InitClusters (); // Step 1 of K-means algo Rithm <br/> void RunKMeans (); // Overall control K-means process <br/> void ShowClusters (); // Show results on screen <br/> void SaveClusters (char * fname); // Save results to file <br/>}; </p> <p> int System:: LoadPatterns () <br/>{< br/> int I, j; <br/> double x = 0; <br/> NumPatterns = 10; // Read # of patterns <br/> SizeVector = 1; // Read dimension of vector <br/> NumClusters = 3; // Read # of clusters K-Means <br/> for (I = 0; I <NumPatterns; I ++) <br/> {// For each vector <br/> for (j = 0; j <SizeVector; j ++) <br/> {// create a pattern <br/> Pattern [I] [j] = x; <br/> x + = I/2; <br/>}< br/> return SUCCESS; <br/>}< br/> //***************************** **************************************** * ***** <br/> // InitClusters * <br/> // Arbitrarily assign a vector to each of the K clusters * <br/> // We choose First K vectors to do this * <br/> //*************************** **************************************** * ******* <br/> void System:: InitClusters () <br/>{< br/> int I, j; <br/> for (I = 0; I <NumClusters; I ++) <br/> {<br/> Cluster [I]. member [0] = I; <br/> for (j = 0; j <SizeVector; j ++) <br/>{< br/> Cluster [I]. center [j] = Pattern [I] [j]; <br/>}</p> <p> void System :: runKMeans () <br/>{< br/> int converged; <B R/> int pass; <br/> pass = 1; <br/> converged = FALSE; <br/> while (converged = FALSE) <br/>{< br/> DistributeSamples (); <br/> converged = CalcNewClustCenters (); <br/> ShowClusters (); <br/>}</p> <p> double System: EucNorm (int p, int c) <br/> {// Calc Euclidean norm of vector difference <br/> double dist, x; // between pattern vector, p, and cluster <br/> int I; // center, c. </p> <p> dist = 0; <br/> for (I = 0; I <SizeVector; I ++) <br/>{< br/> x = (Cluster [c]. center [I]-Pattern [p] [I]) * (Cluster [c]. center [I]-Pattern [p] [I]); <br/> dist + = x; <br/>}< br/> return dist; <br/>}</p> <p> int System: FindClosestCluster (int pat) <br/>{< br/> int I, ClustID; <br/> double MinDist, d; <br/> MinDist = 9.9e + 99; <br/> ClustID =-1; <br/> for (I = 0; I <NumClusters; I ++) <br/>{< br/> d = EucNorm (pat, I ); <br/> // printf ("Distance from pattern % d Cluster % d is % f/n ", pat, I, sqrt (d); <br/> if (d <MinDist) <br/>{< br/> MinDist = d; <br/> ClustID = I; <br/>}</p> <p> if (ClustID <0) <br/>{< br/> // printf ("Aaargh"); <br/> exit (0); <br/>}< br/> return ClustID; <br/>}</p> <p> void System: DistributeSamples () <br/>{< br/> int I, pat, Clustid, MemberIndex; <br/> // Clear membership list for all current clusters <br/> for (I = 0; I <NumClusters; I ++) <br/> {<br/> Clus Ter [I]. numMembers = 0; <br/>}</p> <p> for (pat = 0; pat <NumPatterns; pat ++) <br/> {<br/> // Find cluster center to which the pattern is closest <br/> Clustid = FindClosestCluster (pat ); <br/> // printf ("patern % d assigned to cluster % d/n", pat, Clustid ); <br/> // post this pattern to the cluster <br/> MemberIndex = Cluster [Clustid]. numMembers; <br/> Cluster [Clustid]. member [MemberIndex] = pat; <br/> Cluster [Clustid]. num Members ++; <br/>}</p> <p> int System: CalcNewClustCenters () <br/>{< br/> int ConvFlag, vectID, I, j, k; <br/> double tmp [MAXVECTDIM]; </p> <p> ConvFlag = TRUE; </p> <p> for (I = 0; I <NumClusters; I ++) <br/> {// for each cluster <br/> for (j = 0; j <SizeVector; j ++) <br/> {// clear workspace <br/> tmp [j] = 0.0; <br/>}</p> <p> for (j = 0; j <Cluster [I]. numMembers; j ++) <br/> {// traverse member vectors <br/> VectID = Cluster [I]. member [j]; <br/> for (k = 0; k <SizeVector; k ++) <br/> {// traverse elements of vector <br/> tmp [k] + = Pattern [VectID] [k]; // add (member) pattern elmnt into temp <br/>}</p> <p> for (k = 0; k <SizeVector; k ++) <br/> {// traverse elements of vector <br/> tmp [k] = tmp [k]/Cluster [I]. numMembers; <br/> if (tmp [k]! = Cluster [I]. center [k]) <br/> ConvFlag = FALSE; <br/> Cluster [I]. center [k] = tmp [k]; <br/>}</p> <p >}< br/> return ConvFlag; <br/>}</p> <p> void System: ShowClusters () <br/>{< br/> int cl, I, pi; <br/> printf ("/n"); <br/> for (cl = 0; cl <NumClusters; cl ++) <br/>{< br/> printf ("/n-CLUSTER-% 2d -- Center:", cl); </p> <p> for (pi = 0; pi <SizeVector; pi ++) <br/>{< br/> printf ("% 2f", Cluster [cl]. center [pi]); <br/>}</p> <p> printf ("----/n"); </p> <p> for (I = 0; I <Cluster [cl]. numMembers; I ++) <br/>{< br/> for (pi = 0; pi <SizeVector; pi ++) <br/>{< br/> printf ("% 2f", Pattern [Cluster [cl]. member [I] [pi]); <br/>}</p> <p >}< br/>}</p> <p> void System: SaveClusters (char * fname) <br/>{< br/>}</p> <p> void main (int argc, char * argv []) {<br/> System kmeans; <br/> if (argc <2) {<br/> printf ("USAGE: KMEANS PATTERN_FILE/n"); <br/> exit (0 ); <br/>}</p> <p> if (kmeans. loadPatterns () = FAILURE) <br/>{< br/> exit (0); <br/>}< br/> kmeans. initClusters (); <br/> kmeans. runKMeans (); <br/> kmeans. showClusters (); <br/>}< br/>

 

 

Kmean is a clustering algorithm that classifies image colors. Similar colors are used as a class.

 

 

 

K-MEANS algorithm K-MEANS Algorithm:
The K-means algorithm accepts K input, and then divides n data objects into k clusters to meet the cluster requirements: the object similarity in the same cluster is high; the similarity between objects in different clusters is small. Clustering similarity is calculated by using the mean value of objects in each cluster to obtain a "central object" (gravity center.
The K-means algorithm is described as follows: First, K objects are randomly selected as the initial cluster center from n data objects, then, based on their similarity (distance) with these Clustering Centers, they are allocated to the most similar (represented by the clustering Center) Clustering respectively; then calculate the clustering center of each new cluster (the mean of all objects in the cluster). repeat this process until the standard measure function starts to converge. Generally, the mean variance is used as the standard measure function. k clusters have the following characteristics: each cluster itself is as compact as possible, and each cluster is separated as much as possible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.