RetouchPRO

Go Back   RetouchPRO > Technique > Photo Retouching
Register Blogs FAQ Site Nav Search Today's Posts Mark Forums Read


Photo Retouching "Improving" photos, post-production, correction, etc.

The Math Behind Photoshop's Unsharp Mask and Gauss

Reply
 
Thread Tools
  #1  
Old 04-27-2014, 11:29 AM
Drazick's Avatar
Drazick Drazick is offline
Junior Member
 
Join Date: Nov 2008
Posts: 24
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.
Reply With Quote top
  #2  
Old 04-27-2014, 12:25 PM
Der_W's Avatar
Der_W Der_W is offline
Senior Member
 
Join Date: Jul 2009
Location: Germany
Posts: 558
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
Reply With Quote top
  #3  
Old 04-27-2014, 12:50 PM
Drazick's Avatar
Drazick Drazick is offline
Junior Member
 
Join Date: Nov 2008
Posts: 24
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?
Reply With Quote top
  #4  
Old 04-27-2014, 06:33 PM
klev klev is offline
Senior Member
 
Join Date: Jun 2010
Posts: 1,109
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?
Reply With Quote top
  #5  
Old 04-27-2014, 06:37 PM
Drazick's Avatar
Drazick Drazick is offline
Junior Member
 
Join Date: Nov 2008
Posts: 24
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.
Reply With Quote top
  #6  
Old 04-27-2014, 07:01 PM
Drazick's Avatar
Drazick Drazick is offline
Junior Member
 
Join Date: Nov 2008
Posts: 24
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?
Reply With Quote top
  #7  
Old 04-27-2014, 07:07 PM
klev klev is offline
Senior Member
 
Join Date: Jun 2010
Posts: 1,109
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.
Reply With Quote top
  #8  
Old 04-28-2014, 12:13 AM
Drazick's Avatar
Drazick Drazick is offline
Junior Member
 
Join Date: Nov 2008
Posts: 24
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?
Reply With Quote top
  #9  
Old 04-28-2014, 02:55 AM
klev klev is offline
Senior Member
 
Join Date: Jun 2010
Posts: 1,109
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.
Reply With Quote top
  #10  
Old 04-28-2014, 07:35 AM
Drazick's Avatar
Drazick Drazick is offline
Junior Member
 
Join Date: Nov 2008
Posts: 24
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.
Reply With Quote top
Reply

  RetouchPRO > Technique > Photo Retouching


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -6. The time now is 06:38 PM.


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