[英]Why does transposing a numpy array rotate it 90 degrees?
I am trying to read images from an lmdb
dataset
, augment each one and then save them into another dataset
for being used in my trainings. 我正在尝试从
lmdb
dataset
读取图像,对每个图像进行扩充,然后将它们保存到另一个dataset
以便在我的培训中使用。
These images axis were initially changed to (3,32,32)
when they were being saved into the lmdb dataset
, So in order to augment them I had to transpose them back into their actual shape. 这些图像轴在被保存到
lmdb dataset
时最初被更改为(3,32,32)
,因此为了增强它们,我必须将它们转换回它们的实际形状。
The problem is whenever I try to display them using either matplotlib
's show()
method or scipy
's toimage()
, they show a rotated version of the image. 问题是每当我尝试使用
matplotlib
的show()
方法或scipy
的toimage()
显示它们时,它们都会显示图像的旋转版本。 So we have : 所以我们有:
img_set = np.transpose(data_train,(0,3,2,1))
#trying to display an image using pyplot, makes it look like this:
plt.subplot(1,2,1)
plt.imshow(img_set[0])
showing the same image using toimage
: 使用
toimage
显示相同的图像:
Now if I dont transpose data_train
, pyplot
's show()
generates an error while toimage()
displays the image well: 现在如果我不转换
data_train
, pyplot
的show()
生成错误,而toimage()
会很好地显示图像:
What is happening here? 这里发生了什么?
When I feed the transposed data_train to my augmenter, I also get the result rotated just like previous examples. 当我将转置的data_train提供给我的增强器时,我也像前面的例子一样旋转结果。
Now I'm not sure whether this is a displaying issue, or the actual images are indeed rotated! 现在我不确定这是否是一个显示问题,或者实际图像确实是旋转的!
What should I do ? 我该怎么办 ?
First, look closely. 首先,仔细看看。 The transoposed array is not rotated but mirrored on the diagonal (ie X and Y axes are swapped).
transoposed阵列不旋转但在对角线上镜像(即交换X轴和Y轴)。
The original shape is (3,32,32)
, which I interpret as (RGB, X, Y)
. 原始形状是
(3,32,32)
,我将其解释为(RGB, X, Y)
。 However, imshow
expects an array of shape MxNx3
- the color information must be in the last dimension. 但是,
imshow
期望一个形状MxNx3
的数组 - 颜色信息必须在最后一个维度。
By transposing the array you invert the order of dimensions: (RGB, X, Y)
becomes (Y, X, RGB)
. 通过转置数组,您可以反转维度的顺序:
(RGB, X, Y)
变为(Y, X, RGB)
。 This is fine for matplotlib because the color information is now in the last dimension but X and Y are swapped, too. 这对于matplotlib来说很好,因为颜色信息现在在最后一个维度,但X和Y也是交换的。 If you want to preserve the order of X, Y you can tell
transpose to do so
: 如果你想保留X,Y的顺序你可以告诉
transpose to do so
:
import numpy as np
img = np.zeros((3, 32, 64)) # non-square image for illustration
print(img.shape) # (3, 32, 64)
print(np.transpose(img).shape) # (64, 32, 3)
print(np.transpose(img, [1, 2, 0]).shape) # (32, 64, 3)
When using imshow
to display an image be aware of the following pitfalls: 使用
imshow
显示图像时,请注意以下陷阱:
It treats the image as a matrix, so the dimensions of the array are interpreted as (ROW, COLUMN, RGB), which is equivalent to (VERTICAL, HORIZONTAL, COLOR) or (Y, X, RGB). 它将图像视为矩阵,因此数组的尺寸被解释为(ROW,COLUMN,RGB),相当于(垂直,水平,颜色)或(Y,X,RGB)。
It changes direction of the y axis so the upper left corner is img[0, 0]. 它改变了y轴的方向,所以左上角是img [0,0]。 This is different from matplotlib's normal coordinate system where (0, 0) is the bottom left.
这与matplotlib的常规坐标系不同,其中(0,0)是左下角。
Example: 例:
import matplotlib.pyplot as plt
img = np.zeros((32, 64, 3))
img[1, 1] = [1, 1, 1] # marking the upper right corner white
plt.imshow(img)
Note that the smaller first dimension corresponds to the vertical direction of the image. 注意,较小的第一维对应于图像的垂直方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.