% K-Means Clustering Program 


%Reading an Image
I= imread('C:\p5310002.jpg');
%Finding the number of rows
row = size(I,1);
%Number of Columns
colm = size(I,2);
%Dimension of the matrix
dim = size(I,3);
%Convert the image to double 
A = im2double(I);
%Reshape Image
Image_reshape= reshape(I, row*colm,dim);
[distance, label, tse] = kmeans1(Image_reshape, number);
%Intializing
for k=1:3
     Total_sum=zeros(1,3);
     Counter=zeros(1,3);
     mean_of=zeros(1,3);
 % k represents the rbg colors 
   for i=1:size(label)
      for j=1:number
         if (label(i)== j)
             Total_sum(j)= Total_sum(j)+Image_reshape[i,k];
             counter(j)=counter(j)+1; %increase the j counter by 1
         end 
     end
 end
 
           
 for j=1:number
     mean(j)=sum(j)/count(j);
     fprintf(1,'%d ',counter(j));
 end
 for i=1:size(label)
     for j=1:number
         if(label(i)==j)
             Image_reshape[i,k]=mean_of[j];
         end
     end
 end
 
end
Image_reshape = uint8(Image_reshape);
final_image=reshape(Image_reshape,row,colm,3);
figure;
imshow(Image_reshape)




    