簡體   English   中英

如何在python中將二維方形numpy數組旋轉45度?

[英]How to rotate square numpy array of 2 dimensional by 45 degree in python?

我有一個 numpy 圖像數組。 維度為 2,形狀為 (100,100)。 我想增加更多數據,因為我只有 52 組 numpy 數組。 我想將給定的數組旋轉 45 度。 我應該為此做什么??

假設數組是這樣的

a=[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]

請將給定的數組旋轉 45 度。

您可以使用scipy.ndimage.rotate

import numpy as np
from scipy.ndimage import rotate
x = np.arange(25).reshape(5, -1)
rotate(x, angle=45)

輸出

array([[ 0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  6,  0,  0,  0],
       [ 0,  0,  4,  9, 14,  0,  0],
       [ 0,  3,  8, 12, 16, 21,  0],
       [ 0,  0, 10, 15, 20,  0,  0],
       [ 0,  0,  0, 18,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0]])

不確定 ndimage 通常是否是此類矩陣的良好解決方案。 它可能適用於 45 度,但不適用於其他沒有問題的角度。 在下面的示例中,“20”變為 0。(Numpy 版本 1.18.2,scipy 1.4.1)

import numpy as np
from scipy.ndimage import rotate
x = np.arange(25).reshape(5, -1)*1.0
rotate(x, angle=180,reshape=True)
Out[14]: 
array([[24., 23., 22., 21.,  0.],
       [19., 18., 17., 16., 15.],
       [14., 13., 12., 11., 10.],
       [ 9.,  8.,  7.,  6.,  5.],
       [ 4.,  3.,  2.,  1.,  0.]])

x
Out[15]: 
array([[ 0.,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.],
       [10., 11., 12., 13., 14.],
       [15., 16., 17., 18., 19.],
       [20., 21., 22., 23., 24.]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM