-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy path_moire.py
More file actions
293 lines (232 loc) · 8.32 KB
/
Copy path_moire.py
File metadata and controls
293 lines (232 loc) · 8.32 KB
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
289
290
291
292
293
from __future__ import annotations
import io
import math
import random
from typing import cast
from . import Image, ImageFilter
def _lcd_resampling(img: Image.Image) -> Image.Image:
"""
Simulate an LCD display by mapping each pixel to a single RGB subpixel
in the repeating R-G-B stripe layout.
:param img:
:return: An image.
"""
w, h = img.size
resampled_img = Image.new("RGB", (w, h))
for y in range(h):
num = 1
for x in range(w):
r, g, b = cast("tuple[int, int, int]", img.getpixel((x, y)))
if num % 3 == 0:
resampled_img.putpixel((x, y), (0, 0, b))
elif num % 3 == 1:
resampled_img.putpixel((x, y), (r, 0, 0))
else:
resampled_img.putpixel((x, y), (0, g, 0))
num += 1
return resampled_img
def _projective_transformation(img: Image.Image) -> Image.Image:
"""
Apply a random projective transformation to simulate varying camera
position and orientation relative to the display.
:param img:
:return: An image.
"""
theta = math.radians(random.uniform(-1, 1))
# rotation
a = math.cos(theta)
b = -math.sin(theta)
d = math.sin(theta)
e = math.cos(theta)
# Translation
c = random.uniform(-0.01 * img.width, 0.01 * img.width)
f = random.uniform(-0.01 * img.height, 0.01 * img.height)
# Perspective distortion
g = random.uniform(-1e-5, 1e-5)
h_p = random.uniform(-1e-5, 1e-5)
# H
coeffs = (a, b, c, d, e, f, g, h_p)
return img.transform(
img.size, Image.Transform.PERSPECTIVE, coeffs, resample=Image.Resampling.BICUBIC
)
def _radial_distortion(img: Image.Image, k: float = -1e-7) -> Image.Image:
"""
Use radial distortion function to simulate lens distortion
:param img:
:param k:
:return: An image
"""
w, h = img.size
radial_distort = Image.new("RGB", (w, h))
cx = w / 2
cy = h / 2
for y in range(h):
for x in range(w):
r, g, b = cast("tuple[int, int, int]", img.getpixel((x, y)))
xc = x - cx
yc = y - cy
radius2 = xc**2 + yc**2
factor = 1 + k * radius2
radial_x = int(xc * factor + cx)
radial_y = int(yc * factor + cy)
# Boundary check
if 0 <= radial_x < w and 0 <= radial_y < h:
radial_distort.putpixel((radial_x, radial_y), (r, g, b))
return radial_distort
def _flat_top_kernel(
size: int = 5, sigma: float = 1.0, n: int = 2
) -> list[list[float]]:
"""
Generate a flat-top Gaussian kernel.
:param size: the size of the kernel to be produced
:param sigma: controls the broadness of the Gaussian kernel
:param n: controls the flatness of the kernel peak
:return: An Array
"""
kernel = []
center = size // 2
total = 0.0
for y in range(size):
row = []
for x in range(size):
dx = x - center
dy = y - center
r2 = dx * dx + dy * dy
value = math.exp(-((r2 / (2 * sigma * sigma)) ** n))
row.append(value)
total += value
kernel.append(row)
for y in range(size):
for x in range(size):
kernel[y][x] /= total
return kernel
def _flat_top_filtering(
img: Image.Image, size: int = 5, sigma: float = 1.0, n: int = 2
) -> Image.Image:
"""
Applying the flat top gaussian kernel on the image to simulate anti-aliasing fiter
:param img:
:param size:
:param sigma:
:param n:
:return: An image
"""
kernel = _flat_top_kernel(size=size, sigma=sigma, n=n)
flat_kernel = []
for row in kernel:
flat_kernel.extend(row)
return img.filter(ImageFilter.Kernel((5, 5), flat_kernel, scale=1))
def _bayer_resampling(img: Image.Image) -> Image.Image:
"""
Simulate a Bayer CFA (GRBG) where each pixel only captures one color channel
:param img:
:return: An image
"""
resample = Image.new("RGB", img.size)
for y in range(img.height):
for x in range(img.width):
r, g, b = cast("tuple[int, int, int]", img.getpixel((x, y)))
if y % 2 == 0:
if x % 2 == 0:
resample.putpixel((x, y), (0, g, 0))
else:
resample.putpixel((x, y), (r, 0, 0))
else:
if x % 2 == 0:
resample.putpixel((x, y), (0, 0, b))
else:
resample.putpixel((x, y), (0, g, 0))
return resample
def _add_noise(img: Image.Image) -> Image.Image:
"""
Add standard normal noise to the image to simulate sensor noise
:param img:
:return: An image
"""
noisy = Image.new("RGB", img.size)
for y in range(img.height):
for x in range(img.width):
r, g, b = cast("tuple[int, int, int]", img.getpixel((x, y)))
nr = int(r + random.gauss(0, 1))
ng = int(g + random.gauss(0, 1))
nb = int(b + random.gauss(0, 1))
noisy.putpixel((x, y), (nr, ng, nb))
return noisy
def _clamp(v: int, lo: int, hi: int) -> int:
return lo if v < lo else (hi if v > hi else v)
def _get_channel(img: Image.Image, x: int, y: int, ch: int, w: int, h: int) -> int:
x = _clamp(x, 0, w - 1)
y = _clamp(y, 0, h - 1)
return cast("tuple[int, int, int]", img.getpixel((x, y)))[ch]
def _demosaic_bilinear(img: Image.Image) -> Image.Image:
"""
Reconstruct the full RGB image from the Bayer CFA image using bilinear interpolation
of the other 2 remaining channels from nearby pixels at each pixel
:param img:
:return: An image
"""
w, h = img.size
out = Image.new("RGB", (w, h))
for y in range(h):
for x in range(w):
pixel = cast("tuple[int, int, int]", img.getpixel((x, y)))
if y % 2 == 0 and x % 2 == 0:
new_r = (
_get_channel(img, x - 1, y, 0, w, h)
+ _get_channel(img, x + 1, y, 0, w, h)
) >> 1
new_g = pixel[1]
new_b = (
_get_channel(img, x, y - 1, 2, w, h)
+ _get_channel(img, x, y + 1, 2, w, h)
) >> 1
elif y % 2 == 0 and x % 2 == 1:
new_r = pixel[0]
new_g = (
_get_channel(img, x - 1, y, 1, w, h)
+ _get_channel(img, x + 1, y, 1, w, h)
+ _get_channel(img, x, y - 1, 1, w, h)
+ _get_channel(img, x, y + 1, 1, w, h)
) >> 2
new_b = (
_get_channel(img, x - 1, y - 1, 2, w, h)
+ _get_channel(img, x + 1, y - 1, 2, w, h)
+ _get_channel(img, x - 1, y + 1, 2, w, h)
+ _get_channel(img, x + 1, y + 1, 2, w, h)
) >> 2
elif y % 2 == 1 and x % 2 == 0:
new_r = (
_get_channel(img, x - 1, y - 1, 0, w, h)
+ _get_channel(img, x + 1, y - 1, 0, w, h)
+ _get_channel(img, x - 1, y + 1, 0, w, h)
+ _get_channel(img, x + 1, y + 1, 0, w, h)
) >> 2
new_g = (
_get_channel(img, x - 1, y, 1, w, h)
+ _get_channel(img, x + 1, y, 1, w, h)
+ _get_channel(img, x, y - 1, 1, w, h)
+ _get_channel(img, x, y + 1, 1, w, h)
) >> 2
new_b = pixel[2]
else:
new_r = (
_get_channel(img, x, y - 1, 0, w, h)
+ _get_channel(img, x, y + 1, 0, w, h)
) >> 1
new_g = pixel[1]
new_b = (
_get_channel(img, x - 1, y, 2, w, h)
+ _get_channel(img, x + 1, y, 2, w, h)
) >> 1
out.putpixel(
(x, y),
(_clamp(new_r, 0, 255), _clamp(new_g, 0, 255), _clamp(new_b, 0, 255)),
)
return out
def _denoise(img: Image.Image) -> Image.Image:
return img.filter(ImageFilter.GaussianBlur(radius=1))
def _jpeg_compression(img: Image.Image) -> Image.Image:
buffer = io.BytesIO()
img.save(buffer, format="JPEG")
buffer.seek(0)
return Image.open(buffer).convert("RGB")