使用ndimage的center_of_mass函数来计算高斯峰的位置

4
我试图使用ndimage.measurements.center_of_mass计算高斯2D分布峰值的位置,但发现质心与峰值中心不一致。
import numpy as np
from scipy import ndimage
from scipy import stats
import matplotlib.pyplot as plt

x = np.linspace(-1,1,100)
xv, yv = np.meshgrid(x, x)
r = np.sqrt((xv-0.2)**2 + (yv)**2)
norm2d = stats.norm.pdf(r)
com = ndimage.measurements.center_of_mass(norm2d)
plt.imshow(norm2d, origin="lower")
plt.scatter(*com[::-1])
plt.show()

enter image description here

我如何在不使用最小二乘优化例程的情况下,粗略地计算出一个带噪声的2D高斯分布的峰值位置?


2
ndimage.measurements.maximum_position? - Paulo Almeida
@PauloAlmeida 因为分布不是很好地采样,所以我正在寻找一些计算峰值中心而不是图像最大位置的方法(索引应该是浮点数值)。 - gypaetus
1
你可以通过一些平滑滤波器来处理图像,以消除噪声,然后在平滑后的图像中寻找最大值。 - Jaime
@Jaime 谢谢,我认为这也可以,虽然我需要重新采样到更高分辨率数组。 - gypaetus
1个回答

3
如果您使用前 xx% 的像素,就能得到正确的结果:
hist, bins = np.histogram(norm2d.ravel(), normed=True, bins=100)
threshold = bins[np.cumsum(hist) * (bins[1] - bins[0]) > 0.8][0]
mnorm2d = np.ma.masked_less(norm2d,threshold)
com = ndimage.measurements.center_of_mass(mnorm2d)
plt.imshow(norm2d, origin="lower")
plt.scatter(*com[::-1])
plt.show()

结果:

这里输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接