% Main script to cluster the given image

clear all
close all
clc
im = imread('Rock2.jpg');
%im=im(1:300,1:300,:);
figure(1);imagesc(im);title('Original Rock Photo')
im2 = reshape(im,[size(im,1)*size(im,2) 3]);



% Define cluster number
clusterNo = 3;

% Define maximum number of iterations
maxIter = 500;

% Calculate 
%This function clusters the given image into k clusters.
%  Returns the cmap containing the cluster indices of each pixel and
%  V matrix containing the k cluster centroid locations.
[cmap,V] = KMeansIm(im2, clusterNo, maxIter);
cmap2 = reshape(cmap, [size(im,1) size(im,2)]);
M = V / 255;
clusteredImage = label2rgb(cmap2,M);
% Show the segmented image
figure, imshow(clusteredImage);title('Clustered Image');colorbar
figure, imagesc(cmap2);colorbar;title('Clustered Cmap2')