0

I am trying to curve fit a 2D multivariate normal distribution to an image. The approach here works, however I want to parametrize the result with a covariance matrix, like in scipy.stats.multivariate_normal.

Here is my code:

def mvnorm(data_tuple, mean1, mean2, cov1, cov2, cov3):
     (x, y) = data_tuple
     pos = numpy.dstack((x, y))
     mean = [mean1,mean2]
     cov = [[cov1,cov3],[cov3,cov2]]
     result = scipy.stats.multivariate_normal.pdf(pos, mean, cov)

     return result

 def fit_mvnorm(array, coords, initial_guess):
     shp = numpy.shape(array)
     step1 = abs(coords[0]-coords[1])/shp[1]
     step2 = abs(coords[2]-coords[3])/shp[1]
     x, y = numpy.mgrid[coords[0]:coords[1]:step1, coords[2]:coords[3]:step2]    
     popt,pcov = scipy.optimize.curve_fit(mvnorm, (x,y), array, p0=initial_guess)

     return popt, pcov

My data numpy.ndarray contains the image's brightness values (float) and has shape (548,548). coords is a list of the axis limit coordinates. When I run the function, I get the following error:

ValueError: object too deep for desired array
...
error: Result from function call is not a proper array of floats.

Do you have any idea on what to try?

  • Does ravel() to make the 2d array into a 1d array as suggested in the link as you posted work for you? – aquaplane May 10 '22 at 13:24
  • I've tried that, it gives a `ValueError: operands could not be broadcast together with shapes (548,548) (300304,)` before the other error would've happened – gopromaster May 10 '22 at 18:56

0 Answers0