YES ! it 's called "focus stacking".
the algorithm, quoting from here goes like this:
- Align the images. Changing the focus on a lens, even
if the camera remains fixed, causes a mild zooming on the images.
We need to correct the images so they line up perfectly on top
of each other.
- Perform a gaussian blur on all images
- Compute the laplacian on the blurred image to generate a gradient map
- Create a blank output image with the same size as the original input
images
- For each pixel [x,y] in the output image, copy the pixel [x,y] from
the input image which has the largest gradient [x,y]