# Focus Stacking: Workflow with Open Source Program Focus-Stack

For details, see here: [Focus-Stack Documentation](https://github.com/PetteriAimonen/focus-stack/blob/master/docs/focus-stack.md)

## Installation

1. Download: [Focus-Stack Windows Release](https://github.com/PetteriAimonen/focus-stack/releases/download/1.4/focus-stack_Windows.zip)

2. Unpack the contents to any directory.

3. Create a directory named *work* within the program directory.

4. Create a script named `aaa.cmd` (an arbitrary name) in the program directory.  
   Example for *tif* images (adjust according to your requirements):

   ```
   focus-stack.exe --output=work/output.jpg --no-whitebalance --consistency=2 --depthmap-threshold=110 --halo-radius=20 --verbose --3dview=3dview.png --denoise=2 work/*.tif
   ```

   Example for *jpg* images : (adjust according your requirements)

   ```
   focus-stack.exe --output=work/output.jpg --no-whitebalance --consistency=2 --depthmap-threshold=110 --halo-radius=20 --verbose --3dview=3dview.png --denoise=2 work/*.jpg
   ```

Directory stcructue after installation:

```
 Directory of f:\Programs\focus-stack (root directory)

01/29/2025  11:21 AM         4,118,714 3dview.png
12/28/2024  08:07 PM           265,403 denoised_merge_result.jpg
12/25/2024  09:37 PM    <DIR>          depthmap
12/28/2024  08:07 PM           384,450 depth_inpaint_filled.png
12/28/2024  08:07 PM           111,917 depth_inpaint_lines.png
12/28/2024  08:07 PM            22,804 depth_inpaint_lr_out.png
12/28/2024  08:07 PM             5,619 depth_inpaint_lr_points.png
12/28/2024  08:07 PM            62,359 depth_inpaint_mask.png
12/28/2024  08:07 PM            68,566 depth_inpaint_masked.png
12/28/2024  08:07 PM            34,342 depth_inpaint_points.png
12/25/2024  09:37 PM         1,694,208 focus-stack.exe
12/25/2024  09:37 PM            11,659 focus-stack.html
12/28/2024  08:07 PM           771,480 gauss_amp.png
12/28/2024  08:07 PM           923,217 gauss_dev.png
12/28/2024  08:07 PM           911,687 gauss_mean.png
01/31/2025  12:15 PM                 0 h.txt
12/25/2024  09:37 PM           620,544 jpeg62.dll
01/29/2025  11:34 AM             7,225 l.log
12/25/2024  09:37 PM           156,160 liblzma.dll
12/25/2024  09:37 PM           197,120 libpng16.dll
12/25/2024  09:37 PM           487,936 libprotobuf-lite.dll
12/25/2024  09:37 PM         2,426,368 libprotobuf.dll
12/25/2024  09:37 PM         2,183,168 libprotoc.dll
12/25/2024  09:37 PM             1,066 LICENSE.txt
12/25/2024  09:37 PM         2,138,112 opencv_calib3d.dll
12/25/2024  09:37 PM         3,461,120 opencv_core.dll
12/25/2024  09:37 PM         3,701,760 opencv_dnn.dll
12/25/2024  09:37 PM           759,808 opencv_features2d.dll
12/25/2024  09:37 PM           418,304 opencv_flann.dll
12/25/2024  09:37 PM           287,232 opencv_highgui.dll
12/25/2024  09:37 PM           304,128 opencv_imgcodecs.dll
12/25/2024  09:37 PM         4,866,560 opencv_imgproc.dll
12/25/2024  09:37 PM           553,472 opencv_ml.dll
12/25/2024  09:37 PM           694,272 opencv_objdetect.dll
12/25/2024  09:37 PM           635,392 opencv_photo.dll
12/25/2024  09:37 PM           757,248 opencv_stitching.dll
12/25/2024  09:37 PM           649,728 opencv_video.dll
12/25/2024  09:37 PM           513,024 opencv_videoio.dll
12/25/2024  09:37 PM             5,944 README.txt
01/04/2025  08:58 AM               172 aaa.cmd (script for program execution)
12/25/2024  09:37 PM           443,904 tiff.dll
12/25/2024  09:37 PM           696,320 turbojpeg.dll
12/25/2024  09:37 PM           371,200 webp.dll
12/25/2024  09:37 PM           159,744 webpdecoder.dll
12/25/2024  09:37 PM            20,480 webpdemux.dll
12/25/2024  09:37 PM            39,936 webpmux.dll
01/29/2025  10:57 PM    <DIR>          work
12/25/2024  09:37 PM            89,088 zlib1.dll
              48 File(s)     37,401,342 bytes

 Directory of f:\Programs\focus-stack\work (Directory with input data)

01/29/2025  10:53 PM         1,953,783 2025-01-29-22-53-54.jpg (input image)
01/29/2025  10:54 PM         1,921,067 2025-01-29-22-54-04.jpg (input image)
01/29/2025  10:54 PM         1,799,893 2025-01-29-22-54-12.jpg (input image)
01/29/2025  10:54 PM         1,698,658 2025-01-29-22-54-18.jpg (input image)
01/29/2025  10:54 PM         1,681,539 2025-01-29-22-54-26.jpg (input image)
01/29/2025  10:58 PM         3,353,412 3dview.png
01/29/2025  10:58 PM           458,836 depthmap.png
01/29/2025  10:57 PM           728,678 output.jpg (generated output image)
```



Capture Input Images

In my case, pictures are taken with a microscope camera as follows:

1. Prepare the object and place it securely under the camera.
2. Focus on the top focal point and take a picture.
3. Move the focus plane down until the next level of the object is sharp, and take the next picture.
4. Repeat step 3 for each level of the object to be shown in the final image (avoid taking too many pictures; only capture those that focus on important parts).

Images should be captured in the order of the focal movement sequence from top to bottom, ensuring that all other factors remain as consistent as possible aside from the movement:

- Same magnification
- Same crop
- Same exposure
- Same color balance

The alphabetical order of the image file names should correspond to the chronological sequence.

## Create Improved Image

1. Delete all existing files in the *work* folder.
2. Copy captured images to the *work* folder.
3. Execute `aaa.cmd`.
4. Check the result in `output.jpg` and improve it if necessary (e.g., using Photoshop).



