How to extract R,G,B values with numpy into seperate arrays

后端 未结 3 912
眼角桃花
眼角桃花 2021-01-02 16:37

Suppose I have a image with some dimension (1920, 1080, 3) , I want to extract out R,G,B values into separate arrays R , G, B . I tried to do it li

3条回答
  •  一个人的身影
    2021-01-02 16:44

    This works for me:

    def split_channels(im: np.ndarray):
        assert len(im.shape) == 3 and im.shape[-1] == 3
        return np.squeeze(np.split(im, im.shape[-1], -1), axis=-1)
    

    Note that, the np.split itself is not enough, which will leave you a (M, N, 1) image. But if you want to have (M, N), then the squeeze works.

    You can remove the assert if you have other cases.

提交回复
热议问题