Need help with implementing a 2D elliptical Gaussian function
45 views (last 30 days)
Show older comments
I'm trying to implement a 2D gaussian function, which has an elliptical shape rather than circular. For example if I have a standard Gaussian fuction such as: f=a1*exp(-(((x-b1)/c1).^2+((y-b1)/c1).^2))
where [x,y]=meshgrid(xmin:spacing:xmax,ymin:spacing,yman); I can just f=a1*exp(-((r-b1)/c).^2) where r=sqrt(x^2+y^2);
however if my c1 is not the same, I will have an ellipse. I'm not sure how to implement this. Any help will be highly appreciated.
0 Comments
Answers (2)
David Young
on 5 Jul 2011
You can make an elliptical filter aligned with the axes by replacing the second occurrence of c1 by a different variable, say c2, and giving c1 and c2 different values. Here's some code that demonstrates what I mean; I've taken the opportunity to change b1 to b2 as well, so the centre of the filter can be moved to an arbitrary point:
% Set up mesh
xmin = -100;
xmax = 100;
ymin = -100;
ymax = 100;
spacing = 1;
xvals = xmin:spacing:xmax+spacing/2;
yvals = ymin:spacing:ymax+spacing/2;
[x,y] = meshgrid(xvals, yvals);
% parameters for the gaussian
a1 = 1;
b1 = 20;
b2 = 40;
c1 = 10;
c2 = 40;
% Compute the filter and display it
f=a1.*exp(-(((x-b1)./c1).^2+((y-b2)./c2).^2));
contour(x, y, f);
If you want the ellipse to be oriented in an arbitrary direction, you need to rotate the axes before the computation. This involves multiplying x and y by a rotation matrix. Please say if you also need help with this.
1 Comment
MJ HL
on 22 Aug 2017
Hello David, ... As you guessed I'm from that kind of people that need more help in rotating this filter :) . How should I do this? I need to rotate this filter in an arbitrary direction... thanks
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!