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!
Separate colors and black
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Separate colors and black
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:
However, this will count "surrounding dark gray pixels" as non-black. Perhaps a fuzz would do the trick, or a morphology erode.
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
snibgo's IM pages: im.snibgo.com
Re: Separate colors and black
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.
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Separate colors and black
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
Re: Separate colors and black
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...
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...
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Separate colors and black
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:
mask.png is white where you have colour, otherwise black. We use the mask in two ways (Windows BAT syntax):
r1.png and r2.png are the required results.
Code: Select all
magick index.png -colorspace HCL -channel G -separate +channel -threshold 30% mask.png
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
snibgo's IM pages: im.snibgo.com
Re: Separate colors and black
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.