简体   繁体   English

如何获得正确的2D高斯拟合参数以适应带有噪点的图像

[英]How to get correct parameters for 2D Gaussian fit to an image with noise

I have multiple images of objects where the object of interest (one per image) is near the center of said image. 我有多个对象的图像,其中感兴趣的对象(每个图像一个)位于所述图像的中心附近。 However, it's not necessarily the brightest source in the image, because some of the images also include sources that I am not interested in, but happen to be brighter/more intense. 但是,它不一定是图像中最亮的光源,因为某些图像还包含我不感兴趣的光源,但它们恰好更亮/更强烈。 There is also usually a considerable amount of noise. 通常也有相当数量的噪声。

I would like to fit Gaussians to these 2D numpy image arrays, but am unsure how to effectively do so with these bright sources I don't want. 我想让高斯人适合这些2D numpy图像阵列,但是不确定如何使用这些我不需要的明亮光源有效地做到这一点。 In the end, I'll be stacking all the images (median) with the source of interest centered, so these other bright sources will just disappear. 最后,我将所有图像(中值)以感兴趣的源为中心进行堆叠,因此这些其他明亮的源将消失。 Below is my attempted code so far, where data is a list of multiple 2D arrays (images). 以下是到目前为止我尝试过的代码,其中data是多个2D数组(图像)的列表。

import numpy as np
import scipy

def Gaussian2D(x, y, x_0, y_0, theta, sigma_x, sigma_y, amp):
    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)
    exp_term = a * (x-x_0)**2
    exp_term += 2*b*(x-x_0)*(y-y_0)
    exp_term += c * (y-y_0)**2
    return amp * np.exp(-exp_term)

def GaussianFit(data):
    for data_set in data:
        y_0, x_0 = (np.shape(data_set)[0]//2, np.shape(data_set)[1]//2)
        sigma_x, sigma_y = np.std(data_set, axis=1), np.std(data_set, axis=0)
        fit = scipy.optimize.curve_fit(Gaussian2D(x, y, x_0, y_0, 0, sigma_x, sigma_y, amp), data_set)
    return fit

I've never done function fitting in a code, so I feel pretty lost. 我从未在代码中进行函数拟合,因此感到非常迷茫。 My specific questions are: 我的具体问题是:

  1. How can I define my parameters correctly? 如何正确定义参数? Do I need to flatten by array to get the sigma parameters? 我是否需要按数组展平以获取sigma参数? Also, I noticed in some example code that people made x and y arrays with linspace , so I'm not sure if I need to do that, and I'm also not sure what to put for the amplitude. 另外,在一些示例代码中,我注意到人们使用linspace制作了xy数组,所以我不确定是否需要这样做,并且我也不知道要为振幅加上什么。

  2. How would I handle the fact that I have multiple bright sources per image but only want to fit for the one closest to the center? 我该如何处理一个事实,即每个图像有多个明亮光源,但只想适合最靠近中心的一个光源? Can I somehow specify to look near the center of the image? 我可以指定以某种方式靠近图像中心吗?

  3. I will also need the coordinates of the center source after fitting. 拟合后,我还将需要中心源的坐标。 How can I do this will ensuring it doesn't give me coordinates of other sources instead? 我该怎么做才能确保它不会给我其他来源的坐标?

Any other help or advice is also appreciated. 任何其他帮助或建议,也表示赞赏。 Thank you! 谢谢!

You can do this using a Gaussian Mixture Model . 您可以使用高斯混合模型进行此操作。 I don't think there is a function in SciPy, but there is one in scikit-learn 我认为SciPy中没有功能,但是scikit-learn中有一个功能

Here is a tutorial on this. 是有关此的教程。

(from my answer to this question) (根据我对这个问题的回答)

Then just remove the unwanted distribution from the image and fit to it. 然后只需从图像中删除不需要的分布并适合它即可。

Or there is skimage's blob detection . 或者有skimage的斑点检测

On fitting a 2d Gaussian, read here . 关于拟合2d高斯曲线,请阅读此处 To use this you have to flatten the array as scipy's curve_fit only takes a 1d array. 要使用它,您必须将数组展平,因为scipy的curve_fit仅需要一个1d数组。 But it works fine. 但这很好。

Another approach is described here . 这里介绍另一种方法。 A fit function with already three Gaussians in it is used. 使用已经具有三个高斯函数的拟合函数。 This would work if you know that there are always three (or in your case two) peaks on the image. 如果您知道图像上始终有三个(或者在您的情况下为两个)峰,这将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM