more clarification on hue angle in modulate command

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?".
Post Reply
ashwini
Posts: 10
Joined: 2012-01-30T05:55:49-07:00
Authentication code: 8675308

more clarification on hue angle in modulate command

Post by ashwini »

hi all,
thanks in advance

i am working on identifying region of image and change the color of that region. i am able to identify region of image by "magicwand script". and applied modulate command . i tried this for different images. and each time i got different outputs. for more understanding see below eg:

original image:
Image

Code: Select all

echo shell_exec('sh magicwand.sh 120,129 -t 26 -f mask -r outside -m binary veg.png mveg.png');
echo exec("convert veg.png  -mask mveg.png -modulate 100,100,30 +mask cveg.png");
and got the below img with hue angle 30 getting pink shade.
Image
Image

//-modulate 127,255,60 i am getting yellow as per hue wheel(yellow shade :60 degree)

Image

//-modulate 100,100,120 i am getting green (green shade:120 degree)
Image

but same i applied for other image i got different results:

Code: Select all

echo shell_exec('sh magicwand.sh 185,37 -t 50 -f mask -r outside -m binary flower.png mflower.png'); 
 echo exec("convert flower.png -mask mflower.png -modulate 100,100,30 +mask cflower.png");
Image

//-modulate 100,100,30 i am getting blue shade which i got differs from the result i got for earlier image.
Image

//-modulate 100,100,120 orange shade earlier was green for 120 degree
Image

-modulate 100,100,166 then i got green for hue 166
Image
i gone through http://www.imagemagick.org/Usage/color_mods/#color_mods and not able to understand the 3rd parameter i.e hue in modulate command .
as per i know hue it is angle between the 0 to 360 degree. and modulate command rotates the color ..i am not undestanding the lines..As you can see a value of '33.3' produces a negative, or counter-clockwise rotation of all the colors by approximately 60 degrees, effectively mapping the red to blue, blue to green, and green to red. Using values of '0' or '200' produces a complete 180 degree negation of the colors, without negating the brightness of the image. A value of '300' will produce 360 degrees in color rotation resulting in no change to the image.

can anyone help me to undestand this... so that i got constant result for hue angle. or any other way?


thanks,
ashwini
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: more clarification on hue angle in modulate command

Post by anthony »

The -modulate operators third option is Hue, but all the parameters of -modulate were basied on percentages such that 100% means no change.

As such 100 means no change 0 means rotate 180 degreess and from this 200 means rotate 180 degress in the oppist direction.

So if red hue is 0, a modulate of 0 => cyan, 100 => red (no change), 200 => cyan, 300 => red, 400 => cyan.

So if your image starts with red. then to convert that to some particular hue (in degrees) you need (hue/360)*200 + 100

But remember module rotated all the hues, so if your starting color is not red, then you need to subtract that hue from your desired hue first (you want the difference). so the module value is ((desired_hue-current_hue)/360)*200+100

To get the current hue convert the image to HSL colorspace and get the HUE (red channel) value. that is the hue as a value from 0 to 1 (if normalized), or 0 to QuantumRange (if not normalized)

QuantumRange = 2 ^ Q where Q is the compile time quailty of IM, typically Q=16.



WARNING: Modulate assumes HSL colorspace which does nto take into account intensity of the red green abd blue color vectors. That it the hues get brighter and darker depending on which hue you are using. This is not particulalry useful.

To rotate Hues properly extra transformations are needed. I am hoping to create a correct 'hue rotation' operator that does keep color intensity constant, and when I do I will ensure its parameter is in degrees, starting at zero!

However I am swamped by IMv7 Shell API updates, so this may not be done for a long time. It is on my To Do

Perhaps Fred would like to create a script to do constant intensite hue rotation. Sounds like something right up his alley.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: more clarification on hue angle in modulate command

Post by fmw42 »

See my script, huemap, at the link below. It can change to hue or range of hues to whatever hue value or range of hues you want. Note this is a unix bash script, so if on windows you need cygwin.

An example is here viewtopic.php?f=1&t=20280&p=80642&hilit=hue#p80642


In -modulate, you are changing the hue relative to its current value. It does not set an absolute hue value. There is no way to specify a specific hue value as you don't know what the current hue is.

If you want to convert some angular change of hue (dhue) to the hue value needed for -modulate (mhue), then use

mhue=(200/360)*dhue + 100

a dhue=0 (no change), yields mhue=100
a dhue=180, yields mhue=200
a dhue=360, yields mhue=300

See docs at http://www.imagemagick.org/script/comma ... p#modulate to confirm that this is correct.

But again these are relative changes in hue and not absolute hue values.

However, in my script, huemap, you can change absolute hue values. I separate the hue channel, then do a fuzzy color change from one hue value to another (taking care of the wrap around at 0 and 360 via a modulo). The recombine the modified hue channel with the Lightness and Saturation channels to convert back to RGB.
ashwini
Posts: 10
Joined: 2012-01-30T05:55:49-07:00
Authentication code: 8675308

more clarification on hue angle in modulate command

Post by ashwini »

hi..thanks for reply

as per you said hue in modulate command changes color as per relative value..that means there is no fix value of hue (like 120=>green 60=>yellow,240=> blue)
so i cannot use that hue value in modulate command to get output for respective color 120=>green, 60=>yellow for all images


i am using color picker to select the color value which is in hsv format. so i need constant value of hue i.e if i am selecting green as 120 degree then output image result should in green..etc

as u said about huemap script, it takes source color and replace that with destination color. that means suppose src hue is red then it replaces all red areas.
but i want color replacement should be done on selected region which i am doing by magicwand as i posted eg.

so if modulate command is not helpful in my case..then how can i achieve this both task of identifying region and replacing color..? or is it possible to do region selection by magicwand script as i done in my previous post and on that mask i applied huemap script .

any help will be appreciated!!!


thanks,
ashwini
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: more clarification on hue angle in modulate command

Post by fmw42 »

You can effectively get huemap to process the whole image and then create a mask of the area you want (perhaps with my script magickwand). Then use the mask to composite the modified image from huemap with the original so that you get the change only in the area you want.

see
http://www.imagemagick.org/Usage/compose/#compose
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: more clarification on hue angle in modulate command

Post by anthony »

This is also what is done to change the color of a shirt in...
IM Examples Chroma Key Masking
http://www.imagemagick.org/Usage/photos/#chroma_key

Create a mask to limit what is changed. Select the color in the original image you want to change, calculate the hue rotation needed, and apply it.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply