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:
How to rotate a 2D function

Subject: How to rotate a 2D function

From: kees de Kapper

Date: 6 Mar, 2009 08:28:02

Message: 1 of 3

Dear all,

I have, probably, a stupid question with hopefully an easy answer.

I have got a 2D function, say a simple Gaussian:

x = 1:1:512;
y = 1:1:512;
z = (exp(-((x-256)/20).^2)'*(exp(-((y-256)/40).^2);
%imagesc(z); colormap(gray);

This gives a Gaussian with different length at the axes.

Now I want to rotate this function around its center. So, I thought to transform the coordinates, thus:

x0 = 256; y0 = 256;
x2 = cos(Theta)*(x-x0)-sin(Theta)*(y-y0)+x0;
y2 = sin(Theta)*(x-x0)+cos(Theta)*(x-x0)+y0;

Then apply to the function:
z2 = (exp(-((x2-256)/20).^2)'*(exp(-((y2-256)/40).^2);
%imagesc(z); colormap(gray);

However, this will not do the job. For example, if Theta = pi/2 then x2 = y and y2 = x, which represents the same values. Therefore no rotation is visible.

What goes wrong?
For computational speed and discretization errors I want to avoid "imrotate".

Thanks for your help.

Kees

Subject: How to rotate a 2D function

From: nor ki

Date: 6 Mar, 2009 09:29:01

Message: 2 of 3

"kees de Kapper" <kees_de_kapper@hotmail.com> wrote in message <goqmqi$7m5$1@fred.mathworks.com>...
> Dear all,
>
> I have, probably, a stupid question with hopefully an easy answer.
>
> I have got a 2D function, say a simple Gaussian:
>
> x = 1:1:512;
> y = 1:1:512;
> z = (exp(-((x-256)/20).^2)'*(exp(-((y-256)/40).^2);
> %imagesc(z); colormap(gray);
>
> This gives a Gaussian with different length at the axes.
>
> Now I want to rotate this function around its center. So, I thought to transform the coordinates, thus:
>
> x0 = 256; y0 = 256;
> x2 = cos(Theta)*(x-x0)-sin(Theta)*(y-y0)+x0;
> y2 = sin(Theta)*(x-x0)+cos(Theta)*(x-x0)+y0;
>
> Then apply to the function:
> z2 = (exp(-((x2-256)/20).^2)'*(exp(-((y2-256)/40).^2);
> %imagesc(z); colormap(gray);
>
> However, this will not do the job. For example, if Theta = pi/2 then x2 = y and y2 = x, which represents the same values. Therefore no rotation is visible.
>
> What goes wrong?
> For computational speed and discretization errors I want to avoid "imrotate".
>
> Thanks for your help.
>
> Kees

Hi Kees,

you make the 2D gaussian by multiplying the profile in y by the profile in x, this works only if the axes are parallel to your coordinate axes.
When you rotate your gaussian you have to calculate all positions as no decomposition is possible.
try

Theta = pi/4
x0 = 256; y0 = 256;
[x,y] = meshgrid(1:512,1:512);
x2 = cos(Theta)*(x-x0)-sin(Theta)*(y-y0)+x0;
y2 = sin(Theta)*(x-x0)+cos(Theta)*(x-x0)+y0;
z2 = (exp(-((x2-256)/20).^2).*(exp(-((y2-256)/40).^2)));
imagesc(z2); colormap(gray);

hth
kinor

Subject: How to rotate a 2D function

From: kees de Kapper

Date: 6 Mar, 2009 10:10:03

Message: 3 of 3

Great! this works.

I've tried the solution you gave me earlier, but due to a mistypo it didn't work. You surely helped me to find the bug.

thank you very much.

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