从通道第一个到最后一个更改图像通道排序

时间:2018-03-29 14:34:32

标签: python numpy

我想将这个numpy images数组的顺序更改为channel_last training_data:(2387,1,350,350)to(2387,350,350,1) validation_data:(298,1,350,350)到(298,350,350,1) testing_data:(301,1,350,350)到(301,350,350,1)

我尝试了这个,但它无法正常工作

np.rollaxis(training_data,0,3).shape
np.rollaxis(validation_data,0,3).shape
np.rollaxis(testing_data,0,3).shape

3 个答案:

答案 0 :(得分:2)

您需要np.transpose这样的方法:

training_data = np.transpose(training_data, (0, 2,3,1)

其他人也一样

答案 1 :(得分:0)

如果您要移动的轴的长度为1,则可以执行简单的重塑:

a = np.arange(24).reshape(2, 1, 3, 4)

# Three different methods:
b1 = a.reshape(2, 3, 4, 1)
b2 = np.moveaxis(a, 1, 3)
b3 = a.transpose(0, 2, 3, 1)

# All give the same result:
np.all(b1 == b2) and np.all(b2 == b3)
# True

答案 2 :(得分:-1)

这样做:

np.rollaxis(array_here,axis_to_roll,to_where)

对于您的情况,您希望将轴 0 滚动到最后一个位置(超过 3),因此您必须将 to_where 设为 4(超过 3)。

这会起作用(亲自测试):

np.rollaxis(train_data,0,4)
相关问题