-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathbm3d.py
79 lines (64 loc) · 3.07 KB
/
bm3d.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
from utils import add_gaussian_noise, symetrize
from bm3d_1st_step import bm3d_1st_step
from bm3d_2nd_step import bm3d_2nd_step
from psnr import compute_psnr
def run_bm3d(noisy_im, sigma,
n_H, k_H, N_H, p_H, tauMatch_H, useSD_H, tau_2D_H, lambda3D_H,
n_W, k_W, N_W, p_W, tauMatch_W, useSD_W, tau_2D_W):
k_H = 8 if (tau_2D_H == 'BIOR' or sigma < 40.) else 12
k_W = 8 if (tau_2D_W == 'BIOR' or sigma < 40.) else 12
noisy_im_p = symetrize(noisy_im, n_H)
img_basic = bm3d_1st_step(sigma, noisy_im_p, n_H, k_H, N_H, p_H, lambda3D_H, tauMatch_H, useSD_H, tau_2D_H)
img_basic = img_basic[n_H: -n_H, n_H: -n_H]
assert not np.any(np.isnan(img_basic))
img_basic_p = symetrize(img_basic, n_W)
noisy_im_p = symetrize(noisy_im, n_W)
img_denoised = bm3d_2nd_step(sigma, noisy_im_p, img_basic_p, n_W, k_W, N_W, p_W, tauMatch_W, useSD_W, tau_2D_W)
img_denoised = img_denoised[n_W: -n_W, n_W: -n_W]
return img_basic, img_denoised
if __name__ == '__main__':
import os
import cv2
import numpy as np
# <hyper parameter> -------------------------------------------------------------------------------
n_H = 16
k_H = 8
N_H = 16
p_H = 3
lambda3D_H = 2.7 # ! Threshold for Hard Thresholding
useSD_H = False
tau_2D_H = 'BIOR'
n_W = 16
k_W = 8
N_W = 32
p_W = 3
useSD_W = True
tau_2D_W = 'DCT'
# <\ hyper parameter> -----------------------------------------------------------------------------
im_dir = 'test_data/image'
save_dir = 'temp_test_result'
os.makedirs(save_dir, exist_ok=True)
# for im_name in os.listdir(im_dir):
for im_name in ['Cameraman.png',]:
# sigma_list = [2, 5, 10, 20, 30, 40, 60, 80, 100]
sigma_list = [20]
for sigma in sigma_list:
print(im_name, ' ', sigma)
tauMatch_H = 2500 if sigma < 35 else 5000 # ! threshold determinates similarity between patches
tauMatch_W = 400 if sigma < 35 else 3500 # ! threshold determinates similarity between patches
noisy_dir = 'test_data/sigma' + str(sigma)
im_path = os.path.join(im_dir, im_name)
im = cv2.imread(im_path, cv2.IMREAD_GRAYSCALE)
noisy_im_path = os.path.join(noisy_dir, im_name)
noisy_im = cv2.imread(noisy_im_path, cv2.IMREAD_GRAYSCALE)
im1, im2 = run_bm3d(noisy_im, sigma,
n_H, k_H, N_H, p_H, tauMatch_H, useSD_H, tau_2D_H, lambda3D_H,
n_W, k_W, N_W, p_W, tauMatch_W, useSD_W, tau_2D_W)
psnr_1st = compute_psnr(im, im1)
psnr_2nd = compute_psnr(im, im2)
im1 = (np.clip(im1, 0, 255)).astype(np.uint8)
im2 = (np.clip(im2, 0, 255)).astype(np.uint8)
save_name = im_name[:-4] + '_s' + str(sigma) + '_py_1st_P' + '%.4f' % psnr_1st + '.png'
cv2.imwrite(os.path.join(save_dir, save_name), im1)
save_name = im_name[:-4] + '_s' + str(sigma) + '_py_2nd_P' + '%.4f' % psnr_2nd + '.png'
cv2.imwrite(os.path.join(save_dir, save_name), im2)