Page 1 of 1

Extract colors from image

Posted: 2014-03-03T04:50:11-07:00
by alasdairdf
I'd like to split an image into each color and every color that is darker, so that this becomes black and everything else white.

I see I can get the colormap with identify, so what I would like to extract each one of these colors into a separate image, but also include all darker colors, e.g. I want RGB(242,255,0) and darker to be black, and all else white (and then do this for each color on the colormap to get hundreds of separate images.)

I thought that I might be able to define a darker color as any that has a lower RGB value (multiplying or adding the RGB elements together), but now I'm not so sure as yellow would then be darker than blue, but that's not logical.

So I have 2 questions:
1. How can I extract the colors into separate B&W images as explained above?
2. Is there any standard on how to consider whether one color is 'darker' than another?

Thanks!

Re: Extract colors from image

Posted: 2014-03-03T05:13:46-07:00
by snibgo
For definitions of "darker", see http://www.imagemagick.org/script/comma ... #intensity

Windows syntax:

Code: Select all

convert ^
  rose: ^
  ( +clone -fill rgb(242,255,0) -colorize 100 ) ^
  -grayscale Rec709Luminance ^
  -compose MinusSrc -composite ^
  -fill White +opaque Black ^
   y.png
This:
- reads the built-in image "rose:", but you should use your own file
- makes a copy, turning all the pixels to the chosen colour
- converts both images to greyscale
- subtracts this from the original. Now pixels that were the chosen colour or darker are black.
- changes all non-black pixels to white.

Re: Extract colors from image

Posted: 2014-03-03T05:31:51-07:00
by alasdairdf
Thank you very much for your help.

Unfortunately I get this error testing your code.

Code: Select all

# convert rose: (+clone -fill rgb(242,255,0) -colorize 100) -grayscale Rec709Luminance -compose MinusSrc -composite -fill White +opaque Black y.png
-bash: syntax error near unexpected token `('
Am I doing something wrong?

I am on Linux if that makes a difference.

Re: Extract colors from image

Posted: 2014-03-03T05:45:39-07:00
by Bonzo
With Linux you need to escape the ( and ) so it becomes /( and /) or is it \( and \) ?

Re: Extract colors from image

Posted: 2014-03-03T09:18:17-07:00
by alasdairdf
OK, now I'm getting a different error:

Code: Select all

# convert rose: \( +clone -fill "rgb(242,255,0)" -colorize 100 \) -grayscale Rec709Luminance -compose MinusSrc -composite -fill White +opaque Black y.png
convert: unrecognized option `-grayscale' @ error/convert.c/ConvertImageCommand/1632.

Re: Extract colors from image

Posted: 2014-03-03T09:48:55-07:00
by Bonzo
I do not think "rgb(242,255,0)" this needs the quotes but it needs the \ so rgb\(242,255,0\)

Re: Extract colors from image

Posted: 2014-03-03T10:00:34-07:00
by snibgo
alasdairdf wrote:convert: unrecognized option `-grayscale'
What version IM are you using? If before 6.8.something, I suggest you upgrade.

Re: Extract colors from image

Posted: 2014-03-03T10:28:31-07:00
by alasdairdf
I can't upgrade, I'm using 6.7.7-7 and I need this exact version as it contains a specific bug fix which then broke again in the next version. This was the version magick did to fix the bug-fix and it is the only version that works.

Bonzo, either works. The problem is with -grayscale.

Re: Extract colors from image

Posted: 2014-03-03T10:56:38-07:00
by snibgo
Instead of "-grayscale Rec709Luminance", you could use "-colorspace Gray".

Re: Extract colors from image

Posted: 2014-03-03T11:30:28-07:00
by fmw42
I do not think "rgb(242,255,0)" this needs the quotes but it needs the \ so rgb\(242,255,0\)
quoting "rgb(242,255,0)" works fine on Unix. I use that rather than escapes. It may be better since it will allow spaces between commas.

Re: Extract colors from image

Posted: 2014-03-04T23:24:48-07:00
by alasdairdf
Instead of "-grayscale Rec709Luminance", you could use "-colorspace Gray".
OK, thanks. But is this identical or is "-grayscale Rec709Luminance" going to give better results? It's possible that I could install two different versions of IM and use a newer version for this and the older version for when I need that, but I would only want to do that if I really do need a newer version for better results on this argument.

Thank you for all your help!

Re: Extract colors from image

Posted: 2014-03-05T01:10:07-07:00
by snibgo
alasdairdf wrote:But is this identical or is "-grayscale Rec709Luminance" going to give better results?
Did you read the link I supplied?
Typically the linear Rec709Luminance formula is used, which is the same formula used when converting images to -colorspace gray.