Mutual information
图像中Mutual information的计算公式一般如下:
这之中涉及到两个方面的内容,一个是联合的直方图,一个是各自的直方图。下面是参考代码:
function res = computeMI(img1,img2,bins)
%Compute mutual information
%Quantify images
img1 = im2double(uint16(img1));
img2 = im2double(uint16(img2));
img1 = floor(img1*bins);%Quantify
img2 = floor(img2*bins);
%参考:
%https://www.mathworks.com/matlabcentral/fileexchange/6978-image-registration-2d-using-mutual-information-optimization-toolbox-needed
[rows,cols] = size(img1);
h = hist2d ([img1(:),img2(:)], 0:1:100, 0:1:100);
[r,c] = size(h);
b= h./(r*c); % normalized joint histogram
y_marg=sum(b); %sum of the rows of normalized joint histogram
x_marg=sum(b');%sum of columns of normalized joint histogran
Hy=0;
for i=1:c % col
if( y_marg(i)==0 )
%do nothing
else
Hy = Hy + -(y_marg(i)*(log2(y_marg(i)))); %marginal entropy for image 1
end
end
Hx=0;
for i=1:r %rows
if( x_marg(i)==0 )
%do nothing
else
Hx = Hx + -(x_marg(i)*(log2(x_marg(i)))); %marginal entropy for image 2
end
end
h_xy = -sum(sum(b.*(log2(b+(b==0))))); % joint entropy
res=-(Hx+Hy-h_xy);% Mutual information
%x
end
% https://www.mathworks.com/matlabcentral/fileexchange/1487-2d-histogram-matrix
function mHist = hist2d (mX, vYEdge, vXEdge)
nCol = size(mX, 2);
if nCol < 2
error ('mX has less than two columns')
end
nRow = length (vYEdge)-1;
nCol = length (vXEdge)-1;
vRow = mX(:,1);
vCol = mX(:,2);
mHist = zeros(nRow,nCol);
for iRow = 1:nRow
rRowLB = vYEdge(iRow);
rRowUB = vYEdge(iRow+1);
vColFound = vCol((vRow > rRowLB) & (vRow <= rRowUB));
if (~isempty(vColFound))
vFound = histc (vColFound, vXEdge);
nFound = (length(vFound)-1);
if (nFound ~= nCol)
disp([nFound nCol])
error ('hist2d error: Size Error')
end
[nRowFound, nColFound] = size (vFound);
nRowFound = nRowFound - 1;
nColFound = nColFound - 1;
if nRowFound == nCol
mHist(iRow, :)= vFound(1:nFound)';
elseif nColFound == nCol
mHist(iRow, :)= vFound(1:nFound);
else
error ('hist2d error: Size Error')
end
end
end
end
链接中也给出了引用地址。