【发布时间】:2014-08-05 05:51:44
【问题描述】:
我正在尝试实现两个分布之间的距离测量。详细信息在 here 和 input image 中进行了描述。让我们简单总结一下论文的想法:
- 使用Heaviside函数H将输入图像分为内部区域和外部区域
- 计算区域内和区域外的分布,其中phi是内外边界。
- 计算 Kullback-Leibler 距离
我实现了这个方案,但我遇到了三个问题:
- paper中的log函数是matlab中的log还是log2?
- Log(0) 是无限的,但是我们知道分布结果会返回很多0值。如何忽略它?就我而言,我加上 eps 值,有些人加上 h1(h1==0)=1,这是正确的吗?
- 你能看到我的代码吗?这是正确的吗?我不确定我的实现。
这是我实现该方案的代码:
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
【问题讨论】:
-
如果你想进行代码审查,你应该去codereview.stackexchange.com
-
以及该方案的实施。它更复杂。
标签: matlab image-processing computer-vision image-segmentation