% EMP8_ex2         Test EMP8_cost.m and EMP8_icm_mex (script file)

clear all;

tim = 5;      % Test image no
sigma = 5;    % used to make labeling s.Li (Gaussian smoothing)

disp('EMP8_argin also find cost, ');
s = EMP8_argin(tim, sigma, tim, 'lambda',5, 'verbose',2);   
disp(['Total cost is returned in s.Ci.Etot = ',num2str(s.Ci.Etot)]);
disp(' ');
disp('change lambda to 3.');
s.lambda = 3;
c = EMP8_cost(s);   
disp(['Cost in c (from EMP8_cost) = ',num2str(c.Etot),', time used ',sprintf('%4.1f',1000*c.time),' [ms].']);
disp('Note, since bF4 and bF8 are not changed, objective function is the same.');
s.bF4 = -1;         % this makes bF4 and bF8 to be found again 
c = EMP8_cost(s);   % and find new cost
disp([' new cost (from EMP8_cost) = ',num2str(c.Etot),', time used ',sprintf('%4.1f',1000*c.time),' [ms].']);
disp(' ');
disp('It is not needed to call function again to find cost for a new lambda.');
for lambda = 1:7
    newCost = c.Edata+lambda*(c.E4+c.E8);
    t = sprintf(s.formatCost, sprintf('lambda=%3.1f',lambda), c.Edata, lambda, ...
                c.E4, c.E8, newCost, 100*c.errorRate);
    disp(t);        
end

% set up to use Upq,  reduce weights at edges, 
% a little bit lower values for C4 and C8 (E4 and E8), Cdata and Edata unchanged
disp(' ');
disp('Reduce cost for pixels near the image edges.');
Upq = ones(s.I,s.J,4); 
Upq(:,[1,2,s.J],:) = 0.5; 
Upq([1,2,s.I],:,:) = 0.5;
s = EMP8_argin(s, 'swUpq',2, 'Upq',Upq);    % add options
c = EMP8_cost(s); 
disp(['Cost in c (from EMP8_cost) = ',num2str(c.Etot),', time used ',sprintf('%4.1f',1000*c.time),' [ms].']);

disp(' ');
disp('Reduce cost for pixels different from neighbor in test image.');
s = EMP8_argin(s, 'swUpq',3, 'Uim',teximage(100+tim), 'Utab',[2,120,0.2]);   % change and add
c = EMP8_cost(s); 
disp(['Cost in c (from EMP8_cost) = ',num2str(c.Etot),', time used ',sprintf('%4.1f',1000*c.time),' [ms].']);


disp(' ');
disp('Test EMP8_icm_mex.');
% Pix = int32(-1);  % all pixels 
I = s.I; J = s.J;
L = reshape(s.Li,I,J);  
Pmat = false(I,J);
Pmat(:,1:(J-1)) = Pmat(:,1:(J-1)) | (L(:,1:(J-1)) ~= L(:,2:J)); 
Pmat(:,2:J) = Pmat(:,2:J) | (L(:,1:(J-1)) ~= L(:,2:J)); 
Pmat(1:(I-1),:) = Pmat(1:(I-1),:) | (L(1:(I-1),:) ~= L(2:J,:)); 
Pmat(2:I,:) = Pmat(2:I,:) | (L(1:(I-1),:) ~= L(2:J,:)); 
Pix = int32(find(Pmat));
s.P = numel(Pix);
s.useRand = 1;  % 1 in random order, 0 not random order
s.swUpq = 1;
s.Upq = int16([]);    % and this is not needed any more
s.verbose = 0; 
Param = [s.M,s.I,s.J,s.P, s.bF4,s.bF8,s.swUpq,s.verbose, ...
         s.lambda,s.dR,s.minsumRi,s.maxsumRi, -1,-1,-1,s.useRand];  % 16 parameters
tic; 
[L1, c1] = EMP8_icm_mex(s.Ri, s.Li, Pix, Param);
toc;
er1 = nnz(L1 ~= s.Lt)/(I*J);
s.swUpq = 3;
Param = [s.M,s.I,s.J,s.P, s.bF4,s.bF8,s.swUpq,s.verbose, ...
         s.lambda,s.dR,s.minsumRi,s.maxsumRi, -1,-1,-1,s.useRand];  % 16 parameters
s.Utab = int16(ceil(4096*[2/3 + (1/3)*cos(((0:140)/140)*pi), ones(1,255-140)/3]));
tic;
[L3, c3] = EMP8_icm_mex(s.Ri, s.Li, Pix, s.Uim, s.Utab, Param);
toc;
er3 = nnz(L3 ~= s.Lt)/(I*J);
disp(['Error rate 1 = ',num2str(100*er1),'%, Error rate 3 = ',num2str(100*er3),'%.']);

