I was dealing with color segmentation with MATLAB. I used k-means clustering based on this document and come this far codewise;
global imgRGB; global imgLAB; img = imgRGB; cform = makecform('srgb2lab'); imgLAB = applycform(img, cform); ab = double(imgLAB(:,:,2:3)); rows = size(ab,1) cols = size(ab,2); ab = reshape(ab, rows*cols, 2); cluster = 5; [idx center] = kmeans(ab, cluster, 'distance', 'sqEuclidean', 'Replicates', 5); label = reshape(idx, rows, cols); figure; imshow(label, []); imgSeg = cell(5); rgb_label = repmat(pixel_labels, [1 1 3]); for k=1:cluster color = img; color(rgb_label ~= k) = 0; imgSeg{k} = color; end figure; imshow(imgSeg{1}); I take image as input that is why it is defined global.
For a colored image like the one in link, it produces the grayscale output.

I think it assigns gray tones as colors but I need to assign a color to each cluster. I mean not gray tone but a color. How can I achieve that?