% 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)));
可以用公式表示,图a,b的相关性大小,越大越相关:
方差 :
协方差:
线性相关系数 CC:
其中,u为均值。即为:x 和 y 的方差,除以 x 的标准差与 y 的标准差之积,CC越大越相关。
1)如果 x 等于 y,由公式红色部分可知 CC = 1,最相关;
2)而当x 完全不等于 y,即 x 独立于 y,则由下面公式:
其中E为期望,即均值。当 x 独立于 y,则 :,所以cov(x, y)就为0,CC值为0,最不相关。