【问题标题】:Kullback-Leibler distance between 2 distribution of images matlab2图像分布之间的Kullback-Leibler距离matlab
【发布时间】: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


    【解决方案1】:

    您无法计算具有不同大小的向量的 KL 散度。您必须调整直方图的大小以在两种情况下获得相同的大小。

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

    【讨论】:

    • 如果我按 h1 = histogram(firstImg, 255+1, 0,255); 编辑怎么样?而不是 h1 = histogram(firstImg, max(firstImg(:))+1, 0, max(firstImg(:)));
    • @user3336190 您对两个图像都施加了相同的分箱,所以是的。
    猜你喜欢
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 2011-06-19
    相关资源
    最近更新 更多