Sangram Sangram -4 years ago 290
Python Question

How to extract green channel from RGB image in Python using Scikit-Image library?

I am extremely new to scikit-image (

skimage
) library in Python for image processing (started few minutes ago!). I have used
imread
to read an image file in a
ndarry
. The array is 3 dimensional where the size of the third dimension is 3 (namely one for each of Red, Green and Blue components of an image).

rgb_image = imread("input_rgb_image.jpg")
rgb_image.shape # gives (1411L, 1411L, 3L)


I tried to extract green channel as

green_image = rgb_image[:,:,1]


But when I write this image matrix to an output file as :

imsave("green_output_image.jpg",green_image)


I get an image which doesn't really look ONLY green!

Answer Source

What you are extracting is just a single channel and this shows you how much green colour each pixel has. This will ultimately be visualized as a grayscale image where darker pixels denote that there isn't much "greenness" at those points and lighter pixels denote that there is a high amount of "greenness" at those points.

If I'm interpreting what you're saying properly, you wish visualize the "green" of each colour, then set both the red and blue channels to zero and leave the green channel intact.

So:

rgb_image[:,:,0] = 0
rgb_image[:,:,2] = 0

However, if you just want to extract the green channel and visualize this as a grayscale image as I've mentioned above, then doing what you did above with the setting of your green_image variable is just fine.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download