intmat=zeros(4*4,128*128); genintmat(img);

mask=(img2==22000 | (img2==12000 & xxm<60));
lset=sqrt((xxm-43).^2*1.5+(yym-62).^2/2)-8;
nb=(lset<19 & lset>-2); delt=0.5; niter=50;
if fuserf==0,lset=lset.*(1-nb)+lset1;end
evolve; lset1=newlset; nb1=nb;
c1(1:level0size,:,ii,choice)=level0; l1(ii,choice)=level0size;

mask=(img2==22000 | (img2==12000 & xxm>60));
lset=sqrt((xxm-98).^2*1.5+(yym-62).^2/2)-8;
nb=(lset<19 & lset>-2); delt=0.5; niter=50; 
if fuserf==0,lset=lset.*(1-nb)+lset2;end
evolve; lset2=newlset; nb2=nb;
c2(1:level0size,:,ii,choice)=level0; l2(ii,choice)=level0size;

mask=(img2==22000 | img2==32000);
lset=sqrt((xxm-67).^2+(yym-90).^2)-3;
nb=(lset<10 & lset>-2); delt=0.5; niter=50;
if fuserf==0,lset=lset.*(1-nb)+lset3;end
evolve; lset3=newlset; nb3=nb;
c3(1:level0size,:,ii,choice)=level0; l3(ii,choice)=level0size;

mask=(img2==22000 | img2==0);
lset=sqrt((xxm-67).^2/2+(yym-66).^2)-40;
nb=(lset<7 & lset>-7); delt=0.2; niter=40;
if fuserf==0,lset=lset.*(1-nb)+lset4;end
evolve; lset4=newlset; nb4=nb;
c4(1:level0size,:,ii,choice)=level0; l4(ii,choice)=level0size;

eee=0.1;
wt=((1-or(or(lset1<0,sqrt((xxm-43).^2*1.5+(yym-62).^2/2)-8<-2),...
  or(lset2<0,sqrt((xxm-98).^2*1.5+(yym-62).^2/2)-8<-2)))+eee)/(1+eee); wt=wt';
dmap=min(min(abs(lset1).*nb1+(1-nb1)*1000,abs(lset2).*nb2+(1-nb2)*1000), ...
	min(abs(lset3).*nb3+(1-nb3)*1000,abs(lset4).*nb4+(1-nb4)*1000));
xmap=zeros(N,N); ymap=zeros(N,N); penal=zeros(N,N,4);
xmap(2:N,:)=min(dmap(1:N-1,:),dmap(2:N,:));
ymap(:,2:N)=min(dmap(:,1:N-1),dmap(:,2:N));
penal(:,:,1)=ymap'; penal(:,:,2)=xmap';
penal(:,:,1:2)=h(penal(:,:,1:2));
if flag==1,penal(:,:,1)=penal(:,:,1).*wt; penal(:,:,2)=penal(:,:,2).*wt;end
!rm -f pen.fld
avswrite('pen.fld',penal);
