% clear all
% Im=imread('E:\image\images_seg\robo.jpg');
% ima1=rgb2gray(Im);
%[m1,n1]=size(ima)
ima=double(handles.img);
copy=ima;         % make a copy
ima=ima(:);       % vectorize ima
mi=min(ima);      % deal with negative 
ima=ima-mi+1;     % and zero values
% k=input('enter no of clusters');
k=4;
s=length(ima);

% create image histogram

m=max(ima)+1;
h=zeros(1,m);
hc=zeros(1,m);

for i=1:s
  if(ima(i)>0) h(ima(i))=h(ima(i))+1;end;
end
ind=find(h);
hl=length(ind);

% initiate centroids

mu=(1:k)*m/(k+1);

% start process
obj_fcn1=0;
[x,y]=size(mu);
while(true)
    dist=zeros(k,m);
   
  
  oldmu=mu;
  % current classification  
 
  for i=1:hl
      c=abs(ind(i)-mu);
      cc=find(c==min(c));
      hc(ind(i))=cc(1);
  end
  
  %recalculation of means  
  
  for i=1:k, 
      a=find(hc==i);
      mu(i)=sum(a.*h(a))/sum(h(a));
  end
  
  if(mu==oldmu) break;end;
  
end
ll=0;
for i=1:k
    kk(i)=ll;
    ll=ll+100+155/(k-1);
end

% calculate mask
s=size(copy);
mask=zeros(s);
for i=1:s(1),
for j=1:s(2),
  c=abs(copy(i,j)-mu);
  a=find(c==min(c));
  mask(i,j)=a(1);
  end
end

mu=mu+mi-1;
% recover real range
axes(handles.axes2);
imshow(mask,[]);
E=edge(mask,'canny');
axes(handles.axes3);
imshow(E);
% figure;
% imshow(ima1);

