Source code for ddd_pylib.labeling._connected_components_labeling_operator
from ddd_pylib._base import ImageOperator
from skimage.measure import label
import xarray as xr
[docs]
class ConnectedComponentsLabelingOperator(ImageOperator):
"""
Takes an image and creates assigns a unique ID to all the voxels part of the same isolated connected component.
The `connectivity` is to determine whether two pixels can be linked in diagonal or not.
The possible values are:
- 1 (only direct neighbors)
- 2 (direct and diagonal neighbors)
"""
def __init__(self):
super().__init__()
self.connectivity = self.defaultConnectivity()
[docs]
@staticmethod
def defaultConnectivity() -> int:
return 1
[docs]
def getPrefix(self):
return "CCL-"
[docs]
def setConnectivity(self, connectivity):
"""
Used to switch between direct connectivity and direct+diagonal connectivity.
The possible values are:
- 1 (only direct neighbors)
- 2 (direct and diagonal neighbors)
Args:
connectivity: The value of the wanted connectivity
"""
if connectivity is None:
raise ValueError("Connectivity can't be None")
self.connectivity = connectivity
def _labeling(self, image: xr.DataArray) -> xr.DataArray:
labeled = label(image.values, connectivity=self.connectivity)
return xr.DataArray(
labeled,
dims=image.dims,
attrs=image.attrs
)
def _applyToFrame(self, frameData, t, nT):
image = frameData[0]
return self._labeling(image)