File size: 7,108 Bytes
a80d6bb
 
 
 
 
 
 
 
 
 
c74a070
 
a80d6bb
 
c74a070
a80d6bb
 
c74a070
a80d6bb
 
 
c74a070
a80d6bb
c74a070
 
a80d6bb
 
 
 
c74a070
a80d6bb
 
c74a070
a80d6bb
c74a070
a80d6bb
 
 
 
 
 
 
c74a070
 
a80d6bb
c74a070
a80d6bb
 
 
 
c74a070
a80d6bb
c74a070
 
a80d6bb
 
 
 
 
 
c74a070
a80d6bb
c74a070
 
a80d6bb
 
c74a070
 
 
 
a80d6bb
c74a070
a80d6bb
 
 
 
 
 
c74a070
a80d6bb
 
 
c74a070
 
a80d6bb
 
c74a070
a80d6bb
 
 
c74a070
a80d6bb
 
c74a070
a80d6bb
 
 
c74a070
 
a80d6bb
c74a070
a80d6bb
 
 
c74a070
a80d6bb
 
c74a070
a80d6bb
 
 
c74a070
a80d6bb
 
c74a070
 
a80d6bb
 
c74a070
 
 
a80d6bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c74a070
a80d6bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c74a070
a80d6bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c74a070
a80d6bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c74a070
 
a80d6bb
 
c74a070
a80d6bb
 
c74a070
a80d6bb
 
c74a070
a80d6bb
 
 
c74a070
a80d6bb
c74a070
a80d6bb
c74a070
a80d6bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright 2019-present NAVER Corp.
# CC BY-NC-SA 3.0
# Available only for non-commercial use

import pdb
import numpy as np
from PIL import Image, ImageOps, ImageEnhance


class DummyImg:
    """This class is a dummy image only defined by its size."""

    def __init__(self, size):
        self.size = size

    def resize(self, size, *args, **kwargs):
        return DummyImg(size)

    def expand(self, border):
        w, h = self.size
        if isinstance(border, int):
            size = (w + 2 * border, h + 2 * border)
        else:
            l, t, r, b = border
            size = (w + l + r, h + t + b)
        return DummyImg(size)

    def crop(self, border):
        w, h = self.size
        l, t, r, b = border
        assert 0 <= l <= r <= w
        assert 0 <= t <= b <= h
        size = (r - l, b - t)
        return DummyImg(size)

    def rotate(self, angle):
        raise NotImplementedError

    def transform(self, size, *args, **kwargs):
        return DummyImg(size)


def grab_img(img_and_label):
    """Called to extract the image from an img_and_label input
    (a dictionary). Also compatible with old-style PIL images.
    """
    if isinstance(img_and_label, dict):
        # if input is a dictionary, then
        # it must contains the img or its size.
        try:
            return img_and_label["img"]
        except KeyError:
            return DummyImg(img_and_label["imsize"])

    else:
        # or it must be the img directly
        return img_and_label


def update_img_and_labels(img_and_label, img, persp=None):
    """Called to update the img_and_label"""
    if isinstance(img_and_label, dict):
        img_and_label["img"] = img
        img_and_label["imsize"] = img.size

        if persp:
            if "persp" not in img_and_label:
                img_and_label["persp"] = (1, 0, 0, 0, 1, 0, 0, 0)
            img_and_label["persp"] = persp_mul(persp, img_and_label["persp"])

        return img_and_label

    else:
        # or it must be the img directly
        return img


def rand_log_uniform(a, b):
    return np.exp(np.random.uniform(np.log(a), np.log(b)))


def translate(tx, ty):
    return (1, 0, tx, 0, 1, ty, 0, 0)


def rotate(angle):
    return (np.cos(angle), -np.sin(angle), 0, np.sin(angle), np.cos(angle), 0, 0, 0)


def persp_mul(mat, mat2):
    """homography (perspective) multiplication.
    mat: 8-tuple (homography transform)
    mat2: 8-tuple (homography transform) or 2-tuple (point)
    """
    assert isinstance(mat, tuple)
    assert isinstance(mat2, tuple)

    mat = np.float32(mat + (1,)).reshape(3, 3)
    mat2 = np.array(mat2 + (1,)).reshape(3, 3)
    res = np.dot(mat, mat2)
    return tuple((res / res[2, 2]).ravel()[:8])


def persp_apply(mat, pts):
    """homography (perspective) transformation.
    mat: 8-tuple (homography transform)
    pts: numpy array
    """
    assert isinstance(mat, tuple)
    assert isinstance(pts, np.ndarray)
    assert pts.shape[-1] == 2
    mat = np.float32(mat + (1,)).reshape(3, 3)

    if pts.ndim == 1:
        pt = np.dot(pts, mat[:, :2].T).ravel() + mat[:, 2]
        pt /= pt[2]  # homogeneous coordinates
        return tuple(pt[:2])
    else:
        pt = np.dot(pts, mat[:, :2].T) + mat[:, 2]
        pt[:, :2] /= pt[:, 2:3]  # homogeneous coordinates
        return pt[:, :2]


def is_pil_image(img):
    return isinstance(img, Image.Image)


def adjust_brightness(img, brightness_factor):
    """Adjust brightness of an Image.
    Args:
    img (PIL Image): PIL Image to be adjusted.
    brightness_factor (float):  How much to adjust the brightness. Can be
    any non negative number. 0 gives a black image, 1 gives the
    original image while 2 increases the brightness by a factor of 2.
    Returns:
    PIL Image: Brightness adjusted image.
    Copied from https://github.com/pytorch in torchvision/transforms/functional.py
    """
    if not is_pil_image(img):
        raise TypeError("img should be PIL Image. Got {}".format(type(img)))

    enhancer = ImageEnhance.Brightness(img)
    img = enhancer.enhance(brightness_factor)
    return img


def adjust_contrast(img, contrast_factor):
    """Adjust contrast of an Image.
    Args:
    img (PIL Image): PIL Image to be adjusted.
    contrast_factor (float): How much to adjust the contrast. Can be any
    non negative number. 0 gives a solid gray image, 1 gives the
    original image while 2 increases the contrast by a factor of 2.
    Returns:
    PIL Image: Contrast adjusted image.
    Copied from https://github.com/pytorch in torchvision/transforms/functional.py
    """
    if not is_pil_image(img):
        raise TypeError("img should be PIL Image. Got {}".format(type(img)))

    enhancer = ImageEnhance.Contrast(img)
    img = enhancer.enhance(contrast_factor)
    return img


def adjust_saturation(img, saturation_factor):
    """Adjust color saturation of an image.
    Args:
    img (PIL Image): PIL Image to be adjusted.
    saturation_factor (float):  How much to adjust the saturation. 0 will
    give a black and white image, 1 will give the original image while
    2 will enhance the saturation by a factor of 2.
    Returns:
    PIL Image: Saturation adjusted image.
    Copied from https://github.com/pytorch in torchvision/transforms/functional.py
    """
    if not is_pil_image(img):
        raise TypeError("img should be PIL Image. Got {}".format(type(img)))

    enhancer = ImageEnhance.Color(img)
    img = enhancer.enhance(saturation_factor)
    return img


def adjust_hue(img, hue_factor):
    """Adjust hue of an image.
    The image hue is adjusted by converting the image to HSV and
    cyclically shifting the intensities in the hue channel (H).
    The image is then converted back to original image mode.
    `hue_factor` is the amount of shift in H channel and must be in the
    interval `[-0.5, 0.5]`.
    See https://en.wikipedia.org/wiki/Hue for more details on Hue.
    Args:
    img (PIL Image): PIL Image to be adjusted.
    hue_factor (float):  How much to shift the hue channel. Should be in
    [-0.5, 0.5]. 0.5 and -0.5 give complete reversal of hue channel in
    HSV space in positive and negative direction respectively.
    0 means no shift. Therefore, both -0.5 and 0.5 will give an image
    with complementary colors while 0 gives the original image.
    Returns:
    PIL Image: Hue adjusted image.
    Copied from https://github.com/pytorch in torchvision/transforms/functional.py
    """
    if not (-0.5 <= hue_factor <= 0.5):
        raise ValueError("hue_factor is not in [-0.5, 0.5].".format(hue_factor))

    if not is_pil_image(img):
        raise TypeError("img should be PIL Image. Got {}".format(type(img)))

    input_mode = img.mode
    if input_mode in {"L", "1", "I", "F"}:
        return img

    h, s, v = img.convert("HSV").split()

    np_h = np.array(h, dtype=np.uint8)
    # uint8 addition take cares of rotation across boundaries
    with np.errstate(over="ignore"):
        np_h += np.uint8(hue_factor * 255)
        h = Image.fromarray(np_h, "L")

    img = Image.merge("HSV", (h, s, v)).convert(input_mode)
    return img