RetouchPRO

RetouchPRO (http://www.retouchpro.com/forums/)
-   Photo Retouching (http://www.retouchpro.com/forums/photo-retouching/)
-   -   The Math Behind Photoshop's Unsharp Mask and Gauss (http://www.retouchpro.com/forums/photo-retouching/37729-math-behind-photoshops-unsharp-mask-gauss.html)

Drazick 04-27-2014 10:29 AM

The Math Behind Photoshop's Unsharp Mask and Gauss
 
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?

P.S.
I'm asking here because I know many people here have a deep understanding about the math of Photoshop and how things works internally.

Thank You.

Der_W 04-27-2014 11:25 AM

Re: The Math Behind Photoshop's Unsharp Mask and G
 
Your formula seems to have one too many inv() in it I think.
USM('O') = O + (O-GB) - (O + inv(GB)) works fine and is a 1:1 representation to USM with 100% strength, the Gaussian blur radius and 0 threshold.
Photoshop's Gaussian blur is quite heavily optimized for performance, therefore the "correct" algorithm will give too weak effects: http://www.hackerfactor.com/blog/ind...The-Lines.html

Drazick 04-27-2014 11:50 AM

Re: The Math Behind Photoshop's Unsharp Mask and G
 
Hi,
I went according to this:
http://www.lynda.com/Photoshop-tutor...3/34451-4.html

And he does the "Extra" inverse.
I will try later without to see if it does work :-).

Moreover, if inv(L) = 1 - L (Assuming images normalized into [0, 1]) your equation is:

USM('O') = O + (O-GB) - (O + inv(GB)) = O + O - GB - O - 1 + GB = O - 1.
Which isn't USM by all means.

What strange, my equation yields USM(O) = 3O - 2GB.

It's strange, really strange.
Am I missing something? Maybe I'm wrong about the inversion operation?

klev 04-27-2014 05:33 PM

Re: The Math Behind Photoshop's Unsharp Mask and G
 
What is your bit depth? You mention 0 to 1 and add/subtract, but that only works with linear float values. If you're using 8 to 16 bits per channel in photoshop, the working space and encoding of the image both have baked gamma, so you will not get the right results from standard addition and subtraction. Half floats are also unsupported. PS just promotes them to 32 bits, presumably by zero and sign extension.

Any reason for mentioning python apart from its math libraries?

Drazick 04-27-2014 05:37 PM

Re: The Math Behind Photoshop's Unsharp Mask and G
 
I'm just trying to implement Gaussian Blur and USM as close as possible to Photoshop.

Drazick 04-27-2014 06:01 PM

Re: The Math Behind Photoshop's Unsharp Mask and G
 
Ok,
I think I'm getting close.

Using Photoshop's GB Impulse Response I think I have something close to how Photoshop's Gaussian Blur is working.

The size of the filter is M X M where M = ceil(radius).
The Std of the kernel is radius / 3.

The USM is given by:
USM(O) = O + 2 * (Amount / 100) * (O - B).

Didn't figure out the Threshold yet.

Someone has something better?

klev 04-27-2014 06:07 PM

Re: The Math Behind Photoshop's Unsharp Mask and G
 
You're just hacking random stuff, and you're going to introduce artifacts with that formula, although it may be close. Maybe that's why I dislike the look of PS's sharpening. The MxM thing is how all resampling algorithms work. If you're interested in the topic try reading some of gpu gems or glassner's graphics gems series.

Drazick 04-27-2014 11:13 PM

Re: The Math Behind Photoshop's Unsharp Mask and G
 
Klev,
I was wrong with the formula.
It's M = 2 * ceil(Radius).

You have at least 3 sigma as radius, the Gaussian is practically 0 outside it.
Though probability they use IIR filter for that.

What's interesting is using inv(L) = White - L:

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

Though using Apply Image yields different results.
I mean, create (O - B) ussing Apply Image (Subtract)
Then add (O - B) to O twice using Apply Image (Add).
The result isn't USM.
What's wrong?
Is it just the clipping in each phase?

klev 04-28-2014 01:55 AM

Re: The Math Behind Photoshop's Unsharp Mask and G
 
Are you in 32 bit mode? If not add won't work as you expect, nor will subtract. It doesn't matter that you're using apply image to set them.

Drazick 04-28-2014 06:35 AM

Re: The Math Behind Photoshop's Unsharp Mask and G
 
Hi,
Photoshop doesn't allow Subtract and Add in 32 Bit mode.

So, If I have the photos in MATLAB, how should recreate this?
Should I clip into [0, 255] / [0, 1] after each subtraction / addition?

It sounds weird that the code USM does clipping into [0, 1] / [0, 255] after each step.


All times are GMT -6. The time now is 03:05 AM.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
Copyright 2016 Doug Nelson. All Rights Reserved