How does -unsharp work?

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
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

How does -unsharp work?

Post by GreenKoopa »

At a high level, -unsharp finds edges and exaggerates the differences. A helpful tutorial:
http://www.cambridgeincolour.com/tutori ... p-mask.htm
http://www.cambridgeincolour.com/tutori ... cement.htm

-unsharp has three parameters
  • radius & sigma (area function)
  • amount/gain
  • threshold
My understanding is that three images are combined to create the output
  • The input image.
  • A higher contrast version of the input image.
  • A mask identifying the edges.
The edge mask is created by blurring the original image and subtracting it from the original ( abs(original - blurred_copy) ? ). Radius/sigma specifies the blurring and threshold specifies when to zero small differences.

How is the higher contrast version created? Amount specifies how much contrast. I used to imagine it was something like applying -level 10,90% to the original image. But it seems like colors are shifted away from the local average (sigma also used here?) in testing, which makes sense.

Finally, the three images are combined. My guess is
convert original high_contrast_copy edge_mask -compose Over -composite sharpened_output

It seems the channels are processed separately. -channel works, so HSL or Lab colorspace could easily be used when sharpening.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How does -unsharp work?

Post by snibgo »

An unsharp mask without a threshold:

(1) From the original, create a blurred version.

(2) Create a new image where each new pixel = n * blur + (1-n) * original.

Step (2) is the general interpolation/extrapolation formula. For unsharp masking, n < 0.

Another way of expressing step (2):

(2a) New pixel = original - n * (original - blur)

I don't know how thresholds are implemented in IM. It could in step (2a) by adjusting (original - blur) with "-level 5x100%" or "-black threshold 5%". These would give slightly different results.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How does -unsharp work?

Post by fmw42 »

I am not sure about the threshold either. But the arguments are radiusxsigma+gain+threshold

Radius is usually set to 0 so that it is automatically determined from the sigma (approximately, r=3*sigma), so that the Guassian blur rolls down to close to zero by the time it reaches the end of the window from the window center.

Sigma controls the amount of blur in the Gaussian blur.

Gain is just as snibgo explained

output=input + gain*(input-gaussianblurredinput)

My guess from the description at http://www.imagemagick.org/script/comma ... hp#unsharp that it might be something like

if (input-gaussianblurredinput) > threshold, then apply above, else output=input, but I am not sure if the difference is signed or absolute value.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How does -unsharp work?

Post by GreenKoopa »

Thanks guys. I was making it way to complicated. These appear to be equivalent.

Code: Select all

convert in.png -unsharp 10x4+2+0 1.png
convert in.png -blur 10x4 blur.png
convert in.png blur.png -fx "gain=2;u+gain*(u-v)" 2.png
convert in.png blur.png -set option:compose:args "%[fx:gain=2;-100*gain],%[fx:gain=2;100*(1+gain)]" -compose Blend -composite 3.png
I'll keep playing with this, then maybe tackle threshold.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How does -unsharp work?

Post by fmw42 »

User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How does -unsharp work?

Post by GreenKoopa »

Thanks for the reference Fred. That formula suggests behavior like -black-threshold rather than -level. IM behaves the same way, but with an extra divide by 2.

Above I showed commands without threshold
-fx "gain=2;u+gain*(u-v)"
and this incorporates it
-fx "gain=2;th=0.05;u+gain*(u-v)*(abs(u-v)>=th/2)"
holden
Posts: 79
Joined: 2013-02-07T08:22:57-07:00
Authentication code: 6789

Re: How does -unsharp work?

Post by holden »

Kind of OT, but I noticed by using -unsharp 0x1 my convert time increased almost twofold in a command like this:

Code: Select all

convert -size 3000x2400 xc:white ( target.jpg -resize 3000x -gravity center +repage  -profile sRGB.icm ) -auto-level -unsharp 0x1 -composite target2.jpg 
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: How does -unsharp work?

Post by GreenKoopa »

Yes, probably too tangential. I started a new topic with some ideas.
viewtopic.php?f=1&t=23786
Drazick
Posts: 12
Joined: 2014-04-27T10:33:50-07:00
Authentication code: 6789

Re: How does -unsharp work?

Post by Drazick »

I think Photoshop works a bit different which produces much better results:

viewtopic.php?f=22&t=25477
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How does -unsharp work?

Post by fmw42 »

In principle the differences are how IM and PS measure sigma vs radius and the units for threshold and amount added back. Also whether the two use the same 2-pass 1D blur or a 2D blur. The other issue is that PS adds dot gain and IM has no equivalent. I do not see a way to turn off dot gain in PS, so it is hard to say how much effect that has.

PS amount=100 is equivalent to IM gain=1 ( so there is a 100x factor between the two)

PS threshold is in the range 0 to 255, where IM is in the range 0 to 1.

PS radius may be similar to IM radius or to IM sigma. It is not clear. To be more precise for IM 0xsigma it automatically computes the radius as something like r=3*sigma+1. Thus for IM 0x3, PS would need r=10.

If you compare

PS amount=100, radius=3, 7 (2*sigma+1), 10 (3xsigma+1), threshold=25 to IM -unsharp 0x3+1+0.098, you will get somewhat similar looking images. The IM result is sharper and has less saturation of yellow in the hat. However, none perfectly match. Here I have use 25/255=0.098 (approx 10%).

Original
Image

PS amount=100, radius=3, threshold=25
Image

PS amount=100, radius=7, threshold=25
Image

PS amount=100, radius=10, threshold=25
Image
IM -unsharp 0x3+1+0.098

Image


One can play around for a long time experimenting with variations of the arguments and perhaps find a closer approximation between the two. On the other hand, it may be that there is no perfect equivalent in IM for what you see in PS for any of the reasons above or for some other factor that PS includes, such as dot gain or other processing that it includes.
Drazick
Posts: 12
Joined: 2014-04-27T10:33:50-07:00
Authentication code: 6789

Re: How does -unsharp work?

Post by Drazick »

There's a big difference.
Photoshop does something like "Dark Halos" + "Light Halos".

See what I linked.

I think Image Magick does USM(O) = O + Amount * (O - GB)
Photoshop is different.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How does -unsharp work?

Post by fmw42 »

Drazick wrote: Photoshop does something like "Dark Halos" + "Light Halos".
What does this mean? Do you have any references?

Drazick wrote:I think Image Magick does USM(O) = O + Amount * (O - GB)
Photoshop is different.
Not quite.

USM(O) = O + gain*(O - GB(O)) if (O - GB(O)) < threshold

That is the standard unsharp masking.

What photoshop does is probably similar, but possibly with other processing added, which no one but PS knows as their code is proprietary. So all we can say is that they are somewhat different results and that means difference is the algorithm that only the POS people know. Unless, you know of some reference that explains exactly what PS does and what their arguments mean.

How does GIMP compare to PS? Anyone tested that? Is it closer to PS or to IM? If GIMP is closer to PS, then one could look at the GIMP code and see what they do.
Drazick
Posts: 12
Joined: 2014-04-27T10:33:50-07:00
Authentication code: 6789

Re: How does -unsharp work?

Post by Drazick »

Leave the threshold aside for the aske of this dicussion.

Open Photoshop and try:

USM(O) = O + (O - GB) - Which is as you set Amount to 100.
You'll see it is not replicates Photoshop's USM.

Then try the method I suggested:

USM(O) = O + (O - GB) - inv(O - inv(GB))

And you'll see it's 100%.
The first is the "Light Halos" the other is the "Dark Halos".

Give it a try and look at my thread for references.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How does -unsharp work?

Post by fmw42 »

GIMP code says:

* The radius parameter that is passed to this function is used as
* the standard deviation, and the radius of effect is the
* standard deviation * 2.
Drazick
Posts: 12
Joined: 2014-04-27T10:33:50-07:00
Authentication code: 6789

Re: How does -unsharp work?

Post by Drazick »

I think the documentation isn't updated since I clearly see that GIMP is close to Photoshop.

I promise you Photoshop does what I wrote above.

I just don't know why.
Post Reply