我试图拟合二维高斯到一些灰度图像数据,这是由一个二维数组给出的。 lmfit库实现了一个易于使用的Model类,它应该能够做到这一点。 不幸的是,文档(http://lmfit.github.io/lmfit-py/model.html)只提供了一维拟合的例子。对于我的情况,我简单地用两个独立变量构造lmfit模型。Python lmfit:拟合2D模型
下面的代码似乎对我有效,但会导致scipy抛出“minpack.error:函数调用的结果不是一个适当的浮点数组。
Tom总结:如何将2D(x1,x2) - >(y)数据输入到lmfit模型。
以下是我的方法: 所有内容都包含在GaussianFit2D类中,但以下是重要部分: 这就是高斯函数。文档说有关用户定义的函数
Of course, the model function will have to return an array that will be the same size as the data being modeled. Generally this is handled by also specifying one or more independent variables.
我真的不明白这是什么意思应该,因为对于给定的值X1,X2的唯一合理的结果是标量值。
def _function(self, x1, x2, amp, wid, cen1, cen2):
val = (amp/(np.sqrt(2*np.pi)*wid)) * np.exp(-((x1-cen1)**2+(x2-cen2)**2)/(2*wid**2))
return val
这里生成的模型:
def _buildModel(self, **kwargs):
model = lmfit.Model(self._function, independent_vars=["x1", "x2"],
param_names=["amp", "wid", "cen1", "cen2"])
return model
这是获取数据的功能,构建模型,而params,并呼吁lmfit符合():终于在这里
def fit(self, data, freeX, **kwargs):
freeX = np.asarray(freeX, float)
model = self._buildModel(**kwargs)
params = self._generateModelParams(model, **kwargs)
model.fit(data, x1=freeX[0], x2=freeX[1], params=params)
ANF这个适合的功能被称为:
data = np.asarray(img, float)
gaussFit = GaussianFit2D()
x1 = np.arange(len(img[0, :]))
x2 = np.arange(len(img[:, 0]))
fit = gaussFit.fit(data, [x1, x2])
你需要'lmfit',或其他工具('curve_fit'或SciPy的'leastsq')也未尝不可? – Evert 2014-10-22 07:57:01
lmfit很迷人,因为它很容易为参数,约束等添加初始值......如果我不管理它,那么scipy可能是第二次尝试。 – m0e 2014-10-23 06:52:19