# Wolfram Mathematica

## 3D Reconstruction via Focus Stacking

Microscopes have a shallow depth of field. Image features in front of or behind the focal plane are quickly out of focus and blurred.

Focus stacking is an image processing technique to combine a stack of images with different focal lengths into one by selectively collecting in-focus regions from each image in the stack.

The built-in function ImageFocusCombine performs exactly that task.

Furthermore, one can derive a depth map for every feature in focus and, thus, for the corresponding pixels in the image. The resulting depth map in combination with the original image provides a 3D reconstruction of the specimen under the microscope.

Take the norm of the Laplacian filter as an indicator for a pixel being in or out of focus. The Laplacian filter picks up the high Fourier coefficients, which are subdued first if an image is out of focus.

Then for each pixel, pick the layer that exhibits the largest Laplacian filter norm.

Multiply the resulting binary volume with the focal stack and add up all layers. Thus, you collect only those pixel values that are in focus.

The binary volume depthVol contains the depth information of each pixel. Convert it into a two-dimensional depth map.

The depth information is quite noisy and not equally reliable for all pixel locations. Only edges provide a clear indication if an image region is in focus or not. Thus, use the total focusResponses as a confidence measure for the depth map.

Take only those depth measures with a confidence larger than 0.05 into account.

You can regularize the depth values with MedianFilter and close gaps via FillingTransform.

Display the depth map in 3D using the in-focus image as its texture.