Source code for ddd_pylib.labels_manip._remap_labels_operator
from ._replace_values_operator import ReplaceValuesOperator
import numpy as np
[docs]
class RemapLabelsOperator(ReplaceValuesOperator):
"""
Takes a label map with non-contiguous label indices and remaps them
so the new indices are contiguous.
"""
def __init__(self):
super().__init__()
[docs]
def getPrefix(self) -> str:
return "Remapped-"
def _makeContiguousMapping(self):
img = self.getMainImage()
labels_list = sorted(np.unique(img.values))
dt = img.dtype.type
label_mapping = {
dt(old_label): dt(new_label )
for new_label, old_label in enumerate(labels_list)
}
if 0 in label_mapping:
label_mapping.pop(0)
self.setValuesDict(label_mapping)
def _run(self):
self._makeContiguousMapping()
yield from super()._run()