1

I have a noisy unnormal 2D gaussian array with unknown location, as well as unknown mean and variance. How can I compute the gaussian's center location and its width and height in a certain confidence level?

For sample case like a perfect normal 2D gaussian:

def gauss2d(shape=(200,200),sigma=20):
    m,n = [(ss-1.)/2. for ss in shape]
    y,x = np.ogrid[-m:m+1,-n:n+1]
    h = np.exp( -(x*x + y*y) / (2.*sigma*sigma) )
    h[ h < np.finfo(h.dtype).eps*h.max() ] = 0
    h = h / h.max()
    return h[:150, :150]

skimage.io.imshow(gauss2d()) # it looks like the following

gauss2D skimage result

I can use arg.max() of each pixel to get the center location, but for a noisy case I may need to use arg.max() of each 3x3 size pixels to robustly compute the center. How can I achieve that with python?

I have no idea how to compute the width and height. I am also thinking use some density-based clustering method to give the location info, such as scikit-learn's DBSCAN, but do not know how to do it practically.

Many thanks in advance!

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
Xer
  • 493
  • 1
  • 6
  • 17
  • 2
    take a look at [this](http://stackoverflow.com/questions/21566379/fitting-a-2d-gaussian-function-using-scipy-optimize-curve-fit-valueerror-and-m) – Aaron Oct 28 '16 at 13:52
  • that's exactly what I need! Thank you Aaron! – Xer Oct 28 '16 at 14:18

1 Answers1

1

This is a classical fitting problem and can be solved with e.g. the scipy.optimize-module. First we define the fitting function, i.e. the 2d gaussian:

def gauss2dFunc(xy, xo, yo, sigma):
        x = xy[0]
        y = xy[1]
        return np.exp( -((x-xo)*(x-xo) + (y-yo)*(y-yo)) / (2.*sigma*sigma) ).ravel()

This function needs a tuple of the current coordinates (xy), the center coordinates of the gaussian and the sigma.

Then the meshgrid for the x,y-coordinates is defined and we make a initial guess about the 3 parameters.

xvec = np.array(range(150))
yvec = np.array(range(150))
X,Y = np.meshgrid(xvec,yvec)
initial_guess = [75,75,10] #xo,yo,sigma

Next we call the fitting function from the optimize module with the function pointer, the grid, the data from your function and the initial guesses:

popt, pcov = opt.curve_fit(gauss2dFunc, (X, Y), gauss2d().ravel() , p0=initial_guess)

popt contains now the three parameters in a list.

This answer is inspired by Fitting a 2D Gaussian function using scipy.optimize.curve_fit - ValueError and minpack.error

Community
  • 1
  • 1
serbap
  • 93
  • 3