Speed up Gaussian Fitting of many points
9 views (last 30 days)
Show older comments
I have an image where the location of the peaks are identified with red spots

I have a vector of each x and y coordinate of the red spots. I then apply a Gaussia fit to each one (with a span of 6 pixels). My issue is whilst it all works great, I am looping through the coordinates and performing the fit. Its incredibly slow and I was wondering if there was a faster way.
e.g. xf= yf=
%Perform fit to all objects
for indx=1:l
....
%Now do Gaussian fit
[a(indx),b(indx),c(indx),d(indx),xpeak(indx),ypeak(indx)]=myGaussianFit(xdata,ydata, b0,c0);
% %Increase resolution of x data (by 30)
xdataFine=(linspace(xdata(1),xdata(end),100))';
% Create high res Gauss function
fitGaus = a(indx)*exp(-0.5*((xdataFine-b(indx))/c(indx)).^2)+d(indx);
fwhm(indx) = c(indx) * sqrt(log(256));
I(indx)=indx;
end
2 Comments
Thorsten
on 18 Jan 2016
Edited: Thorsten
on 18 Jan 2016
Before optimizing, it is helpful to use profile to identify where most of the time is spent.
help profile
And it seems that fitGaus is overwritten in each cycle and not used.
And if would be helpful to post your myGaussianFit and sample data to run the code.
Answers (1)
Thorsten
on 18 Jan 2016
Edited: Thorsten
on 18 Jan 2016
So the problem is the fitting. There are some alternative methods if you google "matlab fit 2D gaussian", like http://www.mathworks.com/matlabcentral/fileexchange/37087-fit-2d-gaussian-function-to-data . But think about it: Maybe you spend two hours to find a better function, that gives a 10s speedup. In this case you would have to call your function more than 720 times to compensate for the time it has taken to optimize the function.
4 Comments
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
