-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
37 lines (27 loc) · 1.22 KB
/
Copy pathhelpers.py
File metadata and controls
37 lines (27 loc) · 1.22 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
import numpy as np
def grayscale(pixels):
avg = np.mean(pixels, axis=2, keepdims=True)
return np.repeat(avg, 3, axis=2).astype(np.int16)
def reflect(pixels):
return pixels[:, ::-1].copy()
def blur(pixels):
padded = np.pad(pixels, ((1,1), (1,1), (0,0)), mode='edge')
total = np.zeros_like(pixels, dtype=np.float32)
for r in range(3):
for c in range(3):
total += padded[r:r+pixels.shape[0], c:c+pixels.shape[1]]
return np.round(total / 9.0).astype(np.int16)
def edges(pixels):
img_float = pixels.astype(np.float32)
gx_kernel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=np.float32)
gy_kernel = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype=np.float32)
padded = np.pad(img_float, ((1,1), (1,1), (0,0)), mode='edge')
gx_total = np.zeros_like(img_float)
gy_total = np.zeros_like(img_float)
for r in range(3):
for c in range(3):
slice_zone = padded[r:r+pixels.shape[0], c:c+pixels.shape[1]]
gx_total += slice_zone * gx_kernel[r, c]
gy_total += slice_zone * gy_kernel[r, c]
magnitude = np.sqrt(gx_total**2 + gy_total**2)
return np.round(magnitude).astype(np.int16)