我有一个整数列表。
intList = [96, 98, 120, 163, 158, 166, 201, 201, 159, 98, 93, 73, 77, 72]
这些数字代表14个像素的条带的灰度值,我想对分布拟合曲线并保存顶点的x位置。
上下文的缘故:我真的用(工作多列表的大)列表,包含像素的行的从图像的灰度值的每个列表。对于每行像素,我想绘制一条曲线到数据并将顶点的x位置附加到一个不断增加的列表中。每行像素都会有一些噪点,但只有一个且只有一个清晰的宽广峰值像素强度(下面的示例图片)
我有NumPy,SciPy,matplotlib和pillow,但是我对每个函数中发现的许多功能不是很了解。有人能指出我可能会这样做的模块或功能吗?
要拟合多项式(例如二次),请使用polyfit
:
from pylab import *
x = arange(len(intList))
p = polyfit(x, intList, 2)
a, b, c = p
x0 = -0.5*b/a # x coordinate of vertex
# plot
x = arange(len(intList))
plot(x, intList)
plot(x, polyval(p, x))
axvline(x0, color='r', ls='--')
为了适合更复杂的函数,例如高斯函数,可以使用curve_fit
:
from scipy.optimize import curve_fit
# define function to fit
ffunc = lambda x, a, x0, s: a*exp(-0.5*(x-x0)**2/s**2)
# fit with initial guess a=100, x0=5, s=2
p, _ = curve_fit(ffunc, x, intList, p0=[100,5,2])
x0 = p[1] # location of the mean
# plot
plot(x, intList)
plot(x, ffunc(x, *p))
axvline(x0, color='r', ls='--')
(尽管要拟合高斯,您最好直接计算分布的均值和方差。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句