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

Sophisticated fuzz with HSL definition?

Post by VanGog »

(I am using Windows XP)

Is it possible to use HSV or HSL model to define colors which you want to work with? I mean I have RGB image, but would like to use HSL definition for fuzz.

For example I have image like this:
Image
And want to suppress shadows located on routes. To do it with RGB definition model I would need to select cca 20-30 colors depending on fuzz (the more precise fuzz the more colors I have to specify). But with HSV/HSL definition this would be much more simple to do. I would like to define minimal values in the similar way

Code: Select all

HSL_min1 = { 197, 21, 29}; // ( color is in range of H>=197 && S>=21 && L >=29
HSL_max1 = { 222, 40, 33}; // and H<=222 && S<=40 && L <=33 ) // dark shadows
// or
HSL_min2 = { 104, 8, 31}; // ( color is in range of H>=104 && S>=8 && L >=31
HSL_max2 = { 216, 33, 46}; // and H<=216 && S<=33 && L <=46 ) // light shadows
// and so on ... 
So the final algorithm would look for two sets of colors, if the color is in one of the group, then the following action will be performed...

It is exact definition which includes many of colors without need to define all the colors. Is there any way to do it? I would really welcome it.

So in the result, I would effect the defined colors and have something like this...
Image (shadows suppressed)
Last edited by VanGog on 2014-05-31T10:20:51-07:00, edited 2 times in total.
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 could do it with "-fx". Where did you get the numbers from? I don't think they identify shadows.

Here, I crop an area and find the min and max values of H, S and L. Then I make a mask that is white where pixels are in that range; otherwise black. You could then use the mask to lighten the image, or whatever. Windows BAT syntax.

Code: Select all

for /F "usebackq" %%L in (`%IM%convert ^
  20r45mo.jpg ^
  -crop 30x30+10+50 ^
  -colorspace HSL ^
  -format "minH=%%[fx:minima.r]\nmaxH=%%[fx:maxima.r]\nminS=%%[fx:minima.g]\nmaxS=%%[fx:maxima.g]\nminL=%%[fx:minima.b]\nmaxL=%%[fx:maxima.b]" ^
  info:`) do set %%L

%IM%convert ^
  20r45mo.jpg ^
  -colorspace HSL ^
  -fx "r>=%minH%&&r<%maxH% && g>=%minS%&&g<%maxS% && b>=%minL%&&b<%maxL% ?1:0" ^
  mask.png
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 »

OK, thanks. I tried it without crop on image of size 1024x768 looks it takes very long time or is it looped forever?

Code: Select all

SET minH=197
SET minS=21
SET minL=29
SET maxH=222
SET maxS=40
SET maxL=33

for /F "usebackq" %%L in (`%IM%convert ^
  test.jpg ^
  -colorspace HSL ^
  -format "minH=%%[fx:minima.r]\nmaxH=%%[fx:maxima.r]\nminS=%%[fx:minima.g]\nmaxS=%%[fx:maxima.g]\nminL=%%[fx:minima.b]\nmaxL=%%[fx:maxima.b]" ^
  info:`) do set %%L

%IM%convert ^
  test.jpg ^
  -colorspace HSL ^
  -fx "r>=%minH%&&r<%maxH% && g>=%minS%&&g<%maxS% && b>=%minL%&&b<%maxL% ?1:0" ^
  mask.png
pause
Unfortunately it seems unusable because it consumes too much of time :-/

I also do not understand how your script works but it seems to me that the last part RGB model which is wrong.

Is there any change to skip using for command to run IM?

Note: I am reading about fx and I found function hsl() there. Maybe this one could be used?

Edit: Now I am reading: Making IM Faster (in general) ... maybe should I use Q8 instead Q16 to make the command faster? But still I would like avoid sending multiple calls,
Also:
Avoid using FX, The Special Effects Image Operator, if you can use Alpha Composition, or the simpler Evaluate, Simple Math Operations, or other techniques instead.
If you do need to use these, try restricting its use to the smallest image possible, or to a single channel of the image (when handling grayscale).
But I will need to work with huge images at least 2000x2000 px. So it seems that using fx is not the best way.
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 »

For fx, min and max must be in the range zero to one.

"-fx" is too slow for multi-megapixel images.

You could make a mask of the difference between the image and the average shadow colour.
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 »

Also I am reading:
Building ImageMagick as a shared library (the default) can greatly reduce load time. Libraries and coder modules are only loaded as they are needed so a dynamic version of IM will not load anything that it doesn't need to use during image processing. Also shared libraries tend to remain available so may not need to be reloaded for a second run.
I installed static version due to my inexperience. Maybe this static version loads unnecessary libraries on every run?
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 »

DLL will makes almost difference. To speed it up, you could make a mask of the difference between the image and the average shadow colour.
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 »

But it takes really huge time. I think this method is bad. If I would list all the colors using fuzz with low value - it should be much more faster. But it will be very tedious to get all the colors needed. I wanted to simplify my job.
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 do not think -fuzz will automatically wrap around both sides of red in HSL or similar colorspaces where hue is at 0 = 360.
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 »

Fuzz doesn't wrap. That is one reason why Lab is often a better colorspace for this type of problem.
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 »

snibgo wrote:For fx, min and max must be in the range zero to one.

"-fx" is too slow for multi-megapixel images.

You could make a mask of the difference between the image and the average shadow colour.
I cannot find out why this does not make floats but rounds the numbers:
SET /A minH=(1/360)*197
SET /A minS=21/100
SET /A minL=29/100
SET /A maxH=(1/360)*222
SET /A maxS=40/100
SET /A maxL=33/100

Still it* takes very long even in Q8. * - the first code
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

snibgo wrote:Fuzz doesn't wrap. That is one reason why Lab is often a better colorspace for this type of problem.
What do you mean by wrap?
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

Re: Sophisticated fuzz with HSL definition?

Post by VanGog »

Edit:
VanGog wrote:If I would list all the colors <in RGB> using fuzz with low value - it should be much more faster. But it will be very tedious to get all the colors needed. I wanted to simplify my job.
I meant RGB. But I would leave this idea in the case that it would be possible to use fuzz for HSL. But its not clear to me how to get the HSL value into -fuzz. Should I convert to HSL and then to use fuzz with RGB value or how?
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 »

VanGog wrote:I cannot find out why this does not make floats but rounds the numbers:
SET /A minH=(1/360)*197
Because "set" calculates with integers only. 1/360 is zero, to the nearest integer.
VanGog wrote:What do you mean by wrap?
A hue of 359 degrees is very close to a hue of 1 degree. But "-fuzz" doesn't know this.
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 »

snibgo wrote:
VanGog wrote:What do you mean by wrap?
A hue of 359 degrees is very close to a hue of 1 degree. But "-fuzz" doesn't know this.
Fortunately I don't need to select red hues in this case. I work with colors in the range of yellow - green - cyan - blue. That's all.
snibgo wrote:
VanGog wrote:I cannot find out why this does not make floats but rounds the numbers:
SET /A minH=(1/360)*197
Because "set" calculates with integers only.
Contra
snibgo wrote:For fx, min and max must be in the range zero to one.
How could the script work then when you suggest using floats but batch does not work with floats?
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 »

This works ...

Code: Select all

set minH=0.5472222222222222
... because there is no calculation (no "/A").
snibgo's IM pages: im.snibgo.com
Post Reply