% EMP8_ex4         Test EMP8_erode.m og EMP8_swap.m  (script file)
%                  FTCM is not done here, matrix R should be prepared already.
%               
% This script use:
%    EMP8_argin     to make the struct containing input arguments
%                   Since this is called using a number indicating the
%                   data cost matrix R, a data matrix prepared august 2012
%                   is retrieved by:  LIS_getR.m  (see that)
%    EMP8_erode     to do alpha-erosion
%    LIS_plotcol    to make a color plot of the results
%    EMP8_swap      to do swap-moves

%----------------------------------------------------------------------
% Copyright (c) 2014.  Karl Skretting.  All rights reserved.
% University of Stavanger, Signal Processing Group
% Mail:  karl.skretting@uis.no   Homepage:  http://www.ux.uis.no/~karlsk/
% 
% HISTORY:  dd.mm.yyyy
% Ver. 1.0  13.10.2014  Tested that this example works as intened
%----------------------------------------------------------------------

clear all;
% if (testno == 1) % Good parameters for Gaussian smoothing and erode and swap
% if (testno == 2) % Good parameters for Gaussian smoothing and swap
testno = 1;

if (testno == 1) % Gode parametre med Gaussian smoothing, erode og swap
    lambda = 2;
    sigma = 5;
    opt = struct('verbose',0, 'lowerSize',20, 'Uppersize',5000, 'borderWidth',5, 'lambda',lambda);
    e1 = zeros(12,1);
    e2 = zeros(12,1);
    for tim = 1:12
        s = EMP8_argin(tim, sigma, tim, 'swUpq',3, 'Uim',tim, 'Utab',[2,160,0.3], opt);
        [L,c] = EMP8_erode(s);
        if (tim <= 0)
            figure(tim); clf;
            figTitle = ['Labeling of test image ',int2str(tim),' after using EMP8-erode, swUpq=3.'];
            LIS_plotcol(reshape(L,s.I,s.J), figTitle, c.errorRate, ...
                c.erodeNofComp, c.Etot, c.Edata, c.E4+c.E8);
        end
        if isfield(c,'erode255Cost')
            disp(c.erode255Cost);
        elseif isfield(c,'erodeCost')    
            disp(c.erodeCost);
        else
            t = sprintf(s.formatCost, 'Lerode', c.Edata, c.lambda, c.E4, c.E8, c.Etot, 100*c.errorRate);
            disp(t);
        end
        e1(tim) = c.errorRate;  % store results
        % prepare for swap (border neighborhood)
        s.Li = L; 
        s.borderWidth = [40, 25]; 
        s.maxLoop = 1; 
        s.useRand = false;
        [L,c2] = EMP8_swap(s);
        disp([' ',c2.swapCost]);
        e2(tim) = c2.errorRate;
        if (tim < 7)
            figure(tim); clf;
            figTitle = ['Labeling of test image ',int2str(tim),' after using EMP8-erode-swap, swUpq=3.'];
            LIS_plotcol(reshape(L,s.I,s.J), figTitle, c2.errorRate, ...
                c.erodeNofComp, c2.Etot, c2.Edata, c2.E4+c2.E8);
        end
    end
    disp(['Average error rate after erode:      ',num2str(100*mean(nonzeros(e1)))]);
    disp(['Average error rate after erode-swap: ',num2str(100*mean(nonzeros(e2)))]);
end

if (testno == 2) % Gode parametre med Gaussian smoothing og swap
    lambda = 1.5;
    sigma = 4;
    opt = struct('verbose',0, 'useRand',false, 'bw',-1, 'useLoop',15, 'lambda',lambda);
    e = zeros(12,1);
    for tim = 1:12
        s = EMP8_argin(tim, sigma, tim, 'swUpq',3, 'Uim',tim, 'Utab',[2,160,0.3], opt);
        [L,c] = EMP8_swap(s);
        figure(tim); clf;
        figTitle = ['Labeling of test image ',int2str(tim),' after using EMP8-swap, swUpq=3.'];
        LIS_plotcol(reshape(L,s.I,s.J), figTitle, c.errorRate, -1, c.Etot, c.Edata, c.E4+c.E8);
        disp(c.swapCost);
        e(tim) = c.errorRate;
    end
    disp(['Average error rate (only swap): ',num2str(100*mean(nonzeros(e)))]);
end
