-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRoom.cpp
1278 lines (1060 loc) · 41.6 KB
/
TestRoom.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
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
/*
* TestRoom.cpp
*
* Created on: Feb 18, 2014
* Author: dailos
*/
#include <iostream>
#include <complex>
#include <cmath>
#include <random>
#include "TestRoom.h"
#include "ToolBox.h"
#include "Zernike.h"
#include "NoiseEstimator.h"
#include "Optics.h"
#include "FITS.h"
#include "SparseRecovery.h"
#include "OpticalSetup.h"
#include "ImageQualityMetric.h"
#include "ConvexOptimization.h"
#include <memory>
void test_jacobian()
{
}
//ML playgound:
//linear models
void leastSquearesExperiment()
{
//We have N=15 samples from y=sin(x) in the interval [0,2π] and add gaussian noise
unsigned int N = 5; //Number of samples
const double pi = 3.14159265358979323846; /* pi */
double startPoint(0.0);
double endPoint(2.0 * pi);
double stepSize((endPoint-startPoint)/double(N-1));
cv::Mat x,t;
cv::theRNG() = cv::RNG( cv::getTickCount() );
cv::RNG& rng = cv::theRNG();
for(double xi=startPoint; xi<=endPoint; xi=xi+stepSize)
{
x.push_back(xi);
t.push_back(sin(xi)+rng.gaussian(0.2));
}
std::cout << "x: " << x.t() << std::endl;
std::cout << "t: " << t.t() << std::endl;
//Suppose now I want to modeled the system with a combination of M potentially nonlinear basis functions such as radial basis functions, RBF
auto rbf = [](const double& x, const double& xm, const double& r) -> double
{ //exp(-(x-xm)^2/r^2)
return std::exp(-std::pow(x-xm,2.0)/(r*r));
};
std::vector<cv::Mat> phi_v;
for(double xm=startPoint; xm<=endPoint; xm=xm+stepSize)
{
cv::Mat phi_m;
//We use the same locations for the center of the radial functions
for(double xi=startPoint; xi<=endPoint; xi=xi+stepSize)
{
phi_m.push_back(rbf(xi,xm,1.0));
}
phi_v.push_back(phi_m);
}
cv::Mat phi;
cv::hconcat(phi_v, phi);
//Apply least squares to find the weights
cv::Mat w = (phi.t()*phi+0.5*cv::Mat::eye(N,N,cv::DataType<double>::type)).inv()*phi.t()*t; //Ridge regression
//std::cout << "Ridge regression weights: " << w << std::endl;
//cv::Mat w = (phi.t()*phi).inv()*phi.t()*t; //Least squares
std::vector<double> gamma_v(phi.cols, 1.0);
w = perform_BSBL(phi, t, NoiseLevel::LittleNoise, gamma_v, 1); //Noiseless, LittleNoise
std::cout << "BSBL: " << w << std::endl;
gamma_v = std::vector<double>(phi.cols, 1.0);
w = perform_SBL(phi, t, NoiseLevel::LittleNoise, gamma_v); //Noiseless, LittleNoise
std::cout << "SBL: " << w << std::endl;
std::cout << "END" << std::endl;
/*
cv::Mat mu_x_old = cv::Mat::zeros(phi.cols, 1, cv::DataType<double>::type);
cv::Mat Sigma_x, mu_x;
cv::Mat gamma = cv::Mat::ones(phi.cols, phi.cols, cv::DataType<double>::type);
double lambda(1e-3);
for(unsigned int i=0;i<100;++i)
{
cv::Mat PhiGammPhiT = phi*gamma*phi.t();
cv::Mat lambdaI(PhiGammPhiT.size(), cv::DataType<double>::type);
cv::setIdentity(lambdaI, lambda);
cv::Mat den = PhiGammPhiT + lambdaI;
//Matrix right division should be implemented cv::solve instead of by inverse multiplication
// xA = b => x = b/A but never use x = b * inv(A)!!!
//They are mathematically equivalent but not the same when working with floating numbers
//x = cv.solve( A.t(), b.t() ).t(); equivalent to b/A
//to perform matrix right division: mrdivide(b,A)
cv::Mat H;
cv::solve(den.t(), phi, H, cv::DECOMP_NORMAL);
std::cout << "Hello" << std::endl;
cv::Mat Hy = H.t() * t;
cv::Mat HPhi = H.t() * phi;
mu_x = gamma * Hy;
cv::Mat diff = cv::abs(mu_x_old - mu_x);
double maxVal;
cv::minMaxLoc(diff, nullptr, &maxVal, nullptr, nullptr);
if (maxVal < 1e-4)
{
std::cout << "Solution found." << std::endl;
break;
}
Sigma_x = gamma - gamma * HPhi * gamma;
cv::Mat mu_x2;
cv::multiply(mu_x, mu_x, mu_x2);
gamma = Sigma_x + mu_x2;
double l2 = cv::norm(t-(phi*mu_x), cv::NORM_L2);
lambda = ((l2*l2) + cv::trace(phi*Sigma_x*phi.t()).val[0])/phi.rows;
}
w = mu_x.clone();
std::cout << "Sparse Bayesian Learning weights: " << w << std::endl;
*/
/*
//Show result: (prediction)
cv::Mat result;
cv::Mat cur;
for(double cursor=startPoint;cursor<=endPoint;cursor=cursor+0.01)
{
double val(0.0);
int i(0);
for(double xm=startPoint; xm<=endPoint; xm=xm+stepSize)
{
val = val + w.at<double>(i++,0) * rbf(cursor,xm,1.0);
}
result.push_back(val);
cur.push_back(cursor);
}
//std::cout << "phi: " << phi << std::endl;
cv::Mat plot_data, plot_result;
Plot2d plot1( x,t );
Plot2d plot2( cur,result );
//cv::plot->setPlotBackgroundColor( cv::Scalar( 50, 50, 50 ) ); // i think it is not implemented yet
plot1.setPlotLineColor( cv::Scalar( 255, 255, 255 ) );
plot1.setPlotLineWidth(2);
plot1.setNeedPlotLine(false);
plot1.render( plot_data );
plot2.setPlotLineColor( cv::Scalar( 255, 255, 255 ) );
plot2.setPlotLineWidth(2);
plot2.setNeedPlotLine(false);
plot2.render( plot_result );
cv::Mat gray_result;
cv::cvtColor(plot_result, gray_result, CV_BGR2GRAY);
writeFITS(gray_result, "../gray_result.fits");
*/
/*
cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );// Create a window for display.
cv::imshow( "Display window", plot_data ); // Show our image inside it.
cv::namedWindow( "Display window2", cv::WINDOW_AUTOSIZE );// Create a window for display.
cv::imshow( "Display window2", plot_result ); // Show our image inside it.
cv::waitKey();
*/
}
bool test_NoiseEstimator()
{
//Benchmark
cv::Mat img, dat;
readFITS("../inputs/surfi000.fits", dat);
dat.convertTo(img, cv::DataType<double>::type);
cv::normalize(img, img, 0.0, 1.0, CV_MINMAX);
std::cout << "cols: " << img.cols << " x " << "rows: " << img.rows << std::endl;
//transfor to fourier domain and brings energy to the center
cv::Mat D;
cv::dft(img, D, cv::DFT_COMPLEX_OUTPUT + cv::DFT_SCALE);
fftShift(D);
//remove frequencies beyond cutoff
OpticalSetup tsettings(D.cols);
Zernike zrnk;
D.setTo(0, zrnk.phaseMapZernike(1, D.cols, tsettings.cutoffPixel()) == 0);
//Take back to image domain the remaing frequencies
fftShift(D);
cv::idft(D, img, cv::DFT_REAL_OUTPUT);
//add gaussian noise
cv::Mat noise(img.size(), cv::DataType<double>::type);
cv::Scalar sigma(0.29), mean(0);
cv::theRNG() = cv::RNG( cv::getTickCount() );
cv::randn(noise, mean, sigma);
cv::add(img, noise, img);
//estimate the noise
NoiseEstimator ne;
ne.meanPowerSpectrum(img);
std::cout << "Mean power: " << ne.meanPower() << std::endl;
std::cout << "Sigma: " << ne.sigma() << std::endl;
std::cout << "Sigma 2: " << ne.sigma2() << std::endl;
return true;
}
//cv::randn(cv::Mat a, cv::Mat mean, cv::Mat std)
//fill matrix a with random values from normal distribution with mean vector and std matrix
template<class T>
cv::Mat createRandomMatrix(const unsigned int& xSize, const unsigned int& ySize)
{
double sigma = 5.0;
std::random_device rdevice;
std::default_random_engine generator(rdevice());
std::normal_distribution<> distribution(0, sigma);
cv::Mat A = cv::Mat(xSize, ySize, cv::DataType<T>::type);
for(auto it = A.begin<T>(); it != A.end<T>(); ++it)
{
(*it) = (T)distribution(generator);
}
return A;
}
bool test_minimization()
{
ConvexOptimization mm;
//write in wolfram alpha the following to verify: "minimize{x^8-3*(x+3)^5+5+(y+4)^6+y^5+3*z^2} in x+2*y+3*z=0"
double ce_dat[] = {1.0, 2.0, 3.0};
cv::Mat ce(1,3,cv::DataType<double>::type,ce_dat), Q, R;
householder(ce.t(), Q, R);
int numberOfUnkowns(3);
int Np = numberOfUnkowns - ce.rows;
cv::Mat Q2 = Q(cv::Range::all(), cv::Range(Q.cols - Np, Q.cols ));
std::cout << "Q2: " << Q2 << std::endl;
std::function<double(cv::Mat)> funcc = [] (cv::Mat x) -> double
{//Eq to minimize: x^8-3*(x+3)^5+5+(y+4)^6+y^5+3*z
return std::pow(x.at<double>(0,0),8) - 3 * std::pow(x.at<double>(0,0)+3,5) + 5 +
std::pow(x.at<double>(1,0)+4,6)+ std::pow(x.at<double>(1,0),5) + 3 * std::pow(x.at<double>(2,0),2);
};
std::function<cv::Mat(cv::Mat)> dfuncc_diff = [funcc] (cv::Mat x) -> cv::Mat
{ //make up gradient vector through slopes and tiny differences
double EPS(1.0e-4);
cv::Mat df = cv::Mat::zeros(x.size(), x.type());
for(unsigned int j = 0; j < x.total(); ++j)
{
cv::Mat xh = x.clone();
cv::Mat xl = x.clone();
xh.at<double>(j,0) = xh.at<double>(j,0) + EPS;
xl.at<double>(j,0) = xl.at<double>(j,0) - EPS;
double fh = funcc(xh);
double fl = funcc(xl);
df.at<double>(j,0) = (fh-fl)/(2.0*EPS);
}
return df;
};
std::function<cv::Mat(cv::Mat)> dfuncc = [] (cv::Mat x) -> cv::Mat
{ //Gradient vector function: function derivative with every variable
cv::Mat t(3,1, cv::DataType<double>::type); //Size(3,1)->1 row, 2 colums
t.at<double>(0,0) = 8 * std::pow(x.at<double>(0,0),7) - 15 *
std::pow(x.at<double>(0,0)+3,4);
t.at<double>(1,0) = 5 * std::pow(x.at<double>(1,0),4) + 6 *
std::pow(x.at<double>(1,0)+4,5);
t.at<double>(2,0) = 6 * x.at<double>(2,0);
return t;
};
cv::Mat x0_conv = cv::Mat::zeros(3, 1, cv::DataType<double>::type); //reset starting point
//Lambda function that turn minimize function + constraints problem into minimize function lower dimension problem
auto F_constrained = [] (cv::Mat x, std::function<double(cv::Mat)> func, const cv::Mat& Q2) -> double
{
return func(Q2*x);
};
auto DF_constrained = [] (cv::Mat x, std::function<cv::Mat(cv::Mat)> dfunc, const cv::Mat& Q2) -> cv::Mat
{
return Q2.t() * dfunc(Q2*x);
};
std::function<double(cv::Mat)> f_constrained = std::bind(F_constrained, std::placeholders::_1, funcc, Q2);
std::function<cv::Mat(cv::Mat)> df_constrained = std::bind(DF_constrained, std::placeholders::_1, dfuncc_diff, Q2);
//Define a new starting point with lower dimensions after reduction with contraints
cv::Mat p_constrained = Q2.t() * x0_conv;
mm.perform_BFGS(p_constrained, f_constrained, df_constrained);
x0_conv = Q2 * p_constrained; //Go back to original dimensional
std::cout << "mimumum: " << x0_conv.t() << std::endl;
return true;
}
bool test_minQ2()
{
//there are 4 parameters. The first is double than the third. The last should be zero
double ce_dat[] = {1.0, 0.0, -1.0, 0.0};
cv::Mat ce(1,4,cv::DataType<double>::type,ce_dat), Q, R;
householder(ce.t(), Q, R);
int numberOfUnkowns(4);
int Np = numberOfUnkowns - ce.rows;
cv::Mat Q2 = Q(cv::Range::all(), cv::Range(Q.cols - Np, Q.cols ));
std::cout << "Q2: " << Q2 << std::endl;
cv::Mat bias = cv::Mat::zeros(4, 1, cv::DataType<double>::type);
bias.at<double>(2, 0) = -2.0;
cv::Mat p_constrained = cv::Mat::ones(3, 1, cv::DataType<double>::type);
cv::Mat x0_conv = (Q2 * p_constrained) + bias; //Go back to original dimensional
std::cout << "x cons: " << x0_conv << std::endl;
p_constrained = (x0_conv.t() - bias.t()) * Q2;
std::cout << "x uncons: " << p_constrained << std::endl;
return true;
}
void test_SparseRecovery()
{
double P_array[] = { -0.365062, 0.091490, 0.906873, -0.880990, 0.346249, 0.249187,
-0.909919, 0.380777, -0.413275, 0.060092, 0.927514, 0.639146,
-0.196918, 0.920130, -0.082374, 0.469304, -0.140821, -0.727598 };
cv::Mat P(3, 6, cv::DataType<double>::type, P_array);
double s_array[] = {0.000000, 0.000000, -0.65563, 0.000000, 0.70438, 0.000000};
cv::Mat s(6, 1, cv::DataType<double>::type, s_array);
cv::Mat_<double> x = P * s;
unsigned int sparsity = 2;
//cv::Mat sol = perform_IHT(P, x, sparsity, 0.0);
cv::Mat sol = perform_FISTA(P, x, 0.001);
std::cout << "sol: " << sol.t() << std::endl;
}
bool test_BSL()
{
unsigned int M = 80; // row number of the dictionary matrix
unsigned int N = 164; // column number
unsigned int blkNum = 7; // nonzero block number
unsigned int blkLen = 2; // block length
double SNR = 80; // Signal-to-noise ratio
cv::Mat Phi(M, N, cv::DataType<double>::type);
cv::randn(Phi, cv::Scalar(1.0), cv::Scalar(1.0));
cv::Mat PhiPhi, sumPhiPhi, sqrtsumPhiPhi;
cv::multiply(Phi, Phi, PhiPhi);
cv::reduce(PhiPhi, sumPhiPhi, 0, CV_REDUCE_SUM); //sum every column a reduce matrix to a single row
cv::sqrt(sumPhiPhi, sqrtsumPhiPhi);
cv::divide(Phi, cv::Mat::ones(M,1,cv::DataType<double>::type) * sqrtsumPhiPhi, Phi);
unsigned int totalBlkNumber = N/blkLen;
cv::Mat wgen = cv::Mat::zeros(totalBlkNumber, blkLen, cv::DataType<double>::type);
cv::RNG rng(cv::getTickCount());
for(unsigned int i=0; i<blkNum; ++i)
{
cv::Mat r(1, blkLen, cv::DataType<double>::type);
randn(r, cv::Mat(1, 1, cv::DataType<double>::type, cv::Scalar(rng.uniform(50.0, 100.0)) ), 0.01 * cv::Mat::ones(1, 1, cv::DataType<double>::type));
r.copyTo(wgen.row(i));
}
auto shuffleRows = [](const cv::Mat &matrix) -> cv::Mat
{
std::vector <int> seeds;
for (int cont = 0; cont < matrix.rows; cont++)
{
seeds.push_back(cont);
}
cv::theRNG() = cv::RNG( cv::getTickCount() );
cv::randShuffle(seeds);
cv::Mat output;
for (int cont = 0; cont < matrix.rows; cont++)
{
output.push_back(matrix.row(seeds[cont]));
}
return output;
};
cv::Mat nwgen = shuffleRows(wgen);
cv::Mat xgen = nwgen.reshape(0, nwgen.total());
cv::Mat y = Phi * xgen; //noiseless signal
// Observation noise
cv::Scalar mean, stddev;
cv::meanStdDev(y, mean, stddev);
cv::Mat noise(y.size(), y.type());
cv::randn(noise, cv::Scalar(0.0), cv::Scalar(stddev*std::pow(10,-SNR/20.0)));
double Phi_array[] = { 0.889288, 0.727865, -0.837231, -0.246046, -0.428118, -0.812367,
0.148014, 0.256824, -0.401575, 0.066427, -0.464477, -0.573846,
0.432734, 0.635809, -0.371190, -0.966979, 0.775227, 0.103731 };
cv::Mat nPhi(3, 6, cv::DataType<double>::type, Phi_array);
double x_array[] = {0.00000, 0.00000, 0.00000, 0.00000, -1.68713, -1.40636};
cv::Mat nx(6, 1, cv::DataType<double>::type, x_array);
double y_array[] = { 1.8648, 1.5907, -1.4538};
cv::Mat ny(3, 1, cv::DataType<double>::type, y_array);
//OptAlg
std::vector<double> gamma_v(3, 1.0);
std::cout << "blkLen: " << blkLen << std::endl;
std::cout << "nPhi: " << nPhi << std::endl;
std::cout << "ny: " << ny << std::endl;
cv::Mat res = perform_BSBL(nPhi, ny, NoiseLevel::Noiseless, gamma_v, blkLen);
//cv::Mat res2 = perform_IHT(nPhi, ny, 2);
/*
for(unsigned int i=0;i<1;++i)
{
//gamma_v = std::vector<double>(3, 1.0);
cv::Mat res = perform_BSBL(nPhi, nPhi*nx, NoiseLevel::Noiseless, gamma_v, 2);
//cv::Mat res2 = perform_IHT(nPhi, nPhi*(10e-4*nx), 2);
std::cout << "res: " << res.t() << std::endl;
//std::cout << "res2: " << res2.t() << std::endl;
}
*/
return true;
}
bool test_zernikes()
{
cv::Mat Z5;
double r_c(400.0);
int side_l(1000);
int z_max(10);
std::shared_ptr<Zernike> zrnk = std::make_shared<Zernike>(r_c, side_l, z_max);
unsigned int count_a(1);
for(auto za : zrnk->base())
{
unsigned int count_b(1);
for(auto zb : zrnk->base())
{
double za_l2 = cv::norm(za, cv::NORM_L2);
double zb_l2 = cv::norm(zb, cv::NORM_L2);
double inner_prod = za.dot(zb)/(za_l2*zb_l2);
std::cout << "inner_prod: " << count_a << " con " << count_b << " -> " << inner_prod << std::endl;
count_b++;
}
count_a++;
}
return true;
}
void test_covarianceMatrix()
{
/*
cv::Mat_<float> samples = (cv::Mat_<float>(4, 2) << 500.0, 500.0,
355.8, 355.8,
498.7, 498.7,
123.4, 123.4 );
*/
cv::theRNG() = cv::RNG( cv::getTickCount() );
cv::RNG& rng = cv::theRNG();
cv::Mat_<float> samples;
for(unsigned int i=0;i<100000000;++i)
{
float val1 = rng.uniform(0.0, 1000.0);
float val2 = rng.uniform(0.0, 1000.0);
cv::Mat s = ( cv::Mat_<float>(1, 2) << val1, 234.4 );
samples.push_back( s );
}
cv::Mat cov, mu;
cv::calcCovarMatrix(samples, cov, mu, CV_COVAR_NORMAL | CV_COVAR_ROWS);
cov = cov / (samples.rows - 1);
std::cout << "cov: " << std::endl;
std::cout << cov << std::endl;
std::cout << "mu: " << std::endl;
std::cout << mu << std::endl;
}
/*
void test_nonlinearCompressedSensing()
{
//Test compressed sensing technique over a non-linear measurement process by using the jacobian
std::function<double(cv::Mat)> fx = [] (cv::Mat x) -> double
{ //fx = u^3-v^2
return std::pow(x.at<double>(0,0),3) - std::pow(x.at<double>(1,0),2);
};
std::function<double(cv::Mat)> fy = [] (cv::Mat x) -> double
{ //fy = u^3+v^2
return std::pow(x.at<double>(0,0),3) + std::pow(x.at<double>(1,0),2);
};
std::function<cv::Mat(cv::Mat)> jf = [] (cv::Mat x) -> cv::Mat
{ //Jacobian matrix function: function derivative with every variable
cv::Mat t(2,2, cv::DataType<double>::type);
t.at<double>(0,0) = 3 * std::pow(x.at<double>(0,0),2); //dfx/du
t.at<double>(1,0) = 3 * std::pow(x.at<double>(0,0),2); //dfy/du
t.at<double>(0,1) = -2.0 * x.at<double>(1,0); //dfx/dv
t.at<double>(1,1) = 2.0 * x.at<double>(1,0); //dfy/dv
return t;
};
cv::Mat x0 = cv::Mat::ones(2, 1, cv::DataType<double>::type);
cv::Mat y = -(3*3);
}
*/
/*
bool test_nonsmoothConvexOptimization()
{
ConvexOptimization mm;
cv::Mat Q2 = cv::Mat::eye(2, 2, cv::DataType<double>::type);
std::function<double(cv::Mat)> absXplusAbsY = [] (cv::Mat x) -> double
{ //|x|+|y|
return std::abs(x.at<double>(0,0)) + std::abs(x.at<double>(1,0));
};
//Subdifferential version
std::function<cv::Mat(cv::Mat)> dAbsXplusAbsY = [] (cv::Mat x) -> cv::Mat
{ //Subdifferential vector function: subdifferential with every variable
cv::Mat t(2,1, cv::DataType<std::complex<double> >::type); //Size(2,1)->1 row, 2 colums
auto sign = [](double a, double b) -> double {return b >= 0.0 ? std::abs(a) : -std::abs(a);}; //Consider zero as positive sign
auto sign_ = [](double a, double b) -> double {return b > 0.0 ? std::abs(a) : -std::abs(a);}; //Consider zero as negative sign
double x0 = x.at<double>(0,0);
double x1 = x.at<double>(1,0);
double dx0_h = sign(1.0, x0);
double dx0_l = sign_(1.0, x0);
double dx1_h = sign(1.0, x1);
double dx1_l = sign_(1.0, x1);
if(dx0_h < dx0_l) std::swap(dx0_h, dx0_l);
t.at<std::complex<double> >(0,0) = std::complex<double>(dx0_l, dx0_h);
if(dx1_h < dx1_l) std::swap(dx1_h, dx1_l);
t.at<std::complex<double> >(1,0) = std::complex<double>(dx1_l, dx1_h);
if(dx0_h != dx0_l || dx1_h != dx1_l) std::cout << "nonsmooth point." << std::endl;
return t;
};
std::function<double(cv::Mat)> funcc = [] (cv::Mat xx) -> double
{ //|x-1|+|y-3|+x^2
double x = xx.at<double>(0,0);
double y = xx.at<double>(1,0);
return std::abs(x-1) + std::abs(y-3) + x*x;
};
std::function<cv::Mat(cv::Mat)> dfuncc = [] (cv::Mat xx) -> cv::Mat
{ //Subdifferential vector function: subdifferential with every variable
auto sign = [](double a, double b) -> double {return b >= 0.0 ? std::abs(a) : -std::abs(a);}; //Consider zero as positive sign
auto sign_ = [](double a, double b) -> double {return b > 0.0 ? std::abs(a) : -std::abs(a);}; //Consider zero as negative sign
cv::Mat t(2,1, cv::DataType<std::complex<double> >::type); //Size(2,1)->1 row, 2 colums
double x = xx.at<double>(0,0);
double y = xx.at<double>(1,0);
double dx_h = sign(1.0,x-1.0) + 2 * x;
double dx_l = sign_(1.0,x-1.0) + 2 * x;
double dy_h = sign(1.0, y-3.0);
double dy_l = sign_(1.0, y-3.0);
if(dx_h != dx_l || dy_h != dy_l) std::cout << "nonsmooth point." << std::endl;
if(dx_l>dx_h) std::swap(dx_l, dx_h);
t.at<std::complex<double> >(0,0) = std::complex<double>(dx_l, dx_h);
if(dy_l>dy_h) std::swap(dy_l, dy_h);
t.at<std::complex<double> >(1,0) = std::complex<double>(dy_l, dy_h);
return t;
};
std::function<double(cv::Mat)> onedim = [] (cv::Mat xx) -> double
{ //|x|
double x = xx.at<double>(0,0);
return std::abs(x-4);
};
std::function<cv::Mat(cv::Mat)> donedim = [] (cv::Mat xx) -> cv::Mat
{ //Subdifferential vector function: subdifferential with every variable
auto sign = [](double a, double b) -> double {return b >= 0.0 ? std::abs(a) : -std::abs(a);}; //Consider zero as positive sign
auto sign_ = [](double a, double b) -> double {return b > 0.0 ? std::abs(a) : -std::abs(a);}; //Consider zero as negative sign
cv::Mat t(1,1, cv::DataType<std::complex<double> >::type); //Size(2,1)->1 row, 2 colums
double x = xx.at<double>(0,0);
double dx_h = sign(1.0,x-4);
double dx_l = sign_(1.0,x-4);
if(dx_h != dx_l) std::cout << "nonsmooth point." << std::endl;
if(dx_l>dx_h) std::swap(dx_l, dx_h);
t.at<std::complex<double> >(0,0) = std::complex<double>(dx_l, dx_h);
return t;
};
cv::Mat p = cv::Mat::zeros(2, 1, cv::DataType<double>::type);// + 3.9 * cv::Mat::ones(2, 1, cv::DataType<double>::type);
p.at<double>(0,0) = -13;
p.at<double>(1,0) = -5;
//mm.minimize(p, Q2, funcc, dfuncc);
mm.minimize(p, Q2, absXplusAbsY, dAbsXplusAbsY);
std::cout << "p " << p.t() << std::endl;
std::cout << "fret " << mm.fret() << std::endl;
return true;
}
*/
bool test_convolveDFT_vs_crosscorrelation()
{
bool full(true), corr(true);
cv::Mat B1, B2;
cv::Mat A_real = createRandomMatrix<double>(5,5);
cv::Mat A_imag = createRandomMatrix<double>(5,5);
cv::Mat planes[2] = {A_real, A_imag};
cv::Mat A;
cv::merge(planes, 2, A);
convolveDFT(A, A, B1, full, corr);
//cv::copyMakeBorder(A, A, Top, Bottom, Left, Right, cv::BORDER_CONSTANT);
cv::copyMakeBorder(B1, B1, 1, 0, 1, 0, cv::BORDER_CONSTANT);
fftShift(B1);
B2 = crosscorrelation(A, A);
std::cout << "B1" << splitComplex(B1).first << std::endl;
std::cout << "------------------------------" << std::endl;
std::cout << "B2" << splitComplex(B2).first << std::endl;
return true;
}
/*
void test_udwd_spectrums()
{
cv::Mat dat, input;
readFITS("../inputs/pd.004.fits", dat);
dat.convertTo(input, cv::DataType<double>::type);
cv::Mat in = input(cv::Rect(cv::Point(0,0), cv::Size(450,450))).clone();
if(! in.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
//return -1;
}
std::vector<cv::Mat> output;
cv::Mat residu;
cv::Mat in_spec;
cv::dft(in, in_spec, cv::DFT_COMPLEX_OUTPUT + cv::DFT_SCALE);
// fftShift(in_spec);
swtSpectrums(in_spec, output, residu, 7);
cv::Mat wSum = cv::Mat::zeros(output.back().size(), output.back().type());
cv::Mat o_tmp;
for(cv::Mat it : output)
{
wSum += it;
}
wSum = wSum + residu;
cv::Mat wSum_measure;
// fftShift(wSum);
cv::idft(wSum, wSum_measure, cv::DFT_REAL_OUTPUT);
cv::Mat diff = in - wSum_measure;
writeFITS(wSum_measure, "../d1.fits");
std::cout << "Energy difference: " << std::sqrt(cv::sum(diff.mul(diff)).val[0]) << std::endl;
}
void test_generizedPupilFunctionVsOTF()
{
double diversityFactor_ = -2.21209;
constexpr double PI = 2*acos(0.0);
double c4 = diversityFactor_ * PI/(2.0*std::sqrt(3.0));
Zernike zrnk;
cv::Mat z4 = zrnk.phaseMapZernike(4, 128, 50);
double z4AtOrigen = z4.at<double>(z4.cols/2, z4.rows/2);
cv::Mat pupilAmplitude = zrnk.phaseMapZernike(1, 128, 50);
cv::Mat c = cv::Mat::zeros(14, 1, cv::DataType<double>::type);
c.at<double>(0,3) = 0.8;
c.at<double>(0,4) = 0.3;
c.at<double>(0,6) = 0.5;
c.at<double>(0,9) = 0.02;
cv::Mat c1 = cv::Mat::zeros(14, 1, cv::DataType<double>::type);
c1.at<double>(0,3) = 0.7;
c1.at<double>(0,5) = 0.23;
c1.at<double>(0,6) = 0.9;
c1.at<double>(0,10) = 0.42;
cv::Mat focusedPupilPhase = zrnk.phaseMapZernikeSum(128, 50, c);
cv::Mat focusedPupilPhase1 = zrnk.phaseMapZernikeSum(128, 50, c1);
cv::Mat defocusedPupilPhase = focusedPupilPhase + c4*(z4-z4AtOrigen);
cv::Mat defocusedPupilPhase1 = focusedPupilPhase1 + c4*(z4-z4AtOrigen);
Optics focusedOS(focusedPupilPhase, pupilAmplitude);
Optics focusedOS1(focusedPupilPhase1, pupilAmplitude);
Optics defocusedOS(defocusedPupilPhase, pupilAmplitude);
Optics defocusedOS1(defocusedPupilPhase1, pupilAmplitude);
//cv::Mat result = divComplex(focusedOS.otf(),defocusedOS.otf());
cv::Mat result = focusedOS.generalizedPupilFunction()-defocusedOS.generalizedPupilFunction();
cv::Mat result1 = focusedOS1.generalizedPupilFunction()-defocusedOS1.generalizedPupilFunction();
showComplex(result, "res", false, false);
showComplex(result1, "res1", false, false);
}
void test_wavelet_zernikes_decomposition()
{
cv::Mat input = cv::Mat_<double>(cv::imread("/home/dailos/workspace/fruits.jpg", CV_LOAD_IMAGE_GRAYSCALE));
cv::Mat in = input(cv::Rect(cv::Point(0,0), cv::Size(450,450))).clone();
if(! in.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
//return -1;
}
std::vector<cv::Mat> output;
cv::Mat residu;
udwd(in, output, residu, 7);
}
void test_zernike_wavelets_decomposition()
{
std::vector<cv::Mat> vCat;
Zernike zrnk;
std::map<unsigned int, cv::Mat> cat = zrnk.buildCatalog(20, 200, 200/2);
std::vector<cv::Mat> wavelet_planes;
cv::Mat residu;
unsigned int count(0);
for(std::pair<unsigned int, cv::Mat> i : cat )
{
if(count > 0)
{
wavelet_planes.clear();
udwd(i.second, wavelet_planes, residu, 9);
vCat.push_back(i.second);
vCat.insert( vCat.end(), wavelet_planes.begin(), wavelet_planes.end() );
vCat.push_back(residu);
}
++count;
}
cv::Mat can = makeCanvas(vCat, 70*13, 20);
cv::imshow("zer", can);
cv::waitKey();
}
void test_noiseFilter()
{
unsigned int ncols(5), nrows(5);
cv::Mat H = createRandomMatrix<double>(ncols,nrows);
double filter_upper_limit(4.0);
double filter_lower_limit(0.1);
std::cout << "H: " << std::endl << H << std::endl;
H.setTo(0, H < filter_lower_limit);
std::cout << "H set to 0 below lower: " << std::endl << H << std::endl;
H.setTo(filter_upper_limit, H > filter_upper_limit);
std::cout << "H set to upper above upper: " << std::endl << H << std::endl;
}
bool test_conjComplex()
{
unsigned int ncols(5), nrows(5);
cv::Mat A = makeComplex(createRandomMatrix<double>(ncols,nrows),createRandomMatrix<double>(ncols,nrows));
cv::Mat B;
std::cout << "Matrix A: " << std::endl;
std::cout << A << std::endl;
cv::mulSpectrums(A,conjComplex(A),B, cv::DFT_COMPLEX_OUTPUT);
std::cout << "A multiplied by A*: " << std::endl;
cv::Mat firstResult = (splitComplex(B)).first;
std::cout << firstResult << std::endl;
std::cout << "Squared modulus of A: " << std::endl;
cv::Mat secondResult = (absComplex(A)).mul(absComplex(A));
std::cout << secondResult << std::endl;
std::cout << "Per-element comparison between the two results: " << std::endl;
cv::Mat cmp = (firstResult == secondResult);
std::cout << cmp << std::endl;
return true;
}
void test_selectCentralROI()
{
cv::Mat o = cv::Mat::zeros(10, 20, cv::DataType<double>::type);
float m[] = { 1.0/16, 1.0/4, 3.0/8, 1.0/4, 1.0/16 };
cv::Mat kernelLoG(5, 1, CV_32F, m);
//cv::Mat kernel = kernelLoG * kernelLoG.t();
kernelLoG.copyTo(selectCentralROI(o, kernelLoG.size()));
//cv::dft(o, o, cv::DFT_COMPLEX_OUTPUT + cv::DFT_SCALE);
std::cout << o << std::endl;
}
void test_AWMLE()
{
cv::Mat psf, img, object;
readFITS("/home/dailos/workspace/psf_SAA_SR_51.fits", psf);
readFITS("/home/dailos/workspace/saturno_x_SR53_B_+_Gau01.0.fits", img);
cv::Mat psf_norm, img_norm;
cv::normalize(psf, psf_norm, 0, 1, CV_MINMAX);
cv::normalize(img, img_norm, 0, 1, CV_MINMAX);
cv::imshow("psf", psf_norm);
cv::imshow("img", img_norm);
cv::waitKey();
if(!img.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
//return -1;
}
double sigmaNoise = 1.0;
AWMLE(img_norm, psf_norm, object, sigmaNoise, 7);
}
void test_wavelets()
{
cv::Mat input = cv::Mat_<double>(cv::imread("/home/dailos/workspace/fruits.jpg", CV_LOAD_IMAGE_GRAYSCALE));
cv::Mat in = input(cv::Rect(cv::Point(0,0), cv::Size(450,450))).clone();
if(! in.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
//return -1;
}
std::vector<cv::Mat> output;
cv::Mat residu;
udwd(in, output, residu, 7);
//swt(in, output, residu, 7);
cv::Mat wSum = cv::Mat::zeros(output.back().size(), output.back().type());
cv::Mat o_tmp;
for(cv::Mat it : output)
{
wSum += it;
}
wSum = wSum + residu;
cv::Mat diff = in - wSum;
cv::normalize(wSum, wSum, 0, 1, CV_MINMAX);
cv::imshow("wavelet Sum", wSum);
cv::Mat can = makeCanvas(output, 300, 1);
cv::imshow("can", can);
cv::waitKey();
std::cout << "Energy difference: " << std::sqrt(cv::sum(diff.mul(diff)).val[0]) << std::endl;
}
void test_conv_flaw()
{
cv::Mat out;
double data[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 0.0};
int n = sizeof(data) / sizeof(data[0]);
cv::Mat row(n, 1, cv::DataType<double>::type, data);
cv::Mat kernel = cv::getGaussianKernel(3, -1);
std::cout << "kernel: " << kernel*kernel.t() << std::cout;
std::cout << "row*row: " << row*row.t() << std::endl;
conv_flaw(row*row.t(), kernel*kernel.t(), out);
std::cout << "out: " << out << std::endl;
}
void test_divComplex()
{
std::complex<float> c(73,12);
std::complex<float> d(67,9);
cv::Mat cMat(1,1,cv::DataType<std::complex<float> >::type);
cv::Mat dMat(1,1,cv::DataType<std::complex<float> >::type);
cMat.at<std::complex<float> >(0,0) = c;
dMat.at<std::complex<float> >(0,0) = d;
std::complex<float> r = c/d;
cv::Mat rMat = divComplex(cMat,dMat);
std::cout << "r: " << r << std::endl;
std::cout << "rMat: " << rMat << std::endl;
}
void test_phaseMapZernikeSum()
{
double data[] = {0, 0, 0, -0.2207034660012752, -0.2771620624276502, -0.451531165841092,
-0.3821562081878558, 0.275334782691961, 0.2975674509517756, 0.01384845253351654};
//double data[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5}; //0.5, 0.2, 0.4, 0.8, 0.7, 0.6};
int n = sizeof(data) / sizeof(data[0]);
cv::Mat coeffs(n, 1, CV_64FC1, data);
std::cout << "coeffs: " << coeffs << std::endl;
Zernike zrnk;
cv::Mat phaseMapSum = zrnk.phaseMapZernikeSum(136/2, 32.5019, coeffs);
std::cout << "phaseMapSum.at<double>(30,30): " << phaseMapSum.at<double>(40,40) << std::endl;
for(unsigned int i=4;i<=10;++i)
{
cv::Mat zern = zrnk.phaseMapZernike(i, 136/2, 32.5019);
cv::normalize(zern, zern, 0, 1, CV_MINMAX);
writeOnImage(zern, std::to_string(i));
cv::imshow(std::to_string(i), zern);
}
//cv::normalize(phaseMapSum, phaseMapSum, 0, 1, CV_MINMAX);
//cv::imshow("phaseMapSum", phaseMapSum);
cv::waitKey();
}
bool test_specular()
{
unsigned char data[8][8] = {1,2,3,4,5,4,3,2, 2,3,4,5,6,5,4,3, 3,4,5,6,7,6,5,4, 2,3,4,5,6,5,4,3};
cv::Mat a(8, 8, CV_8UC1, data);
cv::Mat b,c;
cv::flip(a,b,-1);
shift(b,c,1,1);
std::cout << "Specular: " << c << std::endl;
return true;
}
bool test_normalization()
{
unsigned char data[8][8] = {1,2,3,4,5,4,3,2, 2,3,4,5,6,5,4,3, 3,4,5,6,7,6,5,4, 2,3,4,5,6,5,4,3,
1,2,3,4,5,4,3,2, 2,3,4,5,6,5,4,3, 3,4,5,6,7,6,5,4, 2,3,4,5,6,5,4,3};
cv::Mat a(8, 8, CV_8UC1, data);
cv::Mat b = makeComplex(cv::Mat_<float>(a), createRandomMatrix<float>(8,8));
cv::Mat normB;
normComplex(b,normB);
std::cout << normB << std::endl;
return true;
}
void test_Optics()
{
unsigned char data[8][8] = {1,2,3,4,5,4,3,2, 2,3,4,5,6,5,4,3, 3,4,5,6,7,6,5,4, 2,3,4,5,6,5,4,3,
1,2,3,4,5,4,3,2, 2,3,4,5,6,5,4,3, 3,4,5,6,7,6,5,4, 2,3,4,5,6,5,4,3};
cv::Mat p(8, 8, CV_8UC1, data);
cv::Mat a = cv::Mat::ones(p.size(), p.type());
Optics os = Optics(cv::Mat_<float>(p),cv::Mat_<float>(a));
std::cout << "otf():" << std::endl;
std::cout << os.otf() << std::endl;
std::cout << "generalizedPupilFunction():" << std::endl;
std::cout << os.generalizedPupilFunction() << std::endl;
}
void test_Noise()
{
cv::Mat img = cv::Mat::zeros(100,100,CV_32F);
cv::theRNG() = cv::RNG( time (0) );
cv::Mat noise(img.size(), CV_32F);
cv::Scalar s_(20), m_(0);
cv::randn(noise, m_, s_);
cv::Mat blank(img.size(), CV_32F);
cv::Mat tmpImg(img.size(), CV_32F);
img.convertTo(tmpImg, CV_32F);
blank = cv::Mat::zeros(img.size(), CV_32F) + cv::Scalar(50) + noise;
//blank = tmpImg + noise;
//double mean, sigma;
NoiseEstimator estimateNoise;
estimateNoise.kSigmaClipping(blank);
//cout << "mean: " << mean << endl;
//cout << "sigma: " << sigma << endl;