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

后端 未结 3 801
眼角桃花
眼角桃花 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 17:02

    If you want it to use in OpenCV way then you may use cv2.split(), keeping in mind channels of your image:

    b, g, r    = cv2.split(image) # For BGR image
    b, g, r, a = cv2.split(image) # for BGRA image
    

    Or if you may like direct numpy format then you may use directly [which seems to be more efficient as per comments of @igaurav]

    b, g, r    = image[:, :, 0], image[:, :, 1], image[:, :, 2] # For RGB image
    b, g, r, a = image[:, :, 0], image[:, :, 1], image[:, :, 2], image[:, :, 3] # for BGRA image
    

    You may use np.shape[2] to check the number of channels in the given image.

提交回复
热议问题