2

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

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

4

1 回答 1

6

我试图通过使用 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]);

更新

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

第一个图显示了直方图:图像中的颜色代码及其数字。只是为了演示每种颜色的频率。稍后可以看出,pdf 图类似于直方图轮廓 - 一种很好的验证手段。

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

在此处输入图像描述

有时算法找不到合适的近似值,最终只有 2 个分布。您需要再次重新启动代码。曲线下的面积等于 1。为了保持这个约束为真,pdf 分量用参数 p 缩放。

在第三幅图中,描绘了单一分布。它们是 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。

更新

使用上面的符号方程,您可以找到有关您的 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
于 2015-12-29T21:08:10.547 回答