5

I'm currently working on project which is related with color quantization.The algorithm implies as initial step k-means algorithm . My code untill now :

N = 10;
K=7;
I=imread('baboon.bmp');
Idouble = double(I);
mat=zeros(size(I,1)*size(I,2),size(I,3));
R=I(:,:,1);
G=I(:,:,2);
B=I(:,:,3);

mat(:,1)=R(:);
mat(:,2)=G(:);
mat(:,3)=B(:);
[IDX,CENTERS] = kmeans(mat,N);

Next step in the algorithm is finding the most popular color(the color which contains the most pixels in the image) . It seems very easy but somewhat I get stuck when I tried to extract that from IDX variable .

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 1
    The variable `Idouble` is useless. Plus you can merge the last 8 lines in one with `squeeze` and concatenation. Then, I don't understand your question: what is the desired output ? Why do you need kmeans ? Isn't `unique (...,.'rows)` enough ? – Ratbert Aug 31 '15 at 12:42
  • `IDX` should contain the index of the cluster `[1..k]` for each point in `mat`. So `sum(IDX==1)` should give you the number of points in cluster 1, right? – beaker Aug 31 '15 at 14:04
  • 1
    @beaker yes..you're right.. –  Sep 01 '15 at 10:51

1 Answers1

1

kmeans returns the centroids / representative colours that exist in your image stored in CENTERS and IDX gives you the membership of each pixel. Each row of CENTERS is a centroid and with that, IDX tells you which row from CENTERS you would need to reference that a pixel in your image got assigned to after the clustering.

If you want to find the "most popular colour", I'm assuming you mean the most frequently occurring. In that case, apply a mode operation to your cluster membership vector, then use this to index into CENTERS to get the most popular cluster:

idx_popular = mode(IDX);
popular_colour = CENTERS(idx_popular,:);

Minor Note

As kmeans is a randomized algorithm, it is highly probable that your cluster centroids will be different at each iteration - whether they're different all together or the same but in a different order is subject to the algorithm.

I would suggest you run the kmeans algorithm a few times to ensure that you do get the most popular colour with high probability. That can be done by specifying the Replicates flag when running kmeans:

num_repeats = 5; %// For example
[IDX,CENTERS] = kmeans(mat, N, 'Replicates', num_repeats);
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 2
    thank you . I also came across with other solution but this one seems more elegant and definitely faster . –  Sep 01 '15 at 08:44
  • 1
    @AlbertoCarp - No problem at all. I'm glad I interpreted the problem correctly. Good luck! – rayryeng Sep 01 '15 at 17:41