From 8f0ab5b68b7d3a8e1ee8f01393db85aac441b9df Mon Sep 17 00:00:00 2001 From: Eugeniy Mikhailov Date: Mon, 3 Nov 2014 11:37:23 -0500 Subject: added Gauss2DRotFit.m this file was obtained at http://www.mathworks.com/matlabcentral/fileexchange/39448-2d-rotated-gaussian-fit --- Gauss2DRotFit.m | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Gauss2DRotFit.m diff --git a/Gauss2DRotFit.m b/Gauss2DRotFit.m new file mode 100644 index 0000000..a669723 --- /dev/null +++ b/Gauss2DRotFit.m @@ -0,0 +1,96 @@ +function [fitresult, gof] = gauss2DRotFit(im,varargin) +%gauss2DRotFit(im,fop,title) +% Create a fit. +% Input : +% +% im : input image of 2D gaussian +% fop : fitoptions - a structure with 3 fields : Lower,StartPoint and +% Upper. Each of the fields is an array of 7 values : +% offset +% amplitude of the 2D gaussian +% centroid X of the 2D gaussian +% centroid Y of the 2D gaussian +% angle of rotation for the 2D gaussian +% width X of the 2D gaussian +% width Y of the 2D gaussian +% +% Output: +% fitresult : a fit object representing the fit. +% gof : structure with goodness-of fit info. +% + +if nargin==0 + errordlg('gauss2DRotFit has to be called with at least 1 argument'); + return; +end +[sx,sy,sz]=size(im); +if sz==3 + im=rgb2gray(im); +end +[X,Y]=meshgrid(1:sy,1:sx); + + +mystr='Untitled fit'; + +[xData, yData, zData] = prepareSurfaceData( X,Y, im ); + +% Set up fittype and options. +ft = fittype( 'a + b*exp(-(((x-c1)*cosd(t1)+(y-c2)*sind(t1))/w1)^2-((-(x-c1)*sind(t1)+(y-c2)*cosd(t1))/w2)^2)', 'independent', {'x', 'y'}, 'dependent', 'z' ); + + +opts = fitoptions( ft ); +if numel(varargin)>0 + s=varargin{1}; + if isfield(s,'Lower') + opts.Lower=s.Lower; + end + if isfield(s,'StartPoint') + opts.StartPoint=s.StartPoint; + end + if isfield(s,'Upper') + opts.Upper=s.Upper; + end +end +if numel(varargin)==2 + mystr=varargin{2}; +end + +opts.Display = 'Off'; + + +% Fit model to data. +[fitresult, gof,fout] = fit( [xData, yData], zData, ft, opts ); + + +opts +gof +fout +% Create a figure for the plots. +figure( 1); +set(gcf,'Name', '2D Gaussian fit 1' ); +set(gcf,'Position',[10,10,900,600]); + +% Plot fit with data. + +h = plot( fitresult, [xData, yData], zData); +legend( h, mystr, 'im vs. X, Y', 'Location', 'NorthEast' ); +% Label axes +xlabel( 'X' ); +ylabel( 'Y' ); +zlabel( 'im' ); +grid on +colormap(jet); + +% Plot residuals. +figure(2); +set(gcf,'Name', 'Gaussian fit Residuals'); +set(gcf,'Position',[100,100,900,600]); +h = plot( fitresult, [xData, yData], zData, 'Style', 'Residual'); +legend( h, [mystr ' - residuals'], 'Location', 'NorthEast' ); +% Label axes +xlabel( 'X' ); +ylabel( 'Y' ); +zlabel( 'im' ); +grid on +figure(1); + -- cgit v1.2.3