Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to split opencv image to RGB

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.

like image 267
Bamwani Avatar asked Dec 01 '25 02:12

Bamwani


2 Answers

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)
like image 151
Solid7 Avatar answered Dec 03 '25 19:12

Solid7


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.

like image 42
Shawn Mathew Avatar answered Dec 03 '25 19:12

Shawn Mathew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!