-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhelper.py
167 lines (129 loc) · 3.51 KB
/
helper.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
import cv2
import numpy
import math
from PIL import Image, ImageEnhance
# =================CONVERT================
def CV2_to_PIL_img(cv2_im):
"""
Convert Opencv's BGR image to PIL image
Input:
--------
cv2_im: Opencv's BGR
Output:
--------
PIL Image
"""
cv2_im = cv2.cvtColor(cv2_im, cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(cv2_im)
return pil_im
def PIL_to_CV2_img(img):
"""
Convert PIL image to Opencv's BGR image
Input:
--------
img: PIL Image
Output:
--------
Opencv's BGR Image
"""
cv_image = numpy.array(img.convert('RGB'))
cv_image = cv_image[:, :, ::-1].copy()
return cv_image
# ========================================
# =========POLYNOMIAL FUNCTIONS===========
def first_polynomial_function(image):
"""
Implementation of first polynomial function.
Input:
--------
image: Opencv's BGR Image
More detail at `Documentation.pdf`
"""
table = numpy.array([1.657766*i-0.009157128*(i**2) + 0.00002579473*(i**3)
for i in numpy.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
def second_polynomial_function(image):
"""
Implementation of second polynomial function.
Input:
--------
image: Opencv's BGR Image
More detail at `Documentation.pdf`
"""
table = numpy.array([
-4.263256 * math.exp(-14)+1.546429*i-0.005558036*(i**2)+0.00001339286*(i**3)
for i in numpy.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
# ========================================
# ===========GAMMA CORRECTION ============
def adjust_gamma(image, gamma=1.0):
"""
Implementation of gamma correction
Input:
--------
image: Opencv's BGR Image
More detail at `Gamma-Correction.pdf`
"""
invGamma = 1.0 / gamma
table = numpy.array([((i / 255.0) ** invGamma) * 255
for i in numpy.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
# ========================================
# =========CONTRAST & BRIGHTNESS==========
def enhance_contrast(image, factor=1.4):
"""
Enhance contrast base on PIL->ImageEnhance.
Default suitable factor is 1,4.
Input:
image: Opencv's BGR Image
factor: contrast's factor
Output:
Enhanced contrast BGR image
"""
_image = ImageEnhance.Contrast(
CV2_to_PIL_img(image)
).enhance(factor)
return PIL_to_CV2_img(_image)
# ========================================
# =================METHODS================
def reduce_glare(image):
"""
Mixed 4 filter:
1. First polynomial function
2. Gamma correction: g = 0.75
3. Second polynomial function
4. Gamma correction: g = 0.8
Input: BGR Image.\n
Output: Reduce glare.
"""
_image = adjust_gamma(
second_polynomial_function(
adjust_gamma(
first_polynomial_function(image),
0.75
)
),
0.8
)
return _image
def mix_filter(image):
"""
Mixed 4 steps:
1. Reduce glare
2. Enhance contract: f = 1.6
3. Reduce glare
4. Enhance contract: f = 1.4
Input: BGR Image.\n
Output: Reduced glare & clearly image.
"""
_image = enhance_contrast(
reduce_glare(
enhance_contrast(
reduce_glare(image),
factor=1.6
)
),
factor=1.4
)
return _image
# ========================================