顺时针2d矩阵旋转

时间:2018-11-01 15:10:31

标签: python arrays pandas multidimensional-array 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

1 个答案:

答案 0 :(得分:0)

如链接文章中所建议,您不能使用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函数时将reshape标志设置为false。例如:

rotate(array, angle, reshape=False)