## How to convert kmeans output into binary and calculate gradient colour vector?

Asked by Mohsina Zafar

### Mohsina Zafar (view profile)

on 16 Dec 2017
Latest activity Edited by Mohsina Zafar

### Mohsina Zafar (view profile)

on 16 Dec 2017
Hello,
I have classified an image using kmeans clustering. The code is attached below. Now I want to convert the kmeans output into a binary image and the display it.
Then I need to calculate gradient from a symmetrical 3 x 3 neighborhood data e.g. {xi (i = 1, ..., 9)}. Matrix D has d(xi,xj) distances. Distance of D(i) is obtained by summation of d(x(i),x(j)) where j=1,...,9
x(1) corresponds to the smallest of distances D(i) and x(9) corresponds to the largest of the distances D(i). And x(1) and x(9) for all D(i) are what I want to find.
pdist and squareform functions may be used for the gradient calculation.
if true
lab_im=rgb2lab(img); %convert RGB colour space to LAB colour space
im=double(lab_im(:,:,2:3)); %convert into double format
nrowsLAB=size(im,1); %get row size
ncolsLAB=size(im,2); %get column size
im2=reshape(im,nrowsLAB*ncolsLAB,2); %reshape image
cluster_idx2=kmeans(im2,2,'distance','cityblock');
image2=reshape(cluster_idx2,nrowsLAB,ncolsLAB); %reshape image
imagesc(image2); %display image
title('Color segmentation on LAB colour space')
end
Regards