My focal stacking algorithm works by doing a deblur on a weighted sum of the input image stack. For each pixel in each input image, a sharpness value is computed by summing the squared gradients in x and y directions. My algorithm then sums all input image using the sharpness values as weights. It’s as if the result image was taken using a camera with a translating detector described in Hajime Nagahara’s paper. This image can then be deblurred with a PSF that has a similar shape to the one used in that paper (see the Results section).
The Mask image is computed using the same weights, but with original pixel values being an input image’s position in the stack.

References:
Flexible Depth of Field Photography
Hajime Nagahara, Sujit Kuthirummal, Changyin Zhou, Shree K. Nayar
ECCV '08 Proceedings of the 10th European Conference on Computer Vision: Part IV, Pages 60 - 73 
