MATLAB: How to detect number of colors in an image

Image Processing Toolboximage segmentationStatistics and Machine Learning Toolbox

I want to detect the count of colors present in the image and i have to use that value for clusters in color based segmentation using k means

Best Answer

You count the colors by computing a histogram, not by doing kmeans. kmeans() requires that you know how many clusters there are in advance.
I'm attaching code to compute the number of unique colors.
I'm also attaching a kmeans demo to do color segmentation on an image (so you can see how bad it is).
Related Question