-
Notifications
You must be signed in to change notification settings - Fork 106
/
CF.h
2444 lines (2196 loc) · 84.5 KB
/
CF.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
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
*
* Curvature Filter
*
* Yuanhao Gong
**************************************************************************
@ARTICLE{gong:cf,
author={Yuanhao Gong and Ivo F. Sbalzarini},
journal={IEEE Transactions on Image Processing},
title={Curvature filters efficiently reduce certain variational energies},
year={2017},
volume={26},
number={4},
pages={1786-1798},
doi={10.1109/TIP.2017.2658954},
ISSN={1057-7149},
month={April},}
@phdthesis{gong:phd,
title={Spectrally regularized surfaces},
author={Gong, Yuanhao},
year={2015},
school={ETH Zurich, Nr. 22616},
note={http://dx.doi.org/10.3929/ethz-a-010438292}}
*=========================================================================*/
class CF
{
public:
/********************* basic IO *********************/
//read one image from disk
void read(const char* FileName);
//set one image from memory(deep copy)
void set(Mat& src);
//get the unpadded and filtered image
Mat get();
//get the padded and filtered image
Mat get_padded();
//write the result to disk
void write();
void write(const char* FileName);
/********************* PSNR and Ns *********************/
//PSNR
double PSNR();
double PSNR(const Mat& I1, const Mat& I2);
//compute naturalness factor
double Naturalness(){return Naturalness(imgF);}
double Naturalness(const Mat & img);
/********************* curvature and energy *********************/
//compute TV: scheme=0, L1 norm; scheme=1, L2 norm
void TV(const Mat & img, Mat & T, int scheme=0);
//compute MC: 0, standard; 1, my scheme; 2, quadratic fitting; 3, my scheme; 4, isoline
void MC(const Mat & img, Mat & MC, int scheme=0);
//compute GC: 0, standard; 1, my scheme; 2, quadratic fitting
void GC(const Mat & img, Mat & GC, int scheme=0);
//compute energy for given TV, MC, or GC image by L1 norm
double energy(const Mat& img, const int order=1);
//compute data fitting energy between image and imgF
double DataFitEnergy(Mat& tmp, double order);
/**************************** curvature filters ******************************************
Type=0, TV; Type=1, MC; Type=2, GC; Type=3, DC; Type=4, Bernstein;
the stepsize parameter is in (0,1]
ONLY For FilterNoSplit: mode=0, the same as Filter;
mode=1, stochastic, random step in [0, stepsize]
mode=2, spectral regularized
*********************************************************************************************/
void Filter(const int Type, double & time, const int ItNum = 10, const float stepsize=1);
void FilterNoSplit(const int Type, double & time, const int ItNum = 10, const float stepsize=1, const int mode=0);
/**************************** Half-window Regression *************************************
select a half window and perform regression on it
*********************************************************************************************/
/*
general half window filter (not curvature filter anymore):
Type = 0, only half window;
Type = 1, half and quarter window;
Type = 2, only quarter window
*/
void HalfWindow(const int Type, double & time, int ItNum=10,
Mat kernel=getGaussianKernel(7, -1, CV_32F ).t(), const float stepsize=1);
void HalfBox(const int Type, double & time, const int radius=3);
/*********************************************************************************************
******************* generic solver for variational models *****************************
solve variational models by the curvature filter, just guarantee to reduce the total energy
and convergence, but not necessarily to a local minimum
*********************************************************************************************/
//solve |U - I|^DataFitOrder + lambda * |curvature(U)|
void Solver(const int Type, double & time, const int MaxItNum,
const float lambda = 2, const float DataFitOrder = 1, const float stepsize=1);
//solve BlackBox(U,I) + lambda * |curvature(U)|
void BlackBoxSolver(const int Type, double & time, const int MaxItNum, const float lambda,
float (*BlackBox)(int row, int col, Mat& U, Mat & img_orig, float & d), const float stepsize=1);
/*********************************************************************************************
***** the dm is the gradient of regularization energy *****
*********************************************************************************************/
//The projection distance for the full image or at given location
void DM(const int FilterType, const Mat & img, Mat & dm);
//LocationType start_row start_col
// 0 (BC), 1, 1;
// 1 (BT), 2, 2;
// 2 (WC), 1, 2;
// 3 (WT), 2, 1;
void DM(const int FilterType, const int LocationType, const Mat & img, Mat & dm);
/*********************************************************************************************
**************************** Curvature Guided Filter ************************************
*********************************************************************************************/
//compute the curvature from the guided image (scaled to the size of imgF)
Mat GuideCurvature(const char * FileName, const int Type);
//filter the image such that the result is close to the specified curvature
void CurvatureGuidedFilter(const Mat & curv, const int Type, double & time, const int ItNum = 10, const float stepsize=1);
/*********************************************************************************************
**************************** Poisson Solver ************************************
*********************************************************************************************/
void Poisson(const Mat & rhs, Mat & result);
/*********************************************************************************************
**************************** statistics of curvature ********************************
*********************************************************************************************/
//the curvature statistics or Dm statistics for each curvature from the given dir_path
//result is a 1D distribution, we only need [0, Inf) thanks to the symmetry
#if defined(statistics)
void statistics(const int Type, const char* dir_path, Mat& result, bool CurvatureOrDm=true);
#endif
/********************************************************************************************
*********************************************************************************************
********************************* end of public functions ********************************
*********************************************************************************************
*********************************************************************************************/
private:
//padded original, actural float image, result
Mat image, imgF, result;
//four sets, be aware that position is fixed, see split() or Yuanhao Gong's PhD thesis
Mat WC, WT, BC, BT;
//image size
int M, N, M_orig, N_orig, M_half, N_half;
//six pointers
float* p, *p_right, *p_down, *p_rd, *p_pre, *p_Corner;
//pointer to the data
const float* p_data;
private:
//split imgF into four sets
void split();
//merge four sets back to imgF
void merge();
//naturalness evaluation
double Naturalness_search(float* data, const int N, const int offset);
//computing curvature by different schemes
void MC_fit(const Mat & img, Mat & MC);
void GC_new(const Mat & img, Mat & GC);
void GC_fit(const Mat & img, Mat & GC);
Mat GC_LUT_Init();
void GC_LUT(const Mat & LUT, const Mat & img, Mat & GC);
//fit coefficients for quad function
void FiveCoefficient(const Mat & img, Mat & x2, Mat &y2, Mat & xy, Mat & x, Mat & y);
//keep the value that has smaller absolute value
inline void KeepMinAbs(Mat& dm, Mat& d_other);
//find the signed value with minimum abs value, dist contains FOUR floats
inline float SignedMin(float * const dist);
inline float SignedMin_noSplit(const float * const dist);
/*************************************** Split into 4 sets *********************************/
//one is for BT and WC, two is for BC and WT
inline void GC_one(float* p, const float* right, const float* down, const float *rd, const float* pre, const float* corner, const float& stepsize);
inline void GC_two(float* p, const float* right, const float* down, const float *rd, const float* pre, const float* corner, const float& stepsize);
inline void MC_one(float* p, const float* right, const float* down, const float *rd, const float* pre, const float* corner, const float& stepsize);
inline void MC_two(float* p, const float* right, const float* down, const float *rd, const float* pre, const float* corner, const float& stepsize);
inline void TV_one(float* p, const float* right, const float* down, const float *rd, const float* pre, const float* corner, const float& stepsize);
inline void TV_two(float* p, const float* right, const float* down, const float *rd, const float* pre, const float* corner, const float& stepsize);
inline void DC_one(float* p, const float* right, const float* down, const float *rd, const float* pre, const float* corner, const float& stepsize);
inline void DC_two(float* p, const float* right, const float* down, const float *rd, const float* pre, const float* corner, const float& stepsize);
inline void LS_one(float* p, const float* right, const float* down, const float *rd, const float* pre, const float* corner, const float& stepsize);
inline void LS_two(float* p, const float* right, const float* down, const float *rd, const float* pre, const float* corner, const float& stepsize);
/*************************************** Direct on imgF (no split) ***********************/
inline float Scheme_GC(int i, const float * p_pre, const float * p, const float * p_nex, const float * p_guide = NULL);
inline float Scheme_MC(int i, const float * p_pre, const float * p, const float * p_nex, const float * p_guide = NULL);
inline float Scheme_TV(int i, const float * p_pre, const float * p, const float * p_nex, const float * p_guide = NULL);
inline float Scheme_DC(int i, const float * p_pre, const float * p, const float * p_nex, const float * p_guide = NULL);
inline float Scheme_LS(int i, const float * p_pre, const float * p, const float * p_nex, const float * p_guide = NULL);
//some useful functions
private:
//for solving a Poisson equation
void SampleDownOrUp(const Mat & src, Mat & dst, bool Forward=true);
void HalfBox(const int Type, double & time, const Mat & img, Mat & result, Mat & label, const int radius=3);
void HalfBoxMean(const int direction, const Mat & img, Mat & result, const int radius=3);
unsigned int myRand();
//return true if abs(d1) < abs(d2), otherwise false
inline bool myCompareAbs(const float& d1, const float& d2);
float myFastInvSqrt(float x);
};
/********************************************************************************************
*********************************************************************************************
********************************* end of CF class **************************************
*********************************************************************************************
*********************************************************************************************/
double CF::PSNR()
{
return PSNR(image, imgF);
}
double CF::PSNR(const Mat& I1, const Mat& I2)
{
Mat diff;
absdiff(I1, I2, diff); // |I1 - I2|
diff = diff.mul(diff); // |I1 - I2|^2
Scalar d = sum(diff); // sum elements per channel
double sse = d.val[0];
if( sse <= 1e-10) // for small values return zero
return 0;
else
{
double mse =sse /(double)(I1.total());
double psnr = - 10*log10(mse);
return psnr;
}
}
double CF::Naturalness(const Mat& imgF)
{
//compute naturalness factor
const float * p_row ;
const float * pp_row;
int indexX, indexY;
const int Offset = 256;
const int N = 512;
double eps = 0.0001;
double left(0), right(1), mid_left(0), mid_right(0);
Mat GradCDF = Mat::zeros(2, N, CV_32FC1);
float * Gradx = GradCDF.ptr<float>(0);
float * Grady = GradCDF.ptr<float>(1);
float f = 1.0f/((imgF.rows-1)*(imgF.cols-1));
//not efficient but safe way
for(int i = 0; i < imgF.rows - 1; i++)
{
p_row = imgF.ptr<float>(i);
pp_row = imgF.ptr<float>(i+1);
for(int j = 0; j < imgF.cols - 1; j++)
{
//scale back to 255
indexX = Offset + int((p_row[j+1] - p_row[j])*255);
indexY = Offset + int((pp_row[j] - p_row[j])*255);
Gradx[indexX] += f;
Grady[indexY] += f;
}
}
//convert Grad PDF into CDF
for (int j = 1; j < N; ++j)
{
Gradx[j] += Gradx[j-1];
Grady[j] += Grady[j-1];
}
//scale the data
GradCDF -= 0.5f;
GradCDF *= 3.14159f;
//search parameter T for x component
double Natural_x = Naturalness_search(Gradx, N, Offset);
double Natural_y = Naturalness_search(Grady, N, Offset);
//the final naturalness factor
return (Natural_x+Natural_y)/0.7508f;
}
double CF::Naturalness_search(float* data, const int N, const int offset)
{
//Ternary search
float * p_d, *p_d2;
double left(0), right(1), mid_left, mid_right;
double eps(0.0001), tmp, tmp2, error_left, error_right;
while(right-left>=eps)
{
mid_left=left+(right-left)/3;
mid_right=right-(right-left)/3;
error_left = 0; error_right = 0;
p_d = data; p_d2 = data;
for (int i=-offset+1; i<N-offset; ++i)
{
tmp = atan(mid_left*(i)) - (*p_d++);
tmp2 = atan(mid_right*(i)) - (*p_d2++);
error_left += (tmp*tmp);
error_right += (tmp2*tmp2);
}
if(error_left <= error_right)
right=mid_right;
else
left=mid_left;
}
return (mid_left+mid_right)/2;
}
void CF::read(const char* FileName)
{
//load the image and convert to float, pad to even size
//the Dirichlet boundary is used.
Mat tmp = imread(FileName, CV_LOAD_IMAGE_GRAYSCALE);
if(!tmp.data )
{
cout << "*********** Read Image Failed! ************" << std::endl ;
return ;
}
Mat tmp2 = Mat::zeros(tmp.rows, tmp.cols, CV_32FC1);
tmp.convertTo(tmp2, CV_32FC1);
M_orig = tmp2.rows;
N_orig = tmp2.cols;
M = (int)ceil(M_orig/2.0)*2;
N = (int)ceil(N_orig/2.0)*2;
M_half = M/2;
N_half = N/2;
tmp2 /= 255.0f;
image = Mat::zeros(M,N,CV_32FC1);
tmp2.copyTo(image(Range(0,M_orig),Range(0,N_orig)));
image.col(N-1) = image.col(N-2);
image.row(M-1) = image.row(M-2);
image.at<float>(M-1, N-1) = image.at<float>(M-2, N-2);
//set the imgF
imgF = Mat::zeros(M,N,CV_32FC1);
image.copyTo(imgF);
}
void CF::set(Mat& file)
{
//instead of loading image, it can be set directly from the memory.
//the Dirichlet boundary is used.
Mat tmp2 = Mat::zeros(file.rows, file.cols, CV_32FC1);
file.convertTo(tmp2, CV_32FC1);
M_orig = tmp2.rows;
N_orig = tmp2.cols;
M = (int)ceil(M_orig/2.0)*2;
N = (int)ceil(N_orig/2.0)*2;
M_half = M/2;
N_half = N/2;
tmp2 /= 255.0f;
image = Mat::zeros(M,N,CV_32FC1);
tmp2.copyTo(image(Range(0,M_orig),Range(0,N_orig)));
image.col(N-1) = image.col(N-2);
image.row(M-1) = image.row(M-2);
image.at<float>(M-1, N-1) = image.at<float>(M-2, N-2);
//set the imgF
imgF = Mat::zeros(M,N,CV_32FC1);
image.copyTo(imgF);
}
Mat CF::get()
{
return imgF(Range(0, M_orig), Range(0, N_orig));
}
Mat CF::get_padded()
{
return imgF;
}
void CF::write()
{
CF::write("CF.png");
}
void CF::write(const char* FileName)
{
Mat tmp = Mat::zeros(M_orig,N_orig,CV_8UC1);
Mat tmp2 = imgF*255.0f;
tmp2(Range(0,M_orig),Range(0,N_orig)).convertTo(tmp, CV_8UC1);
vector<int> params;
params.push_back(CV_IMWRITE_PNG_COMPRESSION);
params.push_back(9);
imwrite(FileName, tmp, params);
}
//compute Total Variation
void CF::TV(const Mat & imgF, Mat & T, int type)
{
const float * p_row, * pn_row;
float * p_t;
switch(type)
{
case 0://default using L1 norm
for(int i = 1; i < imgF.rows-1; i++)
{
p_row = imgF.ptr<float>(i);
pn_row = imgF.ptr<float>(i+1);
p_t = T.ptr<float>(i);
for(int j = 1; j < imgF.cols-1; j++)
{
p_t[j] = fabsf(p_row[j+1] - p_row[j]) + fabsf(pn_row[j] - p_row[j]);
}
}
break;
case 1:
float gx, gy;
for(int i = 1; i < imgF.rows-1; i++)
{
p_row = imgF.ptr<float>(i);
pn_row = imgF.ptr<float>(i+1);
p_t = T.ptr<float>(i);
for(int j = 1; j < imgF.cols-1; j++)
{
gx = p_row[j+1] - p_row[j]; gy = pn_row[j] - p_row[j];
p_t[j] = sqrt(gx*gx + gy*gy);
}
}
break;
default:
cout<<"The type in TV is not correct."<<endl;
break;
}
}
//compute Mean Curvature
void CF::MC(const Mat& imgF, Mat & MC, int type)
{
//classical scheme is used by default
const float * p_row, *pn_row, *pp_row;
float *p_d;
float Ix, Iy, Ixy, Ixx, Iyy, num, den, tmp;
Mat kernel;
switch(type)
{
case 0: //standard scheme
for(int i = 1; i < imgF.rows-1; i++)
{
p_row = imgF.ptr<float>(i);
pn_row = imgF.ptr<float>(i+1);
pp_row = imgF.ptr<float>(i-1);
p_d = MC.ptr<float>(i);
for(int j = 1; j < imgF.cols-1; j++)
{
Ix = (p_row[j+1] - p_row[j-1])*0.5f;
Iy = (pn_row[j] - pp_row[j])*0.5f;
Ixx = p_row[j+1] - 2*p_row[j] + p_row[j-1];
Iyy = pn_row[j] - 2*p_row[j] + pp_row[j];
Ixy = (pn_row[j-1] - pn_row[j+1]- pp_row[j-1] + pp_row[j+1])*0.25f;
num = (1+Ix*Ix)*Iyy - 2*Ix*Iy*Ixy + (1+Iy*Iy)*Ixx;
tmp = 1.0f + Ix*Ix + Iy*Iy;
den = sqrt(tmp)*tmp*2;
p_d[j] = num/den;
}
}
break;
case 1: //separate kernel from Eq.6.12, add the center pixel later
kernel = (Mat_<float>(1,3) << 0.25f, -1.25f, 0.25f);
sepFilter2D(imgF, MC, CV_32F, kernel, kernel,Point(-1,-1),0,BORDER_REPLICATE);
MC *= -1;
MC += (0.5625f*imgF);//add the center pixel
break;
case 2: //fit by a quadratic function
MC_fit(imgF, MC);
break;
case 3: //another linear kernel
Laplacian(imgF, MC, MC.depth(), 1, 0.25);
break;
case 4: //isoline schemes, be aware the difference
for(int i = 1; i < imgF.rows-1; i++)
{
p_row = imgF.ptr<float>(i);
pn_row = imgF.ptr<float>(i+1);
pp_row = imgF.ptr<float>(i-1);
p_d = MC.ptr<float>(i);
for(int j = 1; j < imgF.cols-1; j++)
{
Ix = (p_row[j+1] - p_row[j-1])*0.5f;
Iy = (pn_row[j] - pp_row[j])*0.5f;
Ixx = p_row[j+1] - 2*p_row[j] + p_row[j-1];
Iyy = pn_row[j] - 2*p_row[j] + pp_row[j];
Ixy = (pn_row[j-1] - pn_row[j+1]- pp_row[j-1] + pp_row[j+1])*0.25f;
num = Ix*Ix*Iyy - 2*Ix*Iy*Ixy + Iy*Iy*Ixx;
den = Ix*Ix + Iy*Iy;
den = sqrt(den)*den + 0.000001f;
p_d[j] = num/den;
}
}
break;
default:
cout<<"The type in MC is not correct."<<endl;
break;
}
}
//fit coefficients
void CF::FiveCoefficient(const Mat & img, Mat & x2, Mat &y2, Mat & xy, Mat & x, Mat & y)
{
Mat kernel_one = (Mat_<float>(1,3) << 1.0f, 1.0f, 1.0f);
Mat kernel_one_h = (Mat_<float>(1,3) << 0.1666667f, -0.333333f, 0.1666667f);
Mat kernel_three_h = (Mat_<float>(1,3) << -0.25f, 0.0f, 0.25f);
Mat kernel_three_v = (Mat_<float>(1,3) << 1.0f, 0.0f, -1.0f);
Mat kernel_four_h = - kernel_three_v;
Mat kernel_four_v = (Mat_<float>(1,3)<<0.1666667f,0.1666667f,0.1666667f);
sepFilter2D(img, x2, img.depth(), kernel_one_h, kernel_one);
sepFilter2D(img, y2, img.depth(), kernel_one, kernel_one_h);
sepFilter2D(img, xy, img.depth(), kernel_three_h, kernel_three_v);
sepFilter2D(img, x, img.depth(), kernel_four_h, kernel_four_v);
sepFilter2D(img, y, img.depth(), kernel_four_v, kernel_three_v);//reuse the same kernel
}
//keep the value that has minimum absolute value in dm
inline void CF::KeepMinAbs(Mat& dm, Mat& d_other)
{
float * p, * p_other;
for (int i = 0; i < dm.rows; ++i)
{
p=dm.ptr<float>(i);
p_other = d_other.ptr<float>(i);
for (int j = 0; j < dm.cols; ++j)
{
if(fabsf(p_other[j])<fabsf(p[j])) p[j] = p_other[j];
}
}
}
//compute Mean Curvature by fitting quad function
void CF::MC_fit(const Mat & img, Mat & MC)
{
Mat x2, y2, xy, x, y;
x2 = Mat::zeros(img.rows, img.cols, CV_32FC1);
y2 = Mat::zeros(img.rows, img.cols, CV_32FC1);
xy = Mat::zeros(img.rows, img.cols, CV_32FC1);
x = Mat::zeros(img.rows, img.cols, CV_32FC1);
y = Mat::zeros(img.rows, img.cols, CV_32FC1);
FiveCoefficient(img, x2, y2, xy, x, y);
float* p_x2, *p_y2, *p_xy, *p_x, *p_y, *p_d;
float num, den;
for (int i = 1; i < img.rows-1; ++i)
{
p_x2 = x2.ptr<float>(i);
p_y2 = y2.ptr<float>(i);
p_xy = xy.ptr<float>(i);
p_x = x.ptr<float>(i);
p_y = y.ptr<float>(i);
p_d = MC.ptr<float>(i);
for (int j = 1; j < img.cols-1; ++j)
{
num = (1+p_x[j]*p_x[j])*p_y2[j] - p_x[j]*p_y[j]*p_xy[j] + (1+p_y[j]*p_y[j])*p_x2[j];
den = 1+p_x[j]*p_x[j] + p_y[j]*p_y[j];
den = sqrt(den)*den;//no multiply 2 here
p_d[j] = num/den;
}
}
}
//compute Gaussian Curvature by fitting quad function
void CF::GC_fit(const Mat & img, Mat & GC)
{
Mat x2, y2, xy, x, y;
x2 = Mat::zeros(img.rows, img.cols, CV_32FC1);
y2 = Mat::zeros(img.rows, img.cols, CV_32FC1);
xy = Mat::zeros(img.rows, img.cols, CV_32FC1);
x = Mat::zeros(img.rows, img.cols, CV_32FC1);
y = Mat::zeros(img.rows, img.cols, CV_32FC1);
FiveCoefficient(img, x2, y2, xy, x, y);
float* p_x2, *p_y2, *p_xy, *p_x, *p_y, *p_d;
float num, den;
for (int i = 1; i < img.rows-1; ++i)
{
p_x2 = x2.ptr<float>(i);
p_y2 = y2.ptr<float>(i);
p_xy = xy.ptr<float>(i);
p_x = x.ptr<float>(i);
p_y = y.ptr<float>(i);
p_d = GC.ptr<float>(i);
for (int j = 1; j < img.cols-1; ++j)
{
num = p_x2[j] * p_y2[j] *4 - p_xy[j]*p_xy[j];
den = 1+p_x[j]*p_x[j] + p_y[j]*p_y[j];
den *= den;
p_d[j] = num/den;
}
}
}
//compute Gaussian curvature
void CF::GC(const Mat & imgF, Mat &GC, int type)
{
//classical scheme is used by default
const float * p_row, *pn_row, *pp_row;
float *p_d;
float Ix, Iy, Ixx, Iyy, Ixy, num, den;
switch(type)
{
case 0: //classical schemes
for(int i = 1; i < imgF.rows-1; i++)
{
p_row = imgF.ptr<float>(i);
pn_row = imgF.ptr<float>(i+1);
pp_row = imgF.ptr<float>(i-1);
p_d = GC.ptr<float>(i);
for(int j = 1; j < imgF.cols-1; j++)
{
Ix = (p_row[j+1] - p_row[j-1])*0.5f;
Iy = (pn_row[j] - pp_row[j])*0.5f;
Ixx = p_row[j+1] - 2*p_row[j] + p_row[j-1];
Iyy = pn_row[j] -2*p_row[j] + pp_row[j];
Ixy = (pn_row[j-1] - pn_row[j+1]- pp_row[j-1] + pp_row[j+1])*0.25f;
num = Ixx*Iyy - Ixy*Ixy;
den = (1.0f + Ix*Ix + Iy*Iy);
den *= den;
p_d[j] = num/den;
}
}
break;
case 1: // the new scheme from my thesis
GC_new(imgF, GC);
break;
case 2: // fit the surface with a quadratic function
GC_fit(imgF, GC);
break;
default:
cout<<"The type in GC is not correct."<<endl;
break;
}
}
//new scheme, Eq.6.16 in my thesis
void CF::GC_new(const Mat & img, Mat & GC)
{
Mat tmp = Mat::zeros(M,N,CV_32FC1);
//six kernel from Eq.6.16
Mat kernel_one = (Mat_<float>(3,3) <<
0.002104f, 0.254187f, 0.002104f,
0.254187f, -1.02516f, 0.254187f,
0.002104f, 0.254187f, 0.002104f);
Mat kernel_two = (Mat_<float>(3,3) <<
0.25f, 0.0f, -0.25f,
-0.5f, 0.0f, 0.5f,
0.25f, 0.0f, -0.25f);
Mat kernel_three = (Mat_<float>(3,3) <<
-0.25f, 0.5f, -0.25f,
0.0f, 0.0f, 0.0f,
0.25f, -0.5f, 0.25f);
Mat kernel_four = (Mat_<float>(3,3) <<
-0.286419f, 0.229983f, -0.286419f,
0.229983f, 0.225743f, 0.229983f,
-0.286419f, 0.229983f, -0.286419f);
Mat kernel_five = (Mat_<float>(3,3) <<
-0.306186f, 0.0f, 0.306186f,
0.0f, 0.0f, 0.0f,
0.306186f, 0.0f, -0.306186f);
Mat kernel_six = (Mat_<float>(3,3) <<
0.0f, -0.176777f, 0.0f,
0.176777f, 0.0f, 0.176777f,
0.0f, -0.176777f, 0.0f);
filter2D(img, GC, CV_32F, kernel_one);
pow(GC, 2, GC);
filter2D(img, tmp, CV_32F, kernel_two);
pow(tmp, 2, tmp);
GC -= tmp;
filter2D(img, tmp, CV_32F, kernel_three);
pow(tmp, 2, tmp);
GC -= tmp;
filter2D(img, tmp, CV_32F, kernel_four);
pow(tmp, 2, tmp);
GC -= tmp;
filter2D(img, tmp, CV_32F, kernel_five);
pow(tmp, 2, tmp);
GC -= tmp;
filter2D(img, tmp, CV_32F, kernel_six);
pow(tmp, 2, tmp);
GC -= tmp;
}
Mat CF::GC_LUT_Init()
{
const int scale = 255*255;
const int scale2 = 2*scale;
int num;
float den;
Mat LUT = Mat::zeros(512, 512, CV_32FC1);
float * p_LUT;
for (int i = -255; i < 1; ++i)
{
p_LUT = LUT.ptr<float>(i+255);
for (int j = -255; j < 255; ++j)
{
num = i*j + scale;
den = sqrt(float(i*i+scale))*sqrt(float(j*j+scale2));//avoid integer overflow
p_LUT[j+255] = num/den;
if (abs(p_LUT[j+255])>1)
{
cout<<i<<" "<<j<<" "<<p_LUT[j+255]<<endl;
}
p_LUT[j+255] = acos(p_LUT[j+255]);
LUT.at<float>(255-i,j+255) = p_LUT[j+255];
}
}
return LUT;
}
//approimate GC by LUT
void CF::GC_LUT(const Mat & LUT, const Mat & img, Mat & GC)
{
const float * p_row, *pp_row, *pn_row, *p_LUT;
float *g;
float total;
int row[4], col[4], offset;
for (int i = 1; i < img.rows-1; ++i)
{
p_row = img.ptr<float>(i);
pp_row = img.ptr<float>(i-1);
pn_row = img.ptr<float>(i+1);
g = GC.ptr<float>(i);
for (int j = 1; j < img.cols-1; ++j)
{
total = 0;
offset = int(255 - 255*p_row[j]);
row[0] = int(pp_row[j]*255) + offset;
col[0] = int(pp_row[j-1]*255) + offset;
col[1] = int(pp_row[j+1]*255) + offset;
row[1] = int(p_row[j-1]*255) + offset;
row[2] = int(p_row[j+1]*255) + offset;
row[3] = int(pn_row[j]*255) + offset;
col[2] = int(pn_row[j-1]*255) + offset;
col[3] = int(pn_row[j+1]*255) + offset;
//top two
p_LUT = LUT.ptr<float>(row[0]);
total += (p_LUT[col[0]] + p_LUT[col[1]]);
//left two
p_LUT = LUT.ptr<float>(row[1]);
total += (p_LUT[col[0]] + p_LUT[col[2]]);
//right two
p_LUT = LUT.ptr<float>(row[2]);
total += (p_LUT[col[1]] + p_LUT[col[3]]);
//bottom two
p_LUT = LUT.ptr<float>(row[3]);
total += (p_LUT[col[2]] + p_LUT[col[3]]);
g[j] = 6.283185f - total;
}
}
}
//compute the curvature energy
double CF::energy(const Mat &img, const int order)
{
switch(order)
{
case 1:
{
Scalar tmp = sum(cv::abs(img));
return tmp(0);
}
case 2:
{
Mat img_power = Mat::zeros(img.rows, img.cols, CV_32FC1);
pow(img, 2, img_power);
Scalar tmp = sum(img_power);
return tmp(0);
}
default:
cout <<"the order in curvature energy is not correct."<<endl;
return 0;
}
}
//compute the energy between image and imgF
double CF::DataFitEnergy(Mat & tmp, double order)
{
tmp = abs(image - imgF);
pow(tmp, order, tmp);
Scalar tmp2 = sum(tmp);
return tmp2(0);
}
//split the image into four sets
void CF::split()
{
WC = Mat::zeros(M_half,N_half,CV_32FC1);
WT = Mat::zeros(M_half,N_half,CV_32FC1);
BC = Mat::zeros(M_half,N_half,CV_32FC1);
BT = Mat::zeros(M_half,N_half,CV_32FC1);
float *p;
for (int i = 0; i < imgF.rows; ++i)
{
p=imgF.ptr<float>(i);
for (int j = 0; j < imgF.cols; ++j)
{
if (i%2==0 && j%2==0) BT.at<float>(i/2,j/2) = p[j];
if (i%2==0 && j%2==1) WT.at<float>(i/2,(j-1)/2) = p[j];
if (i%2==1 && j%2==0) WC.at<float>((i-1)/2,j/2) = p[j];
if (i%2==1 && j%2==1) BC.at<float>((i-1)/2,(j-1)/2) = p[j];
}
}
}
//merge the four sets into one image
void CF::merge()
{
float *p;
for (int i = 0; i < imgF.rows; ++i)
{
p=imgF.ptr<float>(i);
for (int j = 0; j < imgF.cols; ++j)
{
if (i%2==0 && j%2==0) p[j] = BT.at<float>(i/2,j/2);
if (i%2==0 && j%2==1) p[j] = WT.at<float>(i/2,(j-1)/2);
if (i%2==1 && j%2==0) p[j] = WC.at<float>((i-1)/2,j/2);
if (i%2==1 && j%2==1) p[j] = BC.at<float>((i-1)/2,(j-1)/2);
}
}
}
void CF::Filter(const int Type, double & time, const int ItNum, const float stepsize)
{
clock_t Tstart, Tend;
//split imgF into four sets
split();
void (CF::* Local_one)(float* p, const float* p_right, const float* p_down, const float *p_rd,
const float* p_pre, const float* p_Corner, const float& stepsize);
void (CF::* Local_two)(float* p, const float* p_right, const float* p_down, const float *p_rd,
const float* p_pre, const float* p_Corner, const float& stepsize);
switch(Type)
{
case 0:
{
Local_one = &CF::TV_one; Local_two = &CF::TV_two;
cout<<"TV Filter: "; break;
}
case 1:
{
Local_one = &CF::MC_one; Local_two = &CF::MC_two;
cout<<"MC Filter: "; break;
}
case 2:
{
Local_one = &CF::GC_one; Local_two = &CF::GC_two;
cout<<"GC Filter: "; break;
}
case 3:
{
Local_one = &CF::DC_one; Local_two = &CF::DC_two;
cout<<"DC Filter: "; break;
}
case 4:
{
Local_one = &CF::LS_one; Local_two = &CF::LS_two;
cout<<"Bernstein Filter: "; break;
}
default:
{
cout<<"The filter type is wrong. Do nothing."<<endl;
return;
}
}
Tstart = clock();
for(int it=0;it<ItNum;++it)
{
//BC
for (int i = 0; i < M_half-1; ++i)
{
p = BC.ptr<float>(i); p_right = WC.ptr<float>(i);
p_down = WT.ptr<float>(i+1); p_rd = BT.ptr<float>(i+1);
p_pre = WT.ptr<float>(i); p_Corner = BT.ptr<float>(i);
(this->*Local_two)(p, p_right, p_down, p_rd, p_pre, p_Corner, stepsize);
}
//BT
for (int i = 1; i < M_half; ++i)
{
p = BT.ptr<float>(i); p_right = WT.ptr<float>(i);
p_down = WC.ptr<float>(i); p_rd = BC.ptr<float>(i);
p_pre = WC.ptr<float>(i-1); p_Corner = BC.ptr<float>(i-1);
(this->*Local_one)(p, p_right, p_down, p_rd, p_pre, p_Corner, stepsize);
}
//WC
for (int i = 0; i < M_half-1; ++i)
{
p = WC.ptr<float>(i); p_right = BC.ptr<float>(i);
p_down = BT.ptr<float>(i+1); p_rd = WT.ptr<float>(i+1);
p_pre = BT.ptr<float>(i); p_Corner = WT.ptr<float>(i);
(this->*Local_one)(p, p_right, p_down, p_rd, p_pre, p_Corner, stepsize);
}
//WT
for (int i = 1; i < M_half; ++i)
{
p = WT.ptr<float>(i); p_right = BT.ptr<float>(i);
p_down = BC.ptr<float>(i); p_rd = WC.ptr<float>(i);
p_pre = BC.ptr<float>(i-1); p_Corner = WC.ptr<float>(i-1);
(this->*Local_two)(p, p_right, p_down, p_rd, p_pre, p_Corner, stepsize);
}
}
Tend = clock() - Tstart;
time = double(Tend)/(CLOCKS_PER_SEC/1000.0);
//merge four sets back to imgF
merge();
}
//this nosplit is very useful for tasks like deconvolution, where the four sets need to be merged
//every iteration if we use the split scheme.
void CF::FilterNoSplit(const int Type, double & time, const int ItNum, const float stepsize, const int mode)
{
clock_t Tstart, Tend;
const float Max_rand_float = stepsize/RAND_MAX;
float scaled_stepsize;
float (CF::* Local)(int i, const float* p_pre, const float* p, const float* p_nex, const float * p_curv);
switch(Type)
{
case 0:
{
Local = &CF::Scheme_TV; cout<<"TV Filter: "; break;
}
case 1:
{
Local = &CF::Scheme_MC; cout<<"MC Filter: "; break;
}
case 2:
{
Local = &CF::Scheme_GC; cout<<"GC Filter: "; break;
}
case 3:
{
Local = &CF::Scheme_DC; cout<<"DC Filter: "; break;
}
case 4:
{
Local = &CF::Scheme_LS; cout<<"Bernstein Filter: "; break;
}
default:
{
cout<<"The filter type is wrong. Do nothing."<<endl;
return;
}
}
register float d, d_abs;
int start_row_set[4]={1, 2, 1, 2};
int start_col_set[4]={1, 2, 2, 1};
const int M = imgF.rows;
const int N = imgF.cols;
Tstart = clock();
switch(mode)
{
case 0://standard
{
for(int it=0;it<ItNum;++it)
{
for (int set_index = 0; set_index < 4; ++set_index)
{
for (int i = start_row_set[set_index]; i < M-1; ++i,++i)
{
p = imgF.ptr<float>(i);
p_pre = imgF.ptr<float>(i-1);
p_down = imgF.ptr<float>(i+1);
for (int j = start_col_set[set_index]; j < N-1; ++j, ++j)
{
d = (this->*Local)(j,p_pre,p,p_down,NULL);
p[j] += (stepsize*d);
}
}
}
}