重塑图像的 4D numpy 数组正在改变图像数字

reshape 4D numpy Array of the image is changing the images figures

我想将 4d 图像数组从 (50, 100, 100, 128) 更改为 (50,128, 100, 100),但是当我在重塑图像后绘制图像时,图像发生了变化。 所有图像都是来自 50 名患者的 CT 扫描图像,我想将它们用于 3d Resnet 卷积神经网络。此外,每个患者都有128张100*100像素的图像。

原始形状:

data.shape

(50, 100, 100, 128)

来自数据的图像

imgplot = plt.imshow(data[0,:,:,1])
plt.show()

整形后

rd = data.reshape(-1,128,100,100)
rd.shape

(50, 128, 100, 100)
imgplot = plt.imshow(rd [0,1,:,:])
plt.show()

此外,我尝试了转置但没有任何改变

r2data = np.transpose(data)
r2data.shape

(128, 100, 100, 50)

使用 array.transpose() 和所需的轴顺序:

# original 4D array
In [98]: data = np.random.random_sample((50, 100, 100, 128))

# move last axis to second position; reshapes data but would still be a `view`
In [99]: reshaped_data = data.transpose((0, -1, 1, 2))

In [100]: reshaped_data.shape
Out[100]: (50, 128, 100, 100)

如果转置后你真的想要数据的副本,那么你可以强制它这样做:

In [106]: reshaped_data = data.transpose((0, -1, 1, 2)).copy()

In [107]: reshaped_data.flags
Out[107]: 
  C_CONTIGUOUS : True
  F_CONTIGUOUS : False
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False