Thread Subject: Image rotation and NaNs...

Subject: Image rotation and NaNs...

From: Matman86 Di

Date: 27 May, 2009 09:03:01

Message: 1 of 9

Hi everybody,I'm trying to rotate an image but,for some reason,when I make a 360? rotation I can't get the original image,but I got some NaNs too in image matrix and I don't know why...here is the code:

[M N]=size(X);
[n m]=meshgrid(-N/2:N/2-1,-M/2:M/2-1);
np=n*cos(2*pi)-m*sin(2*pi);
mp=n*sin(2*pi)+m*cos(2*pi);
y=interp2(n,m,X,np,mp,'linear');

Thx in advance...

Subject: Image rotation and NaNs...

From: Matman86 Di

Date: 6 Jun, 2009 09:57:01

Message: 2 of 9

"Matman86 Di" <pasqui.squall@email.it> wrote in message <gvivk5$q0m$1@fred.mathworks.com>...
> Hi everybody,I'm trying to rotate an image but,for some reason,when I make a 360? rotation I can't get the original image,but I got some NaNs too in image matrix and I don't know why...here is the code:
>
> [M N]=size(X);
> [n m]=meshgrid(-N/2:N/2-1,-M/2:M/2-1);
> np=n*cos(2*pi)-m*sin(2*pi);
> mp=n*sin(2*pi)+m*cos(2*pi);
> y=interp2(n,m,X,np,mp,'linear');
>
> Thx in advance...

Anyone??T_T

Subject: Image rotation and NaNs...

From: us

Date: 6 Jun, 2009 10:37:00

Message: 3 of 9

"Matman86 Di" <pasqui.squall@email.it> wrote in message <h0dehc$8ui$1@fred.mathworks.com>...
> "Matman86 Di" <pasqui.squall@email.it> wrote in message <gvivk5$q0m$1@fred.mathworks.com>...
> > Hi everybody,I'm trying to rotate an image but,for some reason,when I make a 360? rotation I can't get the original image,but I got some NaNs too in image matrix and I don't know why...here is the code:
> >
> > [M N]=size(X);
> > [n m]=meshgrid(-N/2:N/2-1,-M/2:M/2-1);
> > np=n*cos(2*pi)-m*sin(2*pi);
> > mp=n*sin(2*pi)+m*cos(2*pi);
> > y=interp2(n,m,X,np,mp,'linear');
> >
> > Thx in advance...
>
> Anyone??T_T

well, yes: your results comply exactly with the rule...

     help interp2;
%{
% note: EXTRAPVAL
...
    ZI = INTERP2(...,METHOD,EXTRAPVAL) specificies a method and a scalar
    value for ZI outside of the domain created by X and Y. Thus, ZI will
    equal EXTRAPVAL for any value of YI or XI which is not spanned by Y
    or X respectively. A method must be specified for EXTRAPVAL to be used,
    the default method is 'linear'.
...
%}
% eg,
     X=magic(3);
     [M N]=size(X);
     [n m]=meshgrid(-N/2:N/2-1,-M/2:M/2-1);
     np=n*cos(2*pi)-m*sin(2*pi);
     mp=n*sin(2*pi)+m*cos(2*pi);
     y=interp2(n,m,X,np,mp,'linear');
% look at
     [n;nan(1,size(n,1));np] % <- nan: separator...
% -or-
     [m;nan(1,size(m,1));mp]

us

Subject: Image rotation and NaNs...

From: Matman86 Di

Date: 6 Jun, 2009 17:31:01

Message: 4 of 9

> well, yes: your results comply exactly with the rule...
>
> help interp2;
> %{
> % note: EXTRAPVAL
> ...
> ZI = INTERP2(...,METHOD,EXTRAPVAL) specificies a method and a scalar
> value for ZI outside of the domain created by X and Y. Thus, ZI will
> equal EXTRAPVAL for any value of YI or XI which is not spanned by Y
> or X respectively. A method must be specified for EXTRAPVAL to be used,
> the default method is 'linear'.
> ...
> %}
> % eg,
> X=magic(3);
> [M N]=size(X);
> [n m]=meshgrid(-N/2:N/2-1,-M/2:M/2-1);
> np=n*cos(2*pi)-m*sin(2*pi);
> mp=n*sin(2*pi)+m*cos(2*pi);
> y=interp2(n,m,X,np,mp,'linear');
> % look at
> [n;nan(1,size(n,1));np] % <- nan: separator...
> % -or-
> [m;nan(1,size(m,1));mp]
>
> us

Mmmm...maybe I'm a little dumb today but I can't understand what you just said,could you be more clear please?
I know how interp2 works,but I can't explain why if I make a 360? rotation I can't return with the original values...

Subject: Image rotation and NaNs...

From: Bruno Luong

Date: 6 Jun, 2009 18:45:04

Message: 5 of 9

"Matman86 Di" <pasqui.squall@email.it> wrote in message <h0e94l$h1a$1@fred.mathworks.com>...

> I know how interp2 works,but I can't explain why if I make a 360? rotation I can't return with the original values...

Because when you rotate pi 2*pi, the points (after rotation) do not fall exactly where it is due to numerical error (try to see the value of sin(2*pi), it is not zero), and if it's outside the box, interp2 puts the value to NaN.

And btw, why your point of rotation is not centered in the image? It's off by 1/2 pixel.

Bruno

Subject: Image rotation and NaNs...

From: Matman86 Di

Date: 7 Jun, 2009 08:06:01

Message: 6 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h0edfg$ld0$1@fred.mathworks.com>...
> "Matman86 Di" <pasqui.squall@email.it> wrote in message <h0e94l$h1a$1@fred.mathworks.com>...
>
> > I know how interp2 works,but I can't explain why if I make a 360? rotation I can't return with the original values...
>
> Because when you rotate pi 2*pi, the points (after rotation) do not fall exactly where it is due to numerical error (try to see the value of sin(2*pi), it is not zero), and if it's outside the box, interp2 puts the value to NaN.
>
> And btw, why your point of rotation is not centered in the image? It's off by 1/2 pixel.
>
> Bruno

I answer first to last question:it's because if I make a grid like -M/2:M/2 I don't get the exact number of pixels.
You're right for the NaNs,so I can't do anything about it I guess...
According to you is ok if I make
X(isnan(X))=0;
in order to allow elaborations on the image?

Subject: Image rotation and NaNs...

From: Bruno Luong

Date: 7 Jun, 2009 08:38:01

Message: 7 of 9

"Matman86 Di" <pasqui.squall@email.it> wrote in message <h0fsd9$ej3$1@fred.mathworks.com>...

>
> I answer first to last question:it's because if I make a grid like -M/2:M/2 I don't get the exact number of pixels.

Add 0.5 see below

> You're right for the NaNs,so I can't do anything about it I guess...
> According to you is ok if I make
> X(isnan(X))=0;

No, use extrap option (Jos gave a hint)

X=peaks(40);
[M N]=size(X);
[n m]=meshgrid(-N/2+0.5:N/2-0.5,-M/2+0.5:M/2-0.5);
np=n*cos(2*pi)-m*sin(2*pi);
mp=n*sin(2*pi)+m*cos(2*pi);
y=interp2(n,m,X,np,mp,'linear',0);
imagesc(y)

Bruno

Subject: Image rotation and NaNs...

From: Bruno Luong

Date: 7 Jun, 2009 09:02:01

Message: 8 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h0fu99$guq$1@fred.mathworks.com>...

>
> No, use extrap option (Jos gave a hint)
>

Sorry, it is us who gave a hint, not Jos.

Bruno

Subject: Image rotation and NaNs...

From: Matman86 Di

Date: 7 Jun, 2009 16:53:01

Message: 9 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h0fvm8$gid$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h0fu99$guq$1@fred.mathworks.com>...
>
> >
> > No, use extrap option (Jos gave a hint)
> >
>
> Sorry, it is us who gave a hint, not Jos.
>
> Bruno

Thank you very much!:)

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
code us 6 Jun, 2009 06:39:03
meshgrid us 6 Jun, 2009 06:39:03
interp2 us 6 Jun, 2009 06:39:03
rssFeed for this Thread

Contact us at files@mathworks.com