# Segmentation using Fuzzy Logic-based methods

## Base object

All methods based on Fuzzy Logic are implemented in objects, which inherit from the base object `FuzzySetSegmentator`. Therefore, all are initialised as follows:

```python
def __init__(self, image: numpy.ndarray, class_representation: Dict):
    """
    Initializes the base object for the segmentation using the membership functions of fuzzy sets.

    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.
        class_representation: A dictionary with the representation colour of each class. Each entry in the dictionary
                              must be an integer as the key, and a RGB tuple as value.
    """
```

To perform the segmentation, this base object defines the segment function.

```python
def segment(self, **kwargs) -> SegmentationResult:
    """
    A generic method to compute the colour segmentation of an RGB image.
    """
```

{% hint style="success" %}
Although this page details the objects that implement the segmentation of the different methods, in practice it is not necessary to initialise the object itself. The `Segmentator`object, defined in [Segmentator object](https://mmunar97.gitbook.io/colour-segmentation/usage/segmentator_object), already initialises them automatically in the `segment`function.
{% endhint %}

## `AmanteTrapezoidalSegmentator` object

When initialised, the labels by default are represented with the following dictionary:

```python
class_representation = {0: numpy.array([255, 33, 36]),
                        1: numpy.array([170, 121, 66]),
                        2: numpy.array([255, 146, 0]),
                        3: numpy.array([255, 251, 0]),
                        4: numpy.array([0, 255, 0]),
                        5: numpy.array([0, 253, 255]),
                        6: numpy.array([0, 0, 255]),
                        7: numpy.array([147, 33, 146]),
                        8: numpy.array([255, 64, 255])}
```

Below are the details of the segment function.

```python
def segment(self, remove_achromatic_colours: bool = True) -> SegmentationResult:
    """
    Segments the image using the Amante-Fonseca's membership functions of different fuzzy sets. The method considers
    nine chromatic colour classes, and three achromatic colour classes. Depending on certain conditions, the colours
    present in the image are achromatic colours (black, grey and white).

    Args:
        remove_achromatic_colours: A boolean, indicating if the achromatic colours have to be removed in the image.

    References:
        Amante JC & Fonseca MJ (2012)
        Fuzzy Color Space Segmentation to Identify the Same Dominant Colors as Users.
        18th International Conference on Distributed Multimedia Systems.

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

## `ChamorroTrapezoidalSegmentator` object

When initialised, the labels by default are represented with the following dictionary:

```python
class_representation = {0: numpy.array([255, 33, 36]),
                        1: numpy.array([255, 148, 9]),
                        2: numpy.array([255, 255, 13]),
                        3: numpy.array([186, 255, 15]),
                        4: numpy.array([6, 155, 9]),
                        5: numpy.array([12, 255, 116]),
                        6: numpy.array([11, 254, 255]),
                        7: numpy.array([8, 192, 255]),
                        8: numpy.array([0, 0, 255]),
                        9: numpy.array([92, 8, 253]),
                        10: numpy.array([238, 3, 249]),
                        11: numpy.array([254, 6, 180])}
```

Below are the details of the segment function.

```python
def segment(self, remove_achromatic_colours: bool = True) -> SegmentationResult:
    """
    Segments the image using the Chamorro et al membership functions of different fuzzy sets.

    Args:
        remove_achromatic_colours: A boolean, indicating if the achromatic colours have to be removed in the image.

    References:
        Chamorro-Martínez J, Medina JM, Barranco C, Galán-Perales E, Soto-Hidalgo J. (2007)
        Retrieving images in fuzzy object-relational databases using dominant color descriptors
        Fuzzy Sets Systems. 2007 Feb 1; 158:312–24.

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

## `LiuWangTrapezoidalSegmentator` object

When initialised, the labels by default are represented with the following dictionary:

```python
class_representation = {0: numpy.array([255, 33, 36]),
                        1: numpy.array([248, 149, 29]),
                        2: numpy.array([239, 233, 17]),
                        3: numpy.array([105, 189, 69]),
                        4: numpy.array([111, 204, 221]),
                        5: numpy.array([59, 83, 164]),
                        6: numpy.array([158, 80, 159])}
```

Below are the details of the segment function.

```python
def segment(self, apply_colour_correction: bool = True,
            remove_achromatic_colours: bool = True) -> SegmentationResult:
    """
    Segments the image using the liu-Wang membership functions of different fuzzy sets.

    Args:
        apply_colour_correction: A boolean, indicating if the Gray World balance has to be applied to the original
                                 image.
        remove_achromatic_colours: A boolean, indicating if the achromatic colours have to be removed in the image.

    References:
        Liu C, Wang L. (2016)
        Fuzzy color recognition and segmentation of robot vision scene.
        Proceedings - 8th International Congress of Image Signal Processing CISP 2015. 2016;(Cisp):448–52.

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

## `ShamirTriangularSegmentator` object

When initialised, the labels by default are represented with the following dictionary:

```python
class_representation = {0: numpy.array([255, 33, 36]),
                        1: numpy.array([255, 140, 0]),
                        2: numpy.array([255, 165, 0]),
                        3: numpy.array([255, 255, 0]),
                        4: numpy.array([144, 238, 144]),
                        5: numpy.array([0, 100, 0]),
                        6: numpy.array([0, 255, 255]),
                        7: numpy.array([0, 0, 255]),
                        8: numpy.array([128, 0, 128]),
                        9: numpy.array([255, 0, 255])}
```

Below are the details of the segment function.

```python
def segment(self, remove_achromatic_colours: bool = True) -> SegmentationResult:
    """
    Segments the image using the Shamir membership functions of different fuzzy sets.

    Args:
        remove_achromatic_colours: A boolean, indicating if the achromatic colours have to be removed in the image.

    References:
        Shamir, L. (2006)
        Human Perception-based Color Segmentation Using Fuzzy Logic.
        Proceedings of International Conference on Image Processing, Computer Vision, & Pattern Recognition.

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