Skip to contents

High-performance native tools for focus stacking in digital imaging and microscopy:

  • align_stack(): Sub-pixel image alignment and registration implemented natively in C++ using multi-scale Gaussian pyramid Enhanced Correlation Coefficient (ECC) maximization (affine motion or translation) to eliminate focus breathing, translation, and rotation, with optional automatic border cropping. Zero external Python/OpenCV dependencies.

  • fuse_multifocus(): Multi-Focus Image Fusion (MFIF) supporting:

    1. Deep learning ONNX models (e.g., IFCNN) with either hierarchical tournament tree (strategy = "pyramid") or linear (strategy = "sequential") fusion.

    2. Ultra-fast native C++ Modified Laplacian Extended Depth of Field (method = "laplacian"), ideal for massive stacks of 50-500 images in < 1 second.

  • process_focus_stack(): Orchestrates the complete end-to-end focus stacking pipeline, taking raw images, performing alignment, keyframe subsampling (max_frames), and fusion.

Usage

align_stack(
  images,
  reference_index = NULL,
  method = c("ecc", "translation", "orb"),
  crop = TRUE,
  levels = 3,
  max_iter = 50,
  eps = 1e-04,
  max_frames = NULL,
  verbose = TRUE,
  ...
)

image_align_stack(
  images,
  reference_index = NULL,
  method = c("ecc", "translation", "orb"),
  crop = TRUE,
  levels = 3,
  max_iter = 50,
  eps = 1e-04,
  max_frames = NULL,
  verbose = TRUE,
  ...
)

fuse_multifocus(
  images,
  method = c("ifcnn", "laplacian"),
  strategy = c("pyramid", "sequential"),
  blend = c("soft", "hard"),
  radius = 3,
  power = 6,
  max_frames = NULL,
  model = "ifcnn",
  engine = c("gpu", "cpu"),
  threads = 0L,
  device_id = -1L,
  verbose = TRUE,
  ...
)

image_fuse_multifocus(
  images,
  method = c("ifcnn", "laplacian"),
  strategy = c("pyramid", "sequential"),
  blend = c("soft", "hard"),
  radius = 3,
  power = 6,
  max_frames = NULL,
  model = "ifcnn",
  engine = c("gpu", "cpu"),
  threads = 0L,
  device_id = -1L,
  verbose = TRUE,
  ...
)

process_focus_stack(
  images,
  align = TRUE,
  align_method = c("ecc", "translation", "orb"),
  reference_index = NULL,
  crop = TRUE,
  levels = 3,
  max_iter = 50,
  eps = 1e-04,
  max_frames = NULL,
  method = NULL,
  fusion_method = c("ifcnn", "laplacian"),
  strategy = c("pyramid", "sequential"),
  blend = c("soft", "hard"),
  radius = 3,
  power = 6,
  model = "ifcnn",
  engine = c("gpu", "cpu"),
  threads = 0L,
  device_id = -1L,
  return_aligned = FALSE,
  verbose = TRUE,
  ...
)

image_focus_stack(
  images,
  align = TRUE,
  align_method = c("ecc", "translation", "orb"),
  reference_index = NULL,
  crop = TRUE,
  levels = 3,
  max_iter = 50,
  eps = 1e-04,
  max_frames = NULL,
  method = NULL,
  fusion_method = c("ifcnn", "laplacian"),
  strategy = c("pyramid", "sequential"),
  blend = c("soft", "hard"),
  radius = 3,
  power = 6,
  model = "ifcnn",
  engine = c("gpu", "cpu"),
  threads = 0L,
  device_id = -1L,
  return_aligned = FALSE,
  verbose = TRUE,
  ...
)

Arguments

images

A list of Image objects, an array of images, or a character vector of file paths to images in the stack.

reference_index

Integer specifying the index of the base reference image for alignment. If NULL (default), the central image of the stack (ceiling(length(images) / 2)) is selected as the optical reference.

method

Character specifying the method:

  • In align_stack(): Alignment model ("ecc" for 2D Affine motion or "translation" for 2D shifts).

  • In fuse_multifocus(): Fusion algorithm ("ifcnn" for deep learning or "laplacian" for native C++ EDF).

crop

Logical. Should black/empty borders introduced by affine warping be automatically cropped out? Defaults to TRUE.

levels

Integer specifying number of pyramid levels for coarse-to-fine ECC alignment (default: 3).

max_iter

Integer specifying maximum iterations per pyramid level for ECC convergence (default: 50).

eps

Numeric threshold for ECC convergence (default: 1e-4).

max_frames

Optional integer. If provided and the image stack exceeds this number of frames, automatically subsamples the stack to max_frames evenly spaced focal planes.

verbose

Logical. If TRUE (default), displays progress messages.

...

Additional arguments passed to underlying functions.

strategy

Multi-image fusion strategy for deep learning ("pyramid" or "sequential"). Defaults to "pyramid". Hierarchical tournament tree reduction pairs images at each level, bounding the number of sequential convolution passes per pixel to \(\lceil \log_2(N) \rceil\) and preventing contrast loss and blur over large image stacks.

blend

Character specifying blending mode for Laplacian fusion: "soft" (default, weighted softmax power blending) or "hard" (maximum sharpness pixel selection).

radius

Integer specifying the local window radius for Laplacian sharpness aggregation (default: 3).

power

Numeric exponent for soft Laplacian blending weights (default: 6.0).

model

Character specifying the fusion ONNX model. Defaults to "ifcnn". Can also be a direct path to a custom ONNX model file.

engine

Execution engine: "cpu" (default) or "gpu" (DirectML acceleration).

threads

Integer number of threads for ONNX inference (0 for auto).

device_id

GPU device ID (-1 for default best detected GPU).

align

Logical. Should the image stack be aligned before fusion? Defaults to TRUE.

align_method

Alignment method for process_focus_stack(). One of "ecc" or "translation".

fusion_method

Multi-focus fusion method for process_focus_stack(). One of "ifcnn" (deep learning) or "laplacian" (native C++ Modified Laplacian Extended Depth of Field).

return_aligned

Logical. If TRUE, returns a named list with both fused and aligned images. If FALSE (default), returns only the fused Image.

Value

  • align_stack(): A list of aligned Image objects with identical spatial dimensions.

  • fuse_multifocus(): An all-in-focus Image object.

  • process_focus_stack(): The fused Image object (or a list containing fused and aligned if return_aligned = TRUE).

References

Zhang, Y., Liu, Y., Sun, P., Yan, H., Zhao, X., & Zhang, L. (2020). IFCNN: A general image fusion framework based on convolutional neural network. Information Fusion, 54, 99-118.

Evangelidis, G. D., & Psarakis, E. Z. (2008). Parametric image alignment using enhanced correlation coefficient maximization. IEEE Transactions on Pattern Analysis and Machine Intelligence, 30(10), 1858-1865.

Author

Tiago Olivoto tiagoolivoto@gmail.com

Examples

if (FALSE) { # interactive()
library(pliman)

# Load or read a stack of multi-focus images
imgs <- list(
  image_read("focus_frame1.jpg"),
  image_read("focus_frame2.jpg"),
  image_read("focus_frame3.jpg")
)

# Fast automated pipeline with native C++ Modified Laplacian (zero-AI, ultra-fast)
fused_lap <- process_focus_stack(imgs, fusion_method = "laplacian", blend = "soft")
plot(fused_lap)

# Deep learning IFCNN with hierarchical tournament pyramid reduction
fused_dl <- process_focus_stack(imgs, fusion_method = "ifcnn", strategy = "pyramid")
plot(fused_dl)

# Handling large stacks (e.g. 200 frames from a focus rack video)
# Automatically extract 25 key focal planes for alignment and fusion:
fused_rack <- process_focus_stack(imgs, max_frames = 25, fusion_method = "laplacian")
}