延展到 3D 最后使用新轴并使用 np.unpackbits 沿着同样的 -
3D
np.unpackbits
np.unpackbits(a[...,None], axis=-1) # a is input array
样品运行 -
In [145]: np.random.seed(0) In [146]: a = np.random.randint(0,256,(2,3),dtype=np.uint8) In [147]: a Out[147]: array([[172, 10, 127], [140, 47, 170]], dtype=uint8) In [149]: out = np.unpackbits(a[...,None], axis=-1) In [150]: out Out[150]: array([[[1, 0, 1, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 0, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1]], [[1, 0, 0, 0, 1, 1, 0, 0], [0, 0, 1, 0, 1, 1, 1, 1], [1, 0, 1, 0, 1, 0, 1, 0]]], dtype=uint8)
因此, out[...,0] 将是具有最低有效位的二进制图像,依此类推 out[...,7] 作为最重要的一点。
out[...,0]
out[...,7]