Sophisticated fuzz with HSL definition?

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?".
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

I am trying the first code, now with values in correct format. But the mask is really strange. Does not look like shadows. But when I collected the colors I picked them in photohops. Here are my collected values, minimums and maximums collected from HSB in Photoshop and converted to HSL:
http://www.viewdocsonline.com/document/to8qqp

Code: Select all

SET minH=0.547222222
SET minS=0.21
SET minL=0.29
SET maxH=0.616666667
SET maxS=0.4
SET maxL=0.33
Taken 3 minutes.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Sophisticated fuzz with HSL definition?

Post by snibgo »

Perhaps the units are wrong. The numbers from Photoshop: is hue from 0-360, 0-255 or 0-100? Is Saturation 0-100 or 0-255? Is Lightness 0-100 or 0-255?
snibgo's IM pages: im.snibgo.com
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

They are 360, 100, 100 (°,%, %)

30% of lightness means that is it shadow. R U right your algorithm is correct?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Sophisticated fuzz with HSL definition?

Post by fmw42 »

I believe in IM HSL values are either all percent or 0-360,0-255,0-255 except in -modulate where 100,100,100 means no change.

see http://www.imagemagick.org/script/color.php
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

I used the code suggested by snibgo and now it is damn fast! Using Q8.

Code: Select all

REM avgH=0.581944444
REM avgS=0.305
REM avgL=0.31

SET avgH1=58.1944444
SET avgS1=30.5
SET avgL1=31

SET avgH2=44.44444444
SET avgS2=20.5
SET avgL2=38.5

convert test.png -colorspace HSL -fuzz 15%% -fill Black +opaque "rgb(%avgH1%%%,%avgS1%%%,%avgL2%%%)" -colorspace sRGB result_mask.png
pause
But for some reason / I dont understand why this happens / the shadows turned to strange red tone. Can you explain why this happened? I used +opaque instead of -opaque because this increases the performance cca 4x. So the last thing I need is to fill the shadows with white color.

Edit: I have forgot that I need to replace next set of colors.
With these values
160 20,5 38,5
fmw42 wrote:I believe in IM HSL values are either all percent or 0-360,0-255,0-255 except in -modulate where 100,100,100 means no change.

see http://www.imagemagick.org/script/color.php
So my rgb() function looks for incorrect colors?
Last edited by VanGog on 2014-06-01T13:14:05-07:00, edited 3 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Sophisticated fuzz with HSL definition?

Post by fmw42 »

So my rgb() function looks for incorrect colors?
rgb values are all percent or all in range 0-255. It is only hue that has range 0-360 if not specified as percent.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

How can I calculate correct fuzz for HSL model when my values are:

Code: Select all

      °  ,   % ,    %
max:	222,    40,   33
avg:	209.5,  30.5, 31
diff:  12.5 ,  9,5  , 2
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Sophisticated fuzz with HSL definition?

Post by fmw42 »

fuzz=sqrt((diffR^2+diffG^+diffB^2)/3) where diff is the change in R,G,B that you will allow on either side of the specified R,G,B value. However, you cannot control the fuzz for each R,G,B separately. That is why I suggested that you use my 1D fx computation to create a lut and use the 1D lut with -clut to create your mask image.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

fmw42 wrote:fuzz=sqrt((diffR^2+diffG^+diffB^2)/3)
Then how should I calculate diffR , diffG , diffB when I have colors in HSL?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Sophisticated fuzz with HSL definition?

Post by fmw42 »

If you colorspace is HSL and -fuzz respects that colorspace, then just compute diffH, diffS and diffL. Alternately, you can convert your HSL values to RGB and do the fuzz in RGB space.

Note that diff=(max-min)/2 or (max-ave) or (ave-min)
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

But the diff must be in range of 0-100% ...

Code: Select all

min:	197	21	29
max:	222	40	33
avg:	209,5	30,5	31

diff=3,472222222 // °
diff=(1/360)*100*diff // %
diff=10.3%
For the second group of colors I have somewhat high number - that looks weird.

Code: Select all

avg:	160	20,5	38,5
diff=15,55555556 // °
diff=(1/360)*100*diff // %
diff=21,3184265%
But I think the result is correct. But I need to make the code working so I could replace both sets of colors not just one.

Edit:
I dont know. Maybe it would be best to create two different masks and create different effects for them.
Last edited by VanGog on 2014-06-01T15:00:55-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Sophisticated fuzz with HSL definition?

Post by fmw42 »

Diff cannot be 209. For max=222 and min=197, diff=(222-197)/2=12.5. What range are these values 0-255 or 0-360? If the latter, then as percent = 12.5*100/360=3.472%

But as I keep saying, the resulting fuzz will be global for all color channels. Thus it may be larger than one range and smaller than another range for different channels. There is no fuzz per channel. Therefore, I keep going back to my 1D lut solution which respects your exact ranges.

You keep trying to force a "square peg into a round hole"!
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

I had written wrong numbers (corrected) and I had error in the second formula (corrected). So now the 2nd fuzz is 21,31 which seems more realistic.

Before I reject solution I have to try it and been convinced by own experience. Also I have strict order in which I try to solve the tasks, Also if I have code which I can run immediately then I can try it.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

fmw42 wrote:The solution is easy. Just use -fx to create a 1D binary lookup table from your ranges of allowed H,S,L. Then apply the lut using -clut to the HSL version of the image to make a binary mask. Then apply the mask using -mask with your image (HSL or RGB) to do what further processing you want to do.
I don't know what is lut.
fmw42 wrote:Here is what I have in mind.

It takes little time with -fx to create a 1D lut of size say 360x1 or even 3600x1. Then use -clut to apply the lut to the HSL image, which makes it a mask. This takes little time also since it is a lookup table. The mask is then used to limit the processing only to those pixels that were selected by the -fx expression.

The fx expression is made for each channel H, S, L so you get 3 1D grayscale luts that are combined to make one color lut. This is applied to the HSL image to make a mask.

The fx expressions are just ranges of values (r>X && r<Y)?white:black, for the hue channel

You will have to scale the S and L channels by 360x255, if you are working with HSL color values as 0-360, 0-256, 0-255

So something like the following:

Code: Select all

fact=`convert xc: -format "%[fx:360/255]" info:`
convert -size 360x1 xc: -colorspace HSL -fx "(r>Xr && r<Yr)?white:black" redlut.png
convert -size 360x1 xc: -colorspace HSL -fx "(g>Xg*$fact && g<Yg*$fact)?white:black" greenlut.png
convert -size 360x1 xc: -colorspace HSL -fx "(b>Xb*$fact && b<Yb*$fact)?white:black" bluelut.png

convert redlut.png greenlut.png bluelut.png -combine lut.png

convert \( image -colorspace HSL \) lut.png -clut mask.png

convert image -mask mask.png <processing> result
You can make the expressions as complicated as you want.
OK, I am going to try it but must to retype it to valid Windows syntax

Code: Select all

SET fact=`convert xc: -format "%%[fx:360/255]" info:`
convert -size 360x1 xc: -colorspace HSL -fx "(r>Xr && r<Yr)?white:black" redlut.png
convert -size 360x1 xc: -colorspace HSL -fx "(g>Xg*%fact% && g<Yg*%fact%)?white:black" greenlut.png
convert -size 360x1 xc: -colorspace HSL -fx "(b>Xb*%fact% && b<Yb*%fact%)?white:black" bluelut.png

convert redlut.png greenlut.png bluelut.png -combine lut.png

convert \( image -colorspace HSL \) lut.png -clut mask.png

convert image -mask mask.png <processing> result
Why is there the question mark after and backslash before parenthesis? Why parenthesis are there:

Code: Select all

convert \( image -colorspace HSL \) lut.png -clut mask.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Sophisticated fuzz with HSL definition?

Post by fmw42 »

Why is there the question mark after and backslash before parenthesis? Why parenthesis are there:
Unix syntax requires it. In Windows, remove the \

Note, you have to replace X and Y (for r,g,b) with specific values or use variables. The represent the min and max values you want to allow in the ranges of 0-360 for hue (red), 0-255 for sat (green) and 0-255 for lightness (blue). If you got values from Photoshop, I think Sat may range from 0 to 100 only. So it will need to be converted to the range of 0-255.

What kind of processing do you need to do? If you want to replace values in these ranges with other values, then the script would need to be modified. As it is now, you are creating a 1D look up table (lut) to create a mask from your HSL version of the image, which can then be use for any kind of processing.
Post Reply