clear all
tic
I=imread('C:\Users\exam\Desktop\dst\1.jpeg');
I=rgb2gray(I);
figure,imshow(I,[]);

   copy=double(I);
  I=copy;
I=preprocess(I);
figure,imshow(I,[]);
np=20;
k=15;
[h,ind,population,m]=initiate_de(I,k,np);
firstpopulation=population;
%population=step2(population,np,k);
tmax=5;
cc=0;
n=1;
for iter=1:tmax
    %population=step2(population,np,k)
    %population=step3abc(Data,population,k,np)
    [population]=step3abc_mod_de(h,ind,m,population,k,np);
   
    %print_image(copy,im,I,population,k,np);
    TV=step3d_de(population,k,n,iter,tmax);
    %TV=step2(TV,np,k);
    %print_image(copy,im,I,TV,k,np);
    csv=zeros(1,np);
    flag=0;
    cc=cc+1;
    minimum=inf;
    for j=1:np
        [hcp,cid]=histc_de(ind,population(j,:),m,h,k);
        cspop=valid_cs_de(ind,population,hcp,h,m,1,k,j);
        [hcTV,cid]=histc_de(ind,TV(j,:),m,h,k);
        csTV=valid_cs_de(ind,TV,hcTV,h,m,1,k,j);
        if csTV<cspop
            flag=1;
            population(j,:)=TV(j,:);
            if minimum>csTV
            minimum=csTV;
            bestchr=j;
            end
        else
            if minimum>cspop
            minimum=cspop;
            bestchr=j;
        end
        end
    end
    if flag==0
        break
    end
end
  l=bestchr;
  [hcp,cid]=histc_de(ind,population(l,:),m,h,k);
  s=size(copy);
mask=zeros(s);
cid=0;
for i=1:k
    %if population(l,i)>0.5
        cid=cid+1;
        mu(cid)=population(l,i);
    %end
end
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
figure,imshow(mask,[]);

% [m n]=size(I);
%  C=max(max(mask));
% for ii=1:C
%          clust6=zeros(m,n);
%  vv=find(mask==ii);
%  clust6(vv)=1;
%  
%  figure,imshow(clust6);
% end
% %mu=mu+mi-1;   % recover real range
% axes(handles.axes2);
% imshow(mask,[]);
% 
% E=edge(mask,'canny');
% axes(handles.axes3);
% imshow(E);
% % figure;
% % imshow(I);
%   imshow(FI);
  

