python - 如何使我的 2D 高斯适合我的图像
问题描述
我正在尝试将 2D 高斯拟合到图像中以找到其中最亮点的位置。我的代码如下所示:
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)
图像正在绘制,代码没有给出错误,但拟合不起作用。它只是把一个和x
在哪里。我图像中的像素值非常小。最大值为 0.0007,std dev 为 0.0001,and大几个数量级。所以我相信我的问题是,正因为如此,我的 eq 到处都是零,所以失败了。我想知道是否有更好的方法来构建我的高斯,以便正确绘制? x0
y0
x
y
curve_fit
解决方案
我无权访问您的图像。相反,我生成了一些测试“图像”,如下所示:
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.model 2DGaussian 问题,您尝试使用 just 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)
推荐阅读
- php - GravityForms 基于 ACF 帖子类型字段预填充字段值 - 动态
- python - NLTK 和 Pandas - 将同义词添加到列表中
- xml - 如何使用 Xpath 定位子节点不包含属性的父节点?
- python - 如何使用加密密钥建立安全的 ldap 连接?
- javascript - Jquery 加载返回 [object object]
- ios - WKWebView 进程在 CI 中终止但不在本地
- lit-element - 有没有办法让 lit-html 模板中的标签动态化?
- c - 计算文件中的字母“s”和“S”
- python - 使用 tf.data.Dataset 将数据提供给具有多个输入的 Keras 模型
- api - Google Cloud Vision API“错误的图像数据”错误