function mi=MI(a,b)
%Caculate MI of a and b in the region of the overlap part
%计算重叠部分
[Ma,Na] = size(a);
[Mb,Nb] = size(b);
M=min(Ma,Mb);
N=min(Na,Nb);
%初始化直方图数组
hab = zeros(256,256);
ha = zeros(1,256);
hb = zeros(1,256);
%归一化
imax = max(max(a));
imin = min(min(a));
if imax ~= imin
a = double((a-imin))/double((imax-imin));
else
a = zeros(M,N);
end
imax = max(max(b));
imin = min(min(b));
if imax ~= imin
b = double(b-imin)/double((imax-imin));
else
b = zeros(M,N);
end
a = int16(a*255)+1;
b = int16(b*255)+1;
%统计直方图
for i=1:M
for j=1:N
indexx = a(i,j);
indexy = b(i,j) ;
hab(indexx,indexy) = hab(indexx,indexy)+1;%联合直方图
ha(indexx) = ha(indexx)+1;%a图直方图
hb(indexy) = hb(indexy)+1;%b图直方图
end
end
%计算联合信息熵
hsum = sum(sum(hab));
index = find(hab~=0);
p = hab/hsum;
Hab = sum(-p(index).*log(p(index)));
%计算a图信息熵
hsum = sum(sum(ha));
index = find(ha~=0);
p = ha/hsum;
Ha = sum(-p(index).*log(p(index)));
%计算b图信息熵
hsum = sum(sum(hb));
index = find(hb~=0);
p = hb/hsum;
Hb = sum(-p(index).*log(p(index)));
%计算a和b的互信息(越大匹配结果越好)
mi = Ha+Hb-Hab;