MATLAB中图像之间的Mutual information的计算

本文介绍了如何在图像处理中利用Mutual Information(MI)计算公式来量化两张图像之间的相似性,通过量化、直方图和联合分布的计算,详细展示了Matlab代码示例。重点讲解了联合直方图、边际熵和联合熵的计算,以及MI的实际应用和计算过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

链接中也给出了引用地址。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值