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

AspectAwarePreprocessor(width, height, inter=cv2.INTER_AREA)

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

    opencv interpolation method. See opencv InterpolationFlags.

Source code in pytorch_widedeep/utils/image_utils.py
32
33
34
35
def __init__(self, width: int, height: int, inter=cv2.INTER_AREA):
    self.width = width
    self.height = height
    self.inter = inter

preprocess

preprocess(image)

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

Parameters:

  • image (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:

  • ndarray

    Resized image according to its original image aspect ratio

Source code in pytorch_widedeep/utils/image_utils.py
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
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 = imutils.resize(image, width=self.width, inter=self.inter)
        dH = int((image.shape[0] - self.height) / 2.0)
    else:
        image = imutils.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

SimplePreprocessor(width, height, inter=cv2.INTER_AREA)

Class to resize an image to a certain width and height

Parameters:

  • width (int) –

    output width

  • height (int) –

    output height

  • inter

    opencv interpolation method. See opencv InterpolationFlags.

Source code in pytorch_widedeep/utils/image_utils.py
 97
 98
 99
100
def __init__(self, width: int, height: int, inter=cv2.INTER_AREA):
    self.width = width
    self.height = height
    self.inter = inter

preprocess

preprocess(image)

Returns the resized input image

Parameters:

  • image (ndarray) –

    Input image to be resized

Returns:

  • ndarray

    Resized image

Source code in pytorch_widedeep/utils/image_utils.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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