-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgproc.py
288 lines (244 loc) · 10.8 KB
/
imgproc.py
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Copyright 2021 Dakewe Biotech Corporation. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Realize the function of processing the dataset before training."""
import random
from typing import Any
import numpy as np
import torch
from PIL import Image
from torchvision.transforms import functional as F
__all__ = [
"normalize", "unnormalize",
"image2tensor", "tensor2image",
"convert_rgb_to_y", "convert_rgb_to_ycbcr", "convert_ycbcr_to_rgb",
"center_crop", "random_crop",
"random_rotate", "random_horizontally_flip", "random_vertically_flip",
"random_adjust_brightness", "random_adjust_contrast"
]
def normalize(image: np.ndarray) -> np.ndarray:
"""Normalize the ``OpenCV.imread`` or ``skimage.io.imread`` data.
Args:
image (np.ndarray): The image data read by ``OpenCV.imread`` or ``skimage.io.imread``.
Returns:
Normalized image data. Data range [0, 1].
"""
return image.astype(np.float64) / 255.0
def unnormalize(image: np.ndarray) -> np.ndarray:
"""Un-normalize the ``OpenCV.imread`` or ``skimage.io.imread`` data.
Args:
image (np.ndarray): The image data read by ``OpenCV.imread`` or ``skimage.io.imread``.
Returns:
Denormalized image data. Data range [0, 255].
"""
return image.astype(np.float64) * 255.0
def image2tensor(image: np.ndarray, range_norm: bool, half: bool) -> torch.Tensor:
"""Convert ``PIL.Image`` to Tensor.
Args:
image (np.ndarray): The image data read by ``PIL.Image``
range_norm (bool): Scale [0, 1] data to between [-1, 1]
half (bool): Whether to convert torch.float32 similarly to torch.half type.
Returns:
Normalized image data
Examples:
>>> image = Image.open("image.bmp")
>>> tensor_image = image2tensor(image, range_norm=False, half=False)
"""
tensor = F.to_tensor(image)
if range_norm:
tensor = tensor.mul_(2.0).sub_(1.0)
if half:
tensor = tensor.half()
return tensor
def tensor2image(tensor: torch.Tensor, range_norm: bool, half: bool) -> Any:
"""Converts ``torch.Tensor`` to ``PIL.Image``.
Args:
tensor (torch.Tensor): The image that needs to be converted to ``PIL.Image``
range_norm (bool): Scale [-1, 1] data to between [0, 1]
half (bool): Whether to convert torch.float32 similarly to torch.half type.
Returns:
Convert image data to support PIL library
Examples:
>>> tensor = torch.randn([1, 3, 128, 128])
>>> image = tensor2image(tensor, range_norm=False, half=False)
"""
if range_norm:
tensor = tensor.add_(1.0).div_(2.0)
if half:
tensor = tensor.half()
image = tensor.squeeze_(0).permute(1, 2, 0).mul_(255).clamp_(0, 255).cpu().numpy().astype("uint8")
return image
def convert_rgb_to_y(image: Any) -> Any:
"""Convert RGB image or tensor image data to YCbCr(Y) format.
Args:
image: RGB image data read by ``PIL.Image''.
Returns:
Y image array data.
"""
if type(image) == np.ndarray:
return 16. + (64.738 * image[:, :, 0] + 129.057 * image[:, :, 1] + 25.064 * image[:, :, 2]) / 256.
elif type(image) == torch.Tensor:
if len(image.shape) == 4:
image = image.squeeze_(0)
return 16. + (64.738 * image[0, :, :] + 129.057 * image[1, :, :] + 25.064 * image[2, :, :]) / 256.
else:
raise Exception("Unknown Type", type(image))
def convert_rgb_to_ycbcr(image: Any) -> Any:
"""Convert RGB image or tensor image data to YCbCr format.
Args:
image: RGB image data read by ``PIL.Image''.
Returns:
YCbCr image array data.
"""
if type(image) == np.ndarray:
y = 16. + (64.738 * image[:, :, 0] + 129.057 * image[:, :, 1] + 25.064 * image[:, :, 2]) / 256.
cb = 128. + (-37.945 * image[:, :, 0] - 74.494 * image[:, :, 1] + 112.439 * image[:, :, 2]) / 256.
cr = 128. + (112.439 * image[:, :, 0] - 94.154 * image[:, :, 1] - 18.285 * image[:, :, 2]) / 256.
return np.array([y, cb, cr]).transpose([1, 2, 0])
elif type(image) == torch.Tensor:
if len(image.shape) == 4:
image = image.squeeze(0)
y = 16. + (64.738 * image[0, :, :] + 129.057 * image[1, :, :] + 25.064 * image[2, :, :]) / 256.
cb = 128. + (-37.945 * image[0, :, :] - 74.494 * image[1, :, :] + 112.439 * image[2, :, :]) / 256.
cr = 128. + (112.439 * image[0, :, :] - 94.154 * image[1, :, :] - 18.285 * image[2, :, :]) / 256.
return torch.cat([y, cb, cr], 0).permute(1, 2, 0)
else:
raise Exception("Unknown Type", type(image))
def convert_ycbcr_to_rgb(image: Any) -> Any:
"""Convert YCbCr format image to RGB format.
Args:
image: YCbCr image data read by ``PIL.Image''.
Returns:
RGB image array data.
"""
if type(image) == np.ndarray:
r = 298.082 * image[:, :, 0] / 256. + 408.583 * image[:, :, 2] / 256. - 222.921
g = 298.082 * image[:, :, 0] / 256. - 100.291 * image[:, :, 1] / 256. - 208.120 * image[:, :, 2] / 256. + 135.576
b = 298.082 * image[:, :, 0] / 256. + 516.412 * image[:, :, 1] / 256. - 276.836
return np.array([r, g, b]).transpose([1, 2, 0])
elif type(image) == torch.Tensor:
if len(image.shape) == 4:
image = image.squeeze(0)
r = 298.082 * image[0, :, :] / 256. + 408.583 * image[2, :, :] / 256. - 222.921
g = 298.082 * image[0, :, :] / 256. - 100.291 * image[1, :, :] / 256. - 208.120 * image[2, :, :] / 256. + 135.576
b = 298.082 * image[0, :, :] / 256. + 516.412 * image[1, :, :] / 256. - 276.836
return torch.cat([r, g, b], 0).permute(1, 2, 0)
else:
raise Exception("Unknown Type", type(image))
def center_crop(lr: Any, hr: Any, image_size: int, upscale_factor: int) -> [Any, Any]:
"""Cut ``PIL.Image`` in the center area of the image.
Args:
lr: Low-resolution image data read by ``PIL.Image``.
hr: High-resolution image data read by ``PIL.Image``.
image_size (int): The size of the captured image area. It should be the size of the high-resolution image.
upscale_factor (int): magnification factor.
Returns:
Randomly cropped low-resolution images and high-resolution images.
"""
w, h = hr.size
left = (w - image_size) // 2
top = (h - image_size) // 2
right = left + image_size
bottom = top + image_size
lr = lr.crop((left // upscale_factor,
top // upscale_factor,
right // upscale_factor,
bottom // upscale_factor))
hr = hr.crop((left, top, right, bottom))
return lr, hr
def random_crop(lr: Any, hr: Any, image_size: int, upscale_factor: int) -> [Any, Any]:
"""Will ``PIL.Image`` randomly capture the specified area of the image.
Args:
lr: Low-resolution image data read by ``PIL.Image``.
hr: High-resolution image data read by ``PIL.Image``.
image_size (int): The size of the captured image area. It should be the size of the high-resolution image.
upscale_factor (int): magnification factor.
Returns:
Randomly cropped low-resolution images and high-resolution images.
"""
w, h = hr.size
left = torch.randint(0, w - image_size + 1, size=(1,)).item()
top = torch.randint(0, h - image_size + 1, size=(1,)).item()
right = left + image_size
bottom = top + image_size
lr = lr.crop((left // upscale_factor,
top // upscale_factor,
right // upscale_factor,
bottom // upscale_factor))
hr = hr.crop((left, top, right, bottom))
return lr, hr
def random_rotate(lr: Any, hr: Any, angle: int) -> [Any, Any]:
"""Will ``PIL.Image`` randomly rotate the image.
Args:
lr: Low-resolution image data read by ``PIL.Image``.
hr: High-resolution image data read by ``PIL.Image``.
angle (int): rotation angle, clockwise and counterclockwise rotation.
Returns:
Randomly rotated low-resolution images and high-resolution images.
"""
angle = random.choice((+angle, -angle))
lr = F.rotate(lr, angle)
hr = F.rotate(hr, angle)
return lr, hr
def random_horizontally_flip(lr: Any, hr: Any, p=0.5) -> [Any, Any]:
"""Flip the ``PIL.Image`` image horizontally randomly.
Args:
lr: Low-resolution image data read by ``PIL.Image``.
hr: High-resolution image data read by ``PIL.Image``.
p (optional, float): rollover probability. (Default: 0.5)
Returns:
Low-resolution image and high-resolution image after random horizontal flip.
"""
if torch.rand(1).item() > p:
lr = F.hflip(lr)
hr = F.hflip(hr)
return lr, hr
def random_vertically_flip(lr: Any, hr: Any, p=0.5) -> [Any, Any]:
"""Turn the ``PIL.Image`` image upside down randomly.
Args:
lr: Low-resolution image data read by ``PIL.Image``.
hr: High-resolution image data read by ``PIL.Image``.
p (optional, float): rollover probability. (Default: 0.5)
Returns:
Randomly rotated up and down low-resolution images and high-resolution images.
"""
if torch.rand(1).item() > p:
lr = F.vflip(lr)
hr = F.vflip(hr)
return lr, hr
def random_adjust_brightness(lr: Any, hr: Any) -> [Any, Any]:
"""Set ``PIL.Image`` to randomly adjust the image brightness.
Args:
lr: Low-resolution image data read by ``PIL.Image``.
hr: High-resolution image data read by ``PIL.Image``.
Returns:
Low-resolution image and high-resolution image with randomly adjusted brightness.
"""
# Randomly adjust the brightness gain range.
factor = random.uniform(0.5, 2)
lr = F.adjust_brightness(lr, factor)
hr = F.adjust_brightness(hr, factor)
return lr, hr
def random_adjust_contrast(lr: Any, hr: Any) -> [Any, Any]:
"""Set ``PIL.Image`` to randomly adjust the image contrast.
Args:
lr: Low-resolution image data read by ``PIL.Image``.
hr: High-resolution image data read by ``PIL.Image``.
Returns:
Low-resolution image and high-resolution image with randomly adjusted contrast.
"""
# Randomly adjust the contrast gain range.
factor = random.uniform(0.5, 2)
lr = F.adjust_contrast(lr, factor)
hr = F.adjust_contrast(hr, factor)
return lr, hr