【问题标题】:Fitting a star light with a 3D Gaussian用 3D 高斯拟合星光
【发布时间】:2019-07-07 12:48:30
【问题描述】:

我有一张星图,比如说 15 x 15 像素。 星光可以建模为 3D 高斯,这正是我想要做的。

我使用的函数是 f(x, y) = A·exp(-[(x-xc)^2/2σ^2+(y-yc)^2/2σ^2]) 其中:

  • xc, yc 估计的星心坐标
  • (xc, yc) 处的像素值

我实现的算法是 Levenberg-Marquardt。

我在 L-M 算法中陷入了以下问题 - 在每次迭代中 - 我需要计算一个向量,其中包含我的样本和 f(x, y) 之间的差异。

如果我使用像素位置(行、列)作为 (x, y) 计算高斯 f(x, y),当然除了 xc 和 yc 周围之外,它到处都是 0。这是不正确的。

我正在考虑进行标准化,让 x 和 y 在 0 和 1 之间变化(当然也可以标准化 xc 和 yc)。

这是一个好方法吗?

谢谢。 干杯

【问题讨论】:

  • “星光可以建模为 3D 高斯”——真的吗?你想达到什么目的?此外,尝试为 sigma 使用更大的值,例如图片宽度/高度的一半
  • 我的目标是找出最适合星光的方法,以亚像素精度计算其 FWHM(即 sigma * 2.355),
  • 我在这里没有看到任何编程编码问题,更多的是数学/物理建模问题。我的观点是你的问题是题外话。也许检查其他相关的 Stack Exchange 网站?
  • 好的,抱歉。我去看看。谢谢

标签: c# curve-fitting gaussian


【解决方案1】:

这是一个在curve_fit() 中使用Levenverg-Marquardt 的scipy 实现的图形3D 曲面拟合器示例,该示例应该很容易适应您的需求。

import numpy, scipy, scipy.optimize
import matplotlib
from mpl_toolkits.mplot3d import  Axes3D
from matplotlib import cm # to colormap 3D surfaces from blue to red
import matplotlib.pyplot as plt

graphWidth = 800 # units are pixels
graphHeight = 600 # units are pixels


def func(data, a, b, c):
    X = data[0]
    Y = data[1]
    return a*X + b*Y + c


def SurfacePlot(func, data, fittedParameters):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

    matplotlib.pyplot.grid(True)
    axes = Axes3D(f)

    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    xModel = numpy.linspace(min(x_data), max(x_data), 20)
    yModel = numpy.linspace(min(y_data), max(y_data), 20)
    X, Y = numpy.meshgrid(xModel, yModel)

    Z = func(numpy.array([X, Y]), *fittedParameters)

    axes.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=1, antialiased=True)

    axes.scatter(x_data, y_data, z_data) # show data along with plotted surface

    axes.set_title('Surface Plot (click-drag with mouse)') # add a title for surface plot
    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label
    axes.set_zlabel('Z Data') # Z axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


def ScatterPlot(data):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)

    matplotlib.pyplot.grid(True)
    axes = Axes3D(f)
    x_data = data[0]
    y_data = data[1]
    z_data = data[2]

    axes.scatter(x_data, y_data, z_data)

    axes.set_title('Scatter Plot (click-drag with mouse)')
    axes.set_xlabel('X Data')
    axes.set_ylabel('Y Data')
    axes.set_zlabel('Z Data')

    plt.show()
    plt.close('all') # clean up after using pyplot or else thaere can be memory and process problems


if __name__ == "__main__":
    xData = numpy.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
    yData = numpy.array([11.0, 12.1, 13.0, 14.1, 15.0, 16.1, 17.0, 18.1, 90.0])
    zData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.0, 9.9])

    data = [xData, yData, zData]

    initialParameters = [1.0, 1.0, 1.0] # these are the same as scipy default values in this example

    # here a non-linear surface fit is made with scipy's curve_fit()
    fittedParameters, pcov = scipy.optimize.curve_fit(func, [xData, yData], zData, p0 = initialParameters)

    ScatterPlot(data)
    SurfacePlot(func, data, fittedParameters)

    print('fitted prameters', fittedParameters)

    modelPredictions = func(data, *fittedParameters) 

    absError = modelPredictions - zData

    SE = numpy.square(absError) # squared errors
    MSE = numpy.mean(SE) # mean squared errors
    RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
    Rsquared = 1.0 - (numpy.var(absError) / numpy.var(zData))
    print('RMSE:', RMSE)
    print('R-squared:', Rsquared)

【讨论】:

    猜你喜欢
    • 2019-01-17
    • 1970-01-01
    • 2017-06-14
    • 2018-11-09
    • 2016-11-10
    • 1970-01-01
    • 2017-08-30
    • 2012-07-15
    • 2015-07-26
    相关资源
    最近更新 更多