-
Notifications
You must be signed in to change notification settings - Fork 1
/
processing.py
82 lines (65 loc) · 2.82 KB
/
processing.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
80
81
82
from cv2 import STEREO_SGBM_MODE_HH
import numpy as np
from v1diffusion import utils
from v1diffusion import transforms
from v1diffusion import evolution
def AHE(img, mask, T1, T2=0.5, a0=0.05, b0=0.55, a1=0.2, b1=5, sigma=0.4, epss=0.1, dt1=0.1, dt2=0.1, progress=True):
"""
Adaptation of AHE algorithm as described by Boscain-2018 exploiting the knowledge of the
position of the corruption (reference paper: https://doi.org/10.1007/s10851-018-0810-4)
The default values of the parameters are those suggested by the original paper,
found experimentally.
The lift is performed using the Gaussian lift introduced by Ballerin, Grong
img: input greyscale 2D image (0 is white, 1 is black)
mask: corruption characteristic function (0 is image, 1 is corruption)
T1: time to run the strong smoothing
T2: time to run the weak smoothing
a0: constant parameter of varying coefficients in direction X_1
b0: constant parameter of varying coefficients in direction X_2
a1: exponential parameter of varying coefficients in direction X_1
b1: exponential parameter of varying coefficients in direction X_2
sigma: scaling parameter for the exponential part of the diffusion coefficients
dt1: time interval discretization for strong smoothing
dt2: time interval discretization for weak smoothing
progress: to either show or not tqdm animated progress bar
@returns: the restored 2D greyscale image
"""
I = img.copy()
#Sanity checks
if I.shape != mask.shape:
raise Exception("Mask size mismatch")
if np.max(I)>1:
I = I.astype(np.float32)
I /= 255
#Step 1, simple averaging
I_f = utils.fill_corruption_with_bfs_avg(I, mask)
#Step 2, strong smoothing
uf1 = evolution.evolve_vc_hypoelliptic(transforms.lift_gradient(I_f),I_f, T1, a0=a0, b0=b0, a1=a1, b1=b1, sigma=sigma, epss=epss, dt=dt1)
result1 = transforms.project_max(uf1)
if np.max(result1)>1:
result1 /= np.max(result1)
#Step 3, synthetis (advanced averaging)
#result1 = (I_f+result1)/2
I_f = utils.fill_corruption_with_bfs_avg(I, mask)
#Step 4, weak smoothing
uf2 = evolution.evolve_vc_hypoelliptic(transforms.lift_gradient(result1),result1, T2, dt=dt2)
result2 = transforms.project_max(uf2)
if np.max(result2)>1:
result2 /= np.max(result2)
return result2
def diffusion_with_restarts(img, mask, T=1):
"""
TO IMPLEMENT
"""
I = img.copy()
#Sanity check
if I.shape != mask.shape:
raise Exception("Mask size mismatch")
if np.max(I)>1:
I = I.astype(np.float32)
I /= 255
uf2 = evolution.evolve_vc_hypoelliptic(transforms.lift_gradient(result1),result1, T2, dt=dt2)
result2 = transforms.project_max(uf2)
if np.max(result2)>1:
result2 /= np.max(result2)
return result2