I have some numpy/scipy issue. I have a 3D array that represent an ellipsoid in a binary way [ 0 out of the ellipsoid]. The thing is I would like to rotate my shape of a certain degree. Do you think it's possible ? Or is there an efficient way to write directly the ellipsoid equation with the rotation ?
-
Have a look at Homegeneous coordinates, it allows you all transformations with one matrix multiplication. en.wikipedia.org/wiki/…schlamar– schlamar2011-08-04 10:36:40 +00:00Commented Aug 4, 2011 at 10:36
-
Do you know how you will rotate it ... whether in a 2D fashion or 3D ... whether pitch (nose up or down), yaw (nose left or right), roll (sides up or down) or a combination?jcfollower– jcfollower2011-08-04 12:30:55 +00:00Commented Aug 4, 2011 at 12:30
3 Answers
Just a short answer. If you need more informations or you don't know how to do it, then I will edit this post and add a small example.
The right way to rotate your matrix of data points is to do a matrix multiplication. Your rotation matrix would be probably an n*n-matrix and you have to multiply it with every point. If you have your 3d-matrix you have some thing like i*j*k-points for plotting. This means for your case you have to do it i*j*k-times to find the new points. Maybe you should consider an other matrix for plotting which is just a 2D matrix and just store the plotting points and no zero values.
There are some algorithm to calculate faster the results for low valued matrix, but just google for this.
Did you understood me or do you still have some questions? Sorry for this rough overview.
Best regards
Comments
Take a look at the command numpy.shape
I used it once to transpose an array, but I don't know if it might fit your needs.
Cheers!
4 Comments
rotating by a non rectangular degree is tricky, because the rotated square does no longer fit in the matrix.
The simplest way would be to transpose a 2D array/matrix by:
import numpy as np
... x = np.array([[1,2,3],[4,5,6],[7,8,9]])
x
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
x.T # transpose the array
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
if you require an explicit array rotation check the ndimage package from scipy library:
from scipy import ndimage, datasets
img = datasets.ascent()
img_45 = ndimage.rotate(img, 45, reshape=False)