-
Notifications
You must be signed in to change notification settings - Fork 0
/
openCVtrilateralFilter.cpp
685 lines (555 loc) · 21.8 KB
/
openCVtrilateralFilter.cpp
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#include "openCVtrilateralFilter.h"
#include <math.h>
#include "opencv2/opencv.hpp"
#include "opencv/highgui.h"
#include <vector>
using std::vector;
bool OpenCVtrilateralFilter::trilateralFilter(
cv::Mat& inputImg,
cv::Mat& outputImg,
const float sigmaC,
const float epsilon,
const int filterType
)
{
// Check sizes
if (inputImg.cols != outputImg.cols
|| inputImg.rows != outputImg.rows
|| inputImg.depth() != outputImg.depth()
|| inputImg.channels() != outputImg.channels()
|| inputImg.channels() != 1)
{
return false;
}
if (inputImg.depth() != IPL_DEPTH_32F)
return false;
// Adaptive Neighbourhood pixelwise image
cv::Mat adaptiveNeighbourhood = cv::Mat(inputImg.rows, inputImg.cols, inputImg.type(),cv::Scalar(1));
// x and y gradients
cv::Mat xGradient = cv::Mat(inputImg.rows, inputImg.cols, inputImg.type(),cv::Scalar(1));
cv::Mat yGradient = cv::Mat(inputImg.rows, inputImg.cols, inputImg.type(),cv::Scalar(1));
// Smoothed x and y gradients
cv::Mat xGradientSmooth = cv::Mat(inputImg.rows, inputImg.cols, inputImg.type(),cv::Scalar(1));
cv::Mat yGradientSmooth = cv::Mat(inputImg.rows, inputImg.cols, inputImg.type(),cv::Scalar(1));
// Gradient magnitude
cv::Mat gradientMagnitude = cv::Mat(inputImg.rows, inputImg.cols, inputImg.type(),cv::Scalar(1));
// domain variance for the two filters: sigmaC
// range variance of the two filters: sigmaR
// beta = emperical value
float sigmaR;
const float beta = 0.15f; //Always set between 0.1 and 0.2
//Computes X and Y gradients of the input image
computeGradients(inputImg, xGradient, yGradient );
// Computes the gradient magnitude
computeMagnitude(xGradient, yGradient, gradientMagnitude);
// Get min and max gradient for sigmaR
double minGrad, maxGrad;
minMaxLoc(gradientMagnitude, &minGrad, &maxGrad);
sigmaR = beta * ( (float) maxGrad - (float) minGrad );
// Level Max and Maximum LUT values
int levelMax, maxLUT;
// If using LUT
if (filterType == TYPE_LUT)
{
// maximum level = log2(xsize) or log2(ysize)
levelMax = log2( __min( inputImg.cols, inputImg.rows ) );
maxLUT = pow( 2.f, levelMax-1) + 1;
}
else
{
// Using fast-trilateral
// Find threshold for truncation
maxLUT = int( sigmaC * sqrt( fabs( log(epsilon) ) ) ) + 1;
// Calculate maximum level
levelMax = log2(2 * maxLUT + 1, true);
}
// Calculate Adaptive Neighbourhood
setAdaptiveNeighbourHood(gradientMagnitude, sigmaR, levelMax, adaptiveNeighbourhood);
// Bilaterally filter the X and Y gradients of the input image
// to produce xGradientSmooth and yGradientSmooth.
if (filterType == TYPE_LUT)
BilateralGradientFilterLUT(
xGradient,yGradient, gradientMagnitude, sigmaC, sigmaR,
xGradientSmooth, yGradientSmooth);
else
BilateralGradientFilter(
xGradient, yGradient, gradientMagnitude, sigmaC, sigmaR, epsilon,
xGradientSmooth, yGradientSmooth );
// Performs bilateral filter on the detail signal
if (filterType == TYPE_LUT)
DetailBilateralFilterLUT(
inputImg, adaptiveNeighbourhood, xGradientSmooth, yGradientSmooth,
sigmaC, sigmaR, maxLUT, outputImg);
else
DetailBilateralFilter(
inputImg, adaptiveNeighbourhood, xGradientSmooth, yGradientSmooth,
sigmaC, sigmaR, maxLUT, epsilon, outputImg);
return true;
}
// Computes X and Y gradients of the input image
void OpenCVtrilateralFilter::computeGradients(
cv::Mat& inputImg, cv::Mat& xGradient, cv::Mat& yGradient )
{
// Set up convolution kernels for forward differences
float kernel[] = { -1, 1 };
cv::Mat xKernel = cv::Mat(1,2,CV_32FC1,kernel);
cv::Mat yKernel = cv::Mat(2,1,CV_32FC1,kernel);
cv::Point anchorPt = cv::Point(0,0);
cv::filter2D(inputImg,xGradient, CV_32FC1, xKernel, anchorPt); // x gradient
cv::filter2D(inputImg,yGradient,CV_32FC1, yKernel, anchorPt); // y gradient
}
// Computes the magnitude of the gradients
void OpenCVtrilateralFilter::computeMagnitude(
cv::Mat& xGradient, cv::Mat& yGradient,
cv::Mat& gradientMagnitude)
{
gradientMagnitude = cv::norm(xGradient-yGradient);
/*
float* xGrad = (float*) xGradient.data;
float* yGrad = (float*) yGradient.data;
float* gradMag = (float*) gradientMagnitude.data;
for (int x = 0; x < xGradient.cols * xGradient.rows; x++)
gradMag[x] = cv::norm(xGrad[x], yGrad[x]);
*/
}
// Find the adaptive neighbourhood for image
void OpenCVtrilateralFilter::setAdaptiveNeighbourHood(
cv::Mat& gradientMagnitude, const float sigmaR, const int maxLevel,
cv::Mat& adaptiveNeighbourhood )
{
// Image stacks for max and min neighbourhoods
vector<cv::Mat> minGradientStack, maxGradientStack;
cv::Size imgSize = gradientMagnitude.size();
// Create image stack
for(int i = 0 ; i < maxLevel ; i++)
minGradientStack.push_back( cv::Mat(imgSize,gradientMagnitude.depth(),1) );
for(int i = 0 ; i < maxLevel ; i++)
maxGradientStack.push_back( cv::Mat(imgSize,gradientMagnitude.depth(),1) );
// Build the min-max stack
buildMinMaxImageStack(gradientMagnitude, minGradientStack, maxGradientStack );
// Set up image data references
cv::Mat minImg, maxImg;
cv::Mat magImg = cv::Mat(gradientMagnitude);
cv::Mat adpImg = cv::Mat(adaptiveNeighbourhood);
for(int y = 0; y < imgSize.width; y++)
{
for(int x = 0; x < imgSize.height; x++)
{
int lev;
const float upperThreshold = magImg.ptr(y)[x] + sigmaR;
const float lowerThreshold = magImg.ptr(y)[x] - sigmaR;
// Compute the adaptive neighbourhood based on the similarity of
// the neighborhood gradients
for(lev = 0; lev < maxLevel; lev++)
{
minImg = minGradientStack[lev];
maxImg = maxGradientStack[lev];
if ( maxImg.ptr(y)[x] > upperThreshold || minImg.ptr(y)[x] < lowerThreshold )
break;
}
// Sets the (half) size of the adaptive region
// i.e., floor( ( pow(2.f, lev) + 1 ) / 2.f )
adpImg.ptr(y)[x] = pow(2.f, lev-1);
}
}
}
// Building the Min-Max Stack of Image Gradients
void OpenCVtrilateralFilter::buildMinMaxImageStack(
cv::Mat& gradientMagnitude,
vector<cv::Mat> minStack, vector<cv::Mat> maxStack )
{
const int imgWidth = gradientMagnitude.cols;
const int imgHeight = gradientMagnitude.rows;
// Set up image data references
cv::Mat minImg1, maxImg1, minImg2, maxImg2;
cv::Mat magImg = cv::Mat(gradientMagnitude);
// Set up the bottom level of the pyramid
minImg1 = minStack[0];
maxImg1 = maxStack[0];
// Loop through image setting up bottom stack
for(int y = 0; y < imgHeight ; y++)
{
for (int x = 0; x < imgWidth; x++)
{
float outMin = 1e12;
float outMax = -1e12;
// Loop through local neighbourhood
// To find maximum and minimum values
for(int n = __max(y-1,0) ; n < __min(y+2, imgHeight); n++)
{
for(int m=__max(x-1,0); m < __min(x+2, imgWidth) ; m++)
{
outMin = __min(magImg.ptr(n)[m], outMin);
outMax = __max(magImg.ptr(n)[m], outMax);
}
}
minImg1.ptr(y)[x] = outMin;
maxImg1.ptr(y)[x] = outMax;
}
}
// Loop through image stack
for (int i = 1 ; i < minStack.size(); i++)
{
// Lower level
minImg1 = minStack[i-1];
maxImg1 = maxStack[i-1];
// Current level
minImg2 = minStack[i];
maxImg2 = maxStack[i];
for(int y = 0; y < imgHeight ; y++)
{
for (int x = 0; x < imgWidth; x++)
{
float outMin = 1e12;
float outMax = -1e12;
// Loop through local neighbourhood
// To find maximum and minimum values
for(int n = __max(y-1,0) ; n < __min(y+2, imgHeight); n++)
{
for(int m =__max(x-1,0); m < __min(x+2, imgWidth) ; m++)
{
outMin = __min(minImg1.ptr(n)[m], outMin);
outMax = __max(maxImg1.ptr(n)[m], outMax);
}
}
minImg2.ptr(y)[x] = outMin;
maxImg2.ptr(y)[x] = outMax;
}
}
}
}
// =======================================================================
// Specific for fast version of Trilateral Fitler
// =======================================================================
// Bilaterally filters the X and Y gradients of the input image.
// To produce smoothed x and y gradients
void OpenCVtrilateralFilter::BilateralGradientFilter(
cv::Mat& xGradient, cv::Mat& yGradient,
cv::Mat& gradientMagnitude,
const float sigmaC, const float sigmaR, const float epsilon,
cv::Mat& xGradientSmooth, cv::Mat& yGradientSmooth )
{
// Get image size
const int imgWidth = xGradient.cols;
const int imgHeight = xGradient.rows;
// Constants used for domain / range calculations
const float domainConst = -2.f * sigmaC * sigmaC;
const float rangeConst = -2.f * sigmaR * sigmaR;
// Compute the weight for the domain filter (domainWeight).
// The domain filter is a Gaussian lowpass filter
const int halfSize = int(sigmaC / 2.f);
cv::Mat domainWeightLUT =
cv::Mat(cvSize(halfSize+1,halfSize+1),xGradient.depth(),1);
// Memory reference
cv::Mat domainWeight = cv::Mat(domainWeightLUT);
for (int y = 0; y < domainWeightLUT.rows ; y++)
{
for (int x = 0; x < domainWeightLUT.cols ; x++)
{
// weight for the domain filter (domainWeight)
const float diff = (float) (x*x+y*y);
domainWeight.ptr(y)[x] = (float) exp( diff / domainConst );
}
}
// Memory referencing
cv::Mat xImg = cv::Mat(xGradient);
cv::Mat yImg= cv::Mat(yGradient);
cv::Mat xSmoothImg = cv::Mat(xGradientSmooth);
cv::Mat ySmoothImg= cv::Mat(yGradientSmooth);
cv::Mat magImg= cv::Mat(gradientMagnitude);
// Loop through image
for(int y = 0; y < imgHeight ; y++)
{
for(int x = 0; x < imgWidth ; x++)
{
double normFactor = 0.f;
double tmpX = 0.f;
double tmpY = 0.f;
// Calculate Middle Pixel Normalised-gradient
const float g2 = magImg.ptr(y)[x];
// Loop through local neighbourhood
for (int n = -halfSize; n <= halfSize; n++)
{
for(int m = -halfSize; m <= halfSize; m++)
{
//Compute the weight for the domain filter (domainWeight).
const float dWeight = domainWeight.ptr(abs(n))[abs(m)];
// Only perform calculation if weight above zero
if( dWeight < epsilon ) continue;
// Only perform calculationg if within bounds
const int localX = x + m;
if (localX < 0) continue;
if (localX >= imgWidth) continue;
const int localY = y + n;
if (localY < 0) continue;
if (localY >= imgHeight) continue;
// Calculate Local Normalised Gradient
const float g1 = magImg.ptr(localY)[localX];
//Compute the gradient difference between a pixel and its neighborhood pixel
const float gradDiffSq = (float) pow(g1 - g2, 2);
// Compute the weight for the range filter (rangeWeight). The range filter
// is a Gaussian filter defined by the difference in gradient magnitude.
const float rangeWeight = (float) exp( gradDiffSq / rangeConst );
// Only compute if less than epsilon
if (rangeWeight < epsilon) continue;
tmpX += xImg.ptr(localY)[localX] * dWeight * rangeWeight;
tmpY += yImg.ptr(localY)[localX] * dWeight * rangeWeight;
// Bilateral filter normalized by normFactor
normFactor += dWeight * rangeWeight;
}
}
// Set smoothed image to normalised value
xSmoothImg.ptr(y)[x] = tmpX / normFactor;
ySmoothImg.ptr(y)[x] = tmpY / normFactor;
}
}
}
// Filters the detail signal and computes the output (2nd filtering pass for trilateral filter).
void OpenCVtrilateralFilter::DetailBilateralFilter(
cv::Mat& inputImage, cv::Mat& adaptiveRegion,
cv::Mat& xGradientSmooth, cv::Mat& yGradientSmooth,
const float sigmaC, const float sigmaR,
const int maxDomainSize, const float epsilon,
cv::Mat& outputImg)
{
// Get image size
const int imgWidth = inputImage.cols;
const int imgHeight = inputImage.rows;
// Create constants used throughout code
const double domainConst = -2.f * sigmaC * sigmaC;
const double rangeConst = -2.f * sigmaR * sigmaR;
// Memory referencing
cv::Mat xImg = cv::Mat(xGradientSmooth);
cv::Mat yImg = cv::Mat(yGradientSmooth);
cv::Mat srcImg = cv::Mat(inputImage);
cv::Mat outImg = cv::Mat(outputImg);
cv::Mat adpImg = cv::Mat(adaptiveRegion);
// Define Look Up Table for weightings
// Compute the weight for the domain filter (domainWeight). The domain filter
// is a Gaussian lowpass filter
cv::Mat domainWeightLUT =
cv::Mat( cvSize(maxDomainSize+1,maxDomainSize+1), inputImage.depth(), 1);
// Memory reference
cv::Mat domainWeight = cv::Mat(domainWeightLUT);
for (int y = 0; y < domainWeightLUT.rows ; y++)
{
for (int x = 0; x < domainWeightLUT.cols ; x++)
{
// weight for the domain filter (domainWeight)
const float diff = (float) (x*x+y*y);
domainWeight.ptr(y)[x] = (float) exp( diff / domainConst );
}
}
for(int y = 0; y < imgHeight; y++)
{
for(int x = 0; x < imgWidth; x++)
{
double normFactor = 0.f;
double tmp = 0.f;
// filter window width is calculated from adaptive neighbourhood
// halfsize is half of the filter window width (or maximum LUT value)
const int halfSize = (int) __min( adpImg.ptr(y)[x], maxDomainSize );
// Coefficients defining the centerplane is calculated
// from the smoothed image gradients
// Plane Equation is z = coeffA.x + coeffB.y + coeffC
// coeffA = dI/dx, coeffB = dI/dy, coeffC = I at center pixel of the filter kernel
const float coeffA = xImg.ptr(y)[x];
const float coeffB = yImg.ptr(y)[x];
const float coeffC = srcImg.ptr(y)[x];
for (int n = -halfSize; n <= halfSize; n++) // y scan line
{
for(int m = -halfSize; m <= halfSize; m++) // x scan line
{
// Get domain weight from LUT
const float dWeight = domainWeight.ptr(abs(n))[abs(m)];
// Only perform calculation if weight above zero
if( dWeight < epsilon ) continue;
// Only perform calculationg if within bounds
const int localX = x + m;
if (localX < 0) continue;
if (localX >= imgWidth) continue;
const int localY = y + n;
if (localY < 0) continue;
if (localY >= imgHeight) continue;
// Compute the detail signal (detail) based on the difference between a
// neighborhood pixel and the centerplane passing through the center-pixel
// of the filter window.
const float detail =
srcImg.ptr(localY)[localX] - coeffA * float(m) - coeffB * float(n) - coeffC;
// Compute the weight for the range filter (rangeWeight). The range filter
// is a Gaussian filter defined by the detail signal.
const float rangeWeight = exp( pow(detail,2) / rangeConst );
if ( dWeight < epsilon ) continue;
tmp += detail * dWeight * rangeWeight;
//Detail Bilateral filter normalized by normFactor (eq. 9, Section 3.1)
normFactor += dWeight * rangeWeight;
}
}
// Write result to output image
outImg.ptr(y)[x] = tmp / normFactor + coeffC;
}
}
}
// =======================================================================
// Specific for LUT version of Trilateral Fitler
// =======================================================================
// Bilaterally filters the X and Y gradients of the input image.
// To produce smoothed x and y gradients
void OpenCVtrilateralFilter::BilateralGradientFilterLUT(
cv::Mat& xGradient, cv::Mat& yGradient,
cv::Mat& gradientMagnitude,
const float sigmaC, const float sigmaR,
cv::Mat& xGradientSmooth, cv::Mat& yGradientSmooth )
{
// Get image size
const int imgWidth = xGradient.cols;
const int imgHeight = xGradient.rows;
// Constants used for domain / range calculations
const float domainConst = -2.f * sigmaC * sigmaC;
const float rangeConst = -2.f * sigmaR * sigmaR;
// Compute the weight for the domain filter (domainWeight).
// The domain filter is a Gaussian lowpass filter
const int halfSize = int(sigmaC - 1.f / 2.f);
cv::Mat domainWeightLUT = cv::Mat(cv::Size(halfSize+1,halfSize+1), xGradient.depth(),1);
// Memory reference
cv::Mat domainWeight = cv::Mat(domainWeightLUT);
for (int y = 0; y < domainWeightLUT.rows ; y++)
{
for (int x = 0; x < domainWeightLUT.cols ; x++)
{
// weight for the domain filter (domainWeight)
const float diff = (float) (x*x+y*y);
domainWeight.ptr(y)[x] = (float) exp( diff / domainConst );
}
}
// Memory referencing
cv::Mat xImg = cv::Mat(xGradient);
cv::Mat yImg = cv::Mat(yGradient);
cv::Mat xSmoothImg = cv::Mat(xGradientSmooth);
cv::Mat ySmoothImg = cv::Mat(yGradientSmooth);
cv::Mat magImg = cv::Mat(gradientMagnitude);
// Loop through image
for(int y = 0; y < imgHeight ; y++)
{
for(int x = 0; x < imgWidth ; x++)
{
double normFactor = 0.f;
double tmpX = 0.f;
double tmpY = 0.f;
// Calculate Middle Pixel Normalised-gradient
const float g2 = magImg.ptr(y)[x];
// Loop through local neighbourhood
for (int n = -halfSize; n <= halfSize; n++)
{
for(int m = -halfSize; m <= halfSize; m++)
{
//Compute the weight for the domain filter (domainWeight).
const float dWeight = domainWeight.ptr(abs(n))[abs(m)];
// Only perform calculationg if within bounds
const int localX = x + m;
if (localX < 0) continue;
if (localX >= imgWidth) continue;
const int localY = y + n;
if (localY < 0) continue;
if (localY >= imgHeight) continue;
// Calculate Local Normalised Gradient
const float g1 = magImg.ptr(localY)[localX];
// Compute the gradient difference between a pixel and its neighborhood pixel
const float gradDiffSq = (float) pow(g1 - g2, 2);
// Compute the weight for the range filter (rangeWeight). The range filter
// is a Gaussian filter defined by the difference in gradient magnitude.
const float rangeWeight = (float) exp( gradDiffSq / rangeConst );
tmpX += xImg.ptr(localY)[localX] * dWeight * rangeWeight;
tmpY += yImg.ptr(localY)[localX] * dWeight * rangeWeight;
// Bilateral filter normalized by normFactor
normFactor += dWeight * rangeWeight;
}
}
// Set smoothed image to normalised value
xSmoothImg.ptr(y)[x] = tmpX / normFactor;
ySmoothImg.ptr(y)[x] = tmpY / normFactor;
}
}
}
// Filters the detail signal and computes the output (2nd filtering pass for trilateral filter).
void OpenCVtrilateralFilter::DetailBilateralFilterLUT(
cv::Mat& inputImage, cv::Mat& adaptiveRegion,
cv::Mat& xGradientSmooth, cv::Mat& yGradientSmooth,
const float sigmaC, const float sigmaR,
const int maxDomainSize, cv::Mat& outputImg)
{
// Get image size
const int imgWidth = inputImage.cols;
const int imgHeight = inputImage.rows;
// Create constants used throughout code
const double domainConst = -2.f * sigmaC * sigmaC;
const double rangeConst = -2.f * sigmaR * sigmaR;
// Memory referencing
cv::Mat xImg = cv::Mat(xGradientSmooth);
cv::Mat yImg = cv::Mat(yGradientSmooth);
cv::Mat srcImg = cv::Mat(inputImage);
cv::Mat outImg = cv::Mat(outputImg);
cv::Mat adpImg = cv::Mat(adaptiveRegion);
// Define Look Up Table for weightings
// Compute the weight for the domain filter (domainWeight). The domain filter
// is a Gaussian lowpass filter
cv::Mat domainWeightLUT = cv::Mat( cv::Size(maxDomainSize,maxDomainSize), inputImage.depth(), 1);
// Memory reference
cv::Mat domainWeight = cv::Mat(domainWeightLUT);
for (int y = 0; y < domainWeightLUT.rows ; y++)
{
for (int x = 0; x < domainWeightLUT.cols ; x++)
{
// weight for the domain filter (domainWeight)
const float diff = (float) (x*x+y*y);
domainWeight.ptr(y)[x] = (float) exp( diff / domainConst );
}
}
for(int y = 0; y < imgHeight; y++)
{
for(int x = 0; x < imgWidth; x++)
{
double normFactor = 0.f;
double tmp = 0.f;
// filter window width is calculated from adaptive neighbourhood
// halfsize is half of the filter window width (or maximum LUT value)
const int halfSize = (int) adpImg.ptr(y)[x];
// Coefficients defining the centerplane is calculated
// from the smoothed image gradients
// Plane Equation is z = coeffA.x + coeffB.y + coeffC
// coeffA = dI/dx, coeffB = dI/dy, coeffC = I at center pixel of the filter kernel
const float coeffA = xImg.ptr(y)[x];
const float coeffB = yImg.ptr(y)[x];
const float coeffC = srcImg.ptr(y)[x];
for (int n = -halfSize; n <= halfSize; n++) // y scan line
{
for(int m = -halfSize; m <= halfSize; m++) // x scan line
{
// Get domain weight from LUT
const float dWeight = domainWeight.ptr(abs(n))[abs(m)];
// Only perform calculationg if within bounds
const int localX = x + m;
if (localX < 0) continue;
if (localX >= imgWidth) continue;
const int localY = y + n;
if (localY < 0) continue;
if (localY >= imgHeight) continue;
// Compute the detail signal (detail) based on the difference between a
// neighborhood pixel and the centerplane passing through the center-pixel
// of the filter window.
const float detail =
srcImg.ptr(localY)[localX] - coeffA * float(m) - coeffB * float(n) - coeffC;
// Compute the weight for the range filter (rangeWeight). The range filter
// is a Gaussian filter defined by the detail signal.
const float rangeWeight = exp( pow(detail,2) / rangeConst );
// Add to function
tmp += detail * dWeight * rangeWeight;
//Detail Bilateral filter normalized by normFactor
normFactor += dWeight * rangeWeight;
}
}
// Normalise according to weight
outImg.ptr(y)[x] = tmp / normFactor + coeffC;
}
}
}