clear all
  I=imread('C:\Users\exam2.RVRJCCE.000\Desktop\Brain\orig3.jpg');
%ima1=rgb2gray(Im);
 v=size(I);
    if length(v)==3
        I=rgb2gray(I);
    end
figure;imshow(I);
%I=preprocess(I);
%figure;imshow(I);
 copy=double(I);
np=20;
k=10;
[h,ind,population,m,mi]=initiate_tlbo(I,k,np);
firstpopulation=population;
%population=step2(population,np,k);
tmax=5;
cc=0;
n=1; 
oldmin=0;
for iter=1:tmax
    [population]=step3abc_mod_tlbo(h,ind,m,population,k,np);
    csv=zeros(1,np);
    for j=1:np
        [hcp,cid]=histc_tlbo(ind,population(j,:),m,h,k);
        cspop(j)=valid_cs_tlbo(ind,population,hcp,h,m,1,k,j);
    end
    [best ind1]=min(cspop);
    [TV,cspop]=newoffspring_TLBO1(population,np,ind1,cspop,k,m,mi,h);
    for j=1:np
        [hcTV,cid]=histc_tlbo(ind,TV(j,:),m,h,k);
         csTV(j)=valid_cs_tlbo(ind,TV,hcTV,h,m,1,k,j);
        if csTV(j)<cspop(j)
            population(j,:)=TV(j,:);
            cspop(j)=csTV(j);
        end
    end
    [minimum ind1]=min(cspop);
    if (abs(oldmin-minimum)<0.000001)%flag==0
        break
    else
        oldmin=minimum;
    end
end
   l=ind1;
  [hcp,cid]=histc_tlbo(ind,population(l,:),m,h,k);
  s=size(copy);
mask=zeros(s);
cid=0;
for i=1:k
    
        cid=cid+1;
        mu(cid)=population(l,i);
    
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,[]);
%mu=mu+mi-1;   % recover real range
% FI=imshow(mask,[]);
% figure;
% mask=abs(mask-255);
% imshow(mask,[]);
% figure;
% E=edge(mask,'canny');
% imshow(E);
%imshow(I)
% imshow(im)
  cid