Skip to content

Chromer

Chromer ¤

Bases: Subfunction_Base

A Subfunction class which which can be used for color format transforming.

Transforms:
- grayscale ->  RGB
- RGB       ->  grayscale

Possible target formats: ["rgb", "grayscale"]

Typical use case is converting a grayscale to RGB in order to utilize transfer learning weights based on ImageNet.

Source code in aucmedi/data_processing/subfunctions/chromer.py
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
class Chromer(Subfunction_Base):
    """ A Subfunction class which which can be used for color format transforming.

    ```
    Transforms:
    - grayscale ->  RGB
    - RGB       ->  grayscale

    ```

    Possible target formats: `["rgb", "grayscale"]`

    Typical use case is converting a grayscale to RGB in order to utilize
    transfer learning weights based on ImageNet.
    """
    #---------------------------------------------#
    #                Initialization               #
    #---------------------------------------------#
    def __init__(self, target="rgb"):
        """ Initialization function for creating a Chromer Subfunction which can be passed to a
            [DataGenerator][aucmedi.data_processing.data_generator.DataGenerator].

        Args:
            target (str):               Transformation mode for desired target format.
        """
        # Verify target format
        if target not in ["grayscale", "rgb"]:
            raise ValueError("Unknown target format for Chromer Subfunction",
                             target, "Possibles target formats are: ['grayscale', 'rgb']")
        # Cache target format
        self.target = target

    #---------------------------------------------#
    #                Transformation               #
    #---------------------------------------------#
    def transform(self, image):
        # Verify that image is in correct format
        if self.target == "rgb" and (image.shape[-1] != 1 or \
                                     np.max(image) > 255 or \
                                     np.min(image) < 0):
            raise ValueError("Subfunction Chromer: Image is not in grayscale format!",
                             "Ensure that it is grayscale normalized and has",
                             "a single channel.")
        elif self.target == "grayscale" and (image.shape[-1] != 3 or \
                                             np.max(image) > 255 or \
                                             np.min(image) < 0):
            raise ValueError("Subfunction Chromer: Image is not in RGB format!",
                             "Ensure that it is normalized [0,255] and has 3 channels.")
        # Run grayscale -> RGB
        if self.target == "rgb":
            image_chromed = np.concatenate((image,)*3, axis=-1)
        # Run RGB -> grayscale
        else:
            # Get color intensity values
            r = np.take(image, indices=0, axis=-1)
            g = np.take(image, indices=1, axis=-1)
            b = np.take(image, indices=2, axis=-1)
            # Compute grayscale image
            image_chromed = 0.299 * r + 0.587 * g + 0.114 * b
            # Add channel axis back
            image_chromed = np.expand_dims(image_chromed, axis=-1)
        # Return chromed image
        return image_chromed

__init__(target='rgb') ¤

Initialization function for creating a Chromer Subfunction which can be passed to a DataGenerator.

Parameters:

Name Type Description Default
target str

Transformation mode for desired target format.

'rgb'
Source code in aucmedi/data_processing/subfunctions/chromer.py
61
62
63
64
65
66
67
68
69
70
71
72
73
def __init__(self, target="rgb"):
    """ Initialization function for creating a Chromer Subfunction which can be passed to a
        [DataGenerator][aucmedi.data_processing.data_generator.DataGenerator].

    Args:
        target (str):               Transformation mode for desired target format.
    """
    # Verify target format
    if target not in ["grayscale", "rgb"]:
        raise ValueError("Unknown target format for Chromer Subfunction",
                         target, "Possibles target formats are: ['grayscale', 'rgb']")
    # Cache target format
    self.target = target