Pages: [1]   Go Down

Author Topic: The Math of Photoshop's Unsharp Mask and Gaussian Blur  (Read 10757 times)

Drazick

  • Newbie
  • *
  • Offline Offline
  • Posts: 14
The Math of Photoshop's Unsharp Mask and Gaussian Blur
« on: April 27, 2014, 04:34:15 pm »

Hello,
Has anyone succeeded in reproducing Photoshop's Gaussian Blur and Unsharp Mask?

I have a technique to apply a really nice Local contrast Enhancer based on Unsharp Mask yet I have to reproduce Photoshop's first.
I reproduce Photoshop's Unsharp Mask using some math on layers and the Gaussina Blur.
Assuming 'O' is the layer we want to apply USM on then:

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

Where GB stands for a Gaussian Blurred version of O.
Subtractions and Addition can be done using Photoshop's 'Apply Image' command.

As you can see this is not the "Classic" Unsharp Mask.

Has anyone reproduced it in C / Python / MATLAB?

Thank You.
Logged

Schewe

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 6229
    • http:www.schewephoto.com
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #1 on: April 27, 2014, 05:03:45 pm »

Actually, there isn't one single algorithm in Photoshop G-Blur...there are three of them based on the radius that is set. I used to know the breakdowns of when one algorithm transitioned to another algorithm, but that was a long time ago and before G-Blur radius was upped to beyond 250 pixels. So there may now be 4 algorithms that go up to 1000 pixels.

In terms of USM, I'm pretty sure the G-Blur algorithms are also used as is feathering in Photoshop.

So, trying to exactly duplicate the effects of G-Blurr may be more difficult...you might want to ask on the Adobe Photoshop User to User forums where a couple of Photoshop engineers hang out but I suspect that they be reluctant to say too much :~)
Logged

Drazick

  • Newbie
  • *
  • Offline Offline
  • Posts: 14
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #2 on: April 27, 2014, 06:08:45 pm »

Hi,
Assume I have Photoshop's Gaussian Blur.

How mathematically I can replicate its Unsharp Mask?

I'll explain what doesn't add for me.

Given a layer O.
Its Gaussian Blu Version is given by GB.

Now, using Apply Image and building the required intermediate layers I can replicate USM by:

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

Assuming layers are normalized into [0, 1] and I think the inverse of a layer L is given by inv(L) = 1 - L we get:

USM('O') = O + (O-GB) - inv(O + inv(GB)) = 3*O - 2*GB

Which doesn't make sense.
When I try it in MATLAB I get results completely different.

How come? What am I missing?
Logged

Drazick

  • Newbie
  • *
  • Offline Offline
  • Posts: 14
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #3 on: April 29, 2014, 01:52:28 am »

Actually, there isn't one single algorithm in Photoshop G-Blur...there are three of them based on the radius that is set. I used to know the breakdowns of when one algorithm transitioned to another algorithm, but that was a long time ago and before G-Blur radius was upped to beyond 250 pixels. So there may now be 4 algorithms that go up to 1000 pixels.

In terms of USM, I'm pretty sure the G-Blur algorithms are also used as is feathering in Photoshop.

So, trying to exactly duplicate the effects of G-Blurr may be more difficult...you might want to ask on the Adobe Photoshop User to User forums where a couple of Photoshop engineers hang out but I suspect that they be reluctant to say too much :~)

Hi,
Do you think you figured out the relation between the user input (Radius) and the kernel used for Gaussian Blur?

I mean, the Gaussian Kernel is built by 2 parameters: the radius of the filter and the Standard Deviation of the Gaussian Function.
does anyone know how those are calculated given the user input for Radius?
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #4 on: April 29, 2014, 06:31:43 am »

Hi,
Do you think you figured out the relation between the user input (Radius) and the kernel used for Gaussian Blur?

I mean, the Gaussian Kernel is built by 2 parameters: the radius of the filter and the Standard Deviation of the Gaussian Function.
does anyone know how those are calculated given the user input for Radius?

Hi,

I don't think it is formally published information, so one would have to reverse engineer the behavior to get an idea. Maybe someone has, I do not know if that is the case, but it doesn't look like a straight forward Gaussian implementation, just like the PS bi-cubic interpolation is not straightforward bi-cubic, and 16-bit is not straightforward 16-bit.

However, maybe they are using a somewhat similar approach as ImageMagick does (although Photoshop usually cuts a few corners for speed purposes), specifying only a 'radius' actually sets the Gaussian sigma (standard deviation) which restricts the '0x' kernel radius size to 3x the sigma for 16-bit data, less for 8-bit data. Thus, the kernel size is implicitly specified by the range of significant contributions.

Maybe the radius is the FWHM (Full Width Half Maximum amplitude) size of the used sigma and not the full sigma, who knows ... Adobe moves in strange ways.

Cheers,
Bart
« Last Edit: April 30, 2014, 04:27:08 am by BartvanderWolf »
Logged
== If you do what you did, you'll get what you got. ==

Drazick

  • Newbie
  • *
  • Offline Offline
  • Posts: 14
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #5 on: April 29, 2014, 09:28:02 am »

Hi Bart,
I tried using the Radius as the Standard Deviation (Sigma) of the Gaussian Kernel and ceil(3 * Radius) as the Radius of the filter.

I get results which are way more blurred than Photoshop.

Any idea how to reverse engineer it?

I tried Impulse Response, with no conclusions.
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #6 on: April 29, 2014, 10:37:59 am »

Hi Bart,
I tried using the Radius as the Standard Deviation (Sigma) of the Gaussian Kernel and ceil(3 * Radius) as the Radius of the filter.

I get results which are way more blurred than Photoshop.

Maybe a mix-up between radius and diameter?

Quote
Any idea how to reverse engineer it?

I haven't given the specific Photoshop implementation too much thought, because I create my own blur and deconvolution kernels and use them in various other software applications.

A Java application like ImageJ allows to do all sorts of operations and analyses. Maybe you can use it, if you don't have more suitable tools, to analyze the results of a Gaussian blur in Photoshop, and fit a model that corresponds to another function specification. You may also want to try fitting a Moffat distribution because that allows to influence the kurtosis of the Gaussian distribution.

Here is some more info on how a Moffat distribution is used in an Astro Photography software package.

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==

Drazick

  • Newbie
  • *
  • Offline Offline
  • Posts: 14
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #7 on: April 29, 2014, 10:47:19 am »

Hi,
I have MATLAB.

But even looking at the Impulse Response of the filter using 32 Bit Image wasn't conclusive.

I have MATLAB, nothing it better for that :-).
Logged

Bart_van_der_Wolf

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 8913
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #8 on: April 29, 2014, 11:37:27 am »

I have MATLAB, nothing it better for that :-).

In that case, I suggest to first compare the NormCDF (in a 2D version) with the shape of the PS Gaussian blur, and see if it can come close. 'NormFit' may be useful, from what I see.

Cheers,
Bart
Logged
== If you do what you did, you'll get what you got. ==

nma

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 312
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #9 on: April 29, 2014, 04:49:04 pm »

Hi,
Assume I have Photoshop's Gaussian Blur.

How mathematically I can replicate its Unsharp Mask?

I'll explain what doesn't add for me.

Given a layer O.
Its Gaussian Blu Version is given by GB.

Now, using Apply Image and building the required intermediate layers I can replicate USM by:

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

Assuming layers are normalized into [0, 1] and I think the inverse of a layer L is given by inv(L) = 1 - L we get:

USM('O') = O + (O-GB) - inv(O + inv(GB)) = 3*O - 2*GB

Which doesn't make sense.
When I try it in MATLAB I get results completely different.

How come? What am I missing?

I don't think your result 3*O - 2*GB is mathematically correct.
The problem appears to be how you evaluate inv(O+inv(GB)).

Consider the trivial example: inv(4+inv(1/2))=inv(6) not the same as 1/4+2
Logged

Drazick

  • Newbie
  • *
  • Offline Offline
  • Posts: 14
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #10 on: April 29, 2014, 05:59:01 pm »

Hi,
Do you agree that for any layer L that inv(L) = White - L?

USM(O) = O + (O - B) - inv(O + inv(B)) = 2O - B - inv(O + White - B) = 2O - B - (White - O - White + B) = 2O - B - White + O  + White + B = O + 2 (O - B).

Again, this is right assuming no clipping of the image.

Where do you think I'm wrong?
Logged

Schewe

  • Sr. Member
  • ****
  • Offline Offline
  • Posts: 6229
    • http:www.schewephoto.com
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #11 on: April 29, 2014, 07:45:30 pm »

So, there's a thread on the Photoshop User to User forum...is that you? Here's the thread that Chis Cox has answered in. Reproduce Unsharp Mask Using Gaussian Blur
Logged

Doug Wyrembek

  • Newbie
  • *
  • Offline Offline
  • Posts: 1
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #12 on: June 09, 2014, 06:02:15 pm »

I do not know about the exact photoshop algorithm.  But if you want code and an explanation of Unsharp Mask in C# check out http://www.codeproject.com/Articles/44115/ImageMagic-WPF-Image-Sharpening
Its slow but the results are fairly good.
Logged

Drazick

  • Newbie
  • *
  • Offline Offline
  • Posts: 14
Re: The Math of Photoshop's Unsharp Mask and Gaussian Blur
« Reply #13 on: June 09, 2014, 06:14:20 pm »

Hi,
Actually I managed to replicate Photoshop's USM almost perfectly.

It works just as I wrote above.
It just happens that Photoshop Clips into the [0, 1] range after every addition / subtraction.

Since it clips I can not do the math tricks to get the simple equation (Unless no clipping happened).

Tell you the truth, now that I know how it works and employing a much better smoothing operator I'm working on coding "Super USM", simple plugin for a much better USM.
Logged
Pages: [1]   Go Up