How do you move the color channel ahead of image height and width?

moveaxis places a selected axis at a new position while preserving the relative order of the others.

numpy.moveaxis in practice

A height–width–channel image shaped (2,4,3) becomes (3,2,4). Each pixel retains its color components. Converting between color spaces requires a separate computation.

import numpy as np
np.moveaxis(np.zeros((2,4,3)), -1, 0).shape

Result [3, 2, 4]

中文

如何把彩色图像的通道移到最前?

moveaxis 把指定轴放到目标位置,其他轴保持相对顺序。

numpy.moveaxis 算例

图像按高、宽、通道排列为 (2,4,3),把末轴移到最前得到 (3,2,4)。像素的三个颜色分量仍是原值,颜色空间转换是另一项计算。

import numpy as np
np.moveaxis(np.zeros((2,4,3)), -1, 0).shape

结果 [3, 2, 4]

References / 参考来源

Keep reading / 继续阅读