%%%%Demo Fig.11
% Use flag to choose different methods
% Author: Kaihua Zhang
% Email: cskhzhang@comp.polyu.edu.hk
% Date:  11/7/2011
% Revised: 2/8/2011
clc;clear all;close all;

Img = imread('blossom.jpg');%blossom.jpg, squirrel.gif
Img = double(Img(:,:,1));
[nrow,ncol] = size(Img);

%---------------------------------------
fprintf('You can choose different methods\n')
fprintf('1: RD 2:level set without re-initialization 3:re-initialization method 4:GDRLSE1; 5: GDRLSE2; 6: GDRLSE3\n\n\n')
%--------------------------
flag= input('Please choose your method and then press Enter:\n 1: RD 2:level set without re-initialization 3:re-initialization method 4:GDRLSE1; 5: GDRLSE2; 6: GDRLSE3\n');
% flag = 1; % 1: RD 2:level set without re-initialization 3:re-initialization method; 
%           % 4:GDRLSE1; 5: GDRLSE2; 6: GDRLSE3
%----------------------------Initialization
switch flag
    case 1
        r = 70;
        c0 = 3;
        phi = c0*ones(nrow,ncol);
        phi(r:nrow-r,r:ncol-r) = -c0;
    case 2
        i = 1:ncol;j = 1:nrow;
        [x,y]=meshgrid(i,j);
        R = 150;
        b = nrow/2;
        a = ncol/2;
        phi = sqrt((x-a).^2+(y-b).^2) - R;
    case 3
        i = 1:ncol;j = 1:nrow;
        [x,y]=meshgrid(i,j);
        R = 150;
        b = nrow/2;
        a = ncol/2;
        phi = sqrt((x-a).^2+(y-b).^2) - R;
     case 4
        r = 70;
        c0 = 3;
        phi = c0*ones(nrow,ncol);
        phi(r:nrow-r,r:ncol-r) = -c0;
      case 5
        r = 70;
        c0 = 3;
        phi = c0*ones(nrow,ncol);
        phi(r:nrow-r,r:ncol-r) = -c0;
      case 6        
        r = 70;
        c0 = 3;
        phi = c0*ones(nrow,ncol);
        phi(r:nrow-r,r:ncol-r) = -c0;
end
phi1 = phi;
figure;mesh(phi);
figure;imagesc(Img,[0 255]);colormap(gray);hold on;contour(phi,[0 0],'r','LineWidth',1);hold off;

switch flag;
    case 1
        delt = .1; mu = 0; nu =0.5*255^2;m_cv = 'RD'; Iternum = 1850;
        delt2 = .1;
    case 2
        delt = .1; mu = 0; nu =0.5*255^2;m_cv = 'LSW';Iternum = 5000;
        delt2 = .1;
    case 3
        delt = .1; mu = 0; nu =0.5*255^2;m_cv = 'LSR';Iternum = 2500;
        delt2 = .01;
    case 4
         delt = .1; mu = 0; nu =0.5*255^2;m_cv = 'GDRLSE1';Iternum = 1000;
         delt2 = .01;
    case 5
         delt = 0.1; mu = 0; nu =0.5*255^2;m_cv = 'GDRLSE2';Iternum = 1000;
         delt2 = .01;  
    case 6

         delt = .1; mu = 0; nu =0.1*255^2;m_cv = 'GDRLSE3';Iternum = 5000;
         delt2 = .01;
end

for n=0:Iternum
    strcat(num2str(n),' iterations...')
    phi = NeumannBoundCond(phi);  
    [phi,Fcv] = CV_2D(Img, phi, mu,nu,1, 1, delt, 1,m_cv,delt2);    
        
    if mod(n,10) == 0
    imagesc(Img,[0 255]);colormap(gray);hold on;contour(phi,[0 0],'b','LineWidth',2);    
    iterNum=[num2str(n), ' iterations'];title(iterNum);pause(0.01);hold off;
    end
end
hold on;contour(phi1,[0 0],'r','LineWidth',2);
    figure;mesh(phi);

