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 »

If you need more accuracy, you can make the lookup table size 3600x1 and then multiply all the values in the fx conditionals by 10
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

Like that?

Code: Select all

convert ^
( -size 3600x1 xc: -fx "(i>=%minVDGG_H%*100 && i<%maxVDGG_H%*100)?white:black" ) ^
( -size 3600x1 xc: -fx "(i>=%minVDGG_S%*3.6*100 && i<%maxVDGG_S%*3.6*100)?white:black" ) ^
( -size 3600x1 xc: -fx "(i>=%minVDGG_B%*3.6*100 && i<%maxVDGG_B%*3.6*100)?white:black" ) ^
..
This does not work because all masks are empty :-( And If I would need to make it just 2x more precise? Not 10x. 10 seems to be very slow.
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 »

Typo in my earlier message, which I have fixed. It should have been multiply by 10. This works just fine for me and a few more pixels show up in the mask. It is a bit slower due to increasing the resolution of the look up tables, so that fx needs to process 10x more pixels for each channel. Nevertheless, it went from 0.3 seconds to about 0.5 seconds, which is not much. You will spend more time processing your image for any changes than in creating this mask, I would expect.


infile="test2.png"
inname=`convert "$infile" -format "%t" info:`
Hmin=104
Hmax=216
Smin=8
Smax=33
Bmin=31
Bmax=46
convert -size 3600x1 \
\( xc: -fx "(i>$Hmin*10 && i<$Hmax*10)?white:black" \) \
\( xc: -fx "(i>$Smin*36 && i<$Smax*36)?white:black" \) \
\( xc: -fx "(i>$Bmin*36 && i<$Bmax*36)?white:black" \) \
-combine \
\( "$infile" -colorspace hsb \) \
+swap -clut -separate -evaluate-sequence multiply ${inname}_mask2g.png


You are free to change the size from 360x1 to whatever you want. Just multiply your values by the same factor. So it you use 720x1, then you would multiply your values by 2.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Sophisticated fuzz with HSL definition?

Post by anthony »

Hmmm.. 21 masks!! to mask specific colors to a specific mask...

May I suggest you run your filter on a Hald CLUT
http://www.imagemagick.org/Usage/color_ ... eplacement
so that each color is mapped to a specific 'masking color' (just a single color per mask)

Using HALD images will then let you apply a masking operation in a single step.

You can then extact individual masks if wanted (as it is a single color) or overlay a transparent version of the masking image to 'highlight' what areas belong to want mask.

Also you can then view and adjust mask (flipping between a normal HALD, and the masking HALD) to make minor changes. or just see what color regions were not covered by your filter.

Note Hald images can be used in HSL color space. IM makes no check for colorspace, so any 3 value colorspace should work fine, and you could convert teh HALD to get versions for direct appliation without colorspace changes.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

fmw42:
I have multiplied by 10 for 3600x1 lut. I have still the same result (seems to me).
If you downloaded the zip file and can open the mask and if you will invert the mask and apply to test2.png (test2.jpg in my case). Image with is 915px. You should see pixels:

There are these pixels in the area which I am just viewing:

Code: Select all

empty;   empty;  empty;   empty
empty;   empty;  #30322f; empty
#30322f; #333532; #313330; #2c302f
empty;   empty;  empty;  empty
In HSB:

Code: Select all

empty;  empty;  empty;  empty
empty;  empty;  100,6,20 ; empty
100,6,20 ; 100,6,21 ; 100,6,20 ; 165, 8 , 19
empty;  empty;  empty;  empty 
These gray pixels should be covered by the conditional group "VERY DARK COMMON SHADOWS: - GREEN COMMON" or COMMON GREEN.

Code: Select all

         H     S       B
min:	100	5	16
max:	201	52	28
But none of them is covered. This really looks odd. The condition is covered in this command:
convert ^
( -size 3600x1 xc: -fx "(i>=%minVDCG_H%*10 && i<%maxVDCG_H%*10)?white:black" ) ^
( -size 3600x1 xc: -fx "(i>=%minVDCG_S%*36 && i<%maxVDCG_S%*36)?white:black" ) ^
( -size 3600x1 xc: -fx "(i>=%minVDCG_B%*36 && i<%maxVDCG_B%*36)?white:black" ) ^
-combine ( %infile% -colorspace hsb ) +swap -clut -separate -evaluate-sequence multiply mask_VDCG.png

Hence it looks there is some calculation bug in IM.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

anthony wrote:Hmmm.. 21 masks!! to mask specific colors to a specific mask...

May I suggest you run your filter on a Hald CLUT
http://www.imagemagick.org/Usage/color_ ... eplacement
Thanks a lot. I have already forgot about this method. As far as I remember this was a perfect method and it should be simple to create my own corrected image in Photoshop and then create the Hald image from it. I would like to ask you to explain the term from the reference:
However as IM currently does a linear interpolated lookup of the Hald
What does mean those two words "linear interpolated"?
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
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

I can confirm this.

If you have a pixel with color
74,14,73 (HSB) - which is 181,187,161 and #b5bba1
I must set Saturation max. limit +2 to correct the error by ImageMagick.

Code: Select all

	H	S	Brightness
min:	69	6	57
max:	97	18	77
Part of the batch file:

Code: Select all

SET minSGG_H=69
SET maxSGG_H=97
SET minSGG_S=6
SET maxSGG_S=18
SET minSGG_B=57
SET maxSGG_B=77
convert ^
( -size 720x1 xc: -fx "(i>=%minSGG_H%*2 && i<%maxSGG_H%*2)?white:black" ) ^
( -size 720x1 xc: -fx "(i>=%minSGG_S%*3.6*2 && i<%maxSGG_S%*3.6*2)?white:black" ) ^
( -size 720x1 xc: -fx "(i>=%minSGG_B%*3.6*2 && i<%maxSGG_B%*3.6*2)?white:black" ) ^
-combine  ( %infile%  -colorspace hsb ) +swap -clut -separate -evaluate-sequence multiply mask_SGG.png
Correctly should be:

Code: Select all

	H	S	Brightness
min:	69	6	57
max:	97	18	77
I have tried to select colors like 74,14,73 and 73,15,73 and had to increase the limit at least +2, which is +3 for condition i<max
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 »

As a test, take one of the colors you think is missing and make a solid image for that one color. Then create the mask with my formula. Is it all white or all black? Post your color definition and the code, so we can test for ourselves.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

I cannot find out why I cannot make this working:

Code: Select all

convert map.png -write mpr:input +delete ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFE168") ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#E5bC4C") ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFF1B3") ^
  -evaluate-sequence multiply  highway_result.png
I am trying to select three colors of image and get B/W mask but the file highway_result.png is not created
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 »

You need a space on each side of each parenthesis: ( and ) .
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. I already forgot about it. Now it works but with "add".
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

And how to fix it, to fill it white color? I tried different configurations using -fill white -opaque or +opaque or black -opaque and every time I got only white mask or black empty image.

Code: Select all

convert map.png -write mpr:input +delete ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFE168" ) ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#E5bC4C" ) ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFF1B3" ) ^
  -evaluate-sequence add  highway_result.png
Also how can I apply threshold on the mask? To be exact I need to do Gausian blur 1.2 and then threshold 4. Photoshop has values 1-255 for threshold. I'll probably manage to blur I saw ecamples, but I cannot find how to do the threshold. Maybe I should use +level but I am not sure how co configurate it to get the same effect as in Photoshop.

Code: Select all

convert map.png -write mpr:input +delete ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFE168" ) ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#E5bC4C" ) ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFF1B3" ) ^
  -evaluate-sequence add -blur 0x1.2 +level 1%%,100%% highway_result.png
Edit:
I have solved it, but is there way to make it faster? Also I see nothing when I open it Photoshop but I must convert it to see the mask.

Code: Select all

convert map.png -write mpr:input +delete ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFE168" ) ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#E5bC4C" ) ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFF1B3" ) ^
  -evaluate-sequence add -blur 0x1.2 -threshold 2%% highway_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 »

And how to correct this:

Code: Select all

convert map.png -write mpr:input +delete ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFE168" ) ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#E5bC4C" ) ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFF1B3" ) ^
  ( mpr:input -fuzz 2%% -fill black +opaque "#FFE98E" ) ^  
  -evaluate-sequence add -blur 0x1.6 -threshold 25.5%% highway_result.png
Error: Unbalanced parenthesis. It works with only 3 parenthesis, the fourth fails
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 »

-evaluate-sequence add
Using add rather than multiply is doing a logical || (or). That means that the mask will be white when any test is true. The use of multiply is like doing a logical && (and). That means that the mask will white only when all the tests are simultaneously true. It all depends upon what you are trying to do. I had assumed all along, especially with HSL conditions, that you wanted your mask to be white only when all conditions are true. If you are trying to capture multiple and non-overlapping color ranges, then the use of and is likely what you want. That is why I told you to use -evaluate-sequence add in your final stage of combining the multiply HSL resulting masks, where each HSL set needed to combine the H,S,L conditions with a logical && (multiply)

With regard to your error message, is there any space after any of the ^ characters? You must not have any space after the continue to next line character ^.
Post Reply