Filtering & thresholding

Note

For the moment, this submodule is only used in post processing.

This submodule contains thresolding and filter that are used in post processing.

biom3d.utils.filtering.center(labels: ndarray, idx: int) ndarray[source]

Compute the barycenter of pixels belonging to a specific label.

Parameters:
  • labels (numpy.ndarray) – Label image array where each pixel has an integer label.

  • idx (int) – Label index for which to compute the barycenter.

Returns:

Coordinates of the barycenter as a 1D array (e.g. [y, x] or [z, y, x] depending on dimensions). If no pixels with the given label are found, returns an empty array.

Return type:

numpy.ndarray

biom3d.utils.filtering.closest(labels: ndarray, num: int) int[source]

Find the label index of the object closest to the center of the image.

The function computes the barycenter of all objects (labels 1 to num), then returns the label of the object whose barycenter is closest to the image center.

Parameters:
  • labels (numpy.ndarray) – Label image array where each pixel has an integer label.

  • num (int) – Number of labels (excluding background) to consider.

Returns:

The label index (1-based) of the object closest to the image center. Returns 1 if no objects are found.

Return type:

int

biom3d.utils.filtering.compute_otsu_criteria(im: ndarray, th: float) float[source]

Compute the Otsu criteria value for a given threshold on the image.

This function implements the core step of Otsu’s method, which evaluates the within-class variance weighted by class probabilities for a specific threshold. The goal is to find the threshold minimizing this weighted variance. Found here: https://en.wikipedia.org/wiki/Otsu%27s_method.

Parameters:
  • im (numpy.ndarray) – Grayscale input image as a 2D numpy array.

  • th (float) – Threshold value to evaluate.

Returns:

Weighted sum of variances for the two classes separated by the threshold. Returns np.inf if one class is empty (to ignore this threshold).

Return type:

float

biom3d.utils.filtering.dist_vec(v1: ndarray, v2: ndarray) float[source]

Euclidean distance between two vectors (np.array).

Parameters:
  • v1 (numpy.ndarray) – Vector 1

  • v2 (numpy.ndarray) – Vector 2

Returns:

Euclidean distance between v1 and v2.

Return type:

float

biom3d.utils.filtering.keep_big_volumes(msk: ndarray, thres_rate: float = 0.3) ndarray[source]

Return a mask keeping only the largest connected components based on a volume threshold.

The threshold is computed as: min_volume = thres_rate * otsu_thresholding(volumes) where volumes are the sizes of all connected components (excluding background), and otsu_thresholding finds an adaptive threshold on the volumes distribution.

Parameters:
  • msk (numpy.ndarray) – Input binary mask.

  • thres_rate (float, default=0.3) – Multiplier for the threshold on volumes.

Returns:

Mask with only the connected components whose volume is greater than the threshold. Background remains zero.

Return type:

numpy.ndarray

biom3d.utils.filtering.keep_biggest_volume_centered(msk: ndarray) ndarray[source]

Return a mask with only the connected component closest to the image center, provided its volume is not too small compared to the largest connected component. Otherwise, return the largest connected component.

“Too small” means its volume is less than half of the largest component.

The returned mask intensities are either 0 or msk.max().

Parameters:

msk (numpy.ndarray) – Input binary mask.

Returns:

Mask with only one connected component kept.

Return type:

numpy.ndarray

biom3d.utils.filtering.keep_center_only(msk: ndarray) ndarray[source]

Keep only the connected component in the mask that is closest to the image center.

Parameters:

msk (numpy.ndarray) – Binary mask (2D or 3D) where connected components are to be analyzed.

Returns:

Mask with only the connected component closest to the center. The returned mask has the same dtype as input, with values 0 or 255.

Return type:

numpy.ndarray

biom3d.utils.filtering.otsu_thresholding(im: ndarray) float[source]

Compute the optimal threshold for an image using Otsu’s method.

This function searches for the threshold value that minimizes the weighted within-class variance of the thresholded image.

Parameters:

im (numpy.ndarray) – Grayscale input image as a 2D numpy array.

Returns:

Optimal threshold value computed using Otsu’s method.

Return type:

float

biom3d.utils.filtering.volumes(labels: ndarray) ndarray[source]

Compute the volume (pixel or voxel count) of each label in the label image.

Parameters:

labels (numpy.ndarray) – Label image array where each pixel has an integer label.

Returns:

Array of counts of pixels per label, sorted by label index ascending.

Return type:

numpy.ndarray