Isolate the green colour of a picture and then threshold it to a B&W picture.

15 views (last 30 days)
I have the picture: http://imgur.com/mDqySoy
I want to isolate the green chips and then make them white, while everything else is black. I will show my work so far and two examples of what I have done and if someone points out my mistake I will be very grateful.
Example: I had to do it with the blue chips, which was easier to do. My code:
I = imread('img.png');
J= I(:,:,3); %extracting the blue layer of the image;
B= (J>225); % makes the blue chips white and the background black
The B.png image: http://imgur.com/rV7Egzo
I did the same for the yellow chips:
J2=I(:,:,2); %this extract the green layer of the image.
Y=(J2>240); %this takes the yellow chips and makes the white.
My work on the green chips isolation: G=((J>60)&(J<66))|((J>75)&(J<120)); Image created: http://imgur.com/8jB3Z9Y But as you can see one of the red chips is showing on the picture and also around the pen there is a white border. How can I fix this?

Accepted Answer

Image Analyst
Image Analyst on 19 Oct 2015
You need to use more than one color channel.
Or else convert to hsv colorspace with rgb2hsv() and threshold the hue channel.
  3 Comments
Image Analyst
Image Analyst on 19 Oct 2015
If you do it in RGB space, you'll probably need more than one color channel. For example, the green chips might have a green value of more than 200, but white regions will also have a value of more than 200, so looking at the green channel may not be enough. But the red values may be less than 150 in the green chips, but for the white regions the red values will be more than 150. That's why it's often that you have to look at more than one color channel.
With HSV you can probably get by with looking at just the hue channel, though you might also have to look at the L channel, since really dark value (black) might also have a green hue. For example a pixel with RGB values (0,3,0) is essentially black, but it's hue will be green since green is the only channel that has a non-zero value. But a pixel with those values, you don't want to consider as green, so you might need hues between .4 and .6 but only if the V value is more than .1 or something.
I've attached a few more instructive demos to this message.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!