ndimage的center_of_mass计算高斯峰的位置

时间:2013-08-26 00:16:15

标签: python numpy scipy

我正在尝试使用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

如何在不使用最小二乘优化程序的情况下粗略计算出有噪声的二维高斯分布的峰值位置?

1 个答案:

答案 0 :(得分: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()

结果:

enter image description here