Segmentator object

The Segmentator class defines the basic object for chromatically segmenting an image. This page shows how it works.

Initialization

In order to segment an image, it is only necessary to initialise the object by passing that image as a parameter. Then, the segment method performs the segmentation.

def __init__(self, image: numpy.ndarray):
    """
    Initializes the object that segments a given image into its main colours.

    Args:
        image: A three-dimensional numpy array, representing the image to be segmented, which entries are in 0...255
               range and the channels are BGR.
    """

Note that the function receives the image in tensor format, with discrete inputs, and the channels sorted in BGR.

Segmentation procedure

The segment function receives, as a mandatory argument, the method to be used. The method is determined using the list of values defined by SegmentationAlgorithm, whose values are summarised in the following table.

The parameters of each method must be passed with its corresponding key.

def segment(self, method: SegmentationAlgorithm, **kwargs) -> SegmentationResult:
    """
    Segments the image with the selected method.

    Args:
        method: A SegmentationAlgorithm value, representing the method to be used.

    Returns:
        A SegmentationResult object, containing the classification of each pixel and the elapsed time.
    """

The method returns a SegmentationResult object. For more information about the result, see section Segmentation result.

Segmentation result

The SegmentationResultobject contains the result of the segmentation. Specifically, the values it contains are:

  • segmented_image: It represents the final segmented image, with the different artificial colours associated with each class. It is a RGB image, with the same dimension as the original image. Its type is numpy.ndarray.

  • segmented_classes: Represents the class to which each pixel belongs. It is a single channel image, containing in each entry an integer value representing the colour index of that pixel. Its type is numpy.ndarray.

  • elapsed_time: It is a float value, representing the execution time.

Finally, the SegmentationResult object implements the get_colour_proportion function, which allows to calculate the proportion of pixels whose associated class is the one specified by parameter.

def get_colour_proportion(self, colour_label=None):
    """
    Computes the proportion of pixels in the segmentation with a certain class..

    Args:
        colour_label: An integer, representing the label associated to the red colour. If None, 
                      the label 0 is set by default.

    Returns:
        A float, representing the proportion of pixels whose associated class is colour_label.
    """

Last updated