MATLAB: Kmeans clustering on an image in hsv color space

color classificationcolor segmentationcolor spaceImage Processing Toolbox

we have a code for lab color space….we want similar codes that can work on other colorspaces like hsv.. here is the lab code
he = imread('image2.jpg');
imshow(he), title('H&E image');
text(size(he,2),size(he,1)+15,...
'Image courtesy of Alan Partin, Johns Hopkins University', ...
'FontSize',7,'HorizontalAlignment','right');
cform = makecform('rgb2hsv');
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);
imshow(pixel_labels,[]), title('image labeled by cluster index')

Best Answer

My understanding is that there are two parts to this: (1) use a different transform name on the makecform; and (2) use different weightings on the planes, as the sensitivities show up in different ratios in the different color spaces.
Related Question