用matlab写出求解融合图像的CE(Cross entropy),EN(Entropy),MI(Mutual information),PSNR(Peak signal-to-nosie ration)的代码
时间: 2023-12-26 11:05:26 浏览: 162
以下是用MATLAB计算融合图像CE、EN、MI和PSNR的示例代码:
CE和EN的代码:
```matlab
function [CE, EN] = CE_EN(img1, img2, fused)
% img1, img2分别为待融合图像,fused为融合图像
% CE为融合图像的交叉熵,EN为融合图像的熵
p1 = imhist(img1)/(size(img1,1)*size(img1,2));
p2 = imhist(img2)/(size(img2,1)*size(img2,2));
pf = imhist(fused)/(size(fused,1)*size(fused,2));
CE = -sum(sum(p1.*log2(pf+eps)+p2.*log2(pf+eps)));
EN = -sum(pf.*log2(pf+eps));
end
```
MI的代码:
```matlab
function MI = MI(img1, img2, fused)
% img1, img2分别为待融合图像,fused为融合图像
% MI为融合图像的互信息
p1 = imhist(img1)/(size(img1,1)*size(img1,2));
p2 = imhist(img2)/(size(img2,1)*size(img2,2));
pf = imhist(fused)/(size(fused,1)*size(fused,2));
H1 = -sum(p1.*log2(p1+eps));
H2 = -sum(p2.*log2(p2+eps));
Hf = -sum(pf.*log2(pf+eps));
p12 = jointHistogram(img1, img2)/(size(img1,1)*size(img1,2));
pf12 = jointHistogram(fused, fused)/(size(fused,1)*size(fused,2));
H12 = -sum(pf12(:).*log2(pf12(:)+eps));
MI = H1 + H2 - H12;
end
function [joint_hist] = jointHistogram(img1, img2)
% img1, img2分别为待融合图像
% 计算二维联合直方图
joint_hist = zeros(256,256);
for i=1:size(img1,1)
for j=1:size(img1,2)
joint_hist(img1(i,j)+1,img2(i,j)+1) = joint_hist(img1(i,j)+1,img2(i,j)+1) + 1;
end
end
end
```
PSNR的代码:
```matlab
function PSNR = psnr(img1, img2)
% img1, img2分别为待比较图像
% PSNR为两图像的峰值信噪比
[M,N] = size(img1);
dif = double(img1) - double(img2);
MSE=sum(sum(dif.^2))/(M*N);
PSNR=10*log10(255^2/MSE);
end
```
以上代码中,CE和EN使用的是直方图的方法计算,MI使用的是联合直方图的方法计算,PSNR使用的是公式法计算。
阅读全文