且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用高斯核估计向量的pdf

更新时间:2023-02-27 10:42:00

您有两个问题:

  1. 蓝色和红色图之间的1单位位移.
  2. 蓝色的穗状花序比红色的穗状花序宽且高.

如何解决每个问题:

  1. 这是由于数据范围 0,...,255和索引间隔 1,...,256之间可能引起的混淆引起的.由于您的数据代表一个8位图像,因此值应为0,...,255(而不是1,...,256).您绘制的水平轴应为0,...,255. for行中的i变量也是如此.然后,由于Matlab索引从1开始,因此在索引pdf_est时应使用i+1.

  1. This is caused by a possible confusion between the data range 0,...,255 and the indexing interval 1,...,256. Since your data represents an 8-bit image, values should be 0,...,255 (not 1,...,256). Your plotted horizontal axis should then be 0,...,255. Same goes for the i variable in the for line. And then, since Matlab indexing starts at 1, you should use i+1 when indexing pdf_est.

这是正常行为.您假设内核中的单位方差.要查看更高的蓝色尖峰,可以减小sigma以使内核更窄和更高.但是您永远不会获得与数据完全相同的高度(必要的sigma取决于数据).

This is normal behaviour. You are assuming unit variance in your kernel. To see taller blue spikes you could reduce sigma to make the kernel narrower and taller. But you will never get the exact same height as your data (the necessary sigma would depend on your data).

实际上,您在高度和宽度之间进行了权衡,由sigma控制.但是重要的是,对于任何sigma区域均保持不变.因此,我建议绘制CDF(区域)而不是pdf(区域密度).为此,请绘制累积直方图和pdf(使用 cumsum ).

Actually, you have a tradeoff between height and width, controlled by sigma. But the important thing is that the area remains the same for any sigma. So I suggest plotting the CDF (area) instead of the pdf (area densisty). To do that, plot the accumulated histogram and pdf (using cumsum).

根据1修改的代码

function pdf_est=KDE()
close all;
%%Random values of 20 pixels, range=[1 256]
data=randi([1 256],1,20)-1; %// changed: "-1"

%% Estimate histogram%%%%% 
pdf_est=zeros(1,256);
z=256;

for i=0:z-1 %// changed_ subtracted 1 
    for j=1:length(data)
        pdf_est(i+1)=pdf_est(i+1)+Gaussian(i-data(j)); %// changed: "+1" (twice)
    end
end
%% Plot real histogram 1 to 256; binsize=1;
hold on
plot(0:255, imhist(uint8(data))./length(data),'r'); %// changed: explicit x axis
%% Plot histogram estimation
plot(0:255, pdf_est./length(data),'b'); %// changed: explicit x axis
hold off
function K=Gaussian(x)
   sigma=1; %// change? Set as desired
   K=1./(sqrt(2*pi)*sigma)*exp(-x^2./(2*sigma^2));

根据1和2修改的代码

function pdf_est=KDE()
close all;
%%Random values of 20 pixels, range=[1 256]
data=randi([1 256],1,20)-1; %// changed: "-1"

%% Estimate histogram%%%%% 
pdf_est=zeros(1,256);
z=256;

for i=0:z-1 %// changed: subtracted 1 
    for j=1:length(data)
        pdf_est(i+1)=pdf_est(i+1)+Gaussian(i-data(j)); %// changed: "+1" (twice)
    end
end
%% Plot real histogram 1 to 256; binsize=1;
hold on
plot(0:255, cumsum(imhist(uint8(data))./length(data)),'r'); %// changed: explicit x axis
                                                            %// changed: cumsum
%% Plot histogram estimation
plot(0:255, cumsum(pdf_est./length(data)),'b'); %// changed: explicit x axis
                                                %// changed: cumsum
hold off
function K=Gaussian(x)
   sigma=1; %// change? Set as desired
   K=1./(sqrt(2*pi)*sigma)*exp(-x^2./(2*sigma^2));