forked from Ryanshuai/BM3D_py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bior_2d.py
64 lines (50 loc) · 1.53 KB
/
bior_2d.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
import math
import pywt
import numpy as np
def bior_2d_forward(img):
"""
:wavelet forward transform
:param bior_img:
:return:
"""
assert img.shape[-1] == img.shape[-2]
iter_max = int(math.log2(img.shape[-1]))
coeffs = pywt.wavedec2(img, 'bior1.5', level=iter_max, mode='periodization')
wave_im = np.zeros_like(img, dtype=np.float64)
N = 1
wave_im[..., :N, :N] = coeffs[0]
for i in range(1, iter_max + 1):
wave_im[..., N:2 * N, N:2 * N] = coeffs[i][2]
wave_im[..., 0:N, N: 2 * N] = -coeffs[i][1]
wave_im[..., N: 2 * N, 0:N] = -coeffs[i][0]
N *= 2
return wave_im
def bior_2d_reverse(bior_img):
"""
:wavelet reverse transform
:param bior_img:
:return:
"""
assert bior_img.shape[-1] == bior_img.shape[-2]
iter_max = int(math.log2(bior_img.shape[-1]))
N = 1
rec_coeffs = [bior_img[..., 0:1, 0:1]]
for i in range(iter_max):
LL = bior_img[..., N:2 * N, N:2 * N]
HL = -bior_img[..., 0:N, N: 2 * N]
LH = -bior_img[..., N: 2 * N, 0:N]
t = (LH, HL, LL)
rec_coeffs.append(t)
N *= 2
rec_im = pywt.waverec2(rec_coeffs, 'bior1.5', mode='periodization')
return rec_im
if __name__ == '__main__':
import cv2
img = cv2.imread('Cameraman256.png', cv2.IMREAD_GRAYSCALE)
bior_img = bior_2d_forward(img)
img_ = bior_2d_reverse(bior_img)
diff = np.abs(img - img_)
print(np.max(diff))
print(np.sum(diff))
cv2.imshow('', img_.astype(np.uint8))
cv2.waitKey()