ThresholdOperator#
- class ddd_pylib.labeling.ThresholdOperator[source]#
Bases:
ImageOperatorCreates 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.DataArraywith the same dimensions and attributes as the main image. Values are boolean, with True for foreground and False for background.
- The thresholded image as a
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
- 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)