Automatic thresholding
- auto_threshold(image: np.ndarray, roi_size: int = 7, max_iterations: int = 4) float
Computes an automatic threshold based on image segmentation using an external DLL library.
This function applies an iterative segmentation to the image using the PALM (Point Accumulation for Localized Mean) method. The goal is to progressively refine a threshold based on the standard deviation of the non-segmented pixels.
- Parameters:
- Returns:
Threshold computed after convergence (standard deviation of non-segmented pixels).
- Return type:
Algorithm
The algorithm operates in multiple steps:
Initialization:
Create an empty mask (same size as the image, filled with zeros).
Compute the initial standard deviation of the image (\(\sigma = \sqrt{\frac{1}{N} \sum_{i=1}^{N} (x_i - \bar{x})^2}\)).
Define the half-size of the region of interest (roi_size).
Iterative segmentation:
- Call the DLL PALM function to detect characteristic points with the following parameters:
The threshold is equal to the computed standard deviation.
Watershed is disabled.
Gaussian fitting is disabled.
Sigma is set to 1.
Theta is set to \(\pi / 4\).
Update the mask by enabling the pixels corresponding to the detected regions (ROI around each detected point).
Recompute the standard deviation using the pixels outside the segmentation.
Repeat until max_iterations is reached or until convergence.
Stopping criterion:
If all pixels are segmented before the end of the iterations, the loop stops early. This is only possible if the detected points and their regions of interest cover the entire image.
Usage example
Here is an example of how to use the function:
import ctypes
import numpy as np
from palm_tracer.Processing import Palm
# Utilisation de PALM et Chargement de la DLL
palm = Palm()
# Image factice 100x100 avec des valeurs aléatoires
image = np.random.randint(0, 255, (100, 100), dtype=np.uint8)
# Calcul du seuil
seuil = palm.auto_threshold(image)
print(f"Computed threshold: {seuil}")
Notes
Using the DLL is essential for this function to work correctly.
Performance depends on the image size and on the number of iterations.
The PALM approach provides robust segmentation on noisy images.