Unable to split opencv image to RGB

半腔热情 提交于 2019-12-08 07:24:29

问题


I'm trying to split an image into B,G,R but after splitting, each B G & R have grayscale images.

import cv2
import numpy as np

image = cv2.imread('/path/image.jpg') #I have tried using CV_LOAD_IMAGE_COLOR flag as well as 1
#however,image is read as color image. It is not a grayscale image

b,g,r = cv2.split(image)
#[b,g,r]=np.dsplit(image,image.shape[-1])
#b,g,r = cv2.split(image)
#b = image[:,:,0]
#g = image[:,:,1]
#r = image[:,:,2]

#none of the above worked

cv2.imshow("green",g)
cv2.waitKey(0)
cv2.destroyAllWindows()

please help me to split the image into BGR. I have even tried it with different images.


回答1:


You are sending one channel to imshow. The green one. This will be shown as gray scale. What you want to do is send an image with red and blue channel set to zero in order to "see" it as green.

You are doing it right when splitting, you have the red, green and blue channels. It is the display code that is confusing you and showing the green channel as grayscale.

    import numpy as np
    import cv2

    image = np.random.rand(200, 200, 3)
    b, g, r = cv2.split(image)
    cv2.imshow('green', g)
    cv2.waitKey(0)

    black = np.zeros((200, 200, 3))
    black[:, :, 1] = g # Set only green channel
    cv2.imshow('green', black)
    cv2.waitKey(0)



回答2:


On splitting, each image is a single channel image. Since they are single channel images, when you use cv2.imshow(g) they look like grayscale images. But rest assured, the channels are split correctly.

Often, in a BGR image, each channel looks almost exactly like the BGR image, which is probably where you're getting confused.



来源:https://stackoverflow.com/questions/57118398/unable-to-split-opencv-image-to-rgb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!