00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <iostream>
00012 #include <string>
00013 #include<fstream>
00014
00015 #include "opencv/cv.h"
00016 #include "opencv/cxcore.h"
00017 #include "opencv/highgui.h"
00018
00019
00020 #include "options.h"
00021 #include "utils/libutils.h"
00022 #include "video/libvideo.h"
00023 #include "stats/libstats.h"
00024 #include "dytex/DytexOptions.hpp"
00025 #include "dytex/DytexRegOptions.hpp"
00026 #include "dytex/Dytex.hpp"
00027
00028
00029 Dytex::Dytex()
00030 {
00031 isempty=true;
00032 }
00033
00034
00035 Dytex::Dytex(const DytexOptions &opt) :
00036 dtopt(opt),
00037 Ymean( (dtopt.Yopt == DytexOptions::NONZERO_YMEAN ? dtopt.m : 0),
00038 (dtopt.Yopt == DytexOptions::NONZERO_YMEAN ? 1 : 0), OPT_MAT_TYPE),
00039 A(dtopt.n, dtopt.n, OPT_MAT_TYPE),
00040 C(dtopt.m, dtopt.n, OPT_MAT_TYPE),
00041 mu0(dtopt.n, 1, OPT_MAT_TYPE),
00042 R(dtopt.m, dtopt.Ropt),
00043 Q(dtopt.n, CovMatrix::COV_FULL),
00044 S0(dtopt.n, dtopt.Sopt),
00045 vrows(0), vcols(0)
00046 {
00047 isempty=false;
00048 isCvs=false;
00049 switch(dtopt.Yopt) {
00050 case DytexOptions::NONZERO_YMEAN:
00051 case DytexOptions::ZERO_YMEAN:
00052 break;
00053 default:
00054 CV_Error(-1, "bad Yopt");
00055 }
00056 }
00057
00058
00059
00060 Dytex::~Dytex() {
00061
00062 }
00063
00064
00065 void Dytex::setRegularizer(const DytexRegOptions &dtreg) {
00066 R.setRegularizer(dtreg.Ropt, dtreg.Rval);
00067 Q.setRegularizer(dtreg.Qopt, dtreg.Qval);
00068 S0.setRegularizer(dtreg.Sopt, dtreg.Sval);
00069 }
00070
00071 void Dytex::regularize(bool regA)
00072 {
00073 R.regularize();
00074 Q.regularize();
00075 S0.regularize();
00076
00077 if(regA)
00078 {
00079
00080 double target=0.999;
00081 Mat E;
00082
00083 E=eigc(A);
00084 Mat Eabs=eigc_abs(E);
00085 double minVal,maxVal;
00086 minMaxLoc(Eabs,&minVal,&maxVal);
00087 if(maxVal>target)
00088 {
00089 A=A*target/maxVal;
00090 }
00091 }
00092 }
00093
00094 void Dytex::regularizeA()
00095 {
00096
00097
00098 double target=0.999;
00099 Mat E;
00100
00101 E=eigc(A);
00102 Mat Eabs=eigc_abs(E);
00103 double minVal,maxVal;
00104 minMaxLoc(Eabs,&minVal,&maxVal);
00105 if(maxVal>target)
00106 {
00107 A=A*target/maxVal;
00108 }
00109
00110 }
00111
00112
00113
00114 void Dytex::processVideoTraining(const Mat &vin, Mat &Yout, Mat &Yout_mean) {
00115 Mat Ytmp;
00116
00117 switch(vin.dims) {
00118 case 3:
00119
00120 Ytmp = MatVid::vectorize(vin, true);
00121
00122
00123 if (vrows == 0 && vcols == 0) {
00124 vrows = vin.size[1];
00125 vcols = vin.size[2];
00126 } else {
00127 CV_Assert(vrows == vin.size[1] && vcols == vin.size[2]);
00128 }
00129 break;
00130
00131 case 2:
00132
00133 Ytmp = vin;
00134 break;
00135
00136 default:
00137 CV_Error(-1, "Unsupported video dims (color)");
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147 if (Ytmp.type() != OPT_MAT_TYPE) {
00148
00149 Ytmp.convertTo(Yout, OPT_MAT_TYPE);
00150 } else {
00151 Ytmp.copyTo(Yout);
00152 }
00153
00154
00155 switch (dtopt.Yopt) {
00156 case DytexOptions::NONZERO_YMEAN:
00157
00158 reduce(Yout, Yout_mean, 1, CV_REDUCE_AVG);
00159
00160 for (int t=0; t<Yout.cols; t++)
00161 Yout.col(t) -= Yout_mean;
00162 break;
00163
00164 case DytexOptions::ZERO_YMEAN:
00165
00166 break;
00167 default:
00168 CV_Error(-1, "bad Yopt");
00169 }
00170 }
00171
00172
00173 void Dytex::processVideoTesting(const Mat &vin, Mat &Yout) const {
00174 Mat Ytmp;
00175
00176 switch(vin.dims) {
00177 case 3:
00178
00179 if (!(vrows == 0 && vcols == 0))
00180 CV_Assert(vrows == vin.size[1] && vcols == vin.size[2]);
00181
00182 Ytmp = MatVid::vectorize(vin, true);
00183 break;
00184
00185 case 2:
00186
00187 Ytmp = vin;
00188 break;
00189
00190 default:
00191 CV_Error(-1, "Unsupported video dims (color)");
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201 if (Ytmp.type() != OPT_MAT_TYPE) {
00202
00203 Ytmp.convertTo(Yout, OPT_MAT_TYPE);
00204 } else {
00205 Ytmp.copyTo(Yout);
00206 }
00207
00208
00209 switch (dtopt.Yopt) {
00210 case DytexOptions::NONZERO_YMEAN:
00211
00212 for (int t=0; t<Yout.cols; t++)
00213 Yout.col(t) -= Ymean;
00214 break;
00215
00216 case DytexOptions::ZERO_YMEAN:
00217
00218 break;
00219 default:
00220 CV_Error(-1, "bad Yopt");
00221 }
00222 }
00223
00224
00225
00226 void Dytex::learnLeastSquares(const vector<Mat> &videos) {
00227
00228 int nump = videos.size();
00229 int* tau = (int*) malloc( sizeof(int) * nump );
00230
00231 int* inds = (int*) malloc( sizeof(int) * ( nump + 1 ) );
00232
00233
00234
00235 int tauall;
00236
00237 inds[0] = 0;
00238
00239
00240 for (int p=0; p<nump; p++) {
00241 int mytau = 0;
00242 if (videos[p].dims == 3)
00243 mytau = videos[p].size[0];
00244 else if (videos[p].dims == 2)
00245 mytau = videos[p].cols;
00246 else
00247 CV_Error(-1, "unknown size");
00248
00249 tau[p] = mytau;
00250 inds[p+1] = inds[p]+mytau;
00251 }
00252 tauall = inds[nump];
00253
00254
00255
00256
00257
00258 Mat Yall(dtopt.m, tauall, OPT_MAT_TYPE);
00259 Mat tmp1;
00260
00261
00262
00263
00264 if (dtopt.Yopt == DytexOptions::NONZERO_YMEAN)
00265 Ymean = 0.0;
00266
00267
00268 for (int p=0; p<nump; p++) {
00269 Mat Yall_p = Yall.colRange(inds[p], inds[p+1]);
00270
00271
00272 processVideoTraining(videos[p], Yall_p, tmp1);
00273
00274
00275 if (dtopt.Yopt == DytexOptions::NONZERO_YMEAN)
00276 Ymean += tmp1;
00277 }
00278
00279
00280 if (dtopt.Yopt == DytexOptions::NONZERO_YMEAN) {
00281 Ymean /= nump;
00282 }
00283
00284
00285 if (dtopt.n == 0) {
00286
00287 R.calculate(Yall, true);
00288 free(tau);
00289 free(inds);
00290 return;
00291 }
00292
00293
00294 Mat Xall;
00295 if (Yall.rows > Yall.cols) {
00296
00297
00298 SVD svd(Yall);
00299 svd.u.colRange(0,dtopt.n).copyTo(C);
00300
00301
00302
00303
00304 svd.vt.rowRange(0, dtopt.n).copyTo(Xall);
00305 Mat tmpw = svd.w.rowRange(0, dtopt.n);
00306 scaleRows(Xall, tmpw);
00307
00308
00309
00310
00311 } else {
00312
00313
00314
00315 Mat tmp;
00316 mulTransposed(Yall, tmp, false);
00317 SVD svd(tmp);
00318 svd.u.colRange(0,dtopt.n).copyTo(C);
00319
00320
00321
00322 Xall = C.t()*Yall;
00323 }
00324
00325
00326 if (nump == 1) {
00327
00328 Xall.col(0).copyTo(mu0);
00329 S0.mtx = 0.0;
00330
00331 } else {
00332
00333 Mat Xall0(dtopt.n, nump, OPT_MAT_TYPE);
00334 for (int p=0; p<nump; p++) {
00335 Mat Xall0c = Xall0.col(p);
00336 Xall.col(inds[p]).copyTo(Xall0c);
00337 }
00338
00339 reduce(Xall0, mu0, 1, CV_REDUCE_AVG);
00340
00341 for (int p=0; p<nump; p++)
00342 Xall0.col(p) -= mu0;
00343 S0.calculate(Xall0, true);
00344 }
00345
00346
00347 Mat Phi1all;
00348 Mat Phi2all;
00349
00350 if (nump == 1) {
00351
00352 Phi1all = Xall.colRange(0, Xall.cols-1);
00353 Phi2all = Xall.colRange(1, Xall.cols);
00354
00355 } else {
00356
00357
00358 Phi1all.create(dtopt.n, tauall-nump, OPT_MAT_TYPE);
00359 Phi2all.create(dtopt.n, tauall-nump, OPT_MAT_TYPE);
00360
00361
00362
00363
00364
00365 for (int p=0; p<nump; p++) {
00366 Mat Phi1 = Xall.colRange(inds[p], inds[p+1]-1);
00367 Mat pPhi1all = Phi1all.colRange(inds[p]-p, inds[p+1]-(p+1));
00368
00369
00370 Phi1.copyTo(pPhi1all);
00371
00372 Mat Phi2 = Xall.colRange(inds[p]+1, inds[p+1]);
00373 Mat pPhi2all = Phi2all.colRange(inds[p]-p, inds[p+1]-(p+1));
00374
00375
00376 Phi2.copyTo(pPhi2all);
00377 }
00378 }
00379
00380 Mat iPhi1all;
00381 invert(Phi1all, iPhi1all, DECOMP_SVD);
00382 A = Phi2all*iPhi1all;
00383
00384
00385 Mat V = Phi2all - A*Phi1all;
00386 Q.calculate(V, false);
00387
00388
00389 Mat errory = Yall - C*Xall;
00390 R.calculate(errory, true);
00391
00392
00393 free(tau);
00394 free(inds);
00395 }
00396
00397
00398 void Dytex::learnLeastSquares(const Mat &video) {
00399 vector<Mat> videos(1);
00400 videos[0] = video;
00401 learnLeastSquares(videos);
00402 }
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519 Mat Dytex::synthesize(RNG &rng, int tau, bool video_output, bool display_video, double fps, double alpha, double beta) {
00520
00521
00522
00523
00524 int t = 0;
00525 Mat Xtc = Mat(dtopt.n, 1, OPT_MAT_TYPE);
00526 Mat Xtp = Mat(dtopt.n, 1, OPT_MAT_TYPE);
00527 Mat rvn = Mat(dtopt.n, 1, OPT_MAT_TYPE);
00528 Mat Ytc = Mat(dtopt.m, 1, OPT_MAT_TYPE);
00529
00530
00531 Mat Yout;
00532 Mat u8Y;
00533 if (!display_video) {
00534 CV_Assert(tau>=0);
00535 if (video_output) {
00536 CV_Assert(vrows != 0 && vcols != 0);
00537 Yout = MatVid::create(tau, vrows, vcols, OPT_MAT_TYPE);
00538 } else {
00539 Yout = Mat(dtopt.m, tau, OPT_MAT_TYPE);
00540 }
00541 } else {
00542 CV_Assert(vrows != 0 && vcols != 0);
00543 namedWindow("Dytex::synthesize", CV_WINDOW_AUTOSIZE);
00544 cvMoveWindow("Dytex::synthesize", 100, 100);
00545 u8Y = Mat(vrows, vcols, CV_8UC1);
00546 }
00547
00548
00549 while( (tau < 0) || (t < tau) ) {
00550
00551 if (t == 0) {
00552
00553 mu0.copyTo(Xtc);
00554 S0.randvec(rvn, rng);
00555 Xtc += rvn;
00556
00557 } else {
00558
00559 Q.randvec(rvn, rng);
00560 Xtc = A*Xtp + rvn;
00561 }
00562
00563
00564 Ytc = C*Xtc;
00565 if (dtopt.Yopt == DytexOptions::NONZERO_YMEAN)
00566 Ytc += Ymean;
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577 if (display_video) {
00578
00579 Ytc.convertTo(u8Y, CV_8UC1, alpha, beta);
00580
00581 Mat cframe = MatVid::unvectorize1(u8Y, vrows, vcols);
00582
00583
00584
00585 imshow("Dytex::synthesize", cframe);
00586 waitKey((int)(1000.0/fps));
00587
00588 } else {
00589
00590
00591 if (video_output) {
00592
00593 Mat fY = MatVid::unvectorize1(Ytc, vrows, vcols);
00594 Mat myf = MatVid::frame(Yout, t);
00595 fY.copyTo(myf);
00596
00597 } else {
00598
00599 Mat Youtc = Yout.col(t);
00600 Ytc.copyTo(Youtc);
00601 }
00602 }
00603
00604
00605 Xtc.copyTo(Xtp);
00606 t++;
00607 }
00608
00609 return Yout;
00610 }
00611
00612
00613
00614
00615
00616
00617 ostream& operator<<(ostream& output, const Dytex& dt) {
00618
00619 dt.dump(output);
00620 return output;
00621 }
00622
00623 void Dytex::dump_matlab(ostream &output) const {
00624 output << "clear dt;\n";
00625 output << "dt.Ymean=" << Ymean << ";\n";
00626 output << "dt.Ahat=" << A << ";\n";
00627 output << "dt.Chat=" << C << ";\n";
00628 output << "dt.mu0=" << mu0 << ";\n";
00629 output << "dt.Rhat=" << R.mtx << ";\n";
00630 output << "dt.Qhat=" << Q.mtx << ";\n";
00631 output << "dt.S0=" << S0.mtx << ";\n";
00632 }
00633
00634 void Dytex::dump(ostream& output) const {
00635 output << "Dytex ({" << dtopt << "}; ";
00636 output << "Ymean[" << Ymean.rows << "x" << Ymean.cols << "]; ";
00637 output << "A[" << A.rows << "x" << A.cols << "]; ";
00638 output << "C[" << C.rows << "x" << C.cols << "]; ";
00639 output << "mu0[" << mu0.rows << "x" << mu0.cols << "]; ";
00640 output << "R[" << R << "]; ";
00641 output << "Q[" << Q << "]; ";
00642 output << "S0[" << S0 << "]";
00643 output << ")";
00644 }
00645
00646
00647
00648
00649