ddd_pylib.labeling.

ThresholdOperator#

class ddd_pylib.labeling.ThresholdOperator[source]#

Bases: ImageOperator

Creates a thresholded image (binary mask). The thresholding method can be selected from:

  • Isodata

  • Li

  • Mean

  • Median

  • Minimum

  • Otsu

  • Triangle

  • Yen

  • Local

  • Niblack

  • Sauvola

The default being Otsu.

This operator can handle objects on light and dark background. If you set the blackBackground to True, the higher values will be the foreground (True in the resulting mask). If you set it to False, the lower values will be the foreground.

The threshold can be computed independently for each time point, or globally for the whole image.

If you set the threshold manually, it will override the thresholding method. This range will be the same for all time points whether you activated the independentTime option or not.

Settable parameters:
  • thresholdMethod (setThresholdMethod) – The thresholding method to use

  • independentTime (setIndependentTime) – If True, the threshold is computed independently for each time point. If False, the threshold is computed globally for the whole image.

  • blackBackground (setBlackBackground) – If True, the higher values will be the foreground. If False, the lower values will be the foreground.

  • thresholdValue (setThresholdValue) – If set, the threshold is fixed to this value, and the thresholdMethod is ignored.

  • thresholdRange (setThresholdRange) – If set, the threshold is fixed to this range, and the thresholdMethod is ignored. The range is defined by a low and a high value. The foreground will be the values within this range.

Produces:

result (getResult)

The thresholded image as a xr.DataArray with the same dimensions and attributes as the main image.

Values are boolean, with True for foreground and False for background.

Example

from ddd_pylib.labeling import ThresholdOperator
from ddd_pylib import ImageUtils
from tifffile import imread
from pathlib import Path
import xarray as xr
import numpy as np

root = Path("data-ddd-pylib")
im_name = "nuclei-histo-shift.tif"
im = xr.DataArray(
    imread(root / im_name),
    dims=["T", "Z", "Y", "X"], # (3, 30, 349, 639)
    name=im_name,
)

op = ThresholdOperator()
op.setMainImage(im)
op.setIndependentTime(True) # Handles the shift of histo between time points
op.setBlackBackground(True)
op.setThresholdMethod('Otsu')
op.run()

res = op.getResult().astype(np.uint8)
ImageUtils.writeTiff(
    root / "results",
    res
)

Methods

static defaultBlackBackground()[source]#
Return type:

bool

static defaultFixedRange()[source]#
Return type:

Tuple

static defaultIndependentTime()[source]#
Return type:

bool

static defaultMethod()[source]#
Return type:

str

getPrefix()[source]#

Prefix added to the input image name to generate the output image name.

setBlackBackground(blackBackground)[source]#

Set the value of the blackBackground attribute

Parameters:

blackBackground (bool)

setIndependentTime(independentTime)[source]#

Set the value of the independentTime attribute

Parameters:

independentTime (bool)

setThresholdMethod(method)[source]#

Take a method name and set it in the method attribute

Parameters:

method – A string defining a thresholding method name, which needs to be in the methods set

setThresholdRange(low, high)[source]#
setThresholdValue(value)[source]#