% DIGIFILTER_patch

% === SET VARIABLES ===
hold on
b=1; j=0; X=[];

% === MESSAGE ===
hat=text(0,-20,'Press <ESC> to finish series.','Fontsize',14,'Color','b');

% === DIGITIZE ===
while b<27   
   [x,y,b]=ginput(1);
    if b==3, 
      plot([X(j-1:j,1)],[X(j-1:j,2)],'k');
      plot([X(j,1)],[X(j,2)],'.k');
      j=j-1;
    end
    if b==1,
      j=j+1;
      X(j,:)=[x y]; 
      plot([x],[y],'.m');
      if j>1,   plot([X(j-1,1) x],[X(j-1,2) y],'b');  end
    end
    if (b==99)|(b==67),
       j=j+1;
       X(j,:)=X(1,:);
        plot([X(j,1)],[X(j,2)],'.m');
        if j>1,   plot([X(j-1:j,1)],[X(j-1:j,2)],'m');  end
    end
     
 end
plot([X(:,1); X(1,1)],[X(:,2); X(1,2)],'b');
plot(X(:,1),X(:,2),'.b')

% === ADD TO MASK ===
[Ny,Nx]=size(At);
[x,y]=meshgrid(1:Nx,1:Ny);
MASK=MASK + inpolygon(x,y,X(:,1),X(:,2));

% === DISPLAY ===
DIGIFILTER_display;

delete(hat)

