Search code examples
matlabk-meansimaging

k means code in matlab


So i have this code:

    for i = 1:38
        he = cores{i,1};
        imshow(he), title('H&E image');

        cform = makecform('srgb2lab');
        lab_he = applycform(he,cform);

        ab = double(lab_he(:,:,2:3));
        nrows = size(ab,1);
        ncols = size(ab,2);
        ab = reshape(ab,nrows*ncols,2);

        nColors = 3;
        % repeat the clustering 3 times to avoid local minima
        [cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
                                              'Replicates',3);


        pixel_labels = reshape(cluster_idx,nrows,ncols);
        figure;
        imshow(pixel_labels,[]), title('image labeled by cluster index');


        segmented_images = cell(1,3);
        rgb_label = repmat(pixel_labels,[1 1 3]);

        for k = 1:nColors
            color = he;
            color(rgb_label ~= k) = 0;
            segmented_images{k} = color;
        end

        figure
        imshow(segmented_images{1}), title('objects in cluster 1');

        figure
        imshow(segmented_images{2}), title('objects in cluster 2');

        figure
        imshow(segmented_images{3}), title('objects in cluster 3');
   end

I have 38 different images, with brown, blue and green colors. I want to distinguish them in 3 different variables and that's what this code is doing.

The only problem is that i need to know which is which (for example, the first cell is gonna be always the blue, the second the brown and the last the green) but usually i get them in random order.

Any ideas?


Solution

  • A k-means algorithm needs a starting guess at where the centroids are, which is normally chosen at random. If the problem is reasonably formulated the choice of starting point does not affect the end-result, except that the labels may be in a different order, which is what you're seeing.

    You could look at defining the starting points yourself, removing the randomness that is end-labels to be shuffled each time. Matlab's k-means algorithm allows you to do this by passing the starting points to the `Start' parameter. An example of how this is done is given here.

    If you expect the centroids to correspond to brown, blue and green, I would suggest passing the rgb values for these in as starting centroids. You should be aware that you may unluckily choose starting centroids that do not converge to a solution you are happy with: if so, alter your guess somewhat!