Fit 2D Gaussian Function

63 次查看(过去 30 天)
Hello,
I am having trouble with the code found on this site: https://www.mathworks.com/matlabcentral/fileexchange/37087-fit-2d-gaussian-function-to-data I made the modifications below so that I could use it to run my data:
%%---------User Input---------------------
MdataSize = 256; % Size of nxn data matrix
% parameters are: [Amplitude, x0, sigmax, y0, sigmay, angel(in rad)]
x0 = [1,128,30,128,30,0]; %Inital guess parameters
xs = x0;
% % x = [2,2.2,7,3.4,4.5,+0.02*2*pi]; %centroid parameters
% % noise = 10; % noise in % of centroid peak value (x(1))
noise = 0;
InterpolationMethod = 'nearest'; % 'nearest','linear','spline','cubic'
FitForOrientation = 0; % 0: fit for orientation. 1: do not fit for orientation
%%---Generate centroid to be fitted--------------------------------------
% % xin = x;
% % noise = noise/100 * x(1);
[X,Y] = meshgrid(-MdataSize/2:MdataSize/2);
xdata = zeros(size(X,1),size(Y,2),2);
xdata(:,:,1) = X;
xdata(:,:,2) = Y;
% % [Xhr,Yhr] = meshgrid(linspace(-MdataSize/2,MdataSize/2,300)); % generate high res grid for plot
% % xdatahr = zeros(300,300,2);
% % xdatahr(:,:,1) = Xhr;
% % xdatahr(:,:,2) = Yhr;
%---Generate noisy centroid---------------------
% % Z = D2GaussFunctionRot(x,xdata);
% % Z = Z + noise*(rand(size(X,1),size(Y,2))-0.5);
Z = load('AVG PS of 17_12005_ROI1-4.txt');
My data (Z) is a 256x256 txt file with data at each point. I would like to fit a 2D gaussian, but am getting the following error message:
Error using lsqcurvefit (line 251)
Function value and YDATA sizes are not equal.
Error in Gaussian_Fit_PowerSpectrum (line 57)
[x,resnorm,residual,exitflag] = lsqcurvefit(@D2GaussFunctionRot,x0,xdata,Z,lb,ub);
I have the “D2GaussFunctionRot.m” and “D2GaussFunction.m” codes loaded in the directory and cannot figure out how to proceed. Any help would be appreciated. Thank you!

采纳的回答

Jordan Lui
Jordan Lui 2018-4-16
I noticed in the original code that while MdataSize is 50, the xdata matrix ends up being 51x51x2. This causes many other resulting variables to be "n+1" dimensioned. Are you sure your code modifications reflect this?

更多回答(1 个)

Trippy
Trippy 2022-7-25
You can fix it by doing the following
MdataSize = 255
The idea is function @D2GaussFunctionRot when the input is x0 and xdata, will give out an output of size nXm, which is the exact size of your image/ Z.
  1 个评论
Ham Man
Ham Man 2022-9-16
编辑:Ham Man 2022-9-16
I'm trying this code (Gero Nootz ) to fit Gaussian model to a 2d matrix. I'm getting this error:
Error using snls (line 47)
Objective function is returning undefined values at initial point. lsqcurvefit cannot continue.
Here is the data i'm using:
x0 = [1,0.2,0.5,0.2,0.5,0];
x = x0;
noise = 0;
InterpolationMethod = 'nearest';
FitForOrientation = 0;
xdata(:,:,1) =
NaN -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000
NaN -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000
NaN -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000
NaN -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000
NaN -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000
NaN -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000
NaN -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000
NaN -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000
NaN -1.0000 -0.6000 -0.2000 0.2000 0.6000 1.0000
xdata(:,:,2) =
NaN NaN NaN NaN NaN NaN NaN
-1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
-0.7143 -0.7143 -0.7143 -0.7143 -0.7143 -0.7143 -0.7143
-0.4286 -0.4286 -0.4286 -0.4286 -0.4286 -0.4286 -0.4286
-0.1429 -0.1429 -0.1429 -0.1429 -0.1429 -0.1429 -0.1429
0.1429 0.1429 0.1429 0.1429 0.1429 0.1429 0.1429
0.4286 0.4286 0.4286 0.4286 0.4286 0.4286 0.4286
0.7143 0.7143 0.7143 0.7143 0.7143 0.7143 0.7143
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
zdata =
NaN NaN NaN NaN NaN NaN NaN
NaN 0 123 388 125 0 NaN
NaN 30 614 787 570 53 NaN
NaN 133 765 897 744 180 NaN
NaN 199 832 891 737 270 NaN
NaN 143 810 903 782 210 NaN
NaN 27 590 857 659 63 NaN
NaN 0 100 442 176 0 NaN
NaN NaN NaN NaN NaN NaN NaN
[x,resnorm,residual,exitflag] = lsqcurvefit(@D2GaussFunction,x0,xdata,zdata);
xdata are meshgrid outputs from r/R of pipe cross section and zdata represents the particle distribution in pipe cross section. I want to fit a 2D Gaussian model to the zdata.
I really appreciate any help or thought. Thanks in advance!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Curve Fitting Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by