python - 在 python 中将 2D 高斯总和拟合到 2d 数据?

标签 python scipy

我有一些二维数据,特别是扫描的 X 光片。这些具有重叠点源曝光的测量值。数据示例:http://i.stack.imgur.com/oawlU.png

我想通过对数据拟合二维高斯总和来找到峰值位置。我已经尝试了其他几种方法并取得了一些成功,包括定位全局最大值的“星形搜索”方法,适合高斯并减去它。循环这个可以很好地找到所有的峰,但它不稳定并且不是很准确。我想使用星号搜索输出作为拟合的第一个猜测,但我在实现 scipy.optimize.curve_fit 时遇到了麻烦。

我创建了一个函数 twoD_envelope,它创建了从星搜索中找到的所有高斯分布的二维包络。这会产生以下输出:http://i.stack.imgur.com/4KnpG.png

我的印象是我可以使用它作为 curve_fit 中的初始猜测,但是我得到以下 TypeError:

TypeError: twoD_envelope() takes 4 positional arguments but 358802 were given

358802 比数据大小多 1,这是一个巨大的线索,但我无法弄清楚问题出在哪里!我是一名具有“实用”编码知识的物理学家,因此非常感谢任何输入。

代码如下。

def twoD_envelope(npoints, xls, yls, pars):

    envl = copy.copy(sq_image)
    envl[:] = 0


    for n in range(0,npoints):
        height, cx, cy, width_x, width_y = pars[n]
        FWHM = 0.5*(width_x+width_y)
        g=makeGaussian(shape(sq_image)[0],fwhm=FWHM,center=[cx+xls[n],cy+yls[n]])
        envl = envl + g

    return envl.ravel()

# Create x and y indices
x = np.linspace(0, np.size(sq_image[0]), np.size(sq_image[0])+1)
y = np.linspace(0, np.size(sq_image[1]), np.size(sq_image[1])+1)
x, y = np.meshgrid(x, y)
coords = [x,y]

data = sq_image

initial_guess = twoD_envelope(9,xls,yls,pars)

pars_opt, pars_cov = opt.curve_fit(twoD_envelope, coords, data, p0=initial_guess)

(sq_image 是数据,一个形状为 (599,599)ndarray)

(pars, xls, yxl = 来自星搜索的高斯拟合参数列表)

(makeGaussian是在别处定义的函数)

最佳答案

我认为您的错误消息是您传递给 curve_fit 的函数的结果,它看起来不像是正确的形式。这可能会导致 data 数组被解释为 vars,从而导致您看到的 TypeError(无法运行代码很难判断) .根据文档,传递给 curve_fit 的函数应该,

take the independent variable as the first argument and the parameters to fit as separate remaining arguments.

您可能还需要在 initial_guess 之前使用星号以将其作为元组传递,例如

pars_opt, pars_cov = opt.curve_fit(twoD_envelope, coords, data, p0=*initial_guess)

要拟合单个 2D 高斯分布,其中 p0 大约有 7 个参数,有一个很好的答案可能会有所帮助:Fitting a 2D Gaussian function using scipy.optimize.curve_fit - ValueError and minpack.error .

您的问题的一个可能解决方案可能是遍历您的 2D sq_image 并在本地使用单个 2D 高斯拟合以及来自星搜索的每个初始参数...

编辑:适合高斯的代码。

import scipy.optimize as opt
import numpy as np
import pylab as plt
import matplotlib.cm as cm
import Image


def twoD_Gaussian((x, y), amplitude, xo, yo, sigma_x, sigma_y, theta):
    xo = float(xo)
    yo = float(yo)    
    a = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)
    b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)
    c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)
    g = amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo) 
                            + c*((y-yo)**2)))
    return g.ravel()


# Create x and y indices
I = Image.open('./30155885.png')
p = np.asarray(I).astype('float')
w,h = I.size
x, y = np.mgrid[0:h, 0:w]

#Use only one channel of image
p = p[:,:,0]

#Fit 2D Gaussian
initial_guess = (3,10,10,20,40,0)
popt, pcov = opt.curve_fit(twoD_Gaussian, (x, y), p.ravel(), p0=initial_guess)
data_fitted = twoD_Gaussian((x, y), *popt)


fig, ax = plt.subplots(1, 1)
cb = ax.imshow(p, cmap=plt.cm.jet, origin='bottom',
    extent=(x.min(), x.max(), y.min(), y.max()))
ax.contour(x, y, data_fitted.reshape(x.shape[0], y.shape[1]), 8, colors='w')


plt.colorbar(cb)
plt.show()

图像 30155885 在哪里,

请注意,只使用了图像数据的一个 channel (您应该将数据数组替换为 sq_image 中的一个)。这导致,

enter image description here

关于python - 在 python 中将 2D 高斯总和拟合到 2d 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30155885/

相关文章:

python - 模拟打开(文件)中的行 :

python - 如何在 Python 中读取大文本文件?

python - 在 if 中定义变量

amazon-web-services - aws lambda 上的 Sklearn

python - Python 中具有 sigma 裁剪的二维数组的中值

python - scipy.optimize.fmin_slsqp 的使用

python - 使用python从指数分布和模型生成随机数

python - 在 NumPy 中向量化向量矩阵乘法

python - 一种在 pythons scipy 模块的 coo_matrix 中获取非零值计数的方法?

python - Django:显示名字和姓氏