asmlib-opencv
an ASM(Active Shape Model) implementation by C++ & OpenCV
/media/Data/chenxing/myProj/asmlib-opencv/src/modelfile.h
Go to the documentation of this file.
00001 /*
00002    This library is free software; you can redistribute it and/or
00003    modify it under the terms of the GNU Library General Public
00004    License version 2 as published by the Free Software Foundation.
00005 
00006    This library is distributed in the hope that it will be useful,
00007    but WITHOUT ANY WARRANTY; without even the implied warranty of
00008    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00009    Library General Public License for more details.
00010 
00011    You should have received a copy of the GNU Library General Public License
00012    along with this library; see the file COPYING.LIB.  If not, write to
00013    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00014    Boston, MA 02110-1301, USA.
00015 */
00016 
00017 #ifndef MODELFILE_H
00018 #define MODELFILE_H
00019 
00020 #include <cstdio>
00021 #include <iostream>
00022 #include <fstream>
00023 #include "cv.h"
00024 using cv::PCA;
00025 using cv::Mat_;
00026 using std::cin;
00027 using std::cout;
00028 using std::endl;
00029 using std::fstream;
00030 
00031 namespace StatModel {
00032 // class ModelFile
00033 // {
00034 //     public:
00035 //         virtual void writeInt(int i){ fwrite(&i, sizeof(int), 1, fp); }
00036 //         virtual int readInt(int &i) { fread(&i, sizeof(int), 1, fp); return i; }
00037 //
00038 //         virtual void writeBool(bool b){ fwrite(&b, sizeof(bool), 1, fp); }
00039 //         virtual int readBool(bool &b) { fread(&b, sizeof(bool), 1, fp); return b; }
00040 //
00041 //         virtual void writeReal(double d) { fwrite(&d, sizeof(double), 1, fp); }
00042 //         virtual double readReal(double &d) { fread(&d, sizeof(double), 1, fp); return d; }
00043 //
00044 //         void writePCA(const PCA *p);
00045 //         PCA * readPCA(PCA * &p);
00046 //
00047 //         template < class T >
00048 //         virtual void writeMat(const Mat_<T> &m){
00049 //             writeInt(m.rows);
00050 //             writeInt(m.cols);
00051 //             for (int i=0;i<m.rows;i++)
00052 //                 for (int j=0;j<m.cols;j++)
00053 //                     fwrite(&(m(i, j)), sizeof(T), 1, fp);
00054 //         }
00055 //
00056 //         template < class T >
00057 //         Mat_<T> & readMat(Mat_<T> &m){
00058 //             int r,c;
00059 //             readInt(r);
00060 //             readInt(c);
00061 //             m.create(r, c);
00062 //             for (int i=0;i<r;i++)
00063 //                 for (int j=0;j<c;j++)
00064 //                     fread(&(m(i,j)), sizeof(T), 1, fp);
00065 //             return m;
00066 //         }
00067 //
00068 //         void openFile(const char *fName, const char *mode){
00069 //             fp=fopen(fName, mode);
00070 //             if (fp==NULL){
00071 //                 printf("Model file %s not found!!\n", fName);
00072 //                 throw("Model file %s not found!!");
00073 //             }
00074 //         }
00075 //         void closeFile(){ fclose(fp); fp=NULL; }
00076 //
00077 //         ModelFile(){ fp = NULL; }
00078 //         ~ModelFile(){ if (fp!=NULL) fclose(fp); }
00079 //     private:
00080 //         FILE *fp;
00081 // };
00082 
00084 class ModelFile
00085 {
00086     public:
00087         void writeInt(int i){ fs<<i<<endl; }
00088         int readInt(int &i) { fs>>i; return i; }
00089 
00090         void writeBool(bool b){ fs<<b<<endl; }
00091         int readBool(bool &b) { fs>>b; return b; }
00092 
00093         void writeReal(double d) { fs<<d<<endl; }
00094         double readReal(double &d) { fs>>d; return d; }
00095 
00096         void writePCA(const PCA *p);
00097         PCA * readPCA(PCA * &p);
00098 
00099         template < class T >
00100         void writeMat(const Mat_<T> &m){
00101             writeInt(m.rows);
00102             writeInt(m.cols);
00103             for (int i=0;i<m.rows;i++)
00104                 for (int j=0;j<m.cols;j++)
00105                     fs<<m(i, j)<<endl;
00106         }
00107 
00108 
00109         template < class T >
00110         Mat_<T> & readMat(Mat_<T> &m){
00111             int r,c;
00112             readInt(r);
00113             readInt(c);
00114             m.create(r, c);
00115             for (int i=0;i<r;i++)
00116                 for (int j=0;j<c;j++)
00117                     fs>>m(i,j);
00118             return m;
00119         }
00120 
00121         void openFile(const char *fName, const char *mode){
00122             if (mode[0]=='r')
00123                 fs.open(fName, std::ios_base::in);
00124             else
00125                 fs.open(fName, std::ios_base::out);
00126             if (!fs){
00127                 printf("Model file \"%s\" not found!!\n", fName);
00128                 throw("");
00129             }
00130         }
00131         void closeFile(){ fs.close(); }
00132 
00133         //ModelFile(){  }
00134         ~ModelFile(){ if (fs) fs.close(); }
00135     private:
00136         fstream fs;
00137 };
00138 
00139 typedef ModelFile ModelFileAscii;
00140 } // Namespace
00141 #endif // MODELFILE_H
 All Classes Namespaces Files Functions Variables Typedefs Defines