Separate colors and black

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
grhey
Posts: 4
Joined: 2019-04-24T03:45:37-07:00
Authentication code: 1152

Separate colors and black

Post by grhey »

Hi,
I'm trying to do the following: given a scanned image on which some elements are in color and others are in black on a white background (say, a red and blue logo and some text in black), separate these two kinds of elements into two distinct images, one which would contain only the elements in color and the other only the elements in black. Note that "black" means "black and the surrounding dark gray pixels".
How would you do this?
Thanks in advance!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Separate colors and black

Post by snibgo »

What version IM, on what platform? Please post a sample input.

I think you want two results. One has all the black pixels turned to white. The second has all the non-black turned to white. Is that correct?

For example:

Code: Select all

magick in.png -fill White -opaque Black out1.png

magick in.png -fill White +opaque Black out2.png
However, this will count "surrounding dark gray pixels" as non-black. Perhaps a fuzz would do the trick, or a morphology erode.
snibgo's IM pages: im.snibgo.com
grhey
Posts: 4
Joined: 2019-04-24T03:45:37-07:00
Authentication code: 1152

Re: Separate colors and black

Post by grhey »

Thanks for your answer!
Indeed your interpretation is correct.
Here is an example input: https://pasteboard.co/IbCF4Sl.jpg .
Alas your commands do not produce the expected result, out1 is essentially identical to the input image, and out2 is completely white.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Separate colors and black

Post by snibgo »

Your input is JPG and has no black pixels. "-fuzz" can help, but JPG is a terrible format for image processing. I suggest you re-scan and save in a lossless format (eg PNG or TIFF).
snibgo's IM pages: im.snibgo.com
grhey
Posts: 4
Joined: 2019-04-24T03:45:37-07:00
Authentication code: 1152

Re: Separate colors and black

Post by grhey »

Thanks again for your answer!
I did what you suggested, see https://pasteboard.co/IbDa8xN.png .
Alas, I get the same output: out1 is identical to the input image, out2 completely white...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Separate colors and black

Post by snibgo »

You want to isolate coloured pixels (ie with a high saturation) against non-coloured pixels (low saturation, eg white, gray and black). I would use the C channel of HCL:

Code: Select all

magick index.png -colorspace HCL -channel G -separate +channel -threshold 30% mask.png
mask.png is white where you have colour, otherwise black. We use the mask in two ways (Windows BAT syntax):

Code: Select all

magick ^
  index.png ^
  ( +clone -colorspace HCL -channel G -separate +channel ^
    -threshold 30%% -morphology dilate disk:2 ^
  ) ^
  ( -clone 0-1 ^
    -compose lighten -composite ^
    -write r1.png ^
    +delete ^
  ) ^
  ( -clone 1 -negate ) ^
  -delete 1 ^
  -compose lighten -composite ^
  r2.png
r1.png and r2.png are the required results.
snibgo's IM pages: im.snibgo.com
grhey
Posts: 4
Joined: 2019-04-24T03:45:37-07:00
Authentication code: 1152

Re: Separate colors and black

Post by grhey »

Wow, thank you very much! I could never have figured this by myself... It's almost perfect, apart from the green color that is not correctly isolated, but lowering the threshold solves that problem.
Post Reply