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.
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: Can you reproduce that using PS unsharp mask and if so what values are you using. Presumably the above formula would be for amount=100, and threshold=0. But I do not know what radius 3 means in terms of a gaussian blur the uses radius x sigma values.
Here. PS USM, amount = 100, radius = 3, threshold = 0.

But I agree; how do we know what it actually means..

IMG URL: http://i.imgur.com/u7euqtL.png

Image
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 »

Your two images compare exactly.

Code: Select all

compare -metric rmse ZKwoD4L.png u7euqtL.png null:
0 (0)
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 »

Woow...well then. That seems to indicate the difference between PS and IM is how GB is calculated(?)
I'll post GB(3) next.
Last edited by Casper on 2016-05-21T13:53:12-07:00, edited 1 time in total.
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 »

PS Gaussian Blur, radius 3:
http://i.imgur.com/DjWi3mZ.png

Image
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 did a Gaussian Blur, radius = 3, in PS of a 1 pixel vertical black line on white background.
I then looked at a horizontal "cross section" of this blurred vertical line, and the resulting blurred image had the following pixel values (from left to right):

255, 254, 253, 250, 246, 241, 234, 228, 223, 221, 223, 228, 234, 241, 246, 250, 253, 254, 255

Is there a way for IM to get the same GB profile on a 1 pixel line? In that case we should have found the equivalent blur radius for IM, and we should get an identical sharpened image with the same equivalent blur settings...you would think(?)
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 »

Pretty much this:

Code: Select all

%IM8i%convert xc:black -background White -gravity Center -extent 19x1 -blur 0x3 txt:

# ImageMagick pixel enumeration: 19,1,255,srgb
0,0: (255,255,255)  #FFFFFF  white
1,0: (255,255,255)  #FFFFFF  white
2,0: (253,253,253)  #FDFDFD  srgb(253,253,253)
3,0: (250,250,250)  #FAFAFA  grey98
4,0: (246,246,246)  #F6F6F6  srgb(246,246,246)
5,0: (241,241,241)  #F1F1F1  srgb(241,241,241)
6,0: (234,234,234)  #EAEAEA  srgb(234,234,234)
7,0: (228,228,228)  #E4E4E4  srgb(228,228,228)
8,0: (223,223,223)  #DFDFDF  srgb(223,223,223)
9,0: (221,221,221)  #DDDDDD  srgb(221,221,221)
10,0: (223,223,223)  #DFDFDF  srgb(223,223,223)
11,0: (228,228,228)  #E4E4E4  srgb(228,228,228)
12,0: (234,234,234)  #EAEAEA  srgb(234,234,234)
13,0: (241,241,241)  #F1F1F1  srgb(241,241,241)
14,0: (246,246,246)  #F6F6F6  srgb(246,246,246)
15,0: (250,250,250)  #FAFAFA  grey98
16,0: (253,253,253)  #FDFDFD  srgb(253,253,253)
17,0: (255,255,255)  #FFFFFF  white
18,0: (255,255,255)  #FFFFFF  white
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 »

data=`convert -size 24x50 xc:white -size 1x50 xc:black -size 24x50 xc:white +append \
-gaussian-blur 0x3 -crop 49x1+0+25 +repage -depth 8 txt:- |\
sed -n 's/^.*: [(]\(.*\),.*,.*[)] *[#].*$/\1/p'`
echo "$data"
for item in $data; do
echo "scale=0; 255*$item/65535" | bc
done

Removing all 255 but the first and last in the middle, I get

255 254 253 250 247 241 234 228 223 221 223 228 234 241 247 250 253 254 255

And yours are

255 254 253 250 246 241 234 228 223 221 223 228 234 241 246 250 253 254 255

So the same values for PS radius and IM 0xsigma using a value of 3, except for 247 for IM and 246 for PS.
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 »

If I do the gaussian blur of rad=3 in PS and substitute that in my IM script, I get

Code: Select all

im6937hdri convert lena3.png \
\( lena3_ps_gblur_r3.png \) \
\( -clone 0 -clone 1 +swap -compose minus -composite \) \
\( -clone 1 -negate \) \
\( -clone 0 -clone 3 -compose plus -composite -negate \) \
-delete 1,3 \
\( -clone 1 -clone 2 +swap -compose minus -composite \) \
-delete 1,2 \
-compose plus -composite lena3_im_us_script3_ps_gb_r3.png
Image
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 »

Ok..interesting stuff. Some next steps perhaps?

1) Can you post a link to your source image? I'd like to get the exact same one so we're both using the same. Makes it easier to do binary compares then too.

2) Can you post an image using IM for all steps, with clipping (since PS is clipping too), without threshold. I noticed we have all other permutations, but this one is missing and is the one we really want to use for comparison.
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:If I do the gaussian blur of rad=3 in PS and substitute that in my IM script, I get
This is really weird. This image should be identical to the PS USM one, but it severely lacks contrast and sharpness compared to PS USM. How can there be such a big difference, since the only steps we are doing now are simple addition and subtraction?
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 »

Casper wrote:Ok..interesting stuff. Some next steps perhaps?

1) Can you post a link to your source image? I'd like to get the exact same one so we're both using the same. Makes it easier to do binary compares then too.

2) Can you post an image using IM for all steps, with clipping (since PS is clipping too), without threshold. I noticed we have all other permutations, but this one is missing and is the one we really want to use for comparison.

I posted the original at viewtopic.php?f=22&t=25477&p=110684#p110684

My scripts above have clipping at each step. One would have to use HDRI with clamping off (as I did the one time as noted earlier) to prevent clipping.
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 have an answer to the difference.
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 »

The simplest form of Unsharp masking is simply

O + (O-GB)

So in script form:

Code: Select all

im6937hdri convert lena3.png \
\( -clone 0 -gaussian-blur 0x3 \) \
\( -clone 0 -clone 1 +swap -compose minus -composite \) \
-compose plus -composite lena3_im_uns_simple_script.png
Image
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 »

Ok I took your source and did PS USM to it. You can compare this to the image you created above.

It's really strange. There is a big difference in contrast. Almost like the IM addition and subtraction is clipping too much, not allowing the difference to get pronounced enough. Anyway..I don't have enough experience with IM or how it does what it does. This would require debugging each step in the formula. Even debugging one step would probably reveal the problem.

The only conclusion I get so far is that "-unsharp" produces much better results and is the closest (or best looking) match.

Lena3 PS USM 100, 3, 0:
http://i.imgur.com/zcSecSA.png

Image
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 think I found the answer! This one looks identical to your IM version which used the PS GS as source,

Check this out Lena3 PS USM 50, 3, 0:
http://i.imgur.com/Aky26Gf.png

I think the IM addition and subtraction is doing what PS "Apply Image" does with the "Scale"
setting at 2 instead of 1. This means IM is adding the images and then averaging the result.
PS does not average unless you set Scale to 2 in the "Apply Image" dialog.

Image
Post Reply