function txt = EMP8_ex1(tim, lambda, sigmaTab)
% EMP8_ex1         EM Potts model 8-neighbors, test different setups for erode
%                  FTCM is not done here, matrix R should be prepared already.
%
% Use:   txt = EMP8_ex1(tim, lambda, sigmaTab);        
% -------------------------------------------------------------------------
%  tim      is texture image number (1-13), default 4
%  lambda   the lambda parameter, default 3
%  sigmaTab the sigma values to use, default [5]
% -------------------------------------------------------------------------
% txt = EMP8_ex1(4, 2);  
% txt = EMP8_ex1(4, 2, [2,4,6,8,12,16,3]);

% check input
if (nargin < 1); tim = 4; end;
if (nargin < 2); lambda = 3; end;
if (nargin < 3); sigmaTab = 5; end;
sigma = sigmaTab(end);

opt = struct('verbose',0, 'lowerSize',20, 'Uppersize',5000, 'borderWidth',5, 'lambda',lambda);
% EMP8_argin calls EMP8_cost
tstart = tic;          
% s = EMP8_argin(tim, sigma, tim, 'swUpq',1, opt);
s = EMP8_argin(tim, sigma, tim, 'swUpq',3, 'Uim',tim, 'Utab',[2,160,0.3], opt);
telapsed = toc(tstart);
t = [mfilename,': Prepare input, EMP8_argin(..), time = ',sprintf('%7.1f',1000*telapsed),' [ms].'];
disp(t); txt = t;
% disp(['Results for initial labeling Li (Gaussian smoothing sigma = ',num2str(sigma),') and true Labeling.']);
% t = sprintf(s.formatCost, sprintf('Lg%3.1f',sigma), s.Ci.Edata, s.Ci.lambda, s.Ci.E4, s.Ci.E8, s.Ci.Etot, 100*s.Ci.errorRate);
% disp(t);
t = sprintf(s.formatCost, 'Ltrue', s.Ct.Edata, s.Ct.lambda, s.Ct.E4, s.Ct.E8, s.Ct.Etot, 0);
disp(t); txt = char(txt,t);
% find labeling that minimize data term
[~, L] = min(reshape(s.Ri, s.M, s.I*s.J)); s.Li = uint8(L(:));
s.Li = uint8(L(:));
c = EMP8_cost(s); 
t = sprintf(s.formatCost, 'Lmin', c.Edata, c.lambda, c.E4, c.E8, c.Etot, 100*c.errorRate);
disp(t); txt = char(txt,t);
% find labeling that maximize data term
[~, L] = max(reshape(s.Ri, s.M, s.I*s.J)); 
s.Li = uint8(L(:));
c = EMP8_cost(s); 
t = sprintf(s.formatCost, 'Lmax', c.Edata, c.lambda, c.E4, c.E8, c.Etot, 100*c.errorRate);
disp(t); txt = char(txt,t);
disp(' ');
          
% Gaussian smoothing
for sigma = sigmaTab
    % tstart = tic;   
    Lg = LIS_smooth(double(permute(reshape(s.Ri,s.M,s.I,s.J),[2,3,1])), sigma);
    s.Li = uint8(reshape( Lg, s.I*s.J, 1 )); 
    cg = EMP8_cost(s); 
    % telapsed = toc(tstart);
    % disp([' Gaussian smoothing (sigma = ',num2str(sigma),') and cost,  time = ',sprintf('%7.1f',1000*telapsed),' [ms].']);
    t = sprintf(s.formatCost, sprintf('Lg%3.1f',sigma), cg.Edata, cg.lambda, cg.E4, cg.E8, cg.Etot, 100*cg.errorRate);
    disp(t); txt = char(txt,t);
end
disp(' '); 

% Swap (from Lg)
s.borderWidth = -1;
s.maxLoop = 10;
[~,cs] = EMP8_swap(s);
disp([' Swap and resulting cost, time = ',sprintf('%7.1f',1000*cs.time),' [ms].']);
t = sprintf(s.formatCost, 'Swap', cs.Edata, cs.lambda, cs.E4, cs.E8, cs.Etot, 100*cs.errorRate);
disp(t); txt = char(txt,t);

% erode from Lg
for bw = [0,8,16]   %  erode along borders (within distance)
    %
    s.Li = uint8(reshape( Lg, s.I*s.J, 1 ));
    disp(' ');
    s.borderWidth = bw;
    [Le, ce] = EMP8_erode(s);
    disp([' Erode (borderWidth = ',num2str(bw),') and cost,   time = ',sprintf('%7.1f',1000*ce.time),' [ms].']);
    t = sprintf(s.formatCost, 'Lerode', ce.Edata, ce.lambda, ce.E4, ce.E8, ce.Etot, 100*ce.errorRate);
    disp(t); txt = char(txt,t);
    %
    s.Li = Le;
    s.borderWidth = [40, 25];
    s.maxLoop = 4;
    s.useRand = false;
    [~,cs] = EMP8_swap(s);
    disp([' and swap and resulting cost, additional time = ',sprintf('%7.1f',1000*cs.time),' [ms].']);
    t = sprintf(s.formatCost, 'Le+swap', cs.Edata, cs.lambda, cs.E4, cs.E8, cs.Etot, 100*cs.errorRate);
    disp(t); txt = char(txt,t);
end

end