00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <iostream>
00013 #include <string>
00014 #include<fstream>
00015
00016 #include "opencv/cv.h"
00017 #include "opencv/cxcore.h"
00018 #include "opencv/highgui.h"
00019
00020
00021 #include "options.h"
00022 #include "utils/libutils.h"
00023 #include "video/libvideo.h"
00024 #include "platInd/platInd.hpp"
00025
00026
00027 using namespace cv;
00028 using namespace std;
00029
00030
00031 DatDescriptor::DatDescriptor()
00032 {
00033 data_type = "";
00034 byte_order = "";
00035 data_sets = 0.0;
00036 channels = 0.0;
00037 dimensions.clear();
00038 feature = "";
00039 history="";
00040 }
00041
00042 Mat MatVid::create(int frames, int rows, int cols, int type) {
00043 int sz[] = {frames, rows, cols};
00044 return Mat(3, sz, type);
00045 }
00046
00047
00048 Mat MatVid::readseq(const string &filename, int flags, int fmin, int fmax, int fstep) {
00049
00050 Mat vid;
00051 int filename_length = filename.size();
00052
00053 char* tmpstr = (char*) malloc( sizeof(char) * (filename_length + 100) );
00054
00055 int nframes = (fmax-fmin) / fstep + 1;
00056 int curf= 0;
00057
00058 cout << "loading <" << filename << "> from ["
00059 << fmin << ":" << fstep << ":" << fmax << "] = " << nframes << "\n";
00060
00061
00062
00063
00064 for (int f=fmin; f<=fmax; f+=fstep) {
00065
00066 sprintf(tmpstr, filename.c_str(), f);
00067
00068
00069 Mat cframe = imread(tmpstr, 0);
00070 if (cframe.empty())
00071 CV_Error(-1, "could not find file");
00072
00073 if (f==fmin) {
00074
00075
00076
00077
00078 vid = MatVid::create(nframes, cframe.rows, cframe.cols, CV_8UC1);
00079
00080
00081
00082
00083 }
00084
00085
00086 Mat myFrame = MatVid::frame(vid, curf++);
00087 cframe.copyTo(myFrame);
00088
00089
00090
00091 }
00092
00093
00094
00095 free(tmpstr);
00096 return vid;
00097 }
00098
00099
00100 Mat MatVid::readavi(VideoCapture & vc, int nframes, int fstep, double scale) {
00101
00102 cout << "readavi (nframes=" << nframes << ")";
00103
00104 if (!vc.isOpened())
00105 CV_Error(-1, "video not opened");
00106
00107
00108 int rows = (int)vc.get(CV_CAP_PROP_FRAME_HEIGHT);
00109 int cols = (int)vc.get(CV_CAP_PROP_FRAME_WIDTH);
00110 Mat vid;
00111 Mat cframe, cframe2, cframe3;
00112
00113 cout << "[" << rows << " x " << cols << "] -->";
00114
00115 for (int f=0; f<nframes; f++) {
00116
00117 vc >> cframe;
00118
00119
00120 if (cframe.channels() == 3) {
00121 cvtColor(cframe, cframe2, CV_BGR2GRAY);
00122 } else {
00123 cframe2 = cframe;
00124 }
00125
00126
00127 if (scale != 1.0) {
00128 resize(cframe2, cframe3, Size(), scale, scale, INTER_LINEAR);
00129 } else {
00130 cframe3 = cframe2;
00131 }
00132
00133 if (f==0) {
00134
00135 vid = MatVid::create(nframes, cframe3.size[0], cframe3.size[1], CV_8UC1);
00136 cout << "[" << nframes << " x " << cframe3.size[0] << " x " << cframe3.size[1] << "]";
00137 }
00138
00139
00140 Mat myFrame = MatVid::frame(vid, f);
00141 cframe3.copyTo(myFrame);
00142
00143
00144 for (int ff=1; ff<fstep; ff++)
00145 vc >> cframe;
00146 }
00147
00148 return vid;
00149 }
00150
00151 Mat MatVid::subvid(const Mat &vid, const Range &frange, const Range &rrange, const Range &crange) {
00152 if (frange != Range::all()) {
00153 CV_Assert(frange.start >= 0 && frange.start < vid.size[0]);
00154 CV_Assert(frange.end > 0 && frange.end <= vid.size[0]);
00155 }
00156 if (rrange != Range::all()) {
00157 CV_Assert(rrange.start >= 0 && rrange.start < vid.size[1]);
00158 CV_Assert(rrange.end > 0 && rrange.end <= vid.size[1]);
00159 }
00160 if (crange != Range::all()) {
00161 CV_Assert(crange.start >= 0 && crange.start < vid.size[2]);
00162 CV_Assert(crange.end > 0 && crange.end <= vid.size[2]);
00163 }
00164
00165 Range ra[3] = { frange, rrange, crange };
00166 return Mat(vid, ra);
00167 }
00168
00169 Mat MatVid::subvid(const Mat &vid, int fmin, int fmax, int rmin, int rmax, int cmin, int cmax) {
00170 return subvid(vid, Range(fmin, fmax), Range(rmin, rmax), Range(cmin, cmax));
00171 }
00172
00173
00174 Mat MatVid::repeat(const Mat &img, int nf) {
00175 Mat v;
00176 CV_Assert(nf>0);
00177 v = create(nf, img.rows, img.cols, img.type());
00178 for (int j=0; j<nf; j++) {
00179 Mat f = frame(v, j);
00180 img.copyTo(f);
00181 }
00182 return v;
00183 }
00184
00185
00186
00187 void MatVid::reduce(const Mat &vid, Mat &out, int reduceOp) {
00188 if ((reduceOp == CV_REDUCE_SUM) || (reduceOp == CV_REDUCE_AVG)) {
00189 out.create(vid.size[1], vid.size[2], OPT_MAT_TYPE);
00190 out = 0.0;
00191 } else {
00192 out.create(vid.size[1], vid.size[2], vid.type());
00193 }
00194
00195 Mat vtmp;
00196
00197 for (int z=0; z<vid.size[0]; z++) {
00198 Mat vbz = frame(vid, z);
00199 switch (reduceOp) {
00200 case CV_REDUCE_SUM:
00201 case CV_REDUCE_AVG:
00202 if (vbz.type() == out.type())
00203 out += vbz;
00204 else {
00205 vbz.convertTo(vtmp, out.type());
00206 out += vtmp;
00207 }
00208 break;
00209 default:
00210 CV_Error(-1, "bad option, or unimplemented!");
00211 }
00212 }
00213
00214 if (reduceOp == CV_REDUCE_AVG)
00215 out /= (OPT_F_TYPE)vid.size[0];
00216
00217 }
00218
00219 double MatVid::norm2(const Mat &vid) {
00220 CV_Assert(vid.dims == 3);
00221 CV_Assert(vid.type() == OPT_MAT_TYPE);
00222
00223 double tmp = 0.0;
00224 for (int z=0; z<vid.size[0]; z++)
00225 for (int y=0; y<vid.size[1]; y++)
00226 for (int x=0; x<vid.size[2]; x++) {
00227 double p = vid.at<OPT_F_TYPE>(z, y, x);
00228 tmp += p*p;
00229 }
00230 return tmp;
00231 }
00232
00233 Mat MatVid::frame(const Mat &vid, int f) {
00234
00235
00236 CV_Assert(vid.dims == 3);
00237 CV_Assert((f>=0) && (f<vid.size[0]));
00238 Mat myf = Mat(vid.size[1], vid.size[2], vid.type(),
00239 vid.data+vid.step[0]*f, vid.step[1]);
00240
00241
00242 return myf;
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 }
00268
00269 Mat MatVid::vectorize(const Mat &vid, bool flag_col) {
00270 CV_Assert(vid.dims == 3);
00271
00272
00273 Mat vv;
00274
00275
00276 if (flag_col) {
00277 vv.create(vid.size[1]*vid.size[2], vid.size[0], vid.type());
00278
00279
00280
00281
00282 for (int f=0; f<vid.size[0]; f++) {
00283 Mat myframe = MatVid::frame(vid, f);
00284
00285
00286 for (int y=0; y<myframe.cols; y++) {
00287
00288 Mat vvcol(vv, Range(y*vid.size[1], (y+1)*vid.size[1]), Range(f, f+1));
00289
00290
00291
00292 Mat mycol = myframe.col(y);
00293
00294
00295
00296 mycol.copyTo(vvcol);
00297 }
00298 }
00299
00300 } else {
00301 CV_Error(-1, "not implemented");
00302 }
00303
00304 return vv;
00305 }
00306
00307 Mat MatVid::unvectorize(const Mat &m, int rows, int cols) {
00308 CV_Assert(rows*cols == m.rows);
00309
00310 Mat vid = MatVid::create(m.cols, rows, cols, m.type());
00311
00312
00313
00314 for (int f=0; f<m.cols; f++) {
00315 Mat myframe = MatVid::frame(vid, f);
00316
00317
00318
00319 for (int y=0; y<cols; y++) {
00320 Mat mc(m, Range(y*rows, (y+1)*rows), Range(f, f+1));
00321 Mat myframec = myframe.col(y);
00322
00323
00324 mc.copyTo(myframec);
00325 }
00326 }
00327
00328 return vid;
00329 }
00330
00331
00332 Mat MatVid::vectorize1(const Mat &m, bool flag_col) {
00333 CV_Assert(m.dims == 2);
00334
00335 Mat v(m.rows*m.cols, 1, m.type());
00336
00337
00338
00339
00340 if (flag_col) {
00341
00342
00343 for (int y=0; y<m.cols; y++) {
00344
00345 Mat vcol = v.rowRange(y*m.rows, (y+1)*m.rows);
00346
00347
00348
00349 Mat mycol = m.col(y);
00350
00351
00352
00353 mycol.copyTo(vcol);
00354 }
00355
00356 } else {
00357 CV_Error(-1, "not implemented");
00358 }
00359
00360
00361 return v;
00362 }
00363
00364
00365 Mat MatVid::unvectorize1(const Mat &v, int rows, int cols) {
00366 CV_Assert(v.cols == 1);
00367 CV_Assert(v.rows == rows*cols);
00368
00369
00370
00371
00372 Mat myframe(rows, cols, v.type());
00373
00374
00375 for (int y=0; y<cols; y++) {
00376 Mat vc = v.rowRange(y*rows, (y+1)*rows);
00377 Mat myframec = myframe.col(y);
00378
00379
00380 vc.copyTo(myframec);
00381 }
00382
00383 return myframe;
00384 }
00385
00386
00387
00388 void MatVid::show(const Mat &vid, double fps, double alpha, double beta) {
00389 namedWindow("MatVid::show", CV_WINDOW_AUTOSIZE);
00390 cvMoveWindow("MatVid::show", 100, 100);
00391
00392 for (int p=0; p<vid.size[0]; p++) {
00393 Mat cframe = MatVid::frame(vid, p);
00394 if (cframe.type() == OPT_MAT_TYPE) {
00395 Mat cframei;
00396 cframe.convertTo(cframei, CV_8UC1, alpha, beta);
00397 imshow("MatVid::show", cframei);
00398 } else {
00399 imshow("MatVid::show", cframe);
00400 }
00401 waitKey((int)(1000.0/fps));
00402 }
00403 }
00404
00405
00406
00407
00408
00409
00410 Mat MatVid::montage(const vector<Mat> &vids, int sizex, int spacing, const Scalar &fill, int border) {
00411 int sizey = (int)ceil(((double)vids.size()) / sizex);
00412
00413 CV_Assert(vids[0].dims == 3);
00414
00415
00416 int mframes = vids[0].size[0];
00417 int mrows = vids[0].size[1];
00418 int mcols = vids[0].size[2];
00419 int mtype = vids[0].type();
00420 for (int i=1; i<(int)vids.size(); i++) {
00421 CV_Assert(vids[i].dims == 3);
00422 if (vids[i].size[0] > mframes) mframes = vids[i].size[0];
00423 if (vids[i].size[1] > mrows) mrows = vids[i].size[1];
00424 if (vids[i].size[2] > mcols) mcols = vids[i].size[2];
00425 CV_Assert(mtype == vids[i].type());
00426 }
00427
00428 int montcols = sizex*mcols + (sizex-1)*spacing + border*2;
00429 int montrows = sizey*mrows + (sizey-1)*spacing + border*2;
00430
00431 cout << "montage: (" << sizex << "," << sizey << ") videos; " <<
00432 "video dims=(" << mframes << "," << mrows << "," << mcols << "); --> ";
00433 cout << "(" << montrows << "," << montcols << ");\n";
00434
00435
00436 Mat mont = MatVid::create(mframes, montrows, montcols, mtype);
00437 mont = fill;
00438
00439
00440 int ix = 0;
00441 int iy = 0;
00442 for (int i=0; i<(int)vids.size(); i++) {
00443
00444 int curx = border + ix*(mcols+spacing);
00445 int cury = border + iy*(mrows+spacing);
00446
00447
00448 Mat montpatch = MatVid::subvid(mont, 0, vids[i].size[0],
00449 cury, cury+vids[i].size[1],
00450 curx, curx+vids[i].size[2]);
00451 vids[i].copyTo(montpatch);
00452
00453
00454 ix++;
00455 if (ix >= sizex) {
00456 ix = 0;
00457 iy++;
00458 }
00459 }
00460
00461 return mont;
00462 }
00463
00464 void MatVid::dump_matlab(const Mat &vid, ostream &output) {
00465 output << "cat(3, ";
00466 for (int p=0; p<vid.size[0]; p++) {
00467 Mat cframe = MatVid::frame(vid, p);
00468 if (p>0)
00469 output << ", ";
00470 output << cframe;
00471 }
00472 output << ");\n";
00473 }
00474
00475
00476
00477
00478 Mat MatVid::loaddat(string filename, string opt)
00479 {
00480 Mat output;
00481 Mat *chframe;
00482
00483
00484 DatDescriptor desc=loaddatinfo(filename,checkOptionInStr(opt,string("d")));
00485
00486
00487 int rows=(int)desc.dimensions[0];
00488 int cols=(int)desc.dimensions[1];
00489 int chns=(int)desc.channels;
00490 int tframes=(int)desc.data_sets;
00491 int elbytes=0;
00492 int sz[]={tframes,rows,cols};
00493
00494 chframe=new Mat[chns];
00495
00496 if(desc.data_type.compare("unsigned_1")==0)
00497 {
00498 elbytes=1;
00499 for(int i=0;i<chns;i++)
00500 chframe[i].create(rows,cols,CV_8UC1);
00501 }
00502 else
00503 {
00504 elbytes=8;
00505 for(int i=0;i<chns;i++)
00506 chframe[i].create(rows,cols,CV_64FC1);
00507 }
00508
00509 if( (desc.data_type.compare("unsigned_1")==0) && checkOptionInStr(opt,"t") )
00510 {
00511 output.create(3,sz,CV_8UC(chns));
00512 }
00513 else
00514 {
00515 output.create(3,sz,CV_64FC(chns));
00516 }
00517
00518
00519
00520 filename.append("/data");
00521
00522
00523 ifstream ifile (filename.c_str(), ios::in|ios::binary|ios::ate);
00524
00525 Mat temp;
00526 if (ifile.is_open())
00527 {
00528 ifile.seekg (0, ios::beg);
00529 for(int i=0;i<desc.data_sets;i++)
00530 {
00531 for(int j=0;j<chns;j++)
00532 {
00533 ifile.read ((char*)chframe[j].data, rows*cols*elbytes);
00534 }
00535
00536 merge(chframe,chns,temp);
00537 int abc=temp.channels();
00538
00539 if( (output.depth()==CV_64F) && (temp.depth()==CV_8U) )
00540 temp.convertTo(temp,CV_64FC(chns));
00541
00542 Mat fptr;
00543 fptr=MatVid::frame(output,i);
00544 temp.copyTo(fptr);
00545 }
00546 }
00547
00548 ifile.close();
00549
00550 return output;
00551 }
00552
00553
00554 int MatVid::savedat(string filename,const Mat &input,string ftype,string history,string opt)
00555 {
00556
00557
00558 DatDescriptor desc;
00559 desc.data_type=ftype;
00560 desc.byte_order="lsb";
00561 desc.data_sets=input.size[0];
00562 desc.channels=(double)input.channels();
00563 desc.dimensions.push_back((double)input.size[1]);
00564 desc.dimensions.push_back((double)input.size[2]);
00565 desc.history=history;
00566
00567
00568 PlatInd::makedir(filename);
00569
00570
00571 savedatinfo(filename,desc);
00572
00573
00574
00575
00576
00577 int rows=(int)desc.dimensions[0];
00578 int cols=(int)desc.dimensions[1];
00579 int chns=(int)desc.channels;
00580 int tframes=(int)desc.data_sets;
00581 int elbytes=0;
00582
00583 if(desc.data_type.compare("unsigned_1")==0)
00584 {
00585 elbytes=1;
00586 }
00587 else
00588 {
00589 elbytes=8;
00590 }
00591
00592
00593 filename.append("/data");
00594
00595
00596 ofstream ofile (filename.c_str(), ios::out|ios::binary);
00597
00598
00599
00600 if (ofile.is_open())
00601 {
00602
00603 for(int i=0;i<desc.data_sets;i++)
00604 {
00605
00606 Mat temp,temp1;
00607
00608 MatVid::frame(input,i).copyTo(temp);
00609
00610 if( (temp.depth()==CV_8U) && (desc.data_type.compare("double")==0) )
00611 {
00612 temp.convertTo(temp1,CV_64F);
00613 }
00614 else if( (temp.depth()==CV_64F) && (desc.data_type.compare("unsigned_1")==0) )
00615 {
00616 temp.convertTo(temp1,CV_8U);
00617 }
00618 else
00619 {
00620 temp1=temp;
00621 }
00622
00623
00624 Mat *chmat;
00625 chmat=new Mat[chns];
00626 split(temp1,chmat);
00627
00628 for(int j=0;j<chns;j++)
00629 {
00630 ofile.write ((char*)chmat[j].data, rows*cols*elbytes);
00631 }
00632 temp1.release();
00633
00634
00635 }
00636
00637 ofile.close();
00638
00639
00640 }
00641 cout<<"return"<<endl;
00642 return 0;
00643 }
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729 void MatVid::savedatinfo(string filename, DatDescriptor desc)
00730 {
00731 filename.append("/descriptor");
00732 ofstream dfile(filename.c_str());
00733 if(dfile.is_open())
00734 {
00735 dfile<<"(_data_type "<<desc.data_type<<")"<<endl;
00736 dfile<<"(_byte_order "<<desc.byte_order<<")"<<endl;
00737 dfile<<"(_data_sets "<<desc.data_sets<<")"<<endl;
00738 dfile<<"(_channels "<<desc.channels<<")"<<endl;
00739 dfile<<"(_dimensions "<<desc.dimensions[0]<<" "<<desc.dimensions[1]<<" )"<<endl;
00740 if(desc.history.length()>0)
00741 {
00742 dfile<<"(_history "<<desc.history<<")"<<endl;
00743 }
00744 }
00745 else
00746 {
00747 CV_Error(-1, "Unable to open descriptor file");
00748 }
00749 dfile.close();
00750 }
00751
00752
00753 DatDescriptor MatVid::loaddatinfo(string filename, int verbose)
00754 {
00755
00756 DatDescriptor desc;
00757 filename.append("/descriptor");
00758 ifstream dfile(filename.c_str());
00759
00760
00761 if (dfile.is_open())
00762 {
00763
00764 while ( dfile.good() )
00765 {
00766 string line,str,key,valuestr,temp,temp2;
00767 vector<string> value;
00768
00769 getline(dfile,line);
00770
00771 if(line=="")
00772 continue;
00773
00774 if( (line[0]=='(') && (line[line.length()-1]==')') )
00775 {
00776 str=line.substr(1,line.length()-2);
00777 strtok2(str," ",key,valuestr);
00778 while(1)
00779 {
00780 strtok2(valuestr, " ",temp,temp2);
00781 value.push_back(temp);
00782 if ( temp2.length() == 0 )
00783 {
00784 break;
00785 }
00786 valuestr = temp2;
00787 }
00788
00789 if ( key.compare("_data_type")==0)
00790 desc.data_type = value[0];
00791 else if ( key.compare("_byte_order")==0)
00792 desc.byte_order = value[0];
00793 else if ( key.compare("_data_sets")==0 )
00794 desc.data_sets = strtod(value[0].c_str(),NULL);
00795 else if ( key.compare("_channels")==0 )
00796 desc.channels = strtod(value[0].c_str(),NULL);
00797 else if ( key.compare("_dimensions")==0 )
00798 {
00799 desc.dimensions.push_back(strtod(value[0].c_str(),NULL));
00800 desc.dimensions.push_back(strtod(value[1].c_str(),NULL));
00801 }
00802 else if ( key.compare("_feature")==0 )
00803 desc.feature = value[0];
00804 else
00805 cout<<"loaddat:badkey: "<<"unrecognized descriptor key: "<<key<<endl;
00806 }
00807 }
00808
00809 dfile.close();
00810 }
00811
00812 if ( verbose )
00813 {
00814 cout<<"data_type: "<<desc.data_type<<endl;
00815 cout<<"byte_order: "<<desc.byte_order<<endl;
00816 cout<<"data_sets: "<<desc.data_sets<<endl;
00817 cout<<"channels: "<<desc.channels<<endl;
00818 cout<<"dimensions: "<<desc.dimensions[0]<<" x "<<desc.dimensions[1]<<endl;
00819 cout<<"feature: "<<desc.feature<<endl;
00820 }
00821
00822
00823 return desc;
00824 }
00825
00826
00827
00828 void MatVid::showvid(Mat vid,int fr)
00829 {
00830 int tframes=vid.size[0];
00831
00832
00833 for(int i=0;i<tframes;i++)
00834 {
00835 Mat A=MatVid::frame(vid,i);
00836
00837 if(A.depth()==CV_64F)
00838 A.convertTo(A,CV_8U);
00839
00840
00841 imshow("window",A);
00842
00843 waitKey(1000/fr);
00844
00845 }
00846
00847 }
00848
00849
00850 Mat MatVid::loadavi(string filename)
00851 {
00852 VideoCapture vc;
00853 vc.open(filename);
00854
00855 if(!vc.isOpened())
00856 CV_Error(-1,string("Failed to open file " + filename));
00857
00858
00859 int tf=(int)vc.get(CV_CAP_PROP_FRAME_COUNT);
00860 int cols=(int)vc.get(CV_CAP_PROP_FRAME_WIDTH);
00861 int rows=(int)vc.get(CV_CAP_PROP_FRAME_HEIGHT);
00862
00863 Mat ff;
00864 vc.read(ff);
00865 ff.convertTo(ff,CV_64F);
00866
00867 cout<<tf<<" "<<rows<<" "<<cols<<" "<<endl;
00868 Mat avidata=MatVid::create(tf,rows,cols,ff.type());
00869
00870 Mat temp=MatVid::frame(avidata,0);
00871 ff.copyTo(temp);
00872
00873 for(int i=1;i<tf;i++)
00874 {
00875 vc.read(ff);
00876 ff.convertTo(ff,CV_64F);
00877 Mat temp=MatVid::frame(avidata,i);
00878 ff.copyTo(temp);
00879 }
00880 return avidata;
00881 }
00882
00883 void MatVid::saveavi(string filename,Mat img)
00884 {
00885
00886 VideoWriter vc(filename,0,10,Size(img.size[2],img.size[1]));
00887
00888
00889 for(int i=0;i<img.size[0];i++)
00890 {
00891 Mat ff=MatVid::frame(img,i);
00892 vc<<ff;
00893 }
00894
00895
00896 }