【发布时间】:2015-07-21 06:17:01
【问题描述】:
我有一些 2D 数据,特别是扫描的 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 是数据,ndarray 形状为(599,599))
(pars, xls, yxl = 来自星搜索的高斯拟合参数列表)
(makeGaussian 是在别处定义的函数)
【问题讨论】: