转置图像尺寸

Transpose image dimensions

喂!在Python?

中是否有将图像的(宽度,高度,通道)尺寸更改为(通道,高度,宽度)的解决方案

例如: 224 x 224 x 3 -> 3 x 224 x 224

假设图像表示为 nd.array 您可以使用如下 moveaxis 方法:

x = np.zeros((3, 4, 5))
np.moveaxis(x, 0, -1).shape
# (4, 5, 3)
np.moveaxis(x, -1, 0).shape
# (5, 3, 4)

在您的具体情况下:

x = np.zeros((224, 224, 3))
np.moveaxis(x, (2, 0, 1), (0, 1, 2)).shape
# (3, 224, 224)

您可以阅读以下link中的方法:

https://numpy.org/doc/stable/reference/generated/numpy.moveaxis.html

您可以使用 np.transpose https://numpy.org/doc/1.18/reference/generated/numpy.transpose.html:

new_image = np.transpose(image, (2, 0, 1))