Replace color but only in some regions

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

Re: Replace color but only in some regions

Post by snibgo » 2018-10-19T15:10:58-07:00

I don't know what you are doing with connected components. I would create a mask like this (Windows BAT syntax):

Code: Select all

convert ^
  inGreen.png     ^
  -fuzz 20%% ^
  -transparent #4CFE00 ^
  -alpha extract -negate ^
  -threshold 50%% ^
  ( +clone ^
    -define morphology:compose=Lighten ^
    -morphology HMT 2x3+0+1:0,0,1,-,0,0 ^
    -morphology HMT 2x3+1+1:0,0,-,1,0,0 ^
    -negate ^
  ) ^
  -compose Darken -composite ^
  mask.png
You didn't say what you want to happen at the antialised border, so I've just done a fuzz.

Then we can use the mask to composite purple or any colour only where the mask is white:

Code: Select all

convert ^
  inGreen.png     ^
  ( +clone ^
    -fill Purple -colorize 100 ^
  ) ^
  mask.png ^
  -compose Over -composite ^
  out.png
This can be combined into a single command, of course.
snibgo's IM pages: im.snibgo.com

cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Replace color but only in some regions

Post by cgkas » 2018-10-19T15:55:32-07:00

I've sent the first part in this way

Code: Select all

convert in.png   \
  -fuzz 20%% \
  -transparent '#4CFE00' \
  -alpha extract -negate \
  -threshold 50%% \
  ( +clone \
    -define morphology:compose=Lighten \
    -morphology HMT 2x3+0+1:0,0,1,-,0,0 \
    -morphology HMT 2x3+1+1:0,0,-,1,0,0 \
    -negate \
  ) \
  -compose Darken -composite \
  mask.png
But I get this error

Code: Select all

-bash: -compose: command not found

snibgo
Posts: 11327
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Replace color but only in some regions

Post by snibgo » 2018-10-19T16:11:00-07:00

In bash, don't double the % signs, but do escape the parentheses: \( and \).
snibgo's IM pages: im.snibgo.com

cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Replace color but only in some regions

Post by cgkas » 2018-10-19T16:24:20-07:00

Thanks snibgo.

It seems to work pretty fine. Only some few pixel are still green. I remove the fuzz and more pixel become green, so is not the fix needed.

Besides that, how mix both commands to have a a single command to do this?

Thanks again.

Post Reply