我尝试使用 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)
为了找到它们的交点,你可以构造一个符号函数,把每个分布对应的参数,写一个方程,然后用solve函数求解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