使用Kullback-Leibler(KL)距离matlab代码测量类似信息

时间:2014-08-05 05:51:45

标签: matlab image-processing computer-vision image-segmentation

我正在尝试实现两个发行版之间的距离测量。 hereinput image中描述了详细信息。简而言之,总结了论文的主旨:

  • 通过使用Heaviside函数H
  • 将输入图像划分为内部区域和外部区域
  • 计算区域和外部区域内的分布,其中phi是内部和外部的边界。
  • 计算Kullback-Leibler距离

我实施了这个计划,但我有三个问题:

  1. 纸质中的日志功能是matlab中的log还是log2?
  2. Log(0)是无限的,但我们知道分布结果将返回许多0值。怎么忽略它?在我的情况下,我加上eps值,有些人添加h1(h1 == 0)= 1,这是正确的吗?
  3. 你能看到我的代码吗?这是对的吗?我不确定我的实施。
  4. 这是我实现该方案的代码:

    function main()
    Img=imread('1.bmp');%please download at above link
    Img=double(Img(:,:,1));    
    %% Initial boundary
    c0=2;  %const value
    phi = ones(size(Img(:,:,1))).*c0;
    phi(26:32,28:34) = -c0;    
    %% Heaviside function
    epsilon=1
    Hu=0.5*(1+(2/pi)*atan(phi./epsilon));
    
    %% Inside and outside image
    inImg=Img.*(1-Hu);
    outImg=Img.*(Hu);
    %% Let caclulate KL distance
    h1 =  histogram(inImg, 256, 0, 255); %Histogram of inside
    h2 =  histogram(outImg, 256, 0, 255);%Histogram of outside
    lamda1=KLdist(h1,h2) % distance from h1 to h2
    lamda2=KLdist(h2,h1) % distance from h2 to h1
    end
    
    %%%%%%%%%% function for KL distance%%%%%%%%%%%%%%%
    function [d1,d2]=KLdist(h1,h2)
    d1=sum(h1.*log2(h1+eps)-h1.*log2(h2+eps))
    d2=sum(h2.*log2(h2+eps)-h2.*log2(h1+eps))
    end
    %%%%%%%%%%function for histogram calculation%%%%%%
    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)

让我们逐一回答

  1. 纸质中的日志功能是matlab中的log还是log2?
  2. Ans:这是自然日志。在matlab中,您只需致电log(x+eps)

    1. Log(0)是无限的,但我们知道分布结果将返回许多0值。怎么忽略它?
    2. Ans:忽略零日志。您需要添加一些小值log(x+(x==0)*eps)x,其中{{1}}是您的值

      1. 你能看到我的代码吗?这是对的吗?我不确定我的实施。
      2. Ans:您的代码看起来很好。您可以根据我的建议来改进您的代码。祝你好运