Repository navigation
Expand file tree
/
Copy pathdemo.py
More file actions
149 lines (134 loc) · 4.66 KB
/
Copy pathdemo.py
File metadata and controls
149 lines (134 loc) · 4.66 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from __future__ import annotations
import argparse
import os
import sys
import tempfile
from pathlib import Path
os.environ.setdefault("MPLCONFIGDIR", str(Path(tempfile.gettempdir()) / "matplotlib"))
import numpy as np
from numpy.typing import NDArray
PROJECT_ROOT = Path(__file__).resolve().parents[1]
SRC_DIR = PROJECT_ROOT / "src"
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
if str(SRC_DIR) not in sys.path:
sys.path.insert(0, str(SRC_DIR))
RESULTS_DIR = Path(__file__).resolve().parent / "results"
from denoising import act_filter
from tests.utils import (
ResultsDict,
create_demo_figure,
generate_gaussian_noise,
load_image,
psnr,
save_results_to_mat,
)
def run_demo(
image_path: Path | None = None,
seed: int | None = None,
save_figure: bool = True,
save_to_mat: bool = False,
output_path: Path | None = None,
mat_output_path: Path | None = None,
) -> dict[str, float | str | Path | NDArray[np.float64]]:
rng = np.random.default_rng(seed)
x, selected_path = load_image(rng=rng, image_path=image_path)
## creating a (white/colored) Gaussian noise and its corresponding FFT-PSD
noise, noise_fft_psd, kernel, kernel_name = generate_gaussian_noise(x.shape, rng=rng)
z = x + noise
kernel_power = np.sum(kernel**2)
## adaptive curvelet thresholding
x_est_ksigma = act_filter(z, noise_fft_psd, "ksigma") ## k-sigma hard-shrinkage
x_est_act = act_filter(z, noise_fft_psd, "s") ## soft-shrinkage
results: ResultsDict = {
"ground_truth": x,
"noise": noise,
"noise_fft_psd": noise_fft_psd,
"kernel": kernel,
"kernel_name": kernel_name,
"noisy": z,
"ksigma": x_est_ksigma,
"act": x_est_act,
"image_path": selected_path,
"image_name": selected_path.name,
"psnr_noisy": psnr(x, z),
"psnr_ksigma": psnr(x, x_est_ksigma),
"psnr_act": psnr(x, x_est_act),
}
if save_figure:
if output_path is None:
RESULTS_DIR.mkdir(parents=True, exist_ok=True)
output_path = RESULTS_DIR / f"demo_{selected_path.stem}_{kernel_name}_power_{kernel_power:.4f}.png"
create_demo_figure(results, output_path)
results["figure_path"] = output_path
if save_to_mat:
if mat_output_path is None:
RESULTS_DIR.mkdir(parents=True, exist_ok=True)
mat_output_path = RESULTS_DIR / f"demo_{selected_path.stem}_{kernel_name}_power_{kernel_power:.4f}.mat"
save_results_to_mat(results, mat_output_path)
results["mat_path"] = mat_output_path
return results
def main(
seed: int | None = None,
save_figure: bool = True,
save_to_mat: bool = False,
) -> None:
parser = argparse.ArgumentParser(description="Run the ACT denoising demo.")
parser.add_argument("--image", type=Path, default=None, help="Optional path to a test image.")
parser.add_argument(
"--seed",
type=int,
default=seed,
help="Random seed for repeatable image selection and noise generation.",
)
parser.add_argument(
"--save-figure",
dest="save_figure",
action="store_true",
default=save_figure,
help="Save the 2x3 summary figure.",
)
parser.add_argument(
"--output",
type=Path,
default=None,
help="Optional output path for the saved figure.",
)
parser.add_argument(
"--save-to-mat",
dest="save_to_mat",
action="store_true",
default=save_to_mat,
help="Save the results dictionary to a MAT file.",
)
parser.add_argument(
"--mat-output",
type=Path,
default=None,
help="Optional output path for the saved MAT file.",
)
args = parser.parse_args()
results = run_demo(
image_path=args.image,
seed=args.seed,
save_figure=args.save_figure,
save_to_mat=args.save_to_mat,
output_path=args.output,
mat_output_path=args.mat_output,
)
print(f"Selected image: {results['image_name']}")
print(f"kernel type : {results['kernel_name']}")
print(f"kernel power : {np.sum(results['kernel']**2):.4f}")
print(f"PSNR noisy : {results['psnr_noisy']:.4f} dB")
print(f"PSNR k-sigma : {results['psnr_ksigma']:.4f} dB")
print(f"PSNR ACT : {results['psnr_act']:.4f} dB")
if args.seed is None:
print("Seed used : random")
else:
print(f"Seed used : {args.seed}")
if args.save_figure:
print(f"Saved figure : {results['figure_path']}")
if args.save_to_mat:
print(f"Saved MAT : {results['mat_path']}")
if __name__ == "__main__":
main()