MATLAB: K-means Clustering

image processingk means clustering

I am doing color based segmentation using kmeans clustering.I am using inbuilt function of matlab(kmeans).My input image has object and background where I need to segment the object. so I am using cluster value as 2.But while doing this, for few images, the background has a cluster index of 2 and object as 1 while in some other images, background has cluster index 1 while object 2. How does K-means clustering decide the index for clusters? If there are to be two clusters, will the background be labeled with number 2 and object with 1 or vice-versa?

Best Answer

kmeans chooses seeds by a random process if you do not specify initial locations using the 'start' option. Information on how the algorithm works is in the 'More about' section of the documentation
doc kmeans
There is no way for the algorithm to know what you call 'object' and what you call 'background'. If you give it two start seeds, one in the object and one in the background then these will always remain in this order, but otherwise they can change around with each run of the algorithm unless you fix the random seed, but that would only keep the class centres the same for the same data, not across different data sets obviously.
Related Question