顺时针二维矩阵旋转

clockwise 2d Matrix rotation

我有以下代码将矩阵逆时针旋转 45 度,但不知道如何使其顺时针旋转。 (代码由 Sandeep Sharma 在 Rotate numpy 2D array 提供)

def rotate45(array, clockwise = False):
rot = []
if clockwise == False:
    for i in range(len(array)):
        rot.append([0] * (len(array)+len(array[0])-1))
        for j in range(len(array[i])):
            rot[i][int(i + j)] = array[i][j]

# Thats where something should be changed, but I cant figure out what
else:
    for i in range(len(array)):
        rot.append([0] * (len(array)+len(array[0])-1))
        for j in range(len(array[i])):
            rot[i][int(i + j)] = array[i][j]

return rot

按照链接 post 中的建议,您可以不使用 scipy.ndimage.rotate 吗?

例如:

from scipy.ndimage import rotate
def rotateClockwise45(array):
    return rotate(array, -45)

# Rotate any amount:
# Use negative numbers for clockwise, positive for counter clockwise.
def rotate(array, angle):
    return rotate(array, angle)

此外,如果您想保留原始输入尺寸,可以在调用旋转函数时将重塑标志设置为假。例如:

rotate(array, angle, reshape=False)