% This code is only available for academic non-commercial use. 
% If you are using this code, please give a reference to:
% Zhang, Q., R. Skjetne, I. Metrikin, and S. Løset, “Image Processing for
% Ice Floe Analyses in Broken-ice Model Testing.” Cold Regions Science and
% Technology, Vol. 111, pp. 27-38, 2015.

%% parameter setting
% parameters for image
I = imread('model_ice.jpg');  % input image

Ra_min = 20;  % minimum floe area
Ra = 1000;   % maximum floe area
Rc = 0.9;   % convexity threshlod
Rl = 2;    % threshold ratio between length and width
timer = 2;

se = strel('disk', 3);  % morphology structure element


% parameters for GVF Snake
Num = 150;      % number of GVF iterations
iter = 150;   % number of Snake iterations 
% N = 15;    % iter of deformation


%% other parameters for GVF Snake, DON'T TUNE!
sigma = 0;   % gaussianBlur
GradientOn = 1;  % 1 : Gradient on  0 : Gradient off

GVFOn = 1;   % 1 : GVF  0 : SVF
mu = 0.1;    % parameter for GVF (more noise, increase mu)

alpha = 0.05;   % internal weight that control snake's tension
beta = 0;     % internal weight that control snake's rigidity
gamma = 1;    % step size in one iteration
kappa = 0.6;   % external force weight
Dmin = 0;   % min raster of the snake
Dmax = 1;   % max raster of the snake


%%
bw1 = GVF_distance( I, sigma, GradientOn, GVFOn, Num, mu,...
    iter, alpha, beta, gamma, kappa, Dmin, Dmax, Ra_min, Ra, Rc, Rl, se, timer);

bw2 = bwareaopen(bw1, Ra_min);
figure, imshow(bw2);

bw3 = bw2;
% bw3 = imclearborder(bw3, 4);  % remove object connect to the image boundary
bw3 = bwareaopen(bw3, Ra_min, 4);
bw4 = imfill(bw3, 8, 'holes');

% % rectanglarize
% S = rect(bw4);
% figure, imshow(bw4);
% hold on, 
% for i = 1 : length(S)
%     v = cat(1, S(i).Vertices);
%     c = cat(1, S(i).Center); 
%     line(v(:, 1), v(:, 2));
%     plot(c(1), c(2), 'r+');
% end
% 
% % floe model
% k1 = 0.4; k2 = 2.5;
% s_model = model_ice_model( S, bw4, k1, k2 );




