Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
DengPingFan committed Jul 26, 2017
1 parent ad2b5e1 commit 6d9aaf6
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 0 deletions.
58 changes: 58 additions & 0 deletions S_object.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function Q = S_object(prediction,GT)
% S_object Computes the object similarity between foreground maps and ground
% truth(as proposed in "Structure-measure:A new way to evaluate foreground
% maps" [Deng-Ping Fan et. al - ICCV 2017])
% Usage:
% Q = S_object(prediction,GT)
% Input:
% prediction - Binary/Non binary foreground map with values in the range
% [0 1]. Type: double.
% GT - Binary ground truth. Type: logical.
% Output:
% Q - The object similarity score
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% compute the similarity of the foreground in the object level
prediction_fg = prediction;
prediction_fg(~GT)=0;
O_FG = Object(prediction_fg,GT);

% compute the similarity of the background
prediction_bg = 1.0 - prediction;
prediction_bg(GT) = 0;
O_BG = Object(prediction_bg,~GT);

% combine the foreground measure and background measure together
u = mean2(GT);
Q = u * O_FG + (1 - u) * O_BG;

end

function score = Object(prediction,GT)

% check the input
if isempty(prediction)
score = 0;
return;
end
if isinteger(prediction)
prediction = double(prediction);
end
if (~isa( prediction, 'double' ))
error('prediction should be of type: double');
end
if ((max(prediction(:))>1) || min(prediction(:))<0)
error('prediction should be in the range of [0 1]');
end
if(~islogical(GT))
error('GT should be of type: logical');
end

% compute the mean of the foreground or background in prediction
x = mean2(prediction(GT));

% compute the standard deviations of the foreground or background in prediction
sigma_x = std(prediction(GT));

score = 2.0 * x./(x^2 + 1.0 + sigma_x + eps);
end
140 changes: 140 additions & 0 deletions S_region.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
function Q = S_region(prediction,GT)
% S_region computes the region similarity between the foreground map and
% ground truth(as proposed in "Structure-measure:A new way to evaluate
% foreground maps" [Deng-Ping Fan et. al - ICCV 2017])
% Usage:
% Q = S_region(prediction,GT)
% Input:
% prediction - Binary/Non binary foreground map with values in the range
% [0 1]. Type: double.
% GT - Binary ground truth. Type: logical.
% Output:
% Q - The region similarity score
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% find the centroid of the GT
[X,Y] = centroid(GT);

% divide GT into 4 regions
[GT_1,GT_2,GT_3,GT_4,w1,w2,w3,w4] = divideGT(GT,X,Y);

%Divede prediction into 4 regions
[prediction_1,prediction_2,prediction_3,prediction_4] = Divideprediction(prediction,X,Y);

%Compute the ssim score for each regions
Q1 = ssim(prediction_1,GT_1);
Q2 = ssim(prediction_2,GT_2);
Q3 = ssim(prediction_3,GT_3);
Q4 = ssim(prediction_4,GT_4);

%Sum the 4 scores
Q = w1 * Q1 + w2 * Q2 + w3 * Q3 + w4 * Q4;

end

function [X,Y] = centroid(GT)
% Centroid Compute the centroid of the GT
% Usage:
% [X,Y] = Centroid(GT)
% Input:
% GT - Binary ground truth. Type: logical.
% Output:
% [X,Y] - The coordinates of centroid.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[rows,cols] = size(GT);

if(sum(GT(:))==0)
X = round(cols/2);
Y = round(rows/2);
else
dGT = double(GT);
x = ones(rows,1)*(1:cols);
y = (1:rows)'*ones(1,cols);
area = sum(dGT(:));
X = round(sum(sum(dGT.*x))/area);
Y = round(sum(sum(dGT.*y))/area);
end

end

% divide the GT into 4 regions according to the centroid of the GT and return the weights
function [LT,RT,LB,RB,w1,w2,w3,w4] = divideGT(GT,X,Y)
% LT - left top;
% RT - right top;
% LB - left bottom;
% RB - right bottom;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%width and height of the GT
[hei,wid] = size(GT);
area = wid * hei;

%copy the 4 regions
LT = GT(1:Y,1:X);
RT = GT(1:Y,X+1:wid);
LB = GT(Y+1:hei,1:X);
RB = GT(Y+1:hei,X+1:wid);

%The different weight (each block proportional to the GT foreground region).
w1 = (X*Y)./area;
w2 = ((wid-X)*Y)./area;
w3 = (X*(hei-Y))./area;
w4 = 1.0 - w1 - w2 - w3;
end

%Divide the prediction into 4 regions according to the centroid of the GT
function [LT,RT,LB,RB] = Divideprediction(prediction,X,Y)

%width and height of the prediction
[hei,wid] = size(prediction);

%copy the 4 regions
LT = prediction(1:Y,1:X);
RT = prediction(1:Y,X+1:wid);
LB = prediction(Y+1:hei,1:X);
RB = prediction(Y+1:hei,X+1:wid);

end

function Q = ssim(prediction,GT)
% ssim computes the region similarity between foreground maps and ground
% truth(as proposed in "Structure-measure: A new way to evaluate foreground
% maps" [Deng-Ping Fan et. al - ICCV 2017])
% Usage:
% Q = ssim(prediction,GT)
% Input:
% prediction - Binary/Non binary foreground map with values in the range
% [0 1]. Type: double.
% GT - Binary ground truth. Type: logical.
% Output:
% Q - The region similarity score
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

dGT = double(GT);

[hei,wid] = size(prediction);
N = wid*hei;

%Compute the mean of SM,GT
x = mean2(prediction);
y = mean2(dGT);

%Compute the variance of SM,GT
sigma_x2 = sum(sum((prediction - x).^2))./(N - 1 + eps);
sigma_y2 = sum(sum((dGT - y).^2))./(N - 1 + eps);

%Compute the covariance between SM and GT
sigma_xy = sum(sum((prediction - x).*(dGT - y)))./(N - 1 + eps);

alpha = 4 * x * y * sigma_xy;
beta = (x.^2 + y.^2).*(sigma_x2 + sigma_y2);

if(alpha ~= 0)
Q = alpha./(beta + eps);
elseif(alpha == 0 && beta == 0)
Q = 1.0;
else
Q = 0;
end

end
39 changes: 39 additions & 0 deletions StructureMeasure.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function Q = StructureMeasure(prediction,GT)
% StructureMeasure computes the similarity between the foreground map and
% ground truth(as proposed in "Structure-measure: A new way to evaluate
% foreground maps" [Deng-Ping Fan et. al - ICCV 2017])
% Usage:
% Q = StructureMeasure(prediction,GT)
% Input:
% prediction - Binary/Non binary foreground map with values in the range
% [0 1]. Type: double.
% GT - Binary ground truth. Type: logical.
% Output:
% Q - The computed similarity score
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Check input
if (~isa(prediction,'double'))
error('The prediction should be double type...');
end
if ((max(prediction(:))>1) || min(prediction(:))<0)
error('The prediction should be in the range of [0 1]...');
end
if (~islogical(GT))
error('GT should be logical type...');
end

y = mean2(GT);

if (y==0)% if the GT is completely black
x = mean2(prediction);
Q = 1.0 - x; %only calculate the area of intersection
elseif(y==1)%if the GT is completely white
x = mean2(prediction);
Q = x; %only calcualte the area of intersection
else
alpha = 0.5;
Q = alpha*S_object(prediction,GT)+(1-alpha)*S_region(prediction,GT);
end

end
62 changes: 62 additions & 0 deletions demo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
close all; clear; clc;

% set the RoodDir according to your own environment
RootDir = pwd;

% set the ground truth path and the foreground map path
gtPath = fullfile(RootDir,'demo','GT');
fgPath = fullfile(RootDir,'demo','FG');

% set the result path
resPath = fullfile(RootDir,'demo','Result');
if ~exist(resPath,'dir')
mkdir(resPath);
end

% set the foreground map methods
MethodNames = {'MDF','mc','DISC','rfcn','DCL','dhsnet'};

% load the gtFiles
gtFiles = dir(fullfile(gtPath,'*.png'));

% for each gtFiles
for i = 1:length(gtFiles)
fprintf('Processing %d/%d...\n',i,length(gtFiles));

% load the gt file
[GT,map] = imread(fullfile(gtPath,gtFiles(i).name));
if numel(size(GT))>2
GT = rgb2gray(GT);
end
GT = logical(GT);

% in some dataset(ECSSD) some ground truth is reverse when map is not none
if ~isempty(map) && (map(1)>map(2))
GT = ~GT;
end

% for each saliency method
for j = 1 : length(MethodNames)
% load the saliency map file
predname = [gtFiles(i).name(1:end-4) '_' MethodNames{j} '.png'];
prediction = imread(fullfile(fgPath,predname));
if numel(size(prediction))>2
prediction = rgb2gray(prediction);
end

d_prediction = double(prediction);
d_prediction = reshape(mapminmax(d_prediction(:)',0,1),size(d_prediction));

% evaluate the predicted map against the GT
score = StructureMeasure(d_prediction,GT);
score = roundn(score,-4);

% save the result
resName = sprintf([gtFiles(i).name(1:end-4) '_%.4f_' MethodNames{j} '.png'],score);
imwrite(prediction,fullfile(resPath,resName));
end
end

fprintf('The results are saved in %s\n',resPath);


0 comments on commit 6d9aaf6

Please sign in to comment.