Skip to content

Software Autofocus

The software autofocus module operates in two phases: Focus Evaluation (calculating focus scores for each Z-plane) and Focus Execution (selecting the optimal plane and applying the mechanical offset).

Focus Evaluation Metrics

The software evaluates image focus by analyzing pixel intensity statistics using one of the selected algorithms below.

Maximum Intensity Metric

Conceptual Overview

The Maximum algorithm is designed for samples with sparse, high-contrast fluorescent signals on dark backgrounds. It identifies focus by finding the Z-plane that produces the single brightest pixel, assuming the peak fluorescence is reached at the exact focal plane.

Mathematical Formulation

The algorithm calculates the peak pixel intensity across the entire acquired sensor image:

\[F_{\text{Maximum}} = \max_{x \in [0, W), y \in [0, H)} I(x, y)\]

where:

  • \(I(x, y)\) represents the pixel intensity at coordinates \((x, y)\).
  • \(W\) and \(H\) represent the total width and height of the acquired sensor image in pixels, respectively.

Sharpness Metric

Conceptual Overview

The Sharpness algorithm is designed for dense morphological structures (such as brightfield or phase contrast). It identifies focus by detecting high-frequency contrast gradients, which are strongest when the features are in sharp focus. To minimize background noise and ignore peripheral out-of-focus elements, the algorithm isolates a central region, filters noise, and computes local pixel gradients.

Mathematical Formulation

  1. ROI Definition: To focus on the center of the field of view and ignore peripheral optical aberrations or sample tilt, the algorithm crops the center \(20\%\) of the sensor image. The cropped region width \(W_c\) and height \(H_c\) (in pixels) are defined as:

    \[W_c = 0.2 \cdot W, \quad H_c = 0.2 \cdot H\]

    The crop offsets \(O_w\) and \(O_h\) (in pixels) from the top-left corner are:

    \[O_w = 0.4 \cdot W, \quad O_h = 0.4 \cdot H\]
  2. Noise Reduction: A spatial \(3 \times 3\) median filter is applied to the cropped region to remove single-pixel sensor noise that might skew gradient calculations. The filtered intensity \(M(i, j)\) for each pixel \((i, j)\) within the cropped area is:

    \[M(i, j) = \text{median} \{ I(O_w + i + \delta_x, O_h + j + \delta_y) \}\]

    where \(\delta_x, \delta_y \in \{-1, 0, 1\}\).

    Median Filter Edge Behavior

    When evaluating pixels located on the outer border of the \(20\%\) cropped ROI, the \(3 \times 3\) median window reads adjacent pixels from the surrounding original uncropped sensor image \(I\). Because the cropped ROI is centered, the required border pixels are always valid and accessible within the surrounding sensor space.

  3. Edge Detection: The algorithm applies a diagonal gradient kernel \(K\) to detect high-frequency transitions:

    \[K = \begin{bmatrix} -2 & -1 & 0 \\ -1 & 0 & 1 \\ 0 & 1 & 2 \end{bmatrix}\]

    The local gradient response \(S(k, l)\) is computed as the Frobenius inner product (element-wise product sum, denoted as \(:\)) of the kernel \(K\) and the local \(3 \times 3\) neighborhood matrix \(\mathbf{M}_{3\times 3}(k, l)\) centered at \((k, l)\):

    \[S(k, l) = K : \mathbf{M}_{3\times 3}(k, l) = \sum_{u=-1}^{1} \sum_{v=-1}^{1} K(u+2, v+2) \cdot M(k+u, l+v)\]

    where the local neighborhood matrix is:

    \[\mathbf{M}_{3\times 3}(k, l) = \begin{bmatrix} M(k-1, l-1) & M(k, l-1) & M(k+1, l-1) \\ M(k-1, l) & M(k, l) & M(k+1, l) \\ M(k-1, l+1) & M(k, l+1) & M(k+1, l+1) \end{bmatrix}\]

    Expanding this sum yields the exact pixel intensity equation:

    \[S(k, l) = -2M(k-1, l-1) - M(k, l-1) - M(k-1, l) + M(k+1, l) + M(k, l+1) + 2M(k+1, l+1)\]

    Gradient Kernel Edge Behavior

    To prevent the \(3 \times 3\) gradient kernel from reading outside the boundaries of the median-filtered crop matrix, the algorithm excludes the outermost one-pixel border of the cropped ROI entirely from the edge detection step. The loop limits are strictly bound to \(k \in [1, W_c - 2]\) and \(l \in [1, H_c - 2]\) during summation.

  4. Sharpness Score Computation: The final sharpness score \(F_{\text{Sharpness}}\) is the sum of the squared gradient responses over the cropped area:

    \[F_{\text{Sharpness}} = \sum_{k=1}^{W_c-2} \sum_{l=1}^{H_c-2} [S(k, l)]^2\]

Autofocus Execution and Offset Correction

Once the software calculates the focus scores (\(F_i\)) for all \(N\) acquired Z-planes (where \(N\) is the value specified in the Focus Number field), it determines and applies the optimal focus position through the following steps:

Best Focal Plane Selection

The software identifies the index \(i_{\text{best}}\) of the Z-plane that maximizes the focus score:

\[i_{\text{best}} = \arg\max_{i \in [0, N)} F_i\]

Z-Position Update

The system retrieves the physical Z-stage coordinate \(Z(i_{\text{best}})\) associated with the best-scoring plane.

Mechanical Offset Correction

In the subsequent image acquisition steps, the system drives the focus device to this optimal position:

\[Z_{\text{optimal}} = Z(i_{\text{best}})\]

If a destination device is selected under Apply offset to, the software calculates the relative physical offset:

\[\Delta Z = Z(i_{\text{best}}) - Z_{\text{nominal}}\]

The software then applies this offset correction to the selected destination device, ensuring the sample remains in focus throughout the multi-position or time-lapse sequence.