Skip to content

Image utils

SimplePreprocessor and AspectAwarePreprocessor are directly taked from the great series of Books `Deep Learning for Computer Vision by Adrian. Therefore, all credit for the code in the image_utils module goes to Adrian Rosebrock.

AspectAwarePreprocessor

Class to resize an image to a certain width and height taking into account the image aspect ratio

Parameters:

Name Type Description Default
width int

output width

required
height int

output height

required
inter

opencv interpolation method. See opencv InterpolationFlags.

INTER_AREA
Source code in pytorch_widedeep/utils/image_utils.py
 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
106
107
108
109
110
111
112
class AspectAwarePreprocessor:
    r"""Class to resize an image to a certain width and height taking into account
    the image aspect ratio

    Parameters
    ----------
    width: int
        output width
    height: int
        output height
    inter: interpolation method,  default = ``cv2.INTER_AREA``
        ``opencv`` interpolation method. See ``opencv``
        `InterpolationFlags`.
    """

    def __init__(self, width: int, height: int, inter=cv2.INTER_AREA):
        self.width = width
        self.height = height
        self.inter = inter

    def preprocess(self, image: np.ndarray) -> np.ndarray:
        r"""Returns the resized input image taking into account the image aspect ratio

        Parameters
        ----------
        image: np.ndarray
            Input image to be resized

        Examples
        --------
        >>> import cv2
        >>> from pytorch_widedeep.utils import AspectAwarePreprocessor
        >>> img = cv2.imread("tests/test_data_utils/images/galaxy1.png")
        >>> img.shape
        (694, 890, 3)
        >>> app = AspectAwarePreprocessor(width=224, height=224)
        >>> resized_img = app.preprocess(img)
        >>> resized_img.shape
        (224, 224, 3)

        Returns
        -------
        np.ndarray
            Resized image according to its original image aspect ratio
        """
        (h, w) = image.shape[:2]
        dW = 0
        dH = 0

        if w < h:
            image = resize(image, width=self.width, inter=self.inter)
            dH = int((image.shape[0] - self.height) / 2.0)
        else:
            image = resize(image, height=self.height, inter=self.inter)
            dW = int((image.shape[1] - self.width) / 2.0)

        (h, w) = image.shape[:2]
        image = image[dH : h - dH, dW : w - dW]

        resized_image = cv2.resize(
            image, (self.width, self.height), interpolation=self.inter
        )

        return resized_image

preprocess

preprocess(image)

Returns the resized input image taking into account the image aspect ratio

Parameters:

Name Type Description Default
image ndarray

Input image to be resized

required

Examples:

>>> import cv2
>>> from pytorch_widedeep.utils import AspectAwarePreprocessor
>>> img = cv2.imread("tests/test_data_utils/images/galaxy1.png")
>>> img.shape
(694, 890, 3)
>>> app = AspectAwarePreprocessor(width=224, height=224)
>>> resized_img = app.preprocess(img)
>>> resized_img.shape
(224, 224, 3)

Returns:

Type Description
ndarray

Resized image according to its original image aspect ratio

Source code in pytorch_widedeep/utils/image_utils.py
 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
106
107
108
109
110
111
112
def preprocess(self, image: np.ndarray) -> np.ndarray:
    r"""Returns the resized input image taking into account the image aspect ratio

    Parameters
    ----------
    image: np.ndarray
        Input image to be resized

    Examples
    --------
    >>> import cv2
    >>> from pytorch_widedeep.utils import AspectAwarePreprocessor
    >>> img = cv2.imread("tests/test_data_utils/images/galaxy1.png")
    >>> img.shape
    (694, 890, 3)
    >>> app = AspectAwarePreprocessor(width=224, height=224)
    >>> resized_img = app.preprocess(img)
    >>> resized_img.shape
    (224, 224, 3)

    Returns
    -------
    np.ndarray
        Resized image according to its original image aspect ratio
    """
    (h, w) = image.shape[:2]
    dW = 0
    dH = 0

    if w < h:
        image = resize(image, width=self.width, inter=self.inter)
        dH = int((image.shape[0] - self.height) / 2.0)
    else:
        image = resize(image, height=self.height, inter=self.inter)
        dW = int((image.shape[1] - self.width) / 2.0)

    (h, w) = image.shape[:2]
    image = image[dH : h - dH, dW : w - dW]

    resized_image = cv2.resize(
        image, (self.width, self.height), interpolation=self.inter
    )

    return resized_image

SimplePreprocessor

Class to resize an image to a certain width and height

Parameters:

Name Type Description Default
width int

output width

required
height int

output height

required
inter

opencv interpolation method. See opencv InterpolationFlags.

INTER_AREA
Source code in pytorch_widedeep/utils/image_utils.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class SimplePreprocessor:
    r"""Class to resize an image to a certain width and height

    Parameters
    ----------
    width: int
        output width
    height: int
        output height
    inter: interpolation method, default = ``cv2.INTER_AREA``
        ``opencv`` interpolation method. See ``opencv``
        `InterpolationFlags`.
    """

    def __init__(self, width: int, height: int, inter=cv2.INTER_AREA):
        self.width = width
        self.height = height
        self.inter = inter

    def preprocess(self, image: np.ndarray) -> np.ndarray:
        r"""Returns the resized input image

        Parameters
        ----------
        image: np.ndarray
            Input image to be resized

        Returns
        -------
        np.ndarray
            Resized image

        """
        resized_image = cv2.resize(
            image, (self.width, self.height), interpolation=self.inter
        )

        return resized_image

preprocess

preprocess(image)

Returns the resized input image

Parameters:

Name Type Description Default
image ndarray

Input image to be resized

required

Returns:

Type Description
ndarray

Resized image

Source code in pytorch_widedeep/utils/image_utils.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def preprocess(self, image: np.ndarray) -> np.ndarray:
    r"""Returns the resized input image

    Parameters
    ----------
    image: np.ndarray
        Input image to be resized

    Returns
    -------
    np.ndarray
        Resized image

    """
    resized_image = cv2.resize(
        image, (self.width, self.height), interpolation=self.inter
    )

    return resized_image