forked from Ryanshuai/BM3D_py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ht_filtering_hadamard.py
40 lines (31 loc) · 1016 Bytes
/
ht_filtering_hadamard.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
import numpy as np
from scipy.linalg import hadamard
import math
def ht_filtering_hadamard(group_3D, sigma, lambdaHard3D, doWeight): # group_3D shape=(n*n, nSx_r)
"""
:hard threshold filtering after hadamard transform
:param group_3D:
:param sigma:
:param lambdaHard3D:
:param doWeight:
:return:
"""
nSx_r = group_3D.shape[-1]
coef_norm = math.sqrt(nSx_r)
coef = 1.0 / nSx_r
group_3D_h = hadamard_transform(group_3D)
# hard threshold filtering in this block
T = lambdaHard3D * sigma * coef_norm
T_3D = np.where(np.abs(group_3D_h) > T, 1, 0)
weight = np.sum(T_3D)
group_3D_h = np.where(np.abs(group_3D_h) > T, group_3D_h, 0.)
group_3D = hadamard_transform(group_3D_h)
group_3D *= coef
if doWeight:
weight = 1. / (sigma * sigma * weight) if weight > 0. else 1.
return group_3D, weight
def hadamard_transform(vec):
n = vec.shape[-1]
h_mat = hadamard(n).astype(np.float64)
v_h = vec @ h_mat
return v_h