Remove everything but one color from the image (command line or code)

I would like to extract one color from the image (for example, # a87830), changing every other color to white. The input may be a bit noisy, so neighboring pixels of the same color may be included. Ideally, all non-white pixels at the end will be converted to black (i.e., the Output is an image with a depth of 1 bit).

I may need a batch process, so I hope for something like the imagemagick command line, or something that I could encode using the PHP imagemagick extension. (I am sure that ImageMagick should be able to do this if the necessary parameters can simply be processed, so I marked it, but I am open to other software if it works on Linux.)

Background: I am trying to complete the first stage of preprocessing for the ContourTrace program , which this image is well shown:

enter image description here

+6
source share
2 answers

Here is one example:

convert \ http://i.stack.imgur.com/rK259.png \ -fuzz 33% \ -fill black \ -opaque "#A87830" \ -threshold 12% \ black+white.png 

The -fuzz matches all colors that are in a certain proximity to the color specified by -opaque . No -fuzz , and you will only match those pixels that exactly match "#A87830" .

-threshold converts color pixels to black or white, where the percentage determines the limit: above it becomes black, below it becomes white.

black + white.png

You can change the command from -opaque "<color-definition>" to +opaque "<color-definition>" to invert the color selection value: it will replace pixels that are NOT this color (this time I skipped the -threshold option to save flowers):

 convert \ http://i.stack.imgur.com/rK259.png \ -fuzz 33% \ -fill black \ +opaque "#FFFFFF" \ other.png 

other.png

+7
source

Not sure if this is complicated enough for you. Convert all the pixels that are similar, within 30% of your declared color, to black, and then make the rest white.

 convert in.png -fuzz 30% -fill black -opaque "#a87830" -threshold 10% out.png 

enter image description here

You can “add” more tones that you would like to turn black by adding additional -opaque , for example,

 convert in.jpg -fuzz 20% -fill black \ -opaque "#a87830" \ -opaque "#a6725f" \ -threshold 1% out.png 

which can allow you to reduce fuzz and thereby remove other tones that you do not want.

+9
source

Source: https://habr.com/ru/post/985492/


All Articles