function [totman,totaut,totunion,nman,naut] = dice(watman,wataut)

% values in automated solution
valaut = unique(wataut(wataut > 0));  
naut = length(valaut);

% values in manual solution
valman = unique(watman(watman > 0));  
nman = length(valman);

% cellman = gt(<watman,0);
% cellaut = gt(wataut,0);
% showall(watman)
% showall(wataut)

aunion = zeros(nman,naut);
aman = zeros(nman,naut);
aaut = zeros(nman,naut);
npixman = zeros(nman,1);
for j = 1 : nman
%     msg = ['Manual region number ' int2str(j)];
%     disp(msg);

    % this value        
    valmanhere = valman(j);

    % this region        
    reghereman = eq(watman,valmanhere);                   

    % number of pixels here
    npixman(j) = length(find(reghereman));

    valaut = unique(wataut(reghereman));
    valaut(valaut == 0) = [];
    % check all automated cells with overlap to this manual region
    for k = 1 : numel(valaut)

        % this value
        valauthere = valaut(k);

        % this region
        reghereaut = eq(wataut,valauthere);                   
       
        
        % graded overlap, NOT logical
        intersbw = reghereman .* reghereaut;
%         reggradedunion = logical(unionbw .* (1 - abs(reghereman .* cellman - reghereaut .* cellaut)));
%         valgradedunion = sum(reggradedunion(:));
        ninters = sum(intersbw(:));

        % graded only manual label, NOT logical!
        regautonly = reghereaut;
        regautonly(reghereman) = 0;
        nautonly = sum(regautonly(:));

        % graded only automated label, NOT logical
        regmanonly = reghereman;
        regmanonly(reghereaut) = 0;
        nmanonly = sum(regmanonly(:));

        % the ratios
        aunion(j,valaut(k)) = ninters / (nautonly + ninters + nmanonly);
        aman(j,valaut(k)) = ninters / (ninters + nmanonly);
        aaut(j,valaut(k)) = ninters / (nautonly + ninters);           

    end;               

end;% for j

% relative number of pixels
npixall = sum(npixman);
relnumpix = (npixman /npixall);


% fix the matrix to obtain the maximum distribution
minvalman = maxdistr(aman);
minvalaut = maxdistr(aaut);
minvalunion = maxdistr(aunion);

% remove the infinity values
minvalman(isnan(minvalman)) = 0;
minvalaut(isnan(minvalaut)) = 0;
minvalunion(isnan(minvalunion)) = 0;

% relnumpix
% minvalman
% minvalaut
% minvalunion

%
% Total success rated by the pixels 
%

successman   = relnumpix .* max(minvalman,[],2);  
successaut   = relnumpix .* max(minvalaut,[],2);  
successunion = relnumpix .* max(minvalunion,[],2);

% successman
% successaut
% successunion

totman = sum(successman);        
totaut = sum(successaut);        
totunion = sum(successunion);        

disp(sprintf('  Percentage success man : %6.3f',totman))
disp(sprintf('  Percentage success aut : %6.3f',totaut))
disp(sprintf('  Percentage success union : %6.3f',totunion))    

% totman
% totaut
% totunion
