简体   繁体   English

如何旋转 numpy 数组?

[英]How to rotate a numpy array?

I have some numpy/scipy issue.我有一些 numpy/scipy 问题。 I have a 3D array that represent an ellipsoid in a binary way [ 0 out of the ellipsoid].我有一个 3D 数组,它以二进制方式表示一个椭圆体 [椭圆体中的 0]。 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?或者有没有一种有效的方法可以直接写出带旋转的椭球方程?

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. 您的旋转矩阵可能是n * n矩阵,您必须将其与每个点相乘。 If you have your 3d-matrix you have some thing like i*j*k-points for plotting. 如果您有3D矩阵,则可以使用i*j*k-points的图形进行绘图。 This means for your case you have to do it i*j*k -times to find the new points. 这意味着对于您的情况,您必须执行i*j*k才能找到新的点。 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. 也许您应该考虑另一个绘图矩阵,它只是一个2D矩阵,只存储绘图点而没有零值。

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 最好的祝福

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. 看一下命令numpy.shape我曾经用它来转置数组,但是我不知道它是否适合您的需求。 Cheers! 干杯!

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:如果您需要显式数组旋转,请检查 scipy 库中的ndimage package

from scipy import ndimage, datasets
img = datasets.ascent()
img_45 = ndimage.rotate(img, 45, reshape=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM