Stack Focuser

Author: Michael Umorin (mikeumo at
History: 2002/10/11: First version
2002/11/05: Support for rectangular ROIs; different naming scheme of the resluting image
2003/08/13: Height map option; recordable
2004/03/15: Works with RGB stacks
2006/07/18: Added option of using only one color for focusing (green) and copying the rest of the colors along, which can be useful in human vision applications.
Installation: Download Stack_Focuser_.class to the plugins folder and restart ImageJ.
Description: The plugin is a very simple algorithm of patching a *focused * image from a stack of images corresponding to different focal planes. Plugin provides optional generation of an 8-bit greyscale "height" map, i.e. image of heights at which focused elements are located. It is very important that images in the stack are of the same brightness; otherwise pasting together pieces of different images will create artificial edges.

Code for running this plugin from another plugin or from a macro can be generated using the command recorder (Plugins/Macros/Record).

What the plugin does:

  1. creates a 32-bit float copy of original stack of ROI or the whole image area to preserve precision;
  2. on 32-bit copy finds edges for each slice in a stack by running median 3x3 filter (to reduce noise) and then Sobel filter;
  3. creates a "map" of how far the influence of edges extends; i.e. how far from a focused edge we still think that the image is focused; this is done by taking the maximum value in the neighborhood of the specified size;
  4. for every pixel (x, y) based on the choice of the maximum "edge" value among different slices in the "map" stack [the plugin] copies the pixel value from the appropriate original image to the new image. if selected, sets the corresponding value in the "height" map to the 255*i/stack_size, where i is the "focused" slice.

Plugin works on a currently active 8-bit or 16-bit grayscale stack and accepts rectangular ROI; if no ROI is specified plugin works in the whole image. It is possible to consequetively select a ROI and run plugin several times on the same stack with different kernel sizes since ImageJ allows coexistence of several windows with same title at the same time. The size in pixels (odd integer > 1) of the Maximum square filter is requested; trial and error would be the fastest way to optimize the result. The final image is written to "Focused_"+<original stack title - extension> window. The height map is written to "HeightMap_"+<original stack title - extension> window. No changes are made to the original stack.

For more information see program code. The author would appreciate any suggestions regarding the plugin (improvements, extensions, interface changes, etc.).

See Also: Extended Depth of Field plugin

|Plugins | Home |