"Pstrang Rzekle" <pstrang_w_rzekle.re.move.th.is.@yahoo.com> wrote in message <geolj1$p5l$1@fred.mathworks.com>...
> I've searched the posts for hours. A number of people have submitted similar help requests, but the answers are confusing.
>
> How do you interact with the builtin functions lsqcurvefit or lsqnonlin when your data is a 2D matrix (image)?
>
> I want to curve fit a gaussian surface to my data. I have equations to compute the gaussian given 5 parameters and have written a function to return a 2D matrix given those 5 parameters:
>
> [newParameters,error] = lsqcurvefit(@gaussianfit,[x1 x2 x3 x4 x5],xdata,ydata);
>
> what do I put for xdata?
> what do I put for ydata? My image (2D matrx)?
>
> Kindly provide specific examples.
>
> Thank you.
.
Hi, You are right, it's not very well documented how to do this. You have to reshape your X,Y and F into vectors as following:
.
I=imread('filename.jpg');%assume gray scale, not RGB
[n,m]=size(I);%assumes that I is a nxm matrix
[X,Y]=meshgrid(1:n,1:m);%your xy coordinates
x(:,1)=X(:); % x= first column
x(:,2)=Y(:); % y= second column
f=I(:); % your data f(x,y) (in column vector)
% now define the function in terms of x
% where you use x(:,1)=X and x(:,2)=Y
fun = @(c,x) c(1)+c(2)*exp(((x(:,1)c(3))/c(4)).^2((x(:,2)c(5))/c(6)).^2)
% now solve with lsqcurvefit
options=optimset('TolX',1e6);
c0=[1 1 1 1 1 1]%startguess here
cc=lsqcurvefit(fun,c0,x,f,[],[],options)
Ifit=fun(cc,x); %your fitted gaussian in vector
Ifit=reshape(Ifit,[n m]);%gaussian reshaped as matrix
% plot with surf(X,Y,Ifit) ...
.
/Per
