简体   繁体   English

将多维 NumPy 数组的图像旋转 90 度

[英]Rotating images by 90 degrees for a multidimensional NumPy array

I have a numpy array of shape (7,4,100,100) which means that I have 7 images of 100x100 with depth 4. I want to rotate these images at 90 degrees.我有一个形状为 (7,4,100,100) 的 numpy 数组,这意味着我有 7 张深度为 4 的 100x100 图像。我想将这些图像旋转 90 度。 I have tried:我试过了:

rotated= numpy.rot90(array, 1)

but it changes the shape of the array to (4,7,100,100) which is not desired.但它将数组的形状更改为 (4,7,100,100),这是不希望的。 Any solution for that?有什么解决办法吗?

Another option另外一个选项

You could use scipy.ndimage.rotate , i think that it's more useful than numpy.rot90你可以使用scipy.ndimage.rotate ,我认为它比numpy.rot90更有用

For example,例如,

from scipy.ndimage import rotate
from scipy.misc import imread, imshow

img = imread('raven.jpg')

rotate_img = rotate(img, 90)

imshow(rotate_img)

在此处输入图片说明 在此处输入图片说明

Updated (Beware with interpolation)更新(注意插值)

If you pay attention at the rotated image you will observe a black border on the left, this is because Scipy use interpolation.如果您注意旋转后的图像,您会看到左侧有一个黑色边框,这是因为 Scipy 使用插值。 So, actually the image has been changed.所以,实际上图像已经改变了。 However, if that is a problem for you there are many options able to remove the black borders.但是,如果这对您来说是个问题,那么有许多选项可以去除黑色边框。

See this post .看到这个帖子

One solution without using np.rot90 to rotate in clockwise direction would be to swap the last two axes and then flip the last one -一种不使用np.rot90顺时针旋转的解决方案是交换最后两个轴,然后翻转最后一个轴 -

img.swapaxes(-2,-1)[...,::-1]

For counter-clockwise rotation, flip the second last axis -对于逆时针旋转,翻转倒数第二个轴 -

img.swapaxes(-2,-1)[...,::-1,:]

With np.rot90 , the counter-clockwise rotation would be -使用np.rot90 ,逆时针旋转将是 -

np.rot90(img,axes=(-2,-1))

Sample run -样品运行 -

In [39]: img = np.random.randint(0,255,(7,4,3,5))

In [40]: out_CW = img.swapaxes(-2,-1)[...,::-1] # Clockwise

In [41]: out_CCW = img.swapaxes(-2,-1)[...,::-1,:] # Counter-Clockwise

In [42]: img[0,0,:,:]
Out[42]: 
array([[142, 181, 141,  81,  42],
       [  1, 126, 145, 242, 118],
       [112, 115, 128,   0, 151]])

In [43]: out_CW[0,0,:,:]
Out[43]: 
array([[112,   1, 142],
       [115, 126, 181],
       [128, 145, 141],
       [  0, 242,  81],
       [151, 118,  42]])

In [44]: out_CCW[0,0,:,:]
Out[44]: 
array([[ 42, 118, 151],
       [ 81, 242,   0],
       [141, 145, 128],
       [181, 126, 115],
       [142,   1, 112]])

Runtime test运行时测试

In [41]: img = np.random.randint(0,255,(800,600))

# @Manel Fornos's Scipy based rotate func
In [42]: %timeit rotate(img, 90)
10 loops, best of 3: 60.8 ms per loop

In [43]: %timeit np.rot90(img,axes=(-2,-1))
100000 loops, best of 3: 4.19 µs per loop

In [44]: %timeit img.swapaxes(-2,-1)[...,::-1,:]
1000000 loops, best of 3: 480 ns per loop

Thus, for rotating by 90 degrees or multiples of it, numpy.dot or swapping axes based ones seem pretty good in terms of performance and also more importantly do not perform any interpolation that would change the values otherwise as done by Scipy's rotate based function.因此,对于旋转90度或它的倍数,基于numpy.dotswapping axes的那些在性能方面似乎相当不错,而且更重要的是,不要执行任何会改变值的插值,否则如 Scipy 的基于旋转的函数所做的那样。

Rotate three times counter clockwise: np.rot90(image, 3).逆时针旋转三圈:np.rot90(image, 3)。

It may be three times slower, may not be if the implementation is actually optimized and we are specifying the angle here in 90 increments, not a loop counter.它可能慢三倍,如果实现实际上是优化的并且我们在这里以 90 增量指定角度,而不是循环计数器,则可能不会。

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

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