-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPolSAR-BLF_plugin.h
469 lines (403 loc) · 13.9 KB
/
PolSAR-BLF_plugin.h
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
#
# File : PolSAR-BLF_plugin.h
# ( C++ header file - CImg plug-in )
#
# Description : Header file for the method described in the paper
# 'Iterative Bilateral Filtering of Polarimetric SAR Data'.
# This plugin is using the CImg library.
# ( http://cimg.sourceforge.net )
#
# Copyright : Olivier D'Hondt
# (https://sites.google.com/site/dhondtolivier/)
# (https://github.com/odhondt)
#
# License : CeCILL v2.0
# ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
#
*/
Eigen::Matrix3cf LogMat(const Eigen::Matrix3cf &A)
{
using namespace::Eigen;
SelfAdjointEigenSolver<Matrix3cf> es(A);
Vector3f Va = es.eigenvalues();
Matrix3cf Ve = es.eigenvectors();
// Recomposing the matrix
Va = Va.array().log();
Matrix3f M = Va.asDiagonal();
// return Ve*M*(Ve.adjoint());
return Ve*M*Ve.adjoint();
}
float SymKullbackDist(const Eigen::Matrix3cf &A, const Eigen::Matrix3cf &B)
{
using namespace::Eigen;
float dist = std::real( (A.inverse()*B + B.inverse()*A).trace() );
dist = 0.5 * dist - 3.0;
return (dist>0.0)?dist:0.0;
}
// --- BILATERAL FILTER WITH DIFFERENT MATRIX SIMILARITIES ---
// Bilateral filter with affine invariant similarity
CImgList<T>& polsar_blf_ai(float gammaS = 2.0, float gammaR = 2.0, bool TRICK=false, bool FLATW=false, CImgDisplay *disp = 0)
{
CImgList<T> Res((*this)(0), (*this)(1));
Res(0).fill(1.0);
Res(1).fill(1.0);
float gr2 = gammaR * gammaR;
float gs2 = gammaS * gammaS;
// Calculating the window size for the gaussian weights
int H = ceil(std::sqrt(3.0)*gammaS);
// spatial Gaussian weights
CImg<T> WIm(2*H+1, 2*H+1, 1, 1, 1.0);
if(!FLATW)
cimg_forXY(WIm, x, y){
int s = x - H;
int t = y - H;
WIm(x, y) = std::exp(- (s*s + t*t) / gs2 );
}else{
cimg_forXY(WIm, x, y){
int s = x - H;
int t = y - H;
if((s*s + t*t) <= (H+1)*(H+1))
WIm(x, y) = 1.0;
else
WIm(x, y) = 0.0;
}
}
// Making an image of matrices to speed-up computations
using namespace Eigen;
CImg<Matrix3cf> Data((*this)(0), "xy");
GeneralizedSelfAdjointEigenSolver<Matrix3cf> es;
cimg_forXY((*this)(0), x, y){
Data(x, y) = (*this).get_eigenmat_at(x, y);
}
Matrix3cf TsFilt;
int cnt = 0;
if(!TRICK){
#pragma omp parallel for firstprivate(es, TsFilt)
cimg_forXY(Res[0], x, y){
#pragma omp atomic
++cnt;
float SumWeight = 0;
TsFilt.setZero();
const int tmin = cimg::max(y-H, 0),
tmax = cimg::min(y+H, Res[0].height()-1),
smin = cimg::max(x-H, 0),
smax = cimg::min(x+H, Res[0].width()-1);
for(int t = tmin; t <= tmax; ++t)
for(int s = smin; s <= smax; ++s) {
es.compute(Data(s, t), Data(x, y), EigenvaluesOnly);
const float D = es.eigenvalues().array().log().abs2().sum();
float W = 0.0;
if(!std::isnan(D)) W = std::exp(- D / gr2) * WIm(s - x + H, t - y + H);
TsFilt += W*Data(s, t);
SumWeight += W;
}
if(SumWeight > 1.0e-10) TsFilt /= SumWeight;
else TsFilt = Data(x, y);
Res.set_eigenmat_at(TsFilt, x, y);
if(cnt%9000 == 0 && disp) {
#pragma omp critical
disp->display(Res.get_colcov3()).set_title("Filtering.");
}
}
}else{
//#pragma omp parallel for
#pragma omp parallel for firstprivate(es, TsFilt)
cimg_forXY(Res[0], x, y){
#pragma omp atomic
++cnt;
TsFilt.setZero();
// Computing the weights
float SumWeight = 0.0, WRMax = 0.0;
const int tmin = cimg::max(y-H, 0),
tmax = cimg::min(y+H, Res[0].height()-1),
smin = cimg::max(x-H, 0),
smax = cimg::min(x+H, Res[0].width()-1);
for(int t = tmin; t <= tmax; ++t) {
for(int s = smin; s <= smax; ++s) {
if(s != x || t != y) {
es.compute(Data(s, t), Data(x, y), EigenvaluesOnly);
const float D = es.eigenvalues().array().log().abs2().sum();
float WR = 0.0;
if(!std::isnan(D)){
WR = std::exp(- D / gr2);
if(WR > WRMax && WR < 1.0)
WRMax = WR;
}
const float W = WR * WIm(s - x + H, t - y + H);
TsFilt += W * Data(s, t);
SumWeight += W;
}
}
}
// Adding central coefficient
TsFilt += WRMax*Data(x, y);
SumWeight += WRMax;
if(SumWeight > 1.0e-10) TsFilt /= SumWeight;
else TsFilt = Data(x, y) ;
Res.set_eigenmat_at(TsFilt, x, y);
if(cnt%9000 == 0 && disp) {
#pragma omp critical
disp->display(Res.get_colcov3()).set_title("Filtering.");
}
}
}
return Res.move_to(*this);
}
CImgList<T> get_polsar_blf_ai(float gammaS = 2.0, float gammaR = 2.0, bool TRICK=false, bool FLATW=false, CImgDisplay *disp = 0)
{
return CImgList<T>(*this, false).polsar_blf_ai(gammaS, gammaR, TRICK, FLATW, disp);
}
// Bilateral filter with log Euclidean similarity
CImgList<T>& polsar_blf_le(float gammaS = 2.0, float gammaR = 2.0, bool TRICK=false, bool FLATW=false, CImgDisplay *disp = 0)
{
CImgList<T> Res((*this)(0), (*this)(1));
Res(0).fill(1.0);
Res(1).fill(1.0);
float gr2 = gammaR * gammaR;
float gs2 = gammaS * gammaS;
// Calculating the window size for the gaussian weights
int H = ceil(std::sqrt(3.0)*gammaS);
CImg<T> WIm(2*H+1, 2*H+1, 1, 1, 1.0);
if(!FLATW) {
cimg_forXY(WIm, x, y) {
int s = x - H;
int t = y - H;
WIm(x, y) = std::exp(- (s*s + t*t) / gs2 );
}
}else{
cimg_forXY(WIm, x, y) {
int s = x - H;
int t = y - H;
if((s*s + t*t) <= (H+1)*(H+1))
WIm(x, y) = 1.0;
else
WIm(x, y) = 0.0;
}
}
// Making an image of matrices and pre-computing log to speed-up computations
using namespace Eigen;
CImg<Matrix3cf> Data((*this)(0), "xy");
CImg<Matrix3cf> logData((*this)(0), "xy");
#pragma omp parallel for
cimg_forXY((*this)(0), x, y){
Data(x, y) = (*this).get_eigenmat_at(x, y);
logData(x, y) = LogMat(Data(x, y));
}
Matrix3cf TsFilt;
int cnt = 0;
if(!TRICK){
#pragma omp parallel for firstprivate(TsFilt)
cimg_forXY(Res[0], x, y){
#pragma omp atomic
cnt++;
float SumWeight = 0;
TsFilt.setZero();
const int tmin = cimg::max(y-H, 0),
tmax = cimg::min(y+H, Res[0].height()-1),
smin = cimg::max(x-H, 0),
smax = cimg::min(x+H, Res[0].width()-1);
// Computing the weights
for(int t = tmin; t <= tmax; ++t)
for(int s = smin; s <= smax; ++s) {
//const float D = (LogMat(Data(x, y)) - LogMat(Data(s, t))).squaredNorm();
const float D = (logData(x, y) - logData(s, t)).squaredNorm();
float W = 0.0;
if(!std::isnan(D)) W = std::exp(- D / gr2) * WIm(s - x + H, t - y + H);
TsFilt += W*Data(s, t);
SumWeight += W;
}
// TsFilt /= SumWeight;
if(SumWeight > 1.0e-10) TsFilt /= SumWeight;
else TsFilt = Data(x, y);
Res.set_eigenmat_at(TsFilt, x, y);
if(cnt%9000 == 0 && disp) {
#pragma omp critical
disp->display(Res.get_colcov3()).set_title("Filtering.");
}
}
}
else{
#pragma omp parallel for firstprivate(TsFilt)
cimg_forXY(Res[0], x, y){
#pragma omp atomic
cnt++;
float SumWeight = 0.0, WRMax = 0.0;
TsFilt.setZero();
const int tmin = cimg::max(y-H, 0),
tmax = cimg::min(y+H, Res[0].height()-1),
smin = cimg::max(x-H, 0),
smax = cimg::min(x+H, Res[0].width()-1);
// Computing the weights
for(int t = tmin; t <= tmax; ++t) {
for(int s = smin; s <= smax; ++s) {
if(s != x || t != y) {
//float D = (LogMat(Data(x, y)) - LogMat(Data(s, t))).squaredNorm();
const float D = (logData(x, y) - logData(s, t)).squaredNorm();
float WR = 0.0;
if(!std::isnan(D)){
WR = std::exp(- D / gr2);
if(WR > WRMax && WR < 1.0)
WRMax = WR;
}
const float W = WR * WIm(s - x + H, t - y + H);
TsFilt += W * Data(s, t);
SumWeight += W;
}
}
}
TsFilt += WRMax*Data(x, y);
SumWeight += WRMax;
if(SumWeight > 1.0e-10) TsFilt /= SumWeight;
else TsFilt = Data(x, y);
Res.set_eigenmat_at(TsFilt, x, y);
if(cnt%9000 == 0 && disp) {
#pragma omp critical
disp->display(Res.get_colcov3()).set_title("Filtering.");
}
}
}
return Res.move_to(*this);
}
CImgList<T> get_polsar_blf_le(float gammaS = 2.0, float gammaR = 2.0, bool TRICK=false, bool FLATW=false, CImgDisplay *disp = 0)
{
return CImgList<T>(*this, false).polsar_blf_le(gammaS, gammaR, TRICK, FLATW, disp);
}
// Bilateral with kullback leibler similarity ********
CImgList<float>& polsar_blf_kl(float gammaS = 2.0, float gammaR = 2.0,bool TRICK=false, bool FLATW=false, CImgDisplay *disp = 0)
{
CImgList<T> Res((*this)(0), (*this)(1));
Res(0).fill(1.0);
Res(1).fill(1.0);
float gr2 = gammaR * gammaR;
float gs2 = gammaS * gammaS;
// Calculating the window size for the gaussian weights
int H = ceil(std::sqrt(3.0)*gammaS);
// spatial Gaussian weights
CImg<T> WIm(2*H+1, 2*H+1, 1, 1, 1.0);
if(!FLATW) {
cimg_forXY(WIm, x, y) {
int s = x - H;
int t = y - H;
WIm(x, y) = std::exp(- (s*s + t*t) / gs2 );
}
} else {
cimg_forXY(WIm, x, y) {
int s = x - H;
int t = y - H;
if((s*s + t*t) <= (H+1)*(H+1))
WIm(x, y) = 1.0;
else
WIm(x, y) = 0.0;
}
}
// WIm.display();
// Making an image of matrices to speed-up computations
using namespace Eigen;
CImg<Matrix3cf> Data((*this)(0), "xy");
cimg_forXY((*this)(0), x, y){
Data(x, y) = (*this).get_eigenmat_at(x, y);
}
Matrix3cf TsFilt;
CImg<float> DImg((*this)(0), "xy", 0.0);
int cnt = 0;
if(!TRICK){
#pragma omp parallel for firstprivate(TsFilt)
cimg_forXY(Res[0], x, y){
#pragma omp atomic
++cnt;
float SumWeight = 0;
TsFilt.setZero();
const int tmin = cimg::max(y-H, 0),
tmax = cimg::min(y+H, Res[0].height()-1),
smin = cimg::max(x-H, 0),
smax = cimg::min(x+H, Res[0].width()-1);
for(int t = tmin; t <= tmax; ++t)
for(int s = smin; s <= smax; ++s) {
const float D = SymKullbackDist(Data(x, y), Data(s, t));
float W = 0.0;
if(!std::isnan(D)) W = std::exp(- D / gr2) * WIm(s - x + H, t - y + H);
TsFilt += W*Data(s, t);
SumWeight += W;
}
if(SumWeight > 1e-10) TsFilt /= SumWeight;
else TsFilt = Data(x, y);
Res.set_eigenmat_at(TsFilt, x, y);
if(cnt%9000 == 0 && disp) {
#pragma omp critical
disp->display(Res.get_colcov3()).set_title("Filtering.");
}
}
}else{
#pragma omp parallel for firstprivate(TsFilt)
cimg_forXY(Res[0], x, y){
#pragma omp atomic
++cnt;
TsFilt.setZero();
// Computing the weights
float SumWeight = 0.0, WRMax = 0.0;
const int tmin = cimg::max(y-H, 0),
tmax = cimg::min(y+H, Res[0].height()-1),
smin = cimg::max(x-H, 0),
smax = cimg::min(x+H, Res[0].width()-1);
CImg<float> Img(2*H+1,2*H+1,1,1,0);
for(int t = tmin; t <= tmax; ++t) {
for(int s = smin; s <= smax; ++s) {
if(s != x || t != y) {
const float D = SymKullbackDist(Data(x, y), Data(s, t));
float WR = 0.0;
if(!std::isnan(D) && D >= 0.0) {
WR = std::exp(- D / gr2);
if(WR > WRMax && WR < 1.0) WRMax = WR;
}
const float W = WR * WIm(s - x + H, t - y + H);
Img(s - x + H, t - y + H) = 1;
TsFilt += W * Data(s, t);
SumWeight += W;
}
}
}
// Adding central coefficient
TsFilt += WRMax*Data(x, y);
SumWeight += WRMax;
if(SumWeight > 1.0e-10) TsFilt /= SumWeight;
else TsFilt = Data(x, y) ;
Res.set_eigenmat_at(TsFilt, x, y);
if(cnt%9000 == 0 && disp) {
#pragma omp critical
disp->display(Res.get_colcov3()).set_title("Filtering.");
}
}
}
return Res.move_to(*this);
}
CImgList<T> get_polsar_blf_kl(int WinSiz = 11, float gammaR = 2.0, bool TRICK=false, bool FLATW=false, CImgDisplay *disp = 0)
{
return CImgList<T>(*this, false).polsar_blf_kl(WinSiz, gammaR, TRICK, FLATW, disp);
}