# Matlab 显著性检测模型性能度量线性相关系数 CC

% created: Zoya Bylinskii, Aug 2014

% This finds the linear correlation coefficient between two different
% saliency maps (also called Pearson's linear coefficient).
% score=1 or -1 means the maps are correlated
% score=0 means the maps are completely uncorrelated

function score = CC(saliencyMap1, saliencyMap2)
% saliencyMap1 and saliencyMap2 are 2 real-valued matrices

map1 = im2double(imresize(saliencyMap1, size(saliencyMap2)));
map2 = im2double(saliencyMap2);
% map1=saliencyMap1;
% map2=saliencyMap2;

% normalize both maps
map1 = (map1 - mean(map1(:))) / std(map1(:));
map2 = (map2 - mean(map2(:))) / std(map2(:));

score = corr2(map2, map1);
function r = corr2(varargin)

[a,b] = ParseInputs(varargin{:});

a = a - mean2(a);
b = b - mean2(b);
r = sum(sum(a.*b))/sqrt(sum(sum(a.*a))*sum(sum(b.*b)));

%--------------------------------------------------------
function [A,B] = ParseInputs(varargin)

narginchk(2,2);

A = varargin{1};
B = varargin{2};

validateattributes(A, {'logical' 'numeric'}, {'real','2d'}, mfilename, 'A', 1);
validateattributes(B, {'logical' 'numeric'}, {'real','2d'}, mfilename, 'B', 2);

if any(size(A)~=size(B))
error(message('images:corr2:notSameSize'))
end

if (~isa(A,'double'))
A = double(A);
end

if (~isa(B,'double'))
B = double(B);
end



a = a - mean2(a);
b = b - mean2(b);
r = sum(sum(a.*b))/sqrt(sum(sum(a.*a))*sum(sum(b.*b)));

$D(x) = \frac{1}{mn} \sum_{i=1}^m \sum_{j=1}^n (x_{ij} - u_x)^2$

$cov(x,y) = \frac{1}{mn} \sum_{i=1}^m \sum_{j=1}^n (x_{ij}-u_x) (y_{ij}-u_y)$

$CC = {\color{Red} \frac{cov(x, y)} {\sqrt{cov(x,x)} \sqrt{cov(y,y)}}} = \frac{cov(x, y)}{ \sqrt{D(x)} \sqrt{D(y)}}$

1）如果 x 等于 y，由公式红色部分可知 CC = 1，最相关；

2）而当x 完全不等于 y，即 x 独立于 y，则由下面公式：

$cov(x, y) = E(x y) - E(x) E(y)$