-
Notifications
You must be signed in to change notification settings - Fork 0
/
imaging.py
76 lines (60 loc) · 2.11 KB
/
imaging.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
import cv2
import numpy as np
def resize_image(img, maxwh = 1200):
"""
Resize images s.t. max(w,h) = 1200
Args:
img (np.array): Input image to be resized
maxwh (int): Number of pixels for the longest dimension
Returns:
img_resized (np.array): output resized image
"""
r, c, _ = img.shape
img_scale = maxwh/np.maximum(r, c)
img_resized = cv2.resize(img, (int(np.ceil(c*img_scale)), int(np.ceil(r*img_scale))))
return img_resized
def hist_stretch(img, bit_depth):
"""
Contrast normalization via global histogram stretching
Args:
img (np.array): Input image
bit_depth (int): Number of bits per channel
Returns:
img_out (np.array): Contast normalized image
"""
if np.max(img) - np.min(img) < 1e-5: # Do-nothing
img_out = img
else:
min_img, max_img = np.min(img), np.max(img)
img_out = ((img - min_img) / (max_img - min_img)) * (2 ** bit_depth - 1)
return img_out
def srgb_gamma(img):
"""
Apply gamma correction (forward transformation)
(https://en.wikipedia.org/wiki/SRGB)
Args:
img (np.array): input (linear) image
Returns:
img (np.array): non-linear/gamma-compressed image
"""
for idx_channel in range(img.shape[2]):
this_channel = img[:, :, idx_channel]
img[:, :, idx_channel] = 12.92 * this_channel * (this_channel <= 0.0031308) + (
1.055 * this_channel ** (1 / 2.4) - 0.055) * (this_channel > 0.0031308)
return img
def correct_color_single_image(img, illuminant):
"""
Apply color correction via Von-Kries Model
Args:
img (np.array): input (linear) image
illuminant (np.array): RGB color of light source
Returns:
img_corr (np.array): corrected image s.t. to be
taken under a canonical perfect white light source
"""
highest_gain = np.max(illuminant)
gain = highest_gain / illuminant
img_corr = img.copy()
for idx_channel in range(img.shape[2]):
img_corr[:, :, idx_channel] = img_corr[:, :, idx_channel] * gain[idx_channel]
return img_corr