The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by fmw42 »

I do not know what you mean by "averaging". Do you mean that we need a gain factor of 2?

O + 2*(O-GB) rather than O + 1*(O-GB)
Casper
Posts: 18
Joined: 2016-05-21T08:28:28-07:00
Authentication code: 1151

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by Casper »

Casper wrote:This one looks identical to your IM version which used the PS GS as source,
Bah..forget what I just said. Even with Amount 50% the PS USM version has more contrast.
Maybe I should do 100/3 = 33% since IM is doing 3 math operations.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by fmw42 »

I do not connect the amount to the number of math operators. That does not make sense to me.

What are the list of operations you did in PS to get the same result as my IM one using the PS GB.
Casper
Posts: 18
Joined: 2016-05-21T08:28:28-07:00
Authentication code: 1151

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by Casper »

fmw42 wrote:I do not know what you mean by "averaging". Do you mean that we need a gain factor of 2?

O + 2*(O-GB) rather than O + 1*(O-GB)
Yes. That's along the lines I was thinking. Maybe it's worth a shot if you still have the PS blur as source?
I think I'm wrong though, like you say it doesn't really make sense here. I only noticed that PS can average
the addition and subtraction operators. So if you add 10 and 20 you can get 30 (the default setting), or you
can get 15 ((10+20) / 2), when setting the "Scale" setting to 2.
Casper
Posts: 18
Joined: 2016-05-21T08:28:28-07:00
Authentication code: 1151

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by Casper »

I'll try the whole operation tomorrow manually in PS with Scale 2, and we'll see what we get.
Running out of steam a bit for tonight..:-/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by fmw42 »

Where is this scale in the PS unsharp masking? I only see amount, radius and threshold.
Casper
Posts: 18
Joined: 2016-05-21T08:28:28-07:00
Authentication code: 1151

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by Casper »

fmw42 wrote:Where is this scale in the PS unsharp masking? I only see amount, radius and threshold.
It's in the PS menu "Image/Apply Image...", then select Subtract or Add and you will see Scale and Offset as additional options.
You use Apply Image to do the addition and subtraction operations of the formula (instructions in the link in the first post).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by fmw42 »

When you have time, list for me the exact operations you used in PS with the argument values to reproduce what you found matched the IM results and which IM result that I made above.
Casper
Posts: 18
Joined: 2016-05-21T08:28:28-07:00
Authentication code: 1151

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by Casper »

I didn't test it with Scale 2 yet. The only steps I did where the steps for "O+(O-GB)-inv(O+inv(GB))" this replicates PS USM 100, 3, 0, and closest match for IM was your "-unsharp 0x3" example in that case.

If you like I can make a video showing how I do it if that would help?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by snibgo »

Does the PS "amount" allow values above 100, like (say) 500 or 1000?

If an effect is exaggerated, it is easier to find what causes the effect, and how to replicate it.
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: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by fmw42 »

snibgo wrote:Does the PS "amount" allow values above 100, like (say) 500 or 1000?

If an effect is exaggerated, it is easier to find what causes the effect, and how to replicate it.
PS unsharp masking does allow amounts up to 500%.

The PS apply image seems to be limited to 0 to 100%
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by fmw42 »

Here is another way to do

O+(O-GB)-inv(O+inv(GB))

Code: Select all

im6937hdri convert lena3.png \
\( -clone 0 -gaussian-blur 0x3 \) \
\( -clone 0 -clone 1 +swap -compose minus -composite \) \
\( -clone 1 -negate -clone 0 -compose plus -composite -negate \) \
-delete 1 \
-evaluate-sequence add \
lena3_im_us_script3_3gx9p8x1d.png
Image

It is a bit simpler in that it adds the 3 parts in one step.

Here is IM -unsharp:

Code: Select all

im6937hdri convert lena3.png -unsharp 0x3+1+0 lena3_im_uns_0x3_1_0.png
Image


Here is PS unsharp mask with amount=100 radius=3 threshold=0:
Image

If I use compare, the IM -unsharp is closer to the PS result and very close in fact.

Code: Select all

compare -metric rmse lena3_im_uns_0x3_1_0.png lena3_ps_usm_a100_g3_t0.png null:
131.277 (0.00200317)

This value should not be visually different and my eyes do not see any by flickering the two images back and forth.
Casper
Posts: 18
Joined: 2016-05-21T08:28:28-07:00
Authentication code: 1151

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by Casper »

fmw42 wrote:Here is another way to do

O+(O-GB)-inv(O+inv(GB))

It is a bit simpler in that it adds the 3 parts in one step.
That one looks even more different than any of the previous ones. So either IM is not working as expected, because the result is not equivalent to your more complex approach, or then there's some other error in the setup..

fmw42 wrote:If I use compare, the IM -unsharp is closer to the PS result and very close in fact.

Code: Select all

compare -metric rmse lena3_im_uns_0x3.png lena3_ps_usm_a100_g3_t0.png null:
649.805 (0.00991538)

This value should not be visually different and my eyes do not see any by flickering the two images back and forth.
They are very close, but the PS version has more noise. Look at her forehead when you flick back and forth.
My guess is -unsharp has a hard-coded very small threshold built into it since the sharpening effect seems exactly like the one in PS, but the noise is less in smooth areas.

But I still don't understand why the fully manual version does not work with IM.

I put a video here how I do it in Photoshop. I'm no video producer, so it will be a little slow and clunky..but you should get the idea:
https://www.youtube.com/watch?v=8Yae5V39EhQ
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by fmw42 »

Yes, I forgot about the default .05 threshold when I originally posted, but have changed the post to set the threshold to 0 exactly. See my previous post and http://www.imagemagick.org/script/comma ... hp#unsharp. So my compare rmse value is now even smaller.
Casper
Posts: 18
Joined: 2016-05-21T08:28:28-07:00
Authentication code: 1151

Re: The Math Behind Photoshop's Unsharp Mask and Gaussian Blur

Post by Casper »

fmw42 wrote:Yes, I forgot about the default .05 threshold when I originally posted, but have changed the post to set the threshold to 0 exactly. See my previous post and http://www.imagemagick.org/script/comma ... hp#unsharp. So my compare rmse value is now even smaller.
Good stuff! That's great news. If we'd figure out -unsharp from the start we'd save 4+ hours of head scratching ;)

But just as an exercise it makes me wonder what IM is doing when it composites the images, because all logic
says you should get exactly the same result with the composite approach as with -unsharp. Hgnnnrr..

Does the order of compositing matter..it should not when it's addition.
Are you sure the order is correct for the subtraction cases?
Post Reply