0

I want to keep a particular color in an image and discard all other colors. When I try to keep red color the program works but when I try a similar approach for green color I get nothing. Can anyone help. Below is my program. can't post images due to some reputation thing.

a = imread('image.jpg');
b = rgb2hsv(a);
h = 360 .* b(:,:,1);
s = b(:,:,2);
v = b(:,:,3);
nonred = (h > 20) & (h < 340);
v(nonred)=0;
b(:,:,3)=v;
c=hsv2rgb(b);

And here is the code for green color

a = imread('image.jpg.');
b = rgb2hsv(a);
h = 360 .* b(:,:,1);
s = b(:,:,2);
v = b(:,:,3);
nongreen = (h > 210) & (h < 30);
v(nongreen) = 0;
b(:,:,3)=v;
c=hsv2rgb(b);
  • 1
    What does googling `matlab keep one color` return? – Divakar Jan 29 '15 at 16:57
  • 1
    This is a close-to-duplicate that might help you - probably not close enough to close this as an exact duplicate. http://stackoverflow.com/questions/4063965/how-can-i-convert-an-rgb-image-to-grayscale-but-keep-one-color – J Richard Snape Jan 29 '15 at 17:06

1 Answers1

3

The issue is on the line:

nongreen = (h > 210) & (h < 30);

Which no value of h can satisfy. i.e. you're looking for h greater than 210 AND less than 30.

As your angular range for hue is between 0-360, you can just OR instead:

nongreen = (h > 210) | (h < 30);
stevejpurves
  • 878
  • 6
  • 10