Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab Kullback-Leibler图像2个分布之间的距离_Matlab_Image Processing_Matlab Deployment_Matlab Compiler - Fatal编程技术网

Matlab Kullback-Leibler图像2个分布之间的距离

Matlab Kullback-Leibler图像2个分布之间的距离,matlab,image-processing,matlab-deployment,matlab-compiler,Matlab,Image Processing,Matlab Deployment,Matlab Compiler,我必须计算不同图像的两个分布的Kullback-Leibler(KL)距离。假设我有两个图像,大小分别为5694x1和231x1。现在,我想计算这些图像中两个分布的KL距离。我试着用matlab做,但它没有运行。你能帮我查一下吗?问题是两个分布的矩阵大小不同。您可以在以下位置下载图像测试: %%用于计算KL的主函数 函数d=KLdist(第一个img,第二个img) h1=直方图(firstImg,max(firstImg(:)+1,0,max(firstImg(:))); h2=直方图(第二个

我必须计算不同图像的两个分布的Kullback-Leibler(KL)距离。假设我有两个图像,大小分别为5694x1和231x1。现在,我想计算这些图像中两个分布的KL距离。我试着用matlab做,但它没有运行。你能帮我查一下吗?问题是两个分布的矩阵大小不同。您可以在以下位置下载图像测试:

%%用于计算KL的主函数
函数d=KLdist(第一个img,第二个img)
h1=直方图(firstImg,max(firstImg(:)+1,0,max(firstImg(:)));
h2=直方图(第二个img,最大值(第二个img(:)+1,0,最大值(第二个img(:)));
h1(发现(h1==0))=1;
h2(find(h2==0))=1;
温度=总和(h1.*对数(h1./h2));
温度(isinf(temp))=0;%这里h1(i)==0
d1=总和(温度);
温度=总和(h2.*对数(h2./h1));%其他方向的比较,因为它不是对称的
温度(isinf(temp))=0;
d2=总和(温度);
d=d1+d2
结束
%%用于计算直方图分布的函数
函数[h,bin]=直方图(I,n,min,max)
I=I(:);
范围=最大值-最小值;
drdb=范围/双精度(n);%dr/db-每个箱子的范围变化
h=零(n,1);
箱子=零(n,1);
对于i=1:n
%注意:当说明说“整数舍入内”时,我离开
%这就像浮箱边缘一样,用来处理潜在的浮箱输入
%也就是说,输入是一个概率图像。
低=最小值+(i-1)*drdb;
高=min+i*drdb;

h(i)=sum((i>=low)。*(i=(n*drdb))。*(i无法计算不同大小向量的KL散度。必须调整直方图的大小,才能在这两种情况下获得相同的大小


因此,在调用函数histogram时,为所有输入设置一个恒定数量的存储单元。

如果我按h1=直方图(firstImg,255+10255)编辑,而不是按h1=直方图(firstImg,max)(firstImg(:)+1,0,max(firstImg(:));@user3336190编辑,则两幅图像的存储单元相同,所以是的。
%%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