close all
clear all

im=imread('lewisky.jpg');
[m,n,o]=size(im);
data=double(im);
datax=reshape(data,m*n,o);

%Performing K-means operation
[distance,label,tse]=kmeans1(datax,3);

me = zeros(1, 3);
x = zeros(1, 3);
% Calculating mean
for i = 1:3
    j = find(label==i); %Finding index values for each value of i
    for k = 1:size(j, 1)
        x(k,:)=datax(j(k),:);
    end
    temp(i,1)=mean(x(:,1));
    temp(i,2)=mean(x(:,2));
    temp(i,3)=mean(x(:,3)); 
    end

% Resstructuring the image
index=1;
for i=1:n    
    for j=1:m
        for k = 1:o
            result(j,i,:)=temp(label(index),:);
        end
        index=index+1;
    end
end

new=uint8(result);

figure; imshow(new);
    
