Python 我如何使我的二维高斯适合我的图像
我试图将二维高斯分布拟合到图像中,以找到其中最亮点的位置。我的代码如下所示:Python 我如何使我的二维高斯适合我的图像,python,matplotlib,scipy,Python,Matplotlib,Scipy,我试图将二维高斯分布拟合到图像中,以找到其中最亮点的位置。我的代码如下所示: import numpy as np import astropy.io.fits as fits import os from astropy.stats import mad_std from scipy.optimize import curve_fit import matplotlib.pyplot as plt from matplotlib.patches import Circle from lmfit
import numpy as np
import astropy.io.fits as fits
import os
from astropy.stats import mad_std
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from lmfit.models import GaussianModel
from astropy.modeling import models, fitting
def gaussian(xycoor,x0, y0, sigma, amp):
'''This Function is the Gaussian Function'''
x, y = xycoor # x and y taken from fit function. Stars at 0, increases by 1, goes to length of axis
A = 1 / (2*sigma**2)
eq = amp*np.exp(-A*((x-x0)**2 + (y-y0)**2)) #Gaussian
return eq
def fit(image):
med = np.median(image)
image = image-med
image = image[0,0,:,:]
max_index = np.where(image >= np.max(image))
x0 = max_index[1] #Middle of X axis
y0 = max_index[0] #Middle of Y axis
x = np.arange(0, image.shape[1], 1) #Stars at 0, increases by 1, goes to length of axis
y = np.arange(0, image.shape[0], 1) #Stars at 0, increases by 1, goes to length of axis
xx, yy = np.meshgrid(x, y) #creates a grid to plot the function over
sigma = np.std(image) #The standard dev given in the Gaussian
amp = np.max(image) #amplitude
guess = [x0, y0, sigma, amp] #The initial guess for the gaussian fitting
low = [0,0,0,0] #start of data array
#Upper Bounds x0: length of x axis, y0: length of y axis, st dev: max value in image, amplitude: 2x the max value
upper = [image.shape[0], image.shape[1], np.max(image), np.max(image)*2]
bounds = [low, upper]
params, pcov = curve_fit(gaussian, (xx.ravel(), yy.ravel()), image.ravel(),p0 = guess, bounds = bounds) #optimal fit. Not sure what pcov is.
return params
def plotting(image, params):
fig, ax = plt.subplots()
ax.imshow(image)
ax.scatter(params[0], params[1],s = 10, c = 'red', marker = 'x')
circle = Circle((params[0], params[1]), params[2], facecolor = 'none', edgecolor = 'red', linewidth = 1)
ax.add_patch(circle)
plt.show()
data = fits.getdata('AzTECC100.fits') #read in file
med = np.median(data)
data = data - med
data = data[0,0,:,:]
parameters = fit(data)
#generates a gaussian based on the parameters given
plotting(data, parameters)
图像正在打印,代码没有给出错误,但配件不工作。只要在
x0
和y0
所在的位置放置一个x
。我的图像中的像素值非常小。最大值为0.0007,标准偏差为0.0001,x
和y
大几个数量级。所以我认为我的问题是,正因为如此,我的eq在任何地方都将为零,所以曲线拟合失败。我想知道是否有更好的方法来构造我的高斯曲线,以便它正确地绘制 我无法访问您的图像。相反,我生成了一些测试“图像”,如下所示:
y, x = np.indices((51,51))
x -= 25
y -= 25
data = 3 * np.exp(-0.7 * ((x+2)**2 + (y-1)**2))
此外,我还修改了用于打印的代码,以将圆的半径增加10:
circle = Circle((params[0], params[1]), 10 * params[2], ...)
我又注释了两行:
# image = image[0,0,:,:]
# data = data[0,0,:,:]
我得到的结果如所附图片所示,我认为这是合理的:
问题是否在于如何从FITS
文件中访问数据?(例如,image=image[0,0,:,:]
)数据是4D数组吗?为什么有4个索引
我还看到您在这里问了一个类似的问题:在这个问题中,您尝试只使用astropy.modeling
。我将研究这个问题
注意:您可以替换代码,例如
max_index = np.where(image >= np.max(image))
x0 = max_index[1] #Middle of X axis
y0 = max_index[0] #Middle of Y axis
与
y0, x0 = np.unravel_index(np.argmax(data), data.shape)