Extract colors from image

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
alasdairdf
Posts: 53
Joined: 2012-05-15T04:56:50-07:00
Authentication code: 13

Extract colors from image

Post 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!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extract colors from image

Post 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.
snibgo's IM pages: im.snibgo.com
alasdairdf
Posts: 53
Joined: 2012-05-15T04:56:50-07:00
Authentication code: 13

Re: Extract colors from image

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Extract colors from image

Post by Bonzo »

With Linux you need to escape the ( and ) so it becomes /( and /) or is it \( and \) ?
alasdairdf
Posts: 53
Joined: 2012-05-15T04:56:50-07:00
Authentication code: 13

Re: Extract colors from image

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Extract colors from image

Post by Bonzo »

I do not think "rgb(242,255,0)" this needs the quotes but it needs the \ so rgb\(242,255,0\)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extract colors from image

Post by snibgo »

alasdairdf wrote:convert: unrecognized option `-grayscale'
What version IM are you using? If before 6.8.something, I suggest you upgrade.
snibgo's IM pages: im.snibgo.com
alasdairdf
Posts: 53
Joined: 2012-05-15T04:56:50-07:00
Authentication code: 13

Re: Extract colors from image

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extract colors from image

Post by snibgo »

Instead of "-grayscale Rec709Luminance", you could use "-colorspace Gray".
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extract colors from image

Post 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.
alasdairdf
Posts: 53
Joined: 2012-05-15T04:56:50-07:00
Authentication code: 13

Re: Extract colors from image

Post 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!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extract colors from image

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply