
Photo Retouching "Improving" photos, postproduction, correction, etc. 
 Thread Tools 
#1
 
 
The Math Behind Photoshop's Unsharp Mask and Gauss 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 + (OGB)  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. 
#2
 
 
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 + (OGB)  (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...TheLines.html 
#3
 
 
Re: The Math Behind Photoshop's Unsharp Mask and G
Hi, I went according to this: http://www.lynda.com/Photoshoptutor...3/344514.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 + (OGB)  (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? 
#4
 
 
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? 
#5
 
 
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.

#6
 
 
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? 
#7
 
 
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.

#8
 
 
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? 
#9
 
 
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.

#10
 
 
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. 

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