Page 1 of 2

How does -unsharp work?

Posted: 2013-07-18T15:38:53-07:00
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.

Re: How does -unsharp work?

Posted: 2013-07-18T16:36:38-07:00
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.

Re: How does -unsharp work?

Posted: 2013-07-18T17:19:18-07:00
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.

Re: How does -unsharp work?

Posted: 2013-07-18T17:45:05-07:00
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.

Re: How does -unsharp work?

Posted: 2013-07-18T20:55:58-07:00
by fmw42

Re: How does -unsharp work?

Posted: 2013-07-18T23:27:05-07:00
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)"

Re: How does -unsharp work?

Posted: 2013-07-19T11:42:01-07:00
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 

Re: How does -unsharp work?

Posted: 2013-07-19T11:49:03-07:00
by GreenKoopa
Yes, probably too tangential. I started a new topic with some ideas.
viewtopic.php?f=1&t=23786

Re: How does -unsharp work?

Posted: 2014-04-27T13:24:46-07:00
by Drazick
I think Photoshop works a bit different which produces much better results:

viewtopic.php?f=22&t=25477

Re: How does -unsharp work?

Posted: 2014-04-27T14:26:07-07:00
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.

Re: How does -unsharp work?

Posted: 2014-04-27T14:30:52-07:00
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.

Re: How does -unsharp work?

Posted: 2014-04-27T15:05:49-07:00
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.

Re: How does -unsharp work?

Posted: 2014-04-27T15:12:24-07:00
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.

Re: How does -unsharp work?

Posted: 2014-04-27T15:22:21-07:00
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.

Re: How does -unsharp work?

Posted: 2014-04-27T15:27:33-07:00
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.