fitting a 2d gaussian using lsqcurvefit

Below is the code i am using to fit a 2d gaussian to an image (40 *40), however whenever i run this my results a dont look quite right and b the lsqcurvefit keeps stopping prematurely any help is seriously appreciated.
ydata = y(:);
size(ydata)
[n, m] = size(y);
[X,Y]=meshgrid(1:n,1:m);
x = zeros(n*m, 2);
x(:, 1)= X(:);
x(:, 2)=Y(:);
c0 = [1, 20, 40];
options=optimset('TolX',1e-6);
fun = @(c, x) (1/(sqrt(2*pi*c(1)^2)))*exp(-((c(2)-x(:, 1)).^2 + (c(3) - x(:, 2)).^2)/2*(c(1)));
cc = lsqcurvefit(fun, c0, x, ydata, [], [], options);
Ifit = fun(cc, x);

Réponses (0)

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by