Page 1 of 1

Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T12:04:03-07:00
by mtcarlso
I am trying to color the white (with black stitching) tie on this image [https://s3.amazonaws.com/mtcarlso_help/help/white.png] with some predetermined colors so that the stitching and light/shading details remain. I have met with some success but can't get quite close enough to the desired effect.

My best result has been to use this image [https://s3.amazonaws.com/mtcarlso_help/help/input.png] with a basic red hue as a starter, and to modulate the HSL for a subset of colors with this simple modulate command:

Code: Select all

convert input.png -mask mask.png -modulate [values] output.png
I have been testing with these colors to almost-acceptible results:
Royal: 22,200,30 [https://s3.amazonaws.com/mtcarlso_help/ ... _royal.png]
Poppy: 42,200,98 [https://s3.amazonaws.com/mtcarlso_help/ ... _poppy.png]
Plum: 10,200,60 [https://s3.amazonaws.com/mtcarlso_help/ ... l_plum.png]

As you can see from this crude reference [https://s3.amazonaws.com/mtcarlso_help/ ... erence.png], the output is pretty close to the desired colors, especially poppy and royal. However, we're losing some luminance(?) and a little bit of black detail. This is particularly evident in the plum sample. I have played around with the LCHuv and LCHab color spaces, also using modulate commands but have not been able to create desirable results. For example, this modification [https://s3.amazonaws.com/mtcarlso_help/ ... l_plum.png] seems to be the inverse of what I want dark red where it should be light and light red where it should be dark.

Any recommendations?

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T12:38:23-07:00
by fmw42
I do not know if you can use any arbitrary colorspace. One would have to look in the code. It does accept HSL, HSB and as of 6.8.4.7 LCHuv and LCHab. I think a better colorspace might be HSI or HCLp, but I do not know if those work or default to HSL, if not. I would have to test that or you could.

But to work with other colorspace, you need to use the -define modulate:colorspace , see

http://www.imagemagick.org/Usage/color_ ... colorspace
http://www.imagemagick.org/Usage/color_ ... dulate_HCL


You can work with any colorspace by converting the image to that colorspace and then modifying the Hue and or Sat/Chroma channels as desired, then converting back.

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T13:32:53-07:00
by mtcarlso
Thank you for the quick reply. HCLp and HSI look identical to HSL. Any other thoughts?

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T14:38:01-07:00
by fmw42
mtcarlso wrote:Thank you for the quick reply. HCLp and HSI look identical to HSL. Any other thoughts?

Do the processing as I suggested by converting to HCLp (HSI), separate channels, then combine.

see

http://www.imagemagick.org/Usage/color_basics/#separate
http://www.imagemagick.org/Usage/color_ ... bine_other

Can you post your exact command with values for modulate and your mask image for the image you posted?

I can then try to provide code. Are you on Window or Linux/Mac? What version of IM. I would do this on my Mac, so unix syntax for IM commands.

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T15:01:29-07:00
by mtcarlso
Ahh, I am sorry, I wasn't catching you fully. I will try that now. Thank you for the deep links.

I am running the script through a few different colors, but here are 3 that I have been testing with:

Code: Select all

convert input.png -mask mask.png -modulate 22,200,30 royal.png
convert input.png -mask mask.png -modulate 42,200,98 poppy.png
convert input.png -mask mask.png -modulate 10,200,60 plum.png
The mask I am using is here: https://s3.amazonaws.com/mtcarlso_help/help/mask.png

I am on a Mac running Version: ImageMagick 6.8.8-6 Q16

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T16:06:19-07:00
by fmw42
This command does not seem to work for me on Mac OSX with IM 6.8.9.5 Q16.

Code: Select all

convert input.png -mask mask.png -modulate 10,200,60 plum.png

The result is a black tie. Are you sure you have the arguments to -modulate in the correct order?

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T16:13:41-07:00
by mtcarlso
It certainly makes sense to me to start from the white (https://s3.amazonaws.com/mtcarlso_help/help/white.png) and apply the values from there, I had just had success starting from input.png and so was working from there.

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T16:25:46-07:00
by fmw42
mtcarlso wrote:It certainly makes sense to me to start from the white (https://s3.amazonaws.com/mtcarlso_help/help/white.png) and apply the values from there, I had just had success starting from input.png and so was working from there.
You cannot modulate to change saturation black/gray/white. They have no saturation to change.

Also see my edited comments above. Your command does not work for me. I get a black tie as a result starting with input.png (with the reddish tie).

Also red hue is the same as black hue, both have hue=0.

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T16:35:08-07:00
by fmw42
try something like this

Code: Select all

color="plum"
convert input.png \
\( -clone 0 -colorspace gray +level-colors black,plum \) \
\( mask.png -negate \) -compose over -composite input_$color.png
You can change the color to anything you want (rgb, hex, colornames, hsl etc)

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T16:49:19-07:00
by snibgo
The modulate command won't work well on white.png. In that image, the tie is grey, which means the saturation is very low, and the hue is all over the place. When you shift the hue and increase the saturation, the hue will still be all over the place.

I wouldn't use "-modulate". Instead, something like this seems more obvious. The required colour is given in "rgb(...)". Windows BAT syntax:

Code: Select all

%IM%convert ^
  white.png ^
  ( +clone ^
    -level 0%%,70%% ^
    -alpha set -channel A -evaluate set 20%% +channel ^
    ( +clone -fill rgb(100%%,50%%,50%%) -colorize 100 -alpha off ) ^
    +swap -composite ^
  ) ^
  ( mask.png -negate ) ^
  -composite ^
  r.png

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T17:37:31-07:00
by mtcarlso
@fmw42 - Strange that you were getting a black tie. I reran and still have the same result. The redish tie with the command netted me a dark purple tie. That aside, your other solution works quite well. It does seem that the result loses just a very small bit of brightness or luminance or white (I am not sure which). For example, with royal (#F9FF4B) the result is just a touch darker than I would expect (you can see the desired result in the big blue swatch at the top-center of https://s3.amazonaws.com/mtcarlso_help/ ... erence.png) - any thoughts or tweaks?

@snibgo - Will try to check out your solve before bed. Thanks very much for responding.

And my thanks to both of you for the clarifications on the modulation of (effectively) hue-less colors.

Re: Using HSL and LCHuv to adjust hue on white image

Posted: 2014-07-17T18:02:07-07:00
by fmw42
If you want the color to be a bit brighter change black to grayX (gray5). Adjust X as desired.


color="#F9FF4B"
convert input.png \
\( -clone 0 -colorspace gray +level-colors black,$color \) \
\( mask.png -negate \) -compose over -composite input_$color.png

color="#F9FF4B"
convert input.png \
\( -clone 0 -colorspace gray +level-colors gray5,$color \) \
\( mask.png -negate \) -compose over -composite input2_$color.png