我正在尝试实现两个发行版之间的距离测量。 here和input image中描述了详细信息。简而言之,总结了论文的主旨:
我实施了这个计划,但我有三个问题:
这是我实现该方案的代码:
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
答案 0 :(得分:0)
让我们逐一回答
Ans:这是自然日志。在matlab中,您只需致电log(x+eps)
Ans:忽略零日志。您需要添加一些小值log(x+(x==0)*eps)
或x
,其中{{1}}是您的值
Ans:您的代码看起来很好。您可以根据我的建议来改进您的代码。祝你好运