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

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

Files:
test2_hsb_d-0.png
test2_hsb_d-1.png
test2_hsb_d-2.png
Are these names named as intended?

test2_hsb_mask2_0.png
empty

test2_hsb_mask2_1.png
empty

test2_hsb_mask2_2.png
fine

test2_hsb_mask2.png
empty

Code: Select all

REM Use Raw RGB values from Adobe Photoshop Color Picker:
SET minH1=197
SET minS1=222
SET minB1=21
SET maxH1=40
SET maxS1=29
SET maxB1=33

SET minH2=104
SET minS2=8
SET minB2=31
SET maxH2=216
SET maxS2=33
SET maxB2=46


convert -size 360x1 xc: -fx "(i>=%minH1% && i<=%maxH1%)?white:black" h_lut2.png
convert -size 360x1 xc: -fx "(i>=%minS1%*360/100 && i<=%maxS1%*360/100)?white:black" s_lut2.png
convert -size 360x1 xc: -fx "(i>=%minB1%*360/100 && i<=%maxB1%*360/100)?white:black" b_lut2.png
convert test2.png -colorspace hsb -separate test2_hsb_%%d.png
convert test2_hsb_0.png h_lut2.png -clut test2_hsb_mask2_0.png
convert test2_hsb_1.png s_lut2.png -clut test2_hsb_mask2_1.png
convert test2_hsb_2.png b_lut2.png -clut test2_hsb_mask2_2.png
convert test2_hsb_mask2_0.png test2_hsb_mask2_1.png test2_hsb_mask2_2.png -evaluate-sequence multiply test2_hsb_mask2.png
pause
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 »

Files:
test2_hsb_d-0.png
test2_hsb_d-1.png
test2_hsb_d-2.png
Are these names named as intended?
No, they should end up just as


Files:
test2_hsb_0.png
test2_hsb_1.png
test2_hsb_2.png

This statement is not working

Code: Select all

convert test2.png -colorspace hsb -separate test2_hsb_%%d.png
I do not know the equivalent in Windows syntax.


Note that I did

convert test.png test2.png

because your test.png was giving me a warning or error. So to be safe I converted it in IM. The result looked the same as your file, but did not give me any further messages.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

I have this:

Code: Select all

REM Use Raw RGB values from Adobe Photoshop Color Picker:
SET minH1=197
SET minS1=222
SET minB1=21
SET maxH1=40
SET maxS1=29
SET maxB1=33

SET minH2=104
SET minS2=8
SET minB2=31
SET maxH2=216
SET maxS2=33
SET maxB2=46


convert -size 360x1 xc: -fx "(i>=%minH1% && i<=%maxH1%)?white:black" h_lut2.png
convert -size 360x1 xc: -fx "(i>=%minS1%*360/100 && i<=%maxS1%*360/100)?white:black" s_lut2.png
convert -size 360x1 xc: -fx "(i>=%minB1%*360/100 && i<=%maxB1%*360/100)?white:black" b_lut2.png
convert test2.png -colorspace hsb -separate test2_hsb.png
convert test2_hsb-0.png h_lut2.png -clut test2_hsb_mask2_0.png
convert test2_hsb-1.png s_lut2.png -clut test2_hsb_mask2_1.png
convert test2_hsb-2.png b_lut2.png -clut test2_hsb_mask2_2.png
convert test2_hsb_mask2_0.png test2_hsb_mask2_1.png test2_hsb_mask2_2.png -evaluate-sequence multiply test2_hsb_mask2.png
test2_hsb_mask2_0.png
and
test2_hsb_mask2_1.png
are still empty
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 »

Code: Select all

SET minH1=197
SET minS1=222
SET minB1=21
SET maxH1=40
SET maxS1=29
SET maxB1=33
You should have min less than max.
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 »

Thanks. Works now. I will improve the colors to cover the 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 added one more group of colors and repaired the colors in group 1 and 2, The third group should cover light gray colors of route shadows. However now I am stuck. It looks I have a bug there because it selects green grass/trees non shadows too.

For example exact colors of grass in raw HSB:
89, 34, 49 (is RGB 104 125 82)
89, 34, 50
89, 33, 52
and similar... these values are not included in my settings but they are in the mask. Hue 89° is covered in the third group only but not 33-34 saturation...

Code: Select all

REM Use Raw RGB values from Adobe Photoshop Color Picker:
REM Dark shadows on route and buildings
SET minH1=186
SET maxH1=240
SET minS1=21
SET maxS1=52
SET minB1=23
SET maxB1=54

REM Light shadows on route and buildings
SET minH2=158
SET maxH2=216
SET minS2=8
SET maxS2=33
SET minB2=31
SET maxB2=46

REM Light shadows Gray - on route and buildings
SET minH3=37
SET maxH3=180
SET minS3=5
SET maxS3=12
SET minB3=35
SET maxB3=47


convert -size 360x1 xc: -fx "((i>=%minH1% && i<=%maxH1%)||(i>=%minH2% && i<=%maxH2%)||(i>=%minH3% && i<=%maxH3%)  )?white:black" h_lut2.png
convert -size 360x1 xc: -fx "((i>=%minS1%*360/100 && i<=%maxS1%*360/100)||(i>=%minS2%*360/100 && i<=%maxS2%*360/100)||(i>=%minS3%*360/100 && i<=%maxS3%*360/100)  )?white:black" s_lut2.png
convert -size 360x1 xc: -fx "((i>=%minB1%*360/100 && i<=%maxB1%*360/100)||(i>=%minB2%*360/100 && i<=%maxB2%*360/100)||(i>=%minB3%*360/100 && i<=%maxB3%*360/100)  )?white:black" b_lut2.png
convert test2.png -colorspace hsb -separate test2_hsb.png
convert test2_hsb-0.png h_lut2.png -clut test2_hsb_mask2_0.png
convert test2_hsb-1.png s_lut2.png -clut test2_hsb_mask2_1.png
convert test2_hsb-2.png b_lut2.png -clut test2_hsb_mask2_2.png
convert test2_hsb_mask2_0.png test2_hsb_mask2_1.png test2_hsb_mask2_2.png -evaluate-sequence multiply test2_hsb_mask2.png
pause
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 don't think you can combine conditionals as you have, because until you combine the masks the sets are independent. So you need to make a mask for each set of H,S,B values so that you combine only the values in the one set of H, S, B values together. Then do the same for the second set of H,S,B conditions, and for the third set. You end up with 3 masks, one from each set of conditions. Then combine them together with -evaluate-sequence add (for the logical ||) rather than -evaluate-sequence multiply. The multiply is needed to combine within a set (logical &&) and the add is needed to combine the results from each combined set (logical ||).
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

I undestand you. So it will be better to convert my HSB colors to HSL values and apply your HSL code:

Code: Select all

convert -size 1x360 gradient: -fx "( (u>%minH1%/360 && u<%maxH1%/360)||(u>%minH2%/360 && u<%maxH2%/360)  )?white:black" red_hue_lut.png
convert -size 1x360 gradient: -fx "( (u>%minS1%/255 && u<%maxS1%/255)||(u>%minS2%/360 && u<%maxS2%/360) )?white:black" green_sat_lut.png
convert -size 1x360 gradient: -fx "( (u>%minL1%/255 && u<%maxL1%/255)||(u>%minL2%/360 && u<%maxL2%/360) )?white:black" blue_light_lut.png

convert red_hue_lut.png green_sat_lut.png blue_light_lut.png -evaluate-sequence multiply lut.png
convert ( test.jpg -colorspace HSL ) lut.png -clut mask.png
Because this one is much more faster, simpler and more effective. I can use some online tool to convert my colors to HSL. I will create three masks and it will be better to operate with every mask separately (edit:it does not look there is some tool for it).

I have one more question. Is it possible to use gray colors in mask to create semi-transparency?
Last edited by VanGog on 2014-06-03T15:31:46-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 »

I undestand you. So it will be better to convert my HSB colors to HSV values and apply your HSL code:
No. HSB and HSV are the same and both are different from HSL. Photoshop is giving you HSB values not HSL values. All I want you to do is just use one conditional i>Hmin && i<Hmax, etc for S and B (HSB from PS values). Create the combined mask as in my commands. Then repeat for another set of min and max values to create another mask. Do that as many times as you have sets of min and max. Then combine all the masks together with -evaluate-sequence add to make the final mask.

It is possible to make a grayscale mask, but you will then need to change the code so that the conditionals specify a graylevel rather than white and black. This may not be trivial. You would have to provide an example of how you want your HSB value ranges to become grayscale.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

It was typo. I wanted to type to 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 »

VanGog wrote:It was typo. I wanted to type to HSL.
You are better off staying with HSB, since that is what you get from Photoshop color picker. You could have just as easily used RGB.

There is no point in converting from HSB (or RGB) to HSL and then changing the script to use HSL in place of HSB.

All you are doing is creating a binary mask. Once you have the mask, it can be used for processing in any colorspace even 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 »

This appears to create the same mask, but is more efficient code and faster. Unix syntax.

New code:

Code: Select all

Hmin=104
Hmax=216
Smin=8
Smax=33
Bmin=31
Bmax=46
convert -size 360x1 xc: -fx "(i>$Hmin && i<$Hmax)?white:black" h_lut2.png
convert -size 360x1 xc: -fx "(i>$Smin*3.6 && i<$Smax*3.6)?white:black" s_lut2.png
convert -size 360x1 xc: -fx "(i>$Bmin*3.6 && i<$Bmax*3.6)?white:black" b_lut2.png
convert h_lut2.png s_lut2.png  b_lut2.png -combine lut2.png
convert \( test2.png -colorspace hsb \) lut2.png -clut -separate -evaluate-sequence multiply test2_hsb_mask2c.png
If you want to make it faster, then do the multiplication by 3.6 to the min and max (S and B only) variables before hand and remove the 3.6 from -fx.


Old Code:

Code: Select all

Hmin=104
Hmax=216
Smin=8
Smax=33
Bmin=31
Bmax=46
convert -size 360x1 xc: -fx "(i>$Hmin && i<$Hmax)?white:black" h_lut2.png
convert -size 360x1 xc: -fx "(i>$Smin*360/100 && i<$Smax*360/100)?white:black" s_lut2.png
convert -size 360x1 xc: -fx "(i>$Bmin*360/100 && i<$Bmax*360/100)?white:black" b_lut2.png
convert test2.png -colorspace hsb -separate test2_hsb_%d.png
convert test2_hsb_0.png h_lut2.png -clut test2_hsb_mask2_0.png
convert test2_hsb_1.png s_lut2.png -clut test2_hsb_mask2_1.png
convert test2_hsb_2.png b_lut2.png -clut test2_hsb_mask2_2.png
convert test2_hsb_mask2_0.png test2_hsb_mask2_1.png test2_hsb_mask2_2.png -evaluate-sequence multiply test2_hsb_mask2.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 »

This (unix) code combines it all into one command line.

Code: Select all

infile="test2.png"
Hmin=104
Hmax=216
Smin=8
Smax=33
Bmin=31
Bmax=46
Smin=`convert xc: -format "%[fx:$Smin*3.6]" info:`
Smax=`convert xc: -format "%[fx:$Smax*3.6]" info:`
Bmin=`convert xc: -format "%[fx:$Bmin*3.6]" info:`
Bmax=`convert xc: -format "%[fx:$Bmax*3.6]" info:`
inname=`convert "$infile" -format "%t" info:`
convert \
\( -size 360x1 xc: -fx "(i>$Hmin && i<$Hmax)?white:black" \) \
\( -size 360x1 xc: -fx "(i>$Smin && i<$Smax)?white:black" \) \
\( -size 360x1 xc: -fx "(i>$Bmin && i<$Bmax)?white:black" \) \
\( -clone 0-2 -combine \) -delete 0-2 \
\( "$infile" -colorspace hsb \) \
+swap -clut -separate -evaluate-sequence multiply ${inname}_mask.png
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:
VanGog wrote:There is no point in converting from HSB (or RGB) to HSL and then changing the script to use HSL in place of HSB.
I dont agree with you. There is big difference between the two codes (HSB/HSL) because the HSL is at least 3x faster and performs less executions. Performance is high priority for me. I probably will use Javascript to calculate HSV values.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <script>
  function HSB2HSL(Hhsb,Shsb,B){
  Shsb=Shsb/100;B=B/100;
  L = 0.5 * B * (2 - Shsb)
  Shsl = (Shsb*B)/(1-Math.abs(2*L-1) ) 
  return {H:Hhsb, S:Shsl, L:L }
  }  </script>
  <body onload="var HSL=HSB2HSL(37, 5, 35);document.write(HSL.H + ',' + HSL.S + ',' + HSL.L)">
  </body>
</html>
In the example above I got:
37 , 0.02564102564102564 , 0.34125 (I hope it's correct)

New more complex converter in html here:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title>HSB to HSL convertor for CMD/batch use - edit the file as needed</title>
  </head>
  <script>
  function HSB2HSL(Hhsb,Shsb,B){
    Shsb=Shsb/100;B=B/100;
    L = 0.5 * B * (2 - Shsb)
    Shsl = (Shsb*B)/(1-Math.abs(2*L-1) ) 
  return {H:Hhsb, S:Shsl, L:L }
  }
  
  function printVariables(REM,groupNo){
  document.write("<b>"+REM+"</b><br>");
  // console.log(groupNo);
  var Min = eval('c'+groupNo+'min');
  var Max = eval('c'+groupNo+'max');
  // console.log(Min);
  // console.log(Max);
  document.write(
  'SET minH'+groupNo+'='+Min.H+'<br>'+
  'SET maxH'+groupNo+'='+Max.H+'<br>'+
  'SET minS'+groupNo+'='+(Min.S*100)+'<br>'+
  'SET maxS'+groupNo+'='+(Max.S*100)+'<br>'+
  'SET minL'+groupNo+'='+(Min.L*100)+'<br>'+
  'SET maxL'+groupNo+'='+(Max.L*100)+'<br>'+
  '<p>'
  );
  }
  
  </script>

<script>
/*******************************************
EDIT SECTION BELLOW AS NEEDED
********************************************/
document.write(
'<h3>REM Use HSL colors. Convert raw HSB values from Adobe Photoshop Color Picker by HSB2HSL_converter:</h3></br>'
);
var c1min=HSB2HSL(186,21,23);
var c1max=HSB2HSL(240,52,54);
printVariables('REM Dark shadows on route and buildings',1);

var c2min=HSB2HSL(158,8,31);
var c2max=HSB2HSL(216,33,46);
printVariables('REM Light shadows on route and buildings',2);

var c3min=HSB2HSL(37,5,35);
var c3max=HSB2HSL(180,12,47);
printVariables('REM Light shadows Gray - on route and buildings',3);

</script>
</html>
I just hope that the calculation is correct:
HSB2HSL(186,21,23) converts to 186, 11.731843575418996, 20.585
I am not sure if the HSL code works because I got empty mask if I use first group of conditions.

This is the code I am trying with the converted colors (should be HSL):

Code: Select all

REM Use HSL colors. Convert raw HSB values from Adobe Photoshop Color Picker by HSB2HSL_converter:

REM Dark shadows on route and buildings
SET minH1=186
SET maxH1=240
SET minS1=11.731843575418996
SET maxS1=35.135135135135144
SET minL1=20.585
SET maxL1=39.96

REM Light shadows on route and buildings
SET minH2=158
SET maxH2=216
SET minS2=4.166666666666667
SET maxS2=19.76047904191617
SET minL2=29.759999999999998
SET maxL2=38.41

REM Light shadows Gray - on route and buildings
SET minH3=37
SET maxH3=180
SET minS3=2.564102564102564
SET maxS3=6.382978723404255
SET minL3=34.125
SET maxL3=44.18

convert -size 1x360 gradient: -fx "( u>%minH1%/360 && u<%maxH1%/360 )?white:black" red_hue_lut.png
convert -size 1x360 gradient: -fx "( u>%minS1%/255 && u<%maxS1%/255 )?white:black" green_sat_lut.png
convert -size 1x360 gradient: -fx "( u>%minL1%/255 && u<%maxL1%/255 )?white:black" blue_light_lut.png

convert red_hue_lut.png green_sat_lut.png blue_light_lut.png -evaluate-sequence multiply lut.png
convert ( test.jpg -colorspace HSL ) lut.png -clut mask.png
convert test.jpg -mask mask.png <processing> result.png
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

I tested the converted values in HSL to uses with the previous codes using HSL which we were using earlier and no one of them works.

This code from earlier:

Code: Select all

REM HSB values:
SET Hmin=104
SET Hmax=216
SET Smin=8
SET Smax=33
SET Lmin=31
SET Lmax=46

convert -size 1x360 gradient: -fx "(u.r>=%Hmin%/360 && u.r<=%Hmax%/360) && (u.g>=%Smin%/100 && u.g<=%Smax%/100) && (u.b>=%Lmin%/100 && u.b<=%Lmax%/100)?white:black" lut.png
convert ( test.png -colorspace HSL ) lut.png -clut test_mask_hsl2.png
Has incorrect values of colors, generates mask with incorrect colors.

After I change the colors to HSL:

Code: Select all

REM REAL HSL VALUES:
SET minH1=186
SET maxH1=240
SET minS1=11.731843575418996
SET maxS1=35.135135135135144
SET minL1=20.585
SET maxL1=39.96

convert -size 1x360 gradient: -fx "(u.r>=%minH1%/360 && u.r<=%maxH1%/360) && (u.g>=%minS1%/100 && u.g<=%maxS1%/100) && (u.b>=%minL1%/100 && u.b<=%maxL1%/100)?white:black" lut.png
convert ( test.png -colorspace HSL ) lut.png -clut test_mask_hsl2.png
I have empty mask.
When I check the lut file it is empty. But I don't understand how the code could work. As I understand (?or not?) the first command uses RGB colors but it compares with HSL constants. So how could this work?

Newer code:

Code: Select all

REM Use HSL colors. Convert raw HSB values from Adobe Photoshop Color Picker by HSB2HSL_converter:

REM Dark shadows on route and buildings
SET minH1=186
SET maxH1=240
SET minS1=11.731843575418996
SET maxS1=35.135135135135144
SET minL1=20.585
SET maxL1=39.96

REM Light shadows on route and buildings
SET minH2=158
SET maxH2=216
SET minS2=4.166666666666667
SET maxS2=19.76047904191617
SET minL2=29.759999999999998
SET maxL2=38.41

REM Light shadows Gray - on route and buildings
SET minH3=37
SET maxH3=180
SET minS3=2.564102564102564
SET maxS3=6.382978723404255
SET minL3=34.125
SET maxL3=44.18

REM Postby fmw42 » 2014-06-01T17:32:54+00:00 
convert -size 1x360 gradient: -fx "( u>=%minH1%/360 && u<=%maxH1%/360 )?white:black" red_hue_lut.png
convert -size 1x360 gradient: -fx "( u>=%minS1%/255 && u<=%maxS1%/255 )?white:black" green_sat_lut.png
convert -size 1x360 gradient: -fx "( u>=%minL1%/255 && u<=%maxL1%/255 )?white:black" blue_light_lut.png

convert red_hue_lut.png green_sat_lut.png blue_light_lut.png -evaluate-sequence multiply lut.png
convert ( test.jpg -colorspace HSL ) lut.png -clut mask.png

convert test.jpg -mask mask.png <processing> result.png
pause
blue_light_lut.png and green_sat_lut.png white pixels are positioned at bottom and they cover themselves partially.
red_hue_lut.png is positioned at 2/4 of the height of the image and he cannot cover the pixels of the first two images. Why is not positioned correctly to make overlay? This could be reason why the previous code does not show any pixel in lut file.

Edit:
I tested your new code. It works, generates the mask as expected, but it is still a little bit later than the previous codes in HSL. Can you explain why the HSL version dont work to generate mask?

Edit2:
When you use -fx in one command to search pixels in image, is it possible to use -fx more times for different images in one command?
Last edited by VanGog on 2014-06-04T10:15:02-07:00, edited 1 time in total.
Post Reply