【发布时间】:2014-05-20 15:33:08
【问题描述】:
我必须计算不同图像的两个分布的 Kullback-Leibler (KL) 距离。假设我有两个尺寸分别为 5694x1 和 231x1 的图像。现在,我想计算这些图像中两个分布的 KL 距离。我尝试在matlab中做,但它没有运行。你能帮我检查一下吗?问题是两个分布的矩阵大小不同。你可以在imagetest下载图片测试
%%Main function to calculate KL
function d=KLdist(firstImg,secondImg)
h1 = histogram(firstImg, max(firstImg(:))+1, 0, max(firstImg(:)));
h2 = histogram(secondImg, max(secondImg(:))+1, 0, max(secondImg(:)));
h1(find(h1==0))=1;
h2(find(h2==0))=1;
temp = sum(h1.*log(h1./h2));
temp( isinf(temp) ) = 0; % this resloves where h1(i) == 0
d1 = sum(temp);
temp = sum(h2.*log(h2./h1)); % other direction of compare since it's not symetric
temp( isinf(temp) ) = 0;
d2 = sum(temp);
d = d1 + d2
end
%%Function to calculate histogram distribution
function [h,bins] = histogram(I, n, min, max)
I = I(:);
range = max - min;
drdb = range / double(n); % dr/db - change in range per bin
h = zeros(n,1);
bins = zeros(n,1);
for i=1:n
% note: while the instructions say "within integer round off" I'm leaving
% this as float bin edges, to handle the potential float input
% ie - say the input was a probability image.
low = min + (i-1)*drdb;
high = min + i*drdb;
h(i) = sum( (I>=low) .* (I<high) );
bins(i) = low;
end
h(n) = h(n) + sum( (I>=(n*drdb)) .* (I<=max) ); % include anything we may have missed in the last bin.
h = h ./ sum(h); % "relative frequency"
end
【问题讨论】:
标签: matlab image-processing matlab-deployment matlab-compiler