Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
2D Matrix Least Squares Fitting

Subject: 2D Matrix Least Squares Fitting

From: Pstrang Rzekle

Date: 4 Nov, 2008 05:12:02

Message: 1 of 4

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 built-in 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.

Subject: 2D Matrix Least Squares Fitting

From: Bruno Luong

Date: 4 Nov, 2008 06:18:02

Message: 2 of 4

"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 built-in functions lsqcurvefit or lsqnonlin when your data is a 2D matrix (image)?

Use lsqnonlin. In ythe use-defined function, pass the 2D coordinates (X) and 2D data (YData) as input arguments. Compute the model Y at X from parameters, then calculate the difference between Y and the Ydata, reshape the difference in long row vector.

function F = myfun(fitparam, X, Ydata) % X, YData are 2D

Y = mymodel(fitparam, X); % same dimension as X
D = Y - Ydata;
F = D(:);

return

end

% Call
option = somesetup;
x = lsqnonlin(myfun,param0,[],[],option,X,Ydata)

% Bruno

Subject: 2D Matrix Least Squares Fitting

From: Per Sundqvist

Date: 4 Nov, 2008 08:00:10

Message: 3 of 4

"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 built-in 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 x-y 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',1e-6);
c0=[1 1 1 1 1 1]%start-guess 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

Subject: 2D Matrix Least Squares Fitting

From: Pstrang Rzekle

Date: 4 Nov, 2008 16:15:20

Message: 4 of 4

"Per Sundqvist" <sunkan@fy.chalmers.se> wrote in message <geovea$9op$1@fred.mathworks.com>...
> 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 x-y 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',1e-6);
> c0=[1 1 1 1 1 1]%start-guess 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

Thank you very much! I have implemented something similar to your example. It *seems* to be working. I appreciate your prompt response and will post here if I run into any more problems.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us