利用np.stack()把不同1 channel的灰度图像合并为3 channel的RGB图

np.stack文档解释:


Join a sequence of arrays along a new axis.

The axis parameter specifies the index of the new axis in the dimensions of the result. For example, if axis=0 it will be the first dimension and if axis=-1 it will be the last dimension.

产生一个给定axis的新轴 ,沿着该轴堆砌数组

下面是代码测试:

a = np.arange(8).reshape((2,2,2))
array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])
b = np.arange(8).reshape((2,2,2))+1
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])

第一个2是2个样本,后面2*2是像素矩阵,要将a,b变成2*2*2*2,第4个2表示颜色通道:

c = np.stack((a,b),axis=3)
array([[[[0, 1],
         [1, 2]],

        [[2, 3],
         [3, 4]]],


       [[[4, 5],
         [5, 6]],

        [[6, 7],
         [7, 8]]]])

验证,看c[:,:,:,0]:

 c[:,:,:,0]
array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])

也可变成第2个2是颜色通道:

c = np.stack((a,b),axis=1)
array([[[[0, 1],
         [2, 3]],

        [[1, 2],
         [3, 4]]],


       [[[4, 5],
         [6, 7]],

        [[5, 6],
         [7, 8]]]])

c[:,1,:,:]
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])


c[:,0,:,:]
array([[[0, 1],
        [2, 3]],

       [[4, 5],
        [6, 7]]])

猜你喜欢

转载自blog.csdn.net/j515332019/article/details/108648109