%%%%Test CV model
% Use flag to choose different methods
% Author: Kaihua Zhang
% Email: cskhzhang@comp.polyu.edu.hk
% Date:  7/15/2010
clc;clear all;close all;
%-------------------
flag = 'RD';%'RD': RD method; 'GDRLSE1','GDRLSE2','GDRLSE3':Li et al.,'s methods and Xie's method; 'LSR': re-initialization method
%--------------------
Img = imread('river.jpg');%Your can choose your own image
Img = double(Img(:,:,1));
%----------------Parameters
delt = .1;% timestep for level set evolution equation;
delt2 = .1;% timestep for diffusion regularization equation;
nu = 0.001*255^2;% force generated by shortening curve length;
m_u=0;% constant force: in most case, the value is set to be 0.
Iternum = 600;% iteration number

%---------------Level set function intialization
[nrow,ncol] = size(Img);
figure;  imagesc(Img,[0 255]);colormap(gray);
text(10,10,'Left click to get points, right click to get end point','FontSize',[10],'Color', 'r');
BW = roipoly;
phi =  2*(0.5-BW);
%------------------------------
imagesc(Img);colormap(gray);hold on;contour(phi,[0 0],'Color','w','LineWidth',1);
%------------------------------------
for n = 1:Iternum
    strcat(num2str(n),' iterations')
    phi = NeumannBoundCond(phi);%boundary condtion
    [phi,Fcv] = CV_2D(Img, phi, nu,m_u,1, 1, delt, 1,flag,delt2);
    %-------------------
    if mod(n,50)==0
    imagesc(Img);colormap(gray);hold on;contour(phi,[0 0],'Color','w','LineWidth',1);
    iterNum=[num2str(n), ' iterations'];title(iterNum);hold off;pause(.01);
    end
end
figure;imagesc(sign(phi));
colormap(gray);axis off;title(strcat('Final segmentation result of  ',flag));
figure;mesh(phi);title(strcat('Final level set function of  ',flag));