Page 2 of 2

Re: Replace color but only in some regions

Posted: 2018-10-19T15:10:58-07:00
by snibgo
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.

Re: Replace color but only in some regions

Posted: 2018-10-19T15:55:32-07:00
by cgkas
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

Re: Replace color but only in some regions

Posted: 2018-10-19T16:11:00-07:00
by snibgo
In bash, don't double the % signs, but do escape the parentheses: \( and \).

Re: Replace color but only in some regions

Posted: 2018-10-19T16:24:20-07:00
by cgkas
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.