Kullback-Leibler 2之间的距离分布matlab

时间:2014-05-20 01:52:44

标签: matlab image-processing matlab-deployment matlab-compiler

我必须计算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

1 个答案:

答案 0 :(得分:0)

您无法计算不同大小的矢量的KL分歧。您必须调整直方图的大小以在两种情况下获得相同的大小。

因此,在调用函数直方图时,为所有输入设置一个恒定的二进制数。