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

1

11

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 numpy.ndarray. 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!

Morningglory answered 18/4, 2015 at 14:51 Comment(4)
Ohh. My bad. I think what I have done it just making a new RGB image with smaller size. Thanks for the pointer!Morningglory
Strangely, extracting green only again gives back an red image. Here is the link of the image I was trying to extract green channel from : upload.wikimedia.org/wikipedia/commons/3/37/…Morningglory
Okay. So I was supposed to set red component to zero as : rgb_image[:,:,0] = 0 and so on....Morningglory
Also have a look at the tutorials here and hereUnscathed
S
30

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 to visualize the "green" of each colour. In that case, set both the red and blue channels to zero and leave the green channel intact.

So:

green_image = rgb_image.copy() # Make a copy
green_image[:,:,0] = 0
green_image[:,:,2] = 0

Note that I've made a copy of your original image and changed the channels instead of modifying the original one in case you need it. 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.

Sunset answered 19/4, 2015 at 5:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.