如何在MATLAB中绘制GMD分布的结果?

如何在MATLAB中绘制GMD分布的结果?,matlab,computer-vision,gaussian,mixture-model,Matlab,Computer Vision,Gaussian,Mixture Model,我有来自MATLAB中图像的数据,我想将其分解为高斯混合。图像的计数和二进制位置存储在256x2矩阵“X”中,fitgmdist(X,3)给出了三个高斯的参数。我想画出这些高斯函数并找出它们的交点 我也有点困惑,为什么我的混合模型中的参数如“mu”有两列。每个高斯分布不应该有一个对应的平均值吗?我试图通过使用pout.tif图像来解决这个问题: 图如下: 代码如下: I = imread('pout.tif'); imshow(I); [counts,binLocations] = imh

我有来自MATLAB中图像的数据,我想将其分解为高斯混合。图像的计数和二进制位置存储在256x2矩阵“X”中,fitgmdist(X,3)给出了三个高斯的参数。我想画出这些高斯函数并找出它们的交点


我也有点困惑,为什么我的混合模型中的参数如“mu”有两列。每个高斯分布不应该有一个对应的平均值吗?

我试图通过使用pout.tif图像来解决这个问题:

图如下:

代码如下:

I = imread('pout.tif');
imshow(I);

[counts,binLocations] = imhist(I);

subplot(3, 1, 1);
stem(binLocations, counts, 'MarkerSize', 1 );
xlim([50 200]);

X = I(:);
options = statset('MaxIter', 300); % default value is 100. Sometimes too few to converge

gm = gmdistribution.fit(double(X),3, 'Options', options);

subplot(3, 1, 2);
plot(binLocations, pdf(gm,binLocations));
xlim([50 200]);

subplot(3, 1, 3);
for j=1:3
    line(binLocations,gm.PComponents(j)*normpdf(binLocations,gm.mu(j),sqrt(gm.Sigma(j))),'color','r');
end
xlim([50 200]);
更新

以下是对代码的一些解释:

I = imread('pout.tif');
imshow(I);

[counts,binLocations] = imhist(I);

subplot(3, 1, 1);
stem(binLocations, counts, 'MarkerSize', 1 );
xlim([50 200]);

X = I(:);
options = statset('MaxIter', 300); % default value is 100. Sometimes too few to converge

gm = gmdistribution.fit(double(X),3, 'Options', options);

subplot(3, 1, 2);
plot(binLocations, pdf(gm,binLocations));
xlim([50 200]);

subplot(3, 1, 3);
for j=1:3
    line(binLocations,gm.PComponents(j)*normpdf(binLocations,gm.mu(j),sqrt(gm.Sigma(j))),'color','r');
end
xlim([50 200]);
第一个图显示直方图:图像中的颜色代码及其编号。这只是为了演示每种颜色的频率。稍后可以看到pdf图类似于直方图剖面图-这是一种很好的验证手段

第二个图显示了颜色代码的pdf。该模型通过近似将实际分布描述为k=3正态分布之和:

有时,该算法无法找到合适的近似值,结果只有2个分布。您需要重新启动代码。曲线下的面积等于1。为了保持此约束为真,将使用参数p缩放pdf组件

在第三个图中,描述了单个分布。它们是x的连续函数。你可以用符号来解x,它们看起来像:

f1 = 0.0193*exp(-0.0123*(x - 97.6)^2)

f2 = 0.0295*exp(-0.0581*(x - 83.0)^2)

f3 = 0.0125*exp(-0.00219*(x - 131.0)^2)
为了找到它们的交点,您可以构造一个符号函数,为每个分布输入相应的参数,编写一个方程,然后使用解算函数来解x的方程:

syms x m s p
f = symfun(p/(s*sqrt(2*pi))*exp(-((x-m)^2)/(2*(s^2))), [x, m, s, p]);

for j = 1:3
    ff(j) = f(x, gm.mu(j), sqrt(gm.Sigma(j)), gm.PComponents(j));
end

eqn = ff(1) == ff(2);
sols = solve(eqn, x);

display(vpa(sols, 3));
上述代码查找第一个和第二个PDF的交点。结果是:x=88.1,x=70.0

更新

使用上面的simbolic方程,您可以找到有关PDF的更多信息:

% At which value of x the 3rd pdf is equal to 0.01?
eqn = ff(3) == 0.01;
sols = solve(eqn, x);
display(vpa(sols, 3)); %returns 121.0 and 141.0

% What is the value of the 3rd pdf at x = 141?
sol = subs(ff(3), x, 141);
display(vpa(sol, 3)); %returns 0.0101

嗯,我也做过类似的事情。两个问题-为什么要将gmdistribution.fit应用于整个图像,而不仅仅是计数值?另外,为什么您必须创建GMM的pdf?根据文档,该函数处理数据本身,而不是其统计特性,不是吗?它为n×d矩阵X中的数据计算具有k个分量的高斯混合模型,其中n是观测数,d是数据的维数。在你的例子中,X是256x2,因此函数将第二维度解释为数据的另一个观察值。为什么你最初绘制gm的pdf,然后绘制一个pdf?还有一种方法可以使这些分布连续,而不是在离散点进行计算吗?我更新了我的答案。我还演示了如何找到交叉点。我希望能有帮助。你什么意思?每对分布的交点?