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?".
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 »

You are not using the correct code. Try my code that uses i not u.r etc.

The r, g, b are not really red, green and blue, but simply the first, second and third channel.

The color picker from PS is computing HSB not HSL. So you must ensure that you convert HSB to HSL properly and consistently with what the code expects if you want to use HSL. But note that HSL is hard to work with, since pure r,g,b,c,m,y colors are at 50% lightness. I would shy away from using HSL. However, if you want to use those color values, be sure that you compute the values consistent with H 0-360, S 0-100 and L 0-100. Then use the following code. This is unix and so you will need to convert it to window syntax.

The easiest way to do the conversion is

Code: Select all

convert -size 1x1 xc:"hsb(hval,sval,bval)" -colorspace HSL -format "%[pixel:u.p{0,0}]" info:
That will present you with HSL values in percent. You will then need to convert the hue percent to the range 0 to 360 to use the following code. (or add the conversion of *3.6 in the hue part of -fx, just as for S and L)

Code: Select all

Hmin=your computed value
Hmax=your computed value
Smin=your computed value
Smax=your computed value
Lmin=your computed value
Lmax=your computed value
convert -size 360x1 xc: -fx "(i>$Hmin && i<$Hmax)?white:black" h_lut.png
convert -size 360x1 xc: -fx "(i>$Smin*3.6 && i<$Smax*3.6)?white:black" s_lut.png
convert -size 360x1 xc: -fx "(i>$Lmin*3.6 && i<$Lmax*3.6)?white:black" l_lut.png
convert h_lut.png s_lut.png  l_lut2.png -combine lut.png
convert \( test2.png -colorspace hsl \) lut.png -clut -separate -evaluate-sequence multiply test2_hsl_mask.png
I do not believe that there will any significant time different in computing this mask between HSL and HSB. It will be faster if you use my one command line version so that no intermediate images are saved.

So this should be very fast

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" \) \
\( -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 \
\( test2.png -colorspace hsb \) \
+swap -clut -separate -evaluate-sequence multiply test2_mask.png
You can, if you insist, replace B with L, but I really doubt you will see a time difference.

You can process two images at the same time, but you need to duplicate code and use -write to save the earlier results. However, if you have multiply images to process, the best way would be to put the command in a loop over each image.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

Great. I will learn something new now. When I see you how you work with parenthesis like there are separate images within them it seems quite easy to reach it in one command. This is what I exactly wanted. But I did not test the code, yet, I am also reading about meaning of the commands. I will update when I finish.
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:Great. I will learn something new now. When I see you how you work with parenthesis like there are separate images within them it seems quite easy to reach it in one command. This is what I exactly wanted. But I did not test the code, yet, I am also reading about meaning of the commands. I will update when I finish.
For Windows, remove the \ before \( and \) and replace end of line \ with end of line ^

See
http://www.imagemagick.org/Usage/windows/
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#list_ops
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

I already used to the differences between Unix and Windows syntax.

Note: It would be good idea to take the IM v6 html document and create Windows version by replacing all the Unix syntax differences for Windows syntax, so Windows users woundn't be confused (beginners).

Is it correct to

Code: Select all

( -clone 0-2 -combine )
? Why you create another copy of the images? Shouldn't be more correct to access them directly? Something lke ( ('0-2') -combine )?

Here is my code in Windows, but I got only two white pixels in the image. I did not change the values.

Code: Select all

SET minH=104
SET maxH=216
SET minS=8
SET maxS=33
SET minB=31
SET maxB=46
convert ^
( -size 360x1 xc: -fx "(i>%minH% && i<%maxH%)?white:black" ) ^
( -size 360x1 xc: -fx "(i>%minS% && i<%maxS%)?white:black" ) ^
( -size 360x1 xc: -fx "(i>%minB% && i<%maxB%)?white:black" ) ^
( -clone 0-2 -combine ) -delete 0-2 ^
( test2.jpg -colorspace hsb ) ^
+swap -clut -separate -evaluate-sequence multiply test2_mask.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 »

Here was my original code in unix syntax posted earlier

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
When copying for you in my last email, I forgot about the conversion by *3.6 prior to -fx to make it more efficient. The clones are not needed. I had them there in my testing when I computed the colorspace change before the -fx. So here is the corrected code for the raw HSB values.

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" \) \
\( -size 360x1 xc: -fx "(i>$Smin*3.6 && i<$Smax*3.6)?white:black" \) \
\( -size 360x1 xc: -fx "(i>$Bmin*3.6 && i<$Bmax*3.6)?white:black" \) \
-combine  \( test2.png  -colorspace hsb \) \
+swap -clut -separate -evaluate-sequence multiply test2_mask.png
Or you can do the multiply ahead of time in Windows such that you have

Code: Select all

# ---- below --- not valid multiplications in unix - replace with proper windows syntax
Hmin=104
Hmax=216
Smin=8*3.6
Smax=33*3.6
Bmin=31*3.6
Bmax=46*3.6
# ---- above --- not valid multiplications in unix 
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" \) \
-combine  \( test2.png  -colorspace hsb \) +swap \
-clut -separate -evaluate-sequence multiply test2_mask.png
Doing the multiply by 3.6 ahead of time will save time in the -fx computations, since it would do it for every one of the 360 pixels.

If you want this to be as fast as possible, then do it with R,G,B values (all in the range 0-255) and remove the -colospace hsb. The lut size would be -size 256x1 and no multiplication factors would be needed. You might try both ways and see if you get the same 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 »

Thanks. Now it works.
I need clarification to use of
+swap
Does this command mean that it is like to take the result - the lut image which is the last image - and to place it in the place of +swap? As I understand IM now - first we must define the file or image to work ... so here the +swap takes the last image in the process and passes it to input of -clut command, right?
Last edited by VanGog on 2014-06-04T12:18:58-07:00, edited 2 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 »

The code has created the lut before the image that it works on. The +swap reverse the two image's order so that any further processing uses the image before the lut as is required by the syntax for -clut.

see
http://www.imagemagick.org/Usage/basics/#swap
http://www.imagemagick.org/Usage/color_mods/#color_lut
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 really want this to be as fast as possible, then do it with R,G,B values (all in the range 0-255) and remove the -colospace hsb. The lut size would be -size 256x1 and no multiplication factors would be needed. You might try both ways (hsb and rgb) and see if you get the same 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 »

If I would like to continue with the paranthesis, not to save the mask to file but put it to parenthesis ... how should look the last line?

Should it be like this?

Code: Select all

+swap ( -clut -separate -evaluate-sequence multiply )
or like this:

Code: Select all

(+swap -clut -separate -evaluate-sequence multiply)  
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 »

No parenthesis are needed and using them would be confusing if not correct. Neither of your commands will work properly as is. If you need to do further processing, then I would need to know what you want to do. Any parenthesis processing must reference an image at the beginning, which can be either an image filename or a clone. The idea of the parenthesis is to separate processing of one image from another so that the commands do not get applied to both images. Also there must be a space on both sides of each parenthesis.
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:No parenthesis are needed and using them would be confusing if not correct. Neither of your commands will work properly as is. If you need to do further processing, then I would need to know what you want to do. Any parenthesis processing must reference an image at the beginning, which can be either an image filename or a clone. The idea of the parenthesis is to separate processing of one image from another so that the commands do not get applied to both images. Also there must be a space on both sides of each parenthesis.
Well we have now processed one group of colors right? My goal is to search for more groups of colors separately. It is more convenient for me to use HSB because I plan to use more sets of colors which will be separated according brightness.

I think this is the most precise way how to separate the colors and not mix them togheter. I found that there are green shadows and blue shadows, and the division is simple just according amout of light present in the shadow. This is why it is so simple for me to work with HSB and not RGB. RGB was always just confusing numbers which I never remembered and did not managed them to organize as colors. HSB is nice to organize and find suitable way to nicely arrange the colors into sets.

So I will create masks for dark shadows, masks for medium dark shadows, and soft shadows. I will also separate them to greens and blues. So I will have 3x2 masks. I would copy your lines 6 times and add one line which will create mask for all the colors.
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 »

Just loop over each set of H,S,B min max pairs using the same code, but save the result to mask1.png ... mask6.png. Then after the loop combine them as

convert mask1.png ... mask6.png -evaluate-sequence add finalmask.png

This should work if you want the mask to cover all your conditions and then process the image with the one mask with the same commands for all different shadows.

However, if each type of shadow needs to be processed with different commands, then you need to keep the masks separate and process each mask and the image with the appropriate set of commands. Then put the masks into the alpha channels of each processed image and then flatten them all together.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

Why it is not possible to write something like this:

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


SET minH=104
SET maxH=216
SET minS=8
SET maxS=33
SET minB=31
SET maxB=46

convert ^
( ( -size 360x1 xc: -fx "(i>=%minH1% && i<=%maxH1%)?white:black" ) ^
( -size 360x1 xc: -fx "(i>=%minS1%*3.6 && i<=%maxS1%*3.6)?white:black" ) ^
( -size 360x1 xc: -fx "(i>=%minB1%*3.6 && i<=%maxB1%*3.6)?white:black" ) ^
-combine  ( test2.jpg  -colorspace hsb ) ^
+swap -clut -separate -evaluate-sequence multiply ) ^
( ( -size 360x1 xc: -fx "(i>=%minH2% && i<=%maxH2%)?white:black" ) ^
( -size 360x1 xc: -fx "(i>=%minS2%*3.6 && i<=%maxS2%*3.6)?white:black" ) ^
( -size 360x1 xc: -fx "(i>=%minB2%*3.6 && i<=%maxB2%*3.6)?white:black" ) ^
-combine  ( test2.jpg  -colorspace hsb ) ^
+swap -clut -separate -evaluate-sequence multiply )^
( ( -size 360x1 xc: -fx "(i>=%minH3% && i<=%maxH3%)?white:black" ) ^
( -size 360x1 xc: -fx "(i>=%minS3%*3.6 && i<=%maxS3%*3.6)?white:black" ) ^
( -size 360x1 xc: -fx "(i>=%minB3%*3.6 && i<=%maxB3%*3.6)?white:black" ) ^
-combine  ( test2.jpg  -colorspace hsb ) ^
+swap -clut -separate -evaluate-sequence multiply )^
( -clone 0-2 -combine ) -delete 0-2 ^
+swap -clut -separate -evaluate-sequence multiply test2_final_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 it is not possible to write something like this:
It is, but sadly you have errors. :!:

First there must be spaces before ^ at end of line to separate it from the parenthesis as I said before. I am also not sure about your use clones and why they are there. And you have not combined the masks using -evaluate-sequence add.

Also what happens when you have more than 3 sets or only 2 or only 1 set. You have to use a different combination of commands. Furthermore you have 4 sets but only use 3.

Also you are reading the input multiple times, which will slow things down.

You get more flexibility with a loop. Nevertheless, try this corrected code.

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 test2.jpg -colorspace hsb -write mpr:input +delete ^
-size 360x1 ^
( ( xc: -fx "(i>=%minH1% && i<=%maxH1%)?white:black" ) ^
( xc: -fx "(i>=%minS1%*3.6 && i<=%maxS1%*3.6)?white:black" ) ^
( xc: -fx "(i>=%minB1%*3.6 && i<=%maxB1%*3.6)?white:black" ) ^
-combine mpr:input  +swap -clut -separate -evaluate-sequence multiply ) ^
( ( xc: -fx "(i>=%minH2% && i<=%maxH2%)?white:black" ) ^
( xc: -fx "(i>=%minS2%*3.6 && i<=%maxS2%*3.6)?white:black" ) ^
( xc: -fx "(i>=%minB2%*3.6 && i<=%maxB2%*3.6)?white:black" ) ^
-combine mpr:input  +swap -clut -separate -evaluate-sequence multiply ) ^
( ( xc: -fx "(i>=%minH3% && i<=%maxH3%)?white:black" ) ^
( xc: -fx "(i>=%minS3%*3.6 && i<=%maxS3%*3.6)?white:black" ) ^
( xc: -fx "(i>=%minB3%*3.6 && i<=%maxB3%*3.6)?white:black" ) ^
-combine mpr:input  +swap -clut -separate -evaluate-sequence multiply ) ^
-evaluate-sequence add test2_final_mask.png
Note I convert the input image to hsb only once and save it as in-memory mpr format. see http://www.imagemagick.org/Usage/files/#mpr

Each outer parenthesis saves only the one mask image and there are no intermediate images left besides the in memory image. At the end, no clones are needed, because I simply combine the 3 masks into one using -evaluate-sequence add.

Also I have extracted all the -size 360x1 settings and just referenced it once outside all the parenthesis. That size setting will be set for all the parenthesis.

In fact, I believe that one can remove one layer of parentheses, so that it becomes

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 test2.jpg -colorspace hsb -write mpr:input +delete ^
-size 360x1 ^
( xc: -fx "i>=%minH1% && i<=%maxH1%?white:black" ^
xc: -fx "i>=%minS1%*3.6 && i<=%maxS1%*3.6?white:black"  ^
xc: -fx "i>=%minB1%*3.6 && i<=%maxB1%*3.6?white:black"  ^
-combine mpr:input  +swap -clut -separate -evaluate-sequence multiply ) ^
( xc: -fx "i>=%minH2% && i<=%maxH2%?white:black"  ^
xc: -fx "i>=%minS2%*3.6 && i<=%maxS2%*3.6?white:black"  ^
xc: -fx "i>=%minB2%*3.6 && i<=%maxB2%*3.6?white:black"  ^
-combine mpr:input  +swap -clut -separate -evaluate-sequence multiply ) ^
( xc: -fx "i>=%minH3% && i<=%maxH3%?white:black"  ^
xc: -fx "i>=%minS3%*3.6 && i<=%maxS3%*3.6?white:black"  ^
xc: -fx "i>=%minB3%*3.6 && i<=%maxB3%*3.6?white:black"  ^
-combine mpr:input  +swap -clut -separate -evaluate-sequence multiply ) ^
-evaluate-sequence add test2_final_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 »

I did updated the xls table and the script - about 21 mask created.

http://www.fileconvoy.com/dfl.php?id=g9 ... 54a62b49c7

But I think that IM has some mistake in algorithm and in wrongly rounds values of colors, somewhere in one of the commands we used. Notice that I am not able to located some pixels - I mean that they are not in the mask even that they should be. Probably UM uses some wrong rounding method and round down. When you will take the final mask and put to the testing image and apply, you will find some non-hidden pixels of the shadows. Some of them should be hidden.

PS: It takes longer that I expected, but I needed more masks because this is only way how to prevent to select some incorrect shadows, when my main intend is to select the shadows of routes or buildings... Blue tones are those of buildings or routes, green tones are often of grass and trees.

But the result is better than in my test which I did manually before with Photoshop (now success is that the top of trees are not masked when I select to green).

Of sure I will need to process every mask differently, every one will need a little bit different levels or hues edit. Also the soft light effect should be different according purpose of the mask.
Post Reply