1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# coding=utf-8 |
3
|
|
|
"""Tests for BM3D image denoising.""" |
4
|
|
|
|
5
|
|
|
import multiprocessing |
6
|
|
|
import numpy as np |
7
|
|
|
import skimage.data |
8
|
|
|
from skimage.measure import compare_psnr |
9
|
|
|
import pytest |
10
|
|
|
|
11
|
|
|
import pybm3d |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
@pytest.fixture |
15
|
|
|
def noise_data(): |
16
|
|
|
"""Provide grayscale data for denoising.""" |
17
|
|
|
noise_std_dev = 40.0 |
18
|
|
|
img = skimage.data.camera() |
19
|
|
|
|
20
|
|
|
noise = np.random.normal(0, noise_std_dev, img.shape).astype(img.dtype) |
21
|
|
|
noisy_img = np.clip(img + noise, 0, 255) |
22
|
|
|
return img, noisy_img, noise_std_dev |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
@pytest.fixture |
26
|
|
|
def color_noise_data(): |
27
|
|
|
"""Provide color data for denoising.""" |
28
|
|
|
noise_std_dev = 40.0 |
29
|
|
|
img = skimage.data.astronaut() |
30
|
|
|
|
31
|
|
|
noise = np.random.normal(0, noise_std_dev, img.shape).astype(img.dtype) |
32
|
|
|
noisy_img = np.clip(img + noise, 0, 255) |
33
|
|
|
return img, noisy_img, noise_std_dev |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
def test_import(): |
37
|
|
|
"""Tests for BM3D function availability.""" |
38
|
|
|
import pybm3d |
39
|
|
|
assert callable(pybm3d.bm3d) |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
def test_bm3d(noise_data): |
43
|
|
|
"""Tests BM3D grayscale image denoising.""" |
44
|
|
|
img, noisy_img, noise_std_dev = noise_data |
45
|
|
|
|
46
|
|
|
out = pybm3d.bm3d(noisy_img, noise_std_dev) |
47
|
|
|
|
48
|
|
|
noise_psnr = compare_psnr(img, noisy_img) |
49
|
|
|
out_psnr = compare_psnr(img, out) |
50
|
|
|
|
51
|
|
|
assert out_psnr > noise_psnr |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def test_bm3d_color(color_noise_data): |
55
|
|
|
"""Tests BM3D color image denoising.""" |
56
|
|
|
img, noisy_img, noise_std_dev = color_noise_data |
57
|
|
|
|
58
|
|
|
out = pybm3d.bm3d(noisy_img, noise_std_dev) |
59
|
|
|
|
60
|
|
|
noise_psnr = compare_psnr(img, noisy_img) |
61
|
|
|
out_psnr = compare_psnr(img, out) |
62
|
|
|
|
63
|
|
|
assert out_psnr > noise_psnr |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
def test_fail_patch_size_param(noise_data): |
67
|
|
|
"""Tests expected failure for wrong patch_size parameter value.""" |
68
|
|
|
_, noisy_img, noise_std_dev = noise_data |
69
|
|
|
|
70
|
|
|
with pytest.raises(ValueError): |
71
|
|
|
pybm3d.bm3d(noisy_img, noise_std_dev, patch_size=-1) |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
def test_fail_tau_2d_hard_param(noise_data): |
75
|
|
|
"""Tests expected failure for wrong tau_2D_hard parameter value.""" |
76
|
|
|
_, noisy_img, noise_std_dev = noise_data |
77
|
|
|
|
78
|
|
|
with pytest.raises(ValueError): |
79
|
|
|
pybm3d.bm3d(noisy_img, noise_std_dev, tau_2D_hard="not_supported") |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
def test_fail_tau_2d_wien_param(noise_data): |
83
|
|
|
"""Tests expected failure for wrong tau_2D_wien parameter value.""" |
84
|
|
|
_, noisy_img, noise_std_dev = noise_data |
85
|
|
|
|
86
|
|
|
with pytest.raises(ValueError): |
87
|
|
|
pybm3d.bm3d(noisy_img, noise_std_dev, tau_2D_wien="not_supported") |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def test_fail_color_space_param(noise_data): |
91
|
|
|
"""Tests expected failure for wrong tau_2D_wien parameter value.""" |
92
|
|
|
_, noisy_img, noise_std_dev = noise_data |
93
|
|
|
|
94
|
|
|
with pytest.raises(ValueError): |
95
|
|
|
pybm3d.bm3d(noisy_img, noise_std_dev, color_space="not_supported") |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
def test_fail_no_integer_input(noise_data): |
99
|
|
|
"""Tests expected failure for inputs with type Float.""" |
100
|
|
|
_, noisy_img, noise_std_dev = noise_data |
101
|
|
|
|
102
|
|
|
noisy_img = noisy_img.astype(np.float) |
103
|
|
|
|
104
|
|
|
with pytest.raises(TypeError): |
105
|
|
|
pybm3d.bm3d(noisy_img, noise_std_dev) |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
def test_fail_wrong_num_channel_input(noise_data): |
109
|
|
|
"""Tests expected failure for inputs with wrong number of channels. |
110
|
|
|
|
111
|
|
|
Allowed number of color channels are 1 or 3. |
112
|
|
|
""" |
113
|
|
|
_, noisy_img, noise_std_dev = noise_data |
114
|
|
|
|
115
|
|
|
# build 2 channel input |
116
|
|
|
noisy_img = np.array([noisy_img, noisy_img]).transpose((1, 2, 0)) |
117
|
|
|
|
118
|
|
|
with pytest.raises(IndexError): |
119
|
|
|
pybm3d.bm3d(noisy_img, noise_std_dev) |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def test_fail_exceeding_num_threads(noise_data): |
123
|
|
|
"""Tests expected failure for exceeding num_threads parameter. |
124
|
|
|
|
125
|
|
|
The parameter must not be larger than the number of available CPUs. |
126
|
|
|
""" |
127
|
|
|
_, noisy_img, noise_std_dev = noise_data |
128
|
|
|
|
129
|
|
|
with pytest.raises(ValueError): |
130
|
|
|
num_threads = multiprocessing.cpu_count() + 1 |
131
|
|
|
pybm3d.bm3d(noisy_img, noise_std_dev, num_threads=num_threads) |
132
|
|
|
|