How do I filter out red, white, yellow - but keep the green?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
dgpeters
Posts: 19
Joined: 2013-09-10T15:47:00-07:00
Authentication code: 6789

How do I filter out red, white, yellow - but keep the green?

Post by dgpeters » 2013-09-29T09:41:03-07:00

I need to use Imagemagick to create a color filter that eliminates everything but green. Please don't confuse this request with the idea of creating a color filter that keeps only the green. These are completely different things. So for example a yellow color should be filtered OUT even though yellow contains green in the RGB colorspace. White also contains green but that must also be filtered out in my solution. All I want left are pixels that appear green, and my preference is that they end up being white in a monochrome output for this filter.

My source images contain lots of primary colours so I don't have to worry a lot about photorealistic color spaces. I have spent hours reading the imagemagick documentation about color filtering and I just can't see how to do what I want. There must be a way, and perhaps more than one way.

snibgo
Posts: 12306
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I filter out red, white, yellow - but keep the gr

Post by snibgo » 2013-09-29T10:00:47-07:00

If you mean exactly green, the process is simple:

Code: Select all

convert in.png -fill White -opaque Green out.png
This will turn all pixels that are exactly green into white. See http://www.imagemagick.org/script/comma ... php#opaque

We can first turn all non-green pixels black:

Code: Select all

convert in.png -fill Black +opaque Green -fill White -opaque Green out.png
snibgo's IM pages: im.snibgo.com

User avatar
RitterRunkel
Posts: 7
Joined: 2013-09-17T05:12:16-07:00
Authentication code: 6789
Location: Germany

Re: How do I filter out red, white, yellow - but keep the gr

Post by RitterRunkel » 2013-09-29T10:08:37-07:00

Looks good, and -fuzz %distance is described as well ... could be used to find all other greens, too? =)

Seems simpler than my thoughts. I was thinking at channel seperation, threshold at 50% to binary b/w and then darken the green-b/w with the inverse other two b/w ...
Last edited by RitterRunkel on 2013-09-29T10:29:57-07:00, edited 1 time in total.

snibgo
Posts: 12306
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How do I filter out red, white, yellow - but keep the gr

Post by snibgo » 2013-09-29T10:23:26-07:00

Beware that IM's "green" is actually not (0,100%,0). If you want that colour, the name is "Lime".

Yes, "-fuzz N%" before "opaque" will change colours that are within N% of true green.

"opaque" is a binary operation. It either will change a pixel or it won't. For some purposes, we need a graduated response, and thtat is trickier, but still quite easy.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 25664
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I filter out red, white, yellow - but keep the gr

Post by fmw42 » 2013-09-29T11:32:45-07:00

Perhaps I misunderstand, but why not just extract the green channel of the RGB image?

convert image -channel g -separate +channel result


Another possible approach would be to convert to HSL and threshold the Hue around the green values within some tolerance

dgpeters
Posts: 19
Joined: 2013-09-10T15:47:00-07:00
Authentication code: 6789

Re: How do I filter out red, white, yellow - but keep the gr

Post by dgpeters » 2013-09-29T11:54:03-07:00

I think you are the author of this wonderful utility so I must be extremely nice to you. Thanks for a superb utility.

You asked me why I don't simply want the green. Because that produces false results - namely it also produces hits on yellow, white and anything else that includes green. But from the other people above I seem to have settled on something that works for me: "convert trythis2.png -fill Black -fuzz 25% +opaque Lime -fill White -opaque Lime trythis3.png". However I find that fuzz option to be strangely picky, anything higher or lower than about 25% doesn't get me what I need.

As I improve with this fairly complex program I won't need help as often.

I do have one other technical issue which I will raise in another thread. Thanks guys.

User avatar
fmw42
Posts: 25664
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I filter out red, white, yellow - but keep the gr

Post by fmw42 » 2013-09-29T12:10:09-07:00

I think you are the author of this wonderful utility so I must be extremely nice to you. Thanks for a superb utility.
No, actually it is another person (whose forum ID is Magick). I am just another user. However, I try to help answer questions as best I can and have written a number of IM scripts.

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How do I filter out red, white, yellow - but keep the gr

Post by anthony » 2013-10-07T23:31:42-07:00

Note that pure green (lime) is actually a corner of the RGB color cube.

Now -fuzz 5% +transparent lime will convert all colors but a 3-dimensional sphere around pure green to transparency (if that channel exists) however there is another posibility...

-fuzz 95% -transparent magenta Which converts all colors within 95% sphere of magenta to transparency

Same thing you say. not quite. that last will preserve more colors along the green to yellow, cyan, and dark greens axis, And less along the green to gray diagonal, simply because of the shape of the matching 3-dimensional spheres used.

Which brings us to another aspect of this probelm. A 5% lime sphere will preserve near pure- greens but it will not preserve the SVG green (half dark green) or any other green along the green-black axis, which are also typically regarded as being 'green' in color.

So what about a color that is very near black green? Do you want to preserve that type of green too?

-----

So what if you want to preserve a line of greens from a very dark green to pure green?

PS: One solution is similar to a problem I posted recently about matching multiple exact colors. Make the wanted mutliple (fuzzed) colors transparent, then negate the alpha channel.

Another solution is a transform the image to a Hue color space, match all the greens, and remove the darkest colors, then transform back. That will get all shades of green (or any other hue).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

Post Reply