归一化互信息图像匹配matlab
时间: 2023-10-13 08:10:04 浏览: 80
归一化互信息(Normalized Mutual Information,NMI)是用于图像匹配的一种常见方法。下面是一个用MATLAB实现的归一化互信息图像匹配的示例代码:
```matlab
% 读取两幅图像
img1 = imread('image1.jpg');
img2 = imread('image2.jpg');
% 将图像转为灰度图像
img1 = rgb2gray(img1);
img2 = rgb2gray(img2);
% 计算直方图
hist1 = imhist(img1);
hist2 = imhist(img2);
% 计算归一化互信息
NMI = mutual_info(hist1, hist2) / (entropy(hist1) + entropy(hist2));
% 显示结果
figure;
subplot(1,2,1),imshow(img1);
title('Image 1');
subplot(1,2,2),imshow(img2);
title('Image 2');
fprintf('归一化互信息:%.4f\n', NMI);
```
其中,`mutual_info`和`entropy`是自定义的函数,可以按照以下方式实现:
```matlab
function mi = mutual_info(hist1, hist2)
% 计算互信息
joint_hist = histcounts2(hist1, hist2);
joint_prob = joint_hist / sum(joint_hist(:));
marg_prob_1 = sum(joint_prob, 2);
marg_prob_2 = sum(joint_prob, 1);
mi = sum(sum(joint_prob .* log2(joint_prob ./ (marg_prob_1 * marg_prob_2))));
end
function ent = entropy(hist)
% 计算熵
prob = hist / sum(hist(:));
prob(prob==0) = 1;
ent = -sum(prob .* log2(prob));
end
```
需要注意的是,这里的`imhist`函数用于计算灰度图像的直方图,`histcounts2`函数用于计算两幅图像的联合直方图。