Main Page | Namespace List | Class List | Directories | File List | Class Members | File Members

objcluster.h

Go to the documentation of this file.
00001 // objcluster.h: interface for the objcluster class.
00002 //
00004 
00005 #if !defined(AFX_OBJCLUSTER_H__4B0D7A50_3E37_454B_A627_0C159E376149__INCLUDED_)
00006 #define AFX_OBJCLUSTER_H__4B0D7A50_3E37_454B_A627_0C159E376149__INCLUDED_
00007 
00008 #if _MSC_VER > 1000
00009 #pragma once
00010 #endif // _MSC_VER > 1000
00011 
00012 #include <fstream>
00013 
00014 #include "math.h"
00015 #include "../Utility_hm/TMatrix.h"
00016 using namespace std;
00017 
00018 /* objvector captures all essential information for a single textline,
00019  * derived from objectInfoClass.
00020  * When only use single textline/object as feature primitive of a document page,
00021  * the five double data form the vector for feature similarity computation.
00022  * When pair objects/two textlines are used to form feature primitive, the vector
00023  * of the polygon is formed in a way to capture the shape information.(See pairshape.h)
00024 
00025 */
00026 struct objvector 
00027 {
00028         int             Type;                    // type of the object: textline, tableline, etc. Currently 
00029                                                          // only textline is considered
00030 
00031         // 5-Dim vector
00032         double  FontHeight;
00033         double  Length;                  // Euclidean distance between two ending points
00034         double  Centroid_X;      // Position of the middle points of the line
00035         double  Centroid_Y;
00036         double  Angle;           // angle between the horizontal line and the textline
00037 
00041         objvector()
00042         {
00043                 Type            = 0;
00044                 FontHeight      = 0;
00045                 Length          = 0;
00046                 Centroid_X      = 0;
00047                 Centroid_Y      = 0;
00048                 Angle       = 0;
00049         }
00050 
00054         objvector(int type, double font, double length, double x, double y, double angle)
00055         {
00056                 Type            = type;
00057                 FontHeight      = font;
00058                 Length          = length;
00059                 Centroid_X      = x;
00060                 Centroid_Y      = y;
00061                 Angle       = angle;
00062         }
00063 
00068         void write_stream( ofstream & f )
00069         {
00070                 f << Type << endl;       // type of the object: textline, tableline, etc. Currently 
00071                                                          // only textline is considered
00072 
00073                 // 5-Dim vector
00074                 f << FontHeight << endl;
00075                 f << Length << endl;                    // Euclidean distance between two ending points
00076                 f << Centroid_X << endl;                // Position of the middle points of the line
00077                 f << Centroid_Y << endl;
00078                 f << Angle << endl;                             // angle between the horizontal line and the textline
00079         }
00080 
00085         void read_stream( ifstream &f )
00086         {
00087                 f >> Type;                                      // type of the object: textline, tableline, etc. Currently 
00088                                                                         // only textline is considered
00089 
00090                 // 5-Dim vector
00091                 f >> FontHeight;
00092                 f >> Length;                            // Euclidean distance between two ending points
00093                 f >> Centroid_X ;                       // Position of the middle points of the line
00094                 f >> Centroid_Y ;
00095                 f >> Angle;                                     // angle between the horizontal line and the textline
00096         
00097         }
00098 
00099         objvector operator - (objvector a)
00100         {
00101                 objvector b;
00102                 //ASSERT(Type == a.Type || Type * a.Type == 0);
00103                 b.Type           = Type;
00104                 b.FontHeight = FontHeight - a.FontHeight;
00105                 b.Length         = Length         - a.Length;
00106                 b.Centroid_X = Centroid_X - a.Centroid_X;
00107                 b.Centroid_Y = Centroid_Y - a.Centroid_Y;
00108                 b.Angle          = Angle          - a.Angle;
00109                 
00110                 return b;
00111         }
00112         objvector operator + (objvector a)
00113         {
00114                 objvector b;
00115                 //ASSERT(Type == a.Type || Type * a.Type == 0);
00116                 b.Type           = Type;
00117                 b.FontHeight = FontHeight       + a.FontHeight;
00118                 b.Length         = Length               + a.Length;
00119                 b.Centroid_X = Centroid_X       + a.Centroid_X;
00120                 b.Centroid_Y = Centroid_Y       + a.Centroid_Y;
00121                 b.Angle          = Angle                + a.Angle;
00122                 
00123                 return b;
00124         }
00125 
00126 
00127         objvector operator * (double a)
00128         {
00129                 objvector b;
00130                 b.Type           = Type; 
00131                 b.FontHeight = FontHeight * a;
00132                 b.Length         = Length * a;
00133                 b.Centroid_X = Centroid_X * a;
00134                 b.Centroid_Y = Centroid_Y * a;
00135                 b.Angle          = Angle * a;           
00136                 return b;
00137         }
00138 
00139         objvector operator / (double a)
00140         {
00141                 //ASSERT(a!=0);
00142 
00143                 objvector b;
00144                 b.Type           = Type; 
00145                 b.FontHeight = FontHeight / a;
00146                 b.Length         = Length / a;
00147                 b.Centroid_X = Centroid_X / a;
00148                 b.Centroid_Y = Centroid_Y / a;
00149                 b.Angle          = Angle / a;
00150                 return b;
00151         }
00152 
00153         objvector dotproduct(objvector a)
00154         {
00155                 //ASSERT(Type == a.Type);
00156 
00157                 objvector b             = *this;
00158                 b.Angle                 *= a.Angle;
00159                 b.Centroid_X    *= a.Centroid_X;
00160                 b.Centroid_Y    *= a.Centroid_Y;
00161                 b.Length                *= a.Length;
00162                 b.FontHeight    *= a.FontHeight;
00163                 return b;
00164         }
00165 
00166         objvector dotsqrt()
00167         {
00168                 //ASSERT(Type == a.Type);
00169 
00170                 objvector b             = *this;
00171                 b.Angle                 = sqrt(Angle);
00172                 b.Centroid_X    = sqrt(Centroid_X);
00173                 b.Centroid_Y    = sqrt(Centroid_Y);
00174                 b.Length                = sqrt(Length);
00175                 b.FontHeight    = sqrt(FontHeight);
00176                 return b;
00177         }
00178 
00179         objvector dotdivide(objvector a, bool flag = true)
00180         {
00181                 //ASSERT(Type == a.Type);
00182 
00183                 objvector b             = *this;
00184                 b.Angle                 /= a.Angle;
00185                 if(flag)
00186                 {
00187                         b.Centroid_X    /= a.Centroid_X;
00188                         b.Centroid_Y    /= a.Centroid_Y;
00189                 }
00190                 b.Length                /= a.Length;
00191                 b.FontHeight    /= a.FontHeight;
00192                 return b;
00193         }
00194 
00195         objvector objsqrt()
00196         {
00197                 objvector b;
00198                 b.Type                  = Type;
00199                 b.Angle                 = sqrt(Angle);
00200                 b.Centroid_X    = sqrt(Centroid_X);
00201                 b.Centroid_Y    = sqrt(Centroid_Y);
00202                 b.FontHeight    = sqrt(FontHeight);
00203                 b.Length                = sqrt(Length);
00204 
00205                 return b;
00206         }
00207 
00208         objvector& operator = (objvector a)
00209         {
00210                 Type                    = a.Type;
00211                 FontHeight              = a.FontHeight;
00212                 Length                  = a.Length;
00213                 Centroid_X              = a.Centroid_X;
00214                 Centroid_Y              = a.Centroid_Y;
00215                 Angle                   = a.Angle;
00216 
00217                 return *this;
00218         }
00219 
00220         bool operator == (objvector a)
00221         {
00222                 if (Type != a.Type) return false;
00223                 if (FontHeight != a.FontHeight ) return false;
00224                 if (Length != a.Length) return false;
00225                 if (Centroid_X != a.Centroid_X) return false;
00226                 if (Centroid_Y != a.Centroid_Y) return false;
00227                 if (Angle != a.Angle) return false;
00228 
00229                 return true;
00230         }
00231 
00232         objvector dotabs()
00233         {
00234                 objvector b = *this;
00235                 if(b.Angle <0) b.Angle = -b.Angle;
00236                 if(b.Centroid_X <0) b.Centroid_X = -b.Centroid_X;
00237                 if(b.Centroid_Y <0) b.Centroid_Y = -b.Centroid_Y;
00238                 if(b.FontHeight <0) b.FontHeight = -b.FontHeight;
00239                 if(b.Length <0) b.Length = -b.Length;
00240 
00241                 return b;
00242         }
00243 
00244         double norm()
00245         {
00246                 return sqrt(normsquare());
00247         }
00248 
00249         double normsquare()
00250         {
00251                 return FontHeight*FontHeight + Length*Length+Centroid_X*Centroid_X + Centroid_Y*Centroid_Y + Angle*Angle;
00252         }
00253 
00254         /* weighted norm */
00255         double norm_2(double f_r=1, double a_r=1, double l_r=1, double x_r=1, double y_r=1)
00256         {
00257                 double ratio = f_r + a_r+ l_r + x_r + y_r;
00258                 double dist = FontHeight*FontHeight * f_r + Length*Length * l_r
00259                                         + Centroid_X*Centroid_X * x_r + Centroid_Y*Centroid_Y * y_r 
00260                                         + Angle*Angle * a_r;
00261 
00262                 return sqrt(dist/ratio);
00263         }
00264 
00265 public:
00266         void clear()
00267         {
00268                 Type = 0;
00269                 FontHeight = 0;
00270                 Length = 0;
00271                 Centroid_X = 0;
00272                 Centroid_Y = 0;
00273                 Angle = 0;
00274         }
00275 };
00276 
00277 struct objcluster  
00278 {
00279 public:
00280         objvector       m_centroid;
00281         objvector       m_sigma;                    //standard diveation: multivariate gaussian
00282         int                     m_nbObjects;
00283 
00284         double          m_weight;
00285         double          m_avgnbObjperdoc;   // #objects of this cluster averaged by the number of training tiff images 
00286         double          m_sigmaObjperdoc;   // standard deviation of #objects of this cluster accross the training tiffs
00287 
00288         objcluster();
00289         virtual ~objcluster();
00290 
00291         double Probability(objvector& obj);
00292         
00293         // void   SaveToDisk(CString fname, bool saveWeights = true);
00294         // void   LoadFromDisk(CString fname );
00295 
00296         void write_stream(ofstream & of, bool saveweights);
00297         void read_stream(ifstream &f);
00298 };
00299 
00300 
00301 //struct objclusterset
00302 //{
00303 //public:
00304 //      int m_nClusters;
00305 //      objcluster * m_pClusters;
00306 //
00307 //      objclusterset() { m_nClusters = 0; m_pClusters = NULL; }
00308 //      virtual ~objclusterset() 
00309 //      {
00310 //              for(int i=0; i<m_nClusters; i++)
00311 //                      delete m_pClusters[i];
00312 //      }
00313 //
00314 //      void SaveToDisk(char* path, bool saveweights = true);
00315 //      void   LoadFromDisk(CString fname );
00316 //};
00317 
00318 
00319 
00320 //#define INCREAMENTAL_AMOUNT 12
00321 //
00322 //struct a_Cluster
00323 //{
00324 //      int                             centroid[2]; // index of centroid object/pair, page_index/member_index
00325 //      double                  sum_dist;    // sum of distance from each object pair to centroid pair
00326 //      CTMatrix<int>   memberlist;
00327 //      int                             nMember;
00328 //
00329 //      a_Cluster()
00330 //      {
00331 //              sum_dist = 0;
00332 //              nMember = 0;
00333 //              centroid[0] = centroid[1] = -1;
00334 //      }
00335 //
00336 //      ~a_Cluster()
00337 //      {
00338 //              memberlist.Destruction();
00339 //      }
00340 //      
00341 //      void Add(int page_index, int member_index)
00342 //      {
00343 //              if(nMember == memberlist.GetRows())
00344 //              {
00345 //                      CTMatrix<int> tmp = memberlist;
00346 //                      memberlist.Construction(nMember + INCREAMENTAL_AMOUNT,2);
00347 //                      for(int i=0;i<nMember;i++)
00348 //                              memberlist[i] = tmp[i];
00349 //              }
00350 //
00351 //              memberlist[nMember][0] = page_index;
00352 //              memberlist[nMember][1] = member_index;
00353 //              nMember ++;
00354 //      }
00355 //};
00356 
00357 #endif // !defined(AFX_OBJCLUSTER_H__4B0D7A50_3E37_454B_A627_0C159E376149__INCLUDED_)

Generated on Tue Aug 29 11:42:39 2006 for PageLayoutDOCLIB by  doxygen 1.4.2