-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft
executable file
·213 lines (207 loc) · 6.29 KB
/
draft
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
CImg<float> nlmeans2(CImg<float> img = NULL, int patch_size = 1, double lamda =
-1, double alpha = 3, double sigma = -1, int sampling = 1) {
if (img.is_empty()) {
if (sigma < 0) {
sigma = sqrt(img.variance_noise());
}
const double np = (2 * patch_size + 1) * (2 * patch_size + 1)
* img.spectrum() / (double) sampling;
if (lamda < 0) {
;
if (np < 100) {
lamda = ((((((1.1785e-12 * np - 5.1827e-10) * np + 9.5946e-08)
* np - 9.7798e-06) * np + 6.0756e-04) * np - 0.0248)
* np + 1.9203) * np + 7.9599;
} else {
lamda = (-7.2611e-04 * np + 1.3213) * np + 15.2726;
}
}
CImg<float> dest(img.width(), img.height(), img.depth(), img.spectrum(),
0);
double *uhat = new double[img.spectrum()];
const double h2 = -0.5 / (lamda * sigma * sigma);
if (img.depth() != 1) {
const CImg<> P = img.get_blur(1);
const int n_simu = 64;
CImg<> tmp(n_simu, n_simu, n_simu);
double val = tmp.fill(0.f).noise(sigma).blur(1).pow(2.0).sum()
/ (n_simu * n_simu * n_simu);
const double sig = sqrt(val);
const int patch_size_z = 0;
const int pxi = (int) (alpha * patch_size);
const int pyi = (int) (alpha * patch_size);
const int pzi = 2;
for (int zi = 0; zi < img.depth(); ++zi) {
for (int yi = 0; yi < img.height(); ++yi) {
for (int xi = 0; xi < img.width(); ++xi) {
cimg_forC(img,v) {
uhat[v] = 0;
}
float sw = 0, wmax = -1;
for (int zj = cimg::max(0, zi - pzi);
zi < cimg::min(img.depth(), zi + pzi + 1);
++zj) {
for (int yj = cimg::max(0, yi - pyi);
yi < cimg::min(img.height(), yi + pyi + 1);
++yj) {
for (int xj = cimg::max(0, xi - pxi);
xi
< cimg::min(img.width(),
xi + pxi + 1); ++xj) {
if (cimg::abs(P(xi, yi, zi) - P(xj, yj, zj))
/ sig < 3) {
double d = 0;
int n = 0;
if (xi != xj && yi != yj && zi != zj) {
for (int kz = -patch_size_z;
kz < patch_size_z + 1; kz +=
sampling) {
int zj_ = zj + kz;
int zi_ = zi + kz;
if (zj_ >= 0 && zj < img.depth()
&& zi_ >= 0
&& zi_ < img.depth()) {
for (int ky = -patch_size;
ky <= patch_size;
ky += sampling) {
int yj_ = yj + ky;
int yi_ = yi + ky;
if (yj_ >= 0
&& yj_
< img.height()
&& yi_ >= 0
&& yi_
< img.height()) {
for (int kx =
-patch_size;
kx
<= patch_size;
kx +=
sampling) {
int xj_ = xj
+ kx,
xi_ =
xi
+ kx;
if (xj_ >= 0
&& xj_
< img.width()
&& xi_
>= 0
&& xi_
< img.width())
cimg_forC(img,v) {
double d1 =
(img)(
xj_,
yj_,
zj_,
v)
- (img)(
xi_,
yi_,
zi_,
v);
d +=
d1
* d1;
++n;
}
}
}
}
}
}
float w = (float) std::exp(d * h2);
wmax = w > wmax ? w : wmax;
cimg_forC(img,v)
uhat[v] += w
* (img)(xj, yj, zj, v);
sw += w;
}
}
// add the central pixel
cimg_forC(img,v)
uhat[v] += wmax * (img)(xi, yi, zi, v);
sw += wmax;
cimg_forC(img,v)
dest(xi, yi, zi, v) =
(float) (uhat[v] /= sw);
}
}
}
}
}
}
} else { // 2D case
const CImg<> P = img.get_blur(1); // inspired from Mahmoudi&Sapiro SPletter dec 05
const int n_simu = 512;
CImg<> tmp(n_simu, n_simu);
const double sig = sqrt(
tmp.fill(0.f).noise(sigma).blur(1).pow(2.).sum()
/ (n_simu * n_simu));
const int pxi = (int) (alpha * patch_size), pyi = (int) (alpha
* patch_size); //Define the size of the neighborhood
for (int yi = 0; yi < img.height(); ++yi) {
for (int xi = 0; xi < img.width(); ++xi) {
cimg_forC(img,v)
uhat[v] = 0;
float sw = 0, wmax = -1;
for (int yj = cimg::max(0, yi - pyi);
yj < cimg::min(img.height(), yi + pyi + 1); ++yj)
for (int xj = cimg::max(0, xi - pxi);
xj < cimg::min(img.width(), xi + pxi + 1);
++xj) {
if (cimg::abs(P(xi, yi) - P(xj, yj)) / sig < 3.) {
double d = 0;
int n = 0;
if (!(xi == xj && yi == yj)) //{
for (int ky = -patch_size;
ky < patch_size + 1; ky +=
sampling) {
int yj_ = yj + ky, yi_ = yi + ky;
if (yj_ >= 0 && yj_ < img.height()
&& yi_ >= 0
&& yi_ < img.height())
for (int kx = -patch_size;
kx < patch_size + 1; kx +=
sampling) {
int xj_ = xj + kx, xi_ = xi
+ kx;
if (xj_ >= 0
&& xj_ < img.width()
&& xi_ >= 0
&& xi_ < img.width())
// cimg_forC(img,v) {
// cimg_for(img,){
// double d1 = (img)(xj_,yj_, v) - (img)(xi_,yi_, v);
// d += d1 * d1;
// }
n++;
// }
}
//}
float w = (float) exp(d * h2);
cimg_forC(img,v) {
uhat[v] += w * (img)(xj, yj, v);
}
wmax = w > wmax ? w : wmax; // Store the maximum of the weights
sw += w; // Compute the sum of the weights
}
}
}
// add the central pixel with the maximum weight
cimg_forC(img,v)
uhat[v] += wmax * (img)(xi, yi, v);
sw += wmax;
// Compute the estimate for the current pixel
cimg_forC(img,v)
dest(xi, yi, v) = (float) (uhat[v] /= sw);
}
} // main loop
} // 2d
delete[] uhat;
dest.move_to(img);
return dest;
}
}