gpt4 book ai didi

python - ndimage 的 center_of_mass 用于计算高斯峰的位置

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:17 27 4
gpt4 key购买 nike

我尝试使用 ndimage.measurements.center_of_mass 计算高斯二维分布的峰值位置,发现质心从峰值中心偏移:

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

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

最佳答案

如果使用顶部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

关于python - ndimage 的 center_of_mass 用于计算高斯峰的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18435003/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com