Page 2 of 4

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

Posted: 2016-05-21T13:11:53-07:00
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

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

Posted: 2016-05-21T13:17:18-07:00
by fmw42
Your two images compare exactly.

Code: Select all

compare -metric rmse ZKwoD4L.png u7euqtL.png null:
0 (0)

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

Posted: 2016-05-21T13:19:01-07:00
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.

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

Posted: 2016-05-21T13:22:52-07:00
by Casper
PS Gaussian Blur, radius 3:
http://i.imgur.com/DjWi3mZ.png

Image

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

Posted: 2016-05-21T14:14:26-07:00
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(?)

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

Posted: 2016-05-21T14:42:29-07:00
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

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

Posted: 2016-05-21T15:03:58-07:00
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.

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

Posted: 2016-05-21T15:06:28-07:00
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

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

Posted: 2016-05-21T15:12:31-07:00
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.

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

Posted: 2016-05-21T15:16:05-07:00
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?

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

Posted: 2016-05-21T15:22:40-07:00
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.

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

Posted: 2016-05-21T15:23:33-07:00
by fmw42
I do not have an answer to the difference.

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

Posted: 2016-05-21T15:33:24-07:00
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

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

Posted: 2016-05-21T15:44:00-07:00
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

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

Posted: 2016-05-21T15:57:45-07:00
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