-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwavelets_transforms.py
100 lines (67 loc) · 2.07 KB
/
wavelets_transforms.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
import numpy as np
def haar1d(array):
temp = np.empty_like(array)
# coefficients
s0 = 0.5
s1 = 0.5
w0 = 0.5
w1 = -0.5
h = array.size // 2
for i in range(h):
k = i * 2
temp[i] = (array[k] * s0) + (array[k + 1] * s1)
temp[i + h] = (array[k] * w0) + (array[k + 1] * w1)
return temp
def haar1d_inverse(array):
temp = np.empty_like(array)
# coefficients
s0 = 0.5
s1 = 0.5
w0 = 0.5
w1 = -0.5
h = array.size // 2
for i in range(h):
k = i * 2
temp[k] = ((array[i] * s0) + (array[i + h] * w0)) / w0
temp[k + 1] = ((array[i] * s1) + (array[i + h] * w1)) / s1
return temp
def haar2d(image):
rows, cols = image.shape
for i in range(rows):
image[i, :] = haar1d(image[i, :])
for j in range(cols):
image[:, j] = haar1d(image[:, j])
return image
def haar2d_inverse(image):
rows, cols = image.shape
for j in range(cols):
image[:, j] = haar1d_inverse(image[:, j])
for i in range(rows):
image[i, :] = haar1d_inverse(image[i, :])
return image
def haar_image(image, level=1):
t_image = np.empty_like(image)
for i in range(level):
rows, cols, _ = image.shape
rows //= 1 + i
cols //= 1 + i
r_comp = haar2d(image[:rows, :cols, 0])
g_comp = haar2d(image[:rows, :cols, 1])
b_comp = haar2d(image[:rows, :cols, 2])
t_image[:rows, :cols, 0] = r_comp
t_image[:rows, :cols, 1] = g_comp
t_image[:rows, :cols, 2] = b_comp
return t_image
def haar_inverse_image(image, level=1):
t_image = np.empty_like(image)
for i in range(level, 0, -1):
rows, cols, _ = image.shape
rows = rows // i
cols = cols // i
r_comp = haar2d_inverse(image[:rows, :cols, 0])
g_comp = haar2d_inverse(image[:rows, :cols, 1])
b_comp = haar2d_inverse(image[:rows, :cols, 2])
t_image[:rows, :cols, 0] = r_comp
t_image[:rows, :cols, 1] = g_comp
t_image[:rows, :cols, 2] = b_comp
return t_image