【发布时间】:2020-09-27 02:42:37
【问题描述】:
喂!有没有解决方案可以在 Python 中将图像的(宽度、高度、通道)尺寸更改为(通道、高度、宽度)?
例如:
224 x 224 x 3 -> 3 x 224 x 224
【问题讨论】:
标签: python image dimensions
喂!有没有解决方案可以在 Python 中将图像的(宽度、高度、通道)尺寸更改为(通道、高度、宽度)?
例如:
224 x 224 x 3 -> 3 x 224 x 224
【问题讨论】:
标签: python image dimensions
假设图像表示为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)
您可以在以下链接中了解该方法:
https://numpy.org/doc/stable/reference/generated/numpy.moveaxis.html
【讨论】:
你可以使用np.transposehttps://numpy.org/doc/1.18/reference/generated/numpy.transpose.html:
new_image = np.transpose(image, (2, 0, 1))
【讨论】: