function [ out ] = k_mean_clustering( im , k)
% > input parameter: original image, number of clusters; output parameter: segmented image 
% > converts the image to the necessary data-format 
% > calls vl_kmeans 
% > converts back the result to a normal image

im = double(im);

[width, height, channels] = size(im);

[X, Y] = meshgrid(1:height,1:width);
im(:,:,4) = X;
im(:,:,5) = Y;

im_vector = reshape(im,width*height, 5);

[C, A] = vl_kmeans(im_vector', k);

im_vector(:, 1:3) = C(1:3, A)';

out = uint8(reshape(im_vector(:,1:3),width,height, 3));

end

