Scripting Resources for DigitalMicrograph™

banner

Dave Mitchell's DigitalMicrograph™ Scripting Website

Home | Scripts | Examples | Functions | Recent Updates | Tutorials | Resources | Publications | Consulting | Projects | Contact & Bio |PyJEM| Search

 

zFocus
Function
Extracts the optimum regions of focus to create an in-focus 2D image from a 3D through-focus stack.
Version
version:20221005, v1.1
Author
D. R. G. Mitchell
Acknowledgements
Thanks to Ke-Bin Low for bug reporting and testing.
Comments

Updated in v1.1: In GMS 3 Gatan changed the way the rebin() function works. Unfortunately, revising the script to be compatible with this change would break backward compatibility with GMS 2. I have therefore replaced the Gatan function with a rebinning function of my own, and this script is now fully compatible with both GMS 2 and 3. As with all my scripts running in GMS 3 - you are advised to close ALL workspaces bar the one you are working in. This will make the GMS appear the same (to the script) as GMS 2 - for which it was written.

This will work on any 3D stack which contains a through-focus series of images. These can be acquired in TEM/STEM automatically using the through-focus acquisition script. If you are capturing images on optical or SEM systems - you can manually capture a series of TIFF images at various foci. Then open them in DigitalMicrograph and convert them into a stack using the Stack Alignment script. Then run this script. There is no requirement that the focal intervals be regular. The only requirement is that there is some overlap between in-focus regions in neighbouring slices of the stack. Where focus changes rapidly in regions of major height change, make sure that you capture enough small focal steps (the focal steps do not need to be evenly spaced). Typically, 10 or so focal steps across the whole region of interest will be adequate. For very rough surfaces, or on systems with very low depth of field, you may need to increase this. A script for automating through-focus stack acquisition for SEM has also been released.

The script measures local focus via the variance. This is quite a noisy parameter and binning and fitting of a polynomial to the focus surface is used to deal with that noise. If the specimen surface varies smoothly - eg it is part of a spherical particle, then fitting of a 2nd polynomial to the measured focus surface may work well. If the specimen surface is more convoluted, then using a 4th order polynomial fit to the measured focus surface may work better. Regions of best focus are extracted from the stack as tiles - the size of the tile is controlled by the Kernel Size variable. Smaller kernels (eg 4), will give fine scale control. However, if the image lacks detail or is noisy, then the selection of the best focus may be poor. Using a larger kernel size (eg 30) will reduce the noise and improve the selection. However, the larger spatial sampling will mean that narrow regions, where focus changes rapidly, may not be well selected, due to interference from neighbouring regions of different focus.

The final in-focus image is then assembled from the best focus tiles. To aid optimum parameter selection, the focus surface can be displayed. This shows the size of the tiles used and the numerical value of the slice from which the tile was taken. For a 10 slice stack, the numerical values will vary between 0 and 9. Ensure that the focus surface is a good representation of how focus changes in your specimen. If you know the numerical values of the start and end focus - you can recalibrate the focus surface image to give a representation of the specimen height.

System Requirements
Tested on GMS 2.32 and GMS 3.2.
Known Issues

If your imaging system causes image shifts or magnification changes when adjusting focus, this script will not produce a good result.

Supported
Yes.
Included Files
Single script file.
Source Code

See attached script file.