# [SciPy-user] [bug?] scipy.ndimage.rotate

Alok Singhal as8ca at virginia.edu
Thu Apr 13 03:44:25 CDT 2006

```Hi,

I am using scipy 0.4.8 and numpy 0.9.6 on a Linux machine.

I am trying to figure out scipy.ndimage.rotate() function.  From
info(rotate):

----
rotate(input, angle, axes=(-1, -2), reshape=True, output_type=None,
output=None, order=3, mode='constant', cval=0.0, prefilter=True)

Rotate an array.

The array is rotated in the plane defined by the two axes given by the
axes parameter using spline interpolation of the requested order. The
angle is given in degrees. Points outside the boundaries of the input
are filled according to the given mode. If reshape is true, the output
shape is adapted so that the input array is contained completely in
the output. The parameter prefilter determines if the input is pre-
filtered before interpolation, if False it is assumed that the input
----

I am trying to rotate an array by an arbitrary angle, but the rotation does
not seem to work well unless the angle is a multiple of 90 degrees[1]:

>>> a = eye(4, dtype=Float64)
>>> a
array([[ 1.,  0.,  0.,  0.],
[ 0.,  1.,  0.,  0.],
[ 0.,  0.,  1.,  0.],
[ 0.,  0.,  0.,  1.]])
>>> # Rotation by 90 degrees
>>> b = rotate(a, 90)
>>> b		# looks good
array([[ -2.37169225e-20,   1.23341550e-16,  -2.22044605e-16,
1.00000000e+00],
[  1.23338162e-16,  -2.12801781e-16,   1.00000000e+00,
-2.21990395e-16],
[ -1.70206189e-16,   1.00000000e+00,  -3.51959130e-17,
7.89366944e-17],
[  1.00000000e+00,  -3.99677578e-16,   1.08562519e-16,
3.69983991e-18]])
>>> # Rotation by 45 degrees
>>> b = rotate(a, 45)
>>> b
array([[ 0.        ,  0.        ,  0.        ,  0.02360487,  0.        ,
0.        ],
[ 0.        ,  0.        ,  0.01925501,  0.01925501,  0.27416806,
0.        ],
[ 0.        ,  0.22915545,  0.44416261,  0.44416261,  0.22915545,
0.44507896],
[ 0.        ,  0.22915545,  0.44416261,  0.44416261,  0.22915545,
0.44507896],
[ 0.        ,  0.        ,  0.01925501,  0.01925501,  0.27416806,
0.        ],
[ 0.        ,  0.        ,  0.        ,  0.02360487,  0.        ,
0.        ]])
>>> b = rotate(a, 45, reshape=False)
>>> b
array([[ 0.        ,  0.01925501,  0.01925501,  0.27416806],
[ 0.22915545,  0.44416261,  0.44416261,  0.22915545],
[ 0.22915545,  0.44416261,  0.44416261,  0.22915545],
[ 0.        ,  0.01925501,  0.01925501,  0.27416806]])

I cannot understand the output in this case.  Rotating the Identity matrix
by 45 degrees (or any arbitrary angle) should result in a matrix that still
has 1's "along a line" and zeros everywhere else (or something close to
that).  But as can be seen above, the resulting matrix does not have that
property.  Am I missing something, or is it a bug in ndimage.rotate()?

Thanks,
Alok

[1] Even the case for multiples of 90 degrees "does not work":

>>> a = eye(5, dtype=Float64)
>>> b = rotate(a, 270)
>>> b
array([[  1.23480463e-17,   3.97766672e-18,   9.09628682e-17,
0.00000000e+00,   0.00000000e+00],
[  2.21922632e-18,   1.87363688e-17,  -6.40458552e-17,
1.00000000e+00,   0.00000000e+00],
[  2.81757040e-17,   5.07155895e-16,   1.00000000e+00,
-5.28853491e-16,   0.00000000e+00],
[  6.07356504e-17,   1.00000000e+00,  -5.87237778e-16,
1.79130528e-16,   0.00000000e+00],
[  1.00000000e+00,  -1.00977847e-15,   2.66090318e-16,
-6.32022104e-17,   0.00000000e+00]])
>>> # b[0][4] is not 1
>>> b = rotate(a, 90)
>>> b
array([[  1.23531285e-17,  -2.15688470e-17,   1.23355102e-16,
-2.96048179e-16,   1.00000000e+00],
[ -2.46749168e-17,   6.01901612e-17,  -2.06574395e-16,
1.00000000e+00,   6.07898606e-17],
[ -7.70630575e-18,  -1.12540186e-16,   1.00000000e+00,
-2.06567619e-16,   2.81824802e-17],
[ -4.17228101e-16,   1.00000000e+00,  -8.78678098e-17,
1.87363688e-17,  -9.66972813e-18],
[  1.00000000e+00,  -6.22545499e-16,   1.86127020e-16,
-1.98154888e-17,   1.13553237e-17]])
>>> # Looks OK

--
Alok Singhal                               *   *
Graduate Student, dept. of Astronomy   *           *     *
University of Virginia
http://www.astro.virginia.edu/~as8ca/              *    *

```