def main(mask, connectivity=8, plot=False): '''Labels objects in a binary image, i.e. assigns to all pixels of a connected component an identifier number that is unique for each object in the image. Parameters ---------- mask: numpy.ndarray[Union[numpy.bool, numpy.int32]] binary image that should labeled connectivity: int, optional whether a diagonal (``4``) or square (``8``) neighborhood should be considered (default: ``8``, options: ``{4, 8}``) plot: bool, optional whether a plot should be generated (default: ``False``) Returns ------- jtmodules.label.Output[Union[numpy.ndarray, str]] Note ---- If `mask` is not binary, it will be binarized, i.e. pixels will be set to ``True`` if values are greater than zero and ``False`` otherwise. ''' mask = mask > 0 label_image = label(mask, connectivity) n = len(np.unique(label_image)[1:]) logger.info('identified %d objects', n) if plot: from jtlib import plotting plots = [ plotting.create_mask_image_plot(mask, 'ul'), plotting.create_mask_image_plot(label_image, 'ur') ] figure = plotting.create_figure(plots, title='Labeled image') else: figure = str() return Output(label_image, figure)
def main(mask): '''Registers objects (connected pixel components) in an image for use by other (measurement) modules downstream in the pipeline. In case a binary mask is provided the image is automatically labeled. Parameters ---------- mask: numpy.ndarray[Union[numpy.bool, numpy.uint16]] binary or labeled mask Returns ------- jtmodules.register_objects.Output See also -------- :class:`tmlib.workflow.jterator.handles.SegmentedObjects` ''' if mask.dtype == 'bool': label_image = label(mask) else: label_image = mask label_image = label_image.astype(np.int32) return Output(label_image)
def main(mask): '''Registers objects (connected pixel components) in an image for use by other (measurement) modules downstream in the pipeline. In case a binary mask is provided the image is automatically labeled. Parameters ---------- mask: numpy.ndarray[Union[numpy.bool, numpy.int32]] binary or labeled mask Returns ------- jtmodules.register_objects.Output See also -------- :class:`tmlib.workflow.jterator.handles.SegmentedObjects` ''' if mask.dtype == 'bool': label_image = label(mask) else: label_image = mask return Output(label_image)