Skip to content

Crop

Crop ¤

Bases: Subfunction_Base

A Crop Subfunction class which center/randomly crops a desired shape from an image.

List of valid modes for parameter "mode": ["center", "random"]

2D image

Shape have to be defined as tuple with x and y size: Crop(shape=(224, 224))

Cropping is done via albumentations CenterCrop and RandomCrop transform.
https://albumentations.ai/docs/api_reference/augmentations/crops/transforms/#albumentations.augmentations.crops.transforms.CenterCrop
https://albumentations.ai/docs/api_reference/augmentations/crops/transforms/#albumentations.augmentations.crops.transforms.RandomCrop

3D volume

Shape has to be defined as tuple with x, y and z size: Crop(shape=(224, 224, 244))

Cropping is done via volumentations CenterCrop and RandomCrop transform.
https://github.com/muellerdo/volumentations

Source code in aucmedi/data_processing/subfunctions/crop.py
31
32
33
34
35
36
37
38
39
40
41
42
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
class Crop(Subfunction_Base):
    """ A Crop Subfunction class which center/randomly crops a desired shape from an image.

    List of valid modes for parameter "mode": `["center", "random"]`

    ???+ info "2D image"
        Shape have to be defined as tuple with x and y size: `Crop(shape=(224, 224))`

        Cropping is done via albumentations CenterCrop and RandomCrop transform. <br>
        https://albumentations.ai/docs/api_reference/augmentations/crops/transforms/#albumentations.augmentations.crops.transforms.CenterCrop  <br>
        https://albumentations.ai/docs/api_reference/augmentations/crops/transforms/#albumentations.augmentations.crops.transforms.RandomCrop  <br>

    ???+ info "3D volume"
        Shape has to be defined as tuple with x, y and z size: `Crop(shape=(224, 224, 244))`

        Cropping is done via volumentations CenterCrop and RandomCrop transform. <br>
        https://github.com/muellerdo/volumentations
    """
    #---------------------------------------------#
    #                Initialization               #
    #---------------------------------------------#
    def __init__(self, shape=(224, 224), mode="center"):
        """ Initialization function for creating a Crop Subfunction which can be passed to a
            [DataGenerator][aucmedi.data_processing.data_generator.DataGenerator].

        Args:
            shape (tuple of int):       Desired output shape after cropping.
            mode (str):                 Selected mode for cropping.
        """
        # Initialize parameter
        params = {"p":1.0, "always_apply":True}
        # Select augmentation module and add further parameter depending on dimension
        if len(shape) == 2:
            params["height"] = shape[0]
            params["width"] = shape[1]
            mod = albumentations
        elif len(shape) == 3:
            params["shape"] = shape
            mod = volumentations
        else : raise ValueError("Shape for cropping has to be 2D or 3D!", shape)
        # Initialize cropping transform
        if mode == "center":
            self.aug_transform = mod.Compose([mod.CenterCrop(**params)])
        elif mode == "random":
            self.aug_transform = mod.Compose([mod.RandomCrop(**params)])
        else : raise ValueError("Unknown mode for crop Subfunction", mode,
                                "Possibles modes are: ['center', 'random']")
        # Cache shape
        self.shape = shape

    #---------------------------------------------#
    #                Transformation               #
    #---------------------------------------------#
    def transform(self, image):
        # Perform cropping on image
        image_cropped = self.aug_transform(image=image)["image"]
        # Return cropped image
        return image_cropped

__init__(shape=(224, 224), mode='center') ¤

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

Parameters:

Name Type Description Default
shape tuple of int

Desired output shape after cropping.

(224, 224)
mode str

Selected mode for cropping.

'center'
Source code in aucmedi/data_processing/subfunctions/crop.py
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
def __init__(self, shape=(224, 224), mode="center"):
    """ Initialization function for creating a Crop Subfunction which can be passed to a
        [DataGenerator][aucmedi.data_processing.data_generator.DataGenerator].

    Args:
        shape (tuple of int):       Desired output shape after cropping.
        mode (str):                 Selected mode for cropping.
    """
    # Initialize parameter
    params = {"p":1.0, "always_apply":True}
    # Select augmentation module and add further parameter depending on dimension
    if len(shape) == 2:
        params["height"] = shape[0]
        params["width"] = shape[1]
        mod = albumentations
    elif len(shape) == 3:
        params["shape"] = shape
        mod = volumentations
    else : raise ValueError("Shape for cropping has to be 2D or 3D!", shape)
    # Initialize cropping transform
    if mode == "center":
        self.aug_transform = mod.Compose([mod.CenterCrop(**params)])
    elif mode == "random":
        self.aug_transform = mod.Compose([mod.RandomCrop(**params)])
    else : raise ValueError("Unknown mode for crop Subfunction", mode,
                            "Possibles modes are: ['center', 'random']")
    # Cache shape
    self.shape = shape