TerraLib 4.1
E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/image_processing/TePDIRegGrowSeg.hpp
Go to the documentation of this file.
00001 /*
00002 TerraLib - a library for developing GIS applications.
00003 Copyright  2001, 2002, 2003 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular
00017 purpose. The library provided hereunder is on an "as is" basis, and the
00018 authors have no obligation to provide maintenance, support, updates,
00019 enhancements, or modifications.
00020 In no event shall INPE be held liable to any party
00021 for direct, indirect, special, incidental, or consequential damages arising
00022 out of the use of this library and its documentation.
00023 */
00024 
00025 #ifndef TEPDIREGGROWSEG_HPP
00026   #define TEPDIREGGROWSEG_HPP
00027 
00028   #include "TePDIAlgorithm.hpp"
00029 
00030   #include "../kernel/TeSharedPtr.h"
00031   
00032   #include <map>
00033   
00034   #define MAXCLOSECELLS   5
00035   #define SQR_NC          65025
00036   #define TAMJAN          128
00037   #define STATDEAD        (char)2    
00038   
00040   class TePDIRGSCell;
00041   
00042   
00044   class CloserCells;
00045   
00050   typedef map<long, TePDIRGSCell *> TePDIRGSCellList;    
00051   
00052   
00062 class PDI_DLL TePDIRGSCell
00063 {
00064 public:
00068         TePDIRGSCell ();
00069 
00079    TePDIRGSCell( unsigned long *tuple, long id, short lin, short col, short nban );
00080 
00084    ~TePDIRGSCell();
00085 
00095    void Init( unsigned long *tuple, long id, short lin, short col, short nban );
00096 
00103    void ResetId( long idc ) { Idnumber_ = idc; }
00104 
00110    char Dead()  { return (Stat_ & STATDEAD); }
00111 
00118    void Kill() { Stat_ = STATDEAD; }
00119 
00128    void BoundingRectangle( int& lmin, int& cmin, int& lmax, int& cmax )
00129         { lmin = LinMin_; cmin = ColMin_; lmax = LinMax_; cmax = ColMax_; }
00130 
00136    int LineMax () { return LinMax_;}
00137 
00143    bool Merge( TePDIRGSCell *c );
00144 
00148    void AdjustNeighborhood();//{ Cc_->Adjust(); /*Neighbors_->Adjust();*/ }
00149 
00157    bool AddNeighbor( TePDIRGSCell* c, float dist );
00158 
00166    bool InsertNeighbor( TePDIRGSCell* c, float dist );
00167 
00174    TePDIRGSCell* ClosestNeighbor( float& dist );
00175 
00182    float Distance( TePDIRGSCell *c );
00183 
00189    long Id() { return Idnumber_; }
00190 
00196    long Area() { return Npix_; }
00197 
00203    int GetNban() { return Nban_; }
00204 
00208    void Print();
00209 
00210 private:
00212         char            Stat_;
00213         
00215         long            Idnumber_;
00216         
00218         long            Npix_;
00219         
00221         int             Nban_;
00222         
00224         float           delta_;
00225         
00227         int             LinMin_;
00228         
00230         int             LinMax_;
00231         
00233         int             ColMin_;
00234         
00236         int             ColMax_;
00237         
00239         float           *Media_;
00240         
00242         float           *PreviousMedia_;
00243         
00245         TePDIRGSCellList        *Neighbors_;
00246         
00248         CloserCells     *Cc_;
00249 
00250 };  
00251 
00252 
00256 class PDI_DLL CellStack : public vector< TePDIRGSCell* >
00257 {
00258 public:
00259 
00264         void Push(TePDIRGSCell* c)
00265         { push_back(c); }
00266 
00270         TePDIRGSCell* Peek()
00271     {
00272                 if (size()==0){
00273                         return NULL;
00274                 } else {
00275                         return back();
00276                 }
00277         }
00278 
00283         TePDIRGSCell* Pop()
00284         {
00285                 TePDIRGSCell* c;
00286                 if (size()==0)
00287                 {
00288                         c = new TePDIRGSCell();
00289                 } else {
00290                         c = back();
00291                         pop_back();
00292                 }
00293                 return c;
00294         }
00295 
00299         ~CellStack ()
00300         {
00301                 TePDIRGSCell *b;
00302                 while (size()!=0)
00303                 {
00304                         b = Pop();
00305                         delete b;
00306                 }
00307         }
00308         
00312         void Clear ()
00313         {
00314                 TePDIRGSCell *b;
00315                 while (size()!=0)
00316                 {
00317                         b = Pop();
00318                         delete b;
00319                 }
00320         }
00321 };
00322 
00323 
00334 class PDI_DLL CloserCells
00335 {
00336 public :
00340         TePDIRGSCell    *cmin[MAXCLOSECELLS];
00341 
00346         float   dmin[MAXCLOSECELLS];
00347         
00351         CloserCells()
00352         {
00353                 for(short i = 0; i < MAXCLOSECELLS; i++ )
00354                 {
00355                         cmin[i] = NULL;
00356                         dmin[i] = 1000000.;
00357                 }
00358         }
00359 
00365         void
00366         Insert( TePDIRGSCell *cell, float dist )
00367         {
00368                 TePDIRGSCell    **pci,
00369                         **p;
00370                 float   *di,
00371                         *d;
00372 
00373                 for( pci = &cmin[0], di = &dmin[0]; pci < &cmin[MAXCLOSECELLS]; pci++, di++ )
00374                 {
00375                         if( *pci == NULL )
00376                         {
00377                                 *pci = cell;
00378                                 *di  = dist;
00379                                 return;
00380                         }
00381                         if( dist <= *di )
00382                         {
00383                                 if( dist == *di && (*pci)->Id() < cell->Id() )
00384                                         continue;
00385                                 if( pci < &cmin[MAXCLOSECELLS] )
00386                                 {
00387                                         for( p = &cmin[MAXCLOSECELLS-1], d = &dmin[MAXCLOSECELLS-1]; p > pci; p--, d-- )
00388                                         {
00389                                                 *p = *(p-1);
00390                                                 *d = *(d-1);
00391                                         }
00392                                 }
00393                                 *pci = cell;
00394                                 *di  = dist;
00395                                 return;
00396                         }
00397                 }
00398         }
00399 
00405         void
00406         Update( TePDIRGSCell *cell, float dist )
00407         {
00408                 char    ok = 1;
00409                 TePDIRGSCell    **pci,
00410                         **pc0,
00411                         **pc01,
00412                         **pc02;
00413                 float *di = 0;
00414                 float *d0 = 0;
00415                 float *d01 = 0;
00416 
00417                 pc0 = NULL;
00418                 for( pci = &cmin[0], di = &dmin[0]; pci < &cmin[MAXCLOSECELLS]; pci++, di++ )
00419                 {
00420                         if( *pci == NULL )
00421                         {
00422                                 pc0 = pci;
00423                                 d0  = di;
00424                         }
00425                         else if( (*pci)->Dead() )
00426                         {
00427                                 *pci = NULL;
00428                                 pc0 = pci;
00429                                 d0  = di;
00430                         }
00431                         else if( *pci == cell )
00432                         {
00433                                 *pci = NULL;
00434                                 pc0  = pci;
00435                                 d0   = di;
00436                         }
00437 
00438                         if( ok && dist <= *di )
00439                         {
00440                                 if( dist == *di )
00441                                 {
00442                                         if( *pci == NULL )
00443                                         {
00444                                                 if( pci < &cmin[MAXCLOSECELLS-1] )
00445                                                         continue;
00446                                         }
00447                                         else if( (*pci)->Id() < cell->Id() )
00448                                                 continue;
00449                                 }
00450                                 if( pc0 != NULL )
00451                                 {
00452                                         for( pc01 = pc0, d01 = d0; pc01 < pci-1; pc01++, d01++ )
00453                                         {
00454                                                 *pc01 = *(pc01+1);
00455                                                 *d01  = *(d01+1);
00456                                         }
00457                                         *pc01 = cell;
00458                                         *d01  = dist;
00459                                 }
00460                                 else if( pci < &cmin[MAXCLOSECELLS-1] )
00461                                 {
00462                                         for( pc01 = pci+1, d01 = di+1; pc01 < &cmin[MAXCLOSECELLS-1]; pc01++, d01++ )
00463                                         {
00464                                                 if( (*pc01) == NULL ) break;
00465                                                 if( (*pc01)->Dead() ) break;
00466                                                 if( *pc01 == cell )   break;
00467                                         }
00468                                         for( pc02 = pc01; pc02 > pci; pc02-- )
00469                                         {
00470                                                 *pc02 = *(pc02-1);
00471                                                 *d01  = *(d01-1);
00472                                                 d01--;
00473                                         }
00474                                         *pci = cell;
00475                                         *di  = dist;
00476                                 }
00477                                 else
00478                                 {
00479                                         *pci = cell;
00480                                         *di  = dist;
00481                                 }
00482                                 ok   = 0;
00483                         }
00484                 }
00485                 return;
00486         }
00487 
00493         TePDIRGSCell*
00494         Minimum( float& dist )
00495         {
00496                 if( cmin[0] != NULL )
00497                 {
00498                         if( !cmin[0]->Dead() )
00499                         {
00500                                 dist = dmin[0];
00501                                 return cmin[0];
00502                         }
00503                 }
00504                 for( short i = 1; i < MAXCLOSECELLS; i++ )
00505                 {
00506                         if( cmin[i] != NULL )
00507                                 if( !cmin[i]->Dead() )
00508                                 {
00509                                         dist = dmin[i];
00510                                         return cmin[i];
00511                                 }
00512                 }
00513 
00514                 return NULL;
00515         }
00516 
00520         void
00521         Adjust()
00522         {
00523                 for( short i = 0; i < MAXCLOSECELLS; i++ )
00524                         if( cmin[i] != NULL )
00525                                 if( cmin[i]->Dead() )
00526                                         cmin[i] = NULL;
00527         }
00528 
00532         void
00533         Reset()
00534         {
00535                 for(short i = 0; i < MAXCLOSECELLS; i++ )
00536                 {
00537                         cmin[i] = NULL;
00538                         dmin[i] = 1000000.0;
00539                 }
00540         }
00541 };
00542 
00543 
00576   class PDI_DLL TePDIRegGrowSeg : public TePDIAlgorithm {
00577     public :
00578       
00583       TePDIRegGrowSeg();
00584 
00588       ~TePDIRegGrowSeg();
00589       
00599       bool CheckParameters( const TePDIParameters& parameters ) const;      
00600 
00601     protected :
00602     
00604       TePDITypes::TePDIRasterPtrType  Imagein_;
00605       
00607       TePDITypes::TePDIRasterPtrType  Imagelab_;
00608       
00610       TePDITypes::TePDIRasterPtrType  Imageexc_;
00611       
00613       TePDIRGSCellList        *ListCell_;
00614       
00616       TePDIRGSCell            **WindowCell_;
00617       
00619       long            OnLimbo_;
00620       
00622       unsigned long   *tuple_;
00623       
00625       long            NWindow_;
00626       
00628       long            WindowLines_;
00629       
00631       long            WindowColumns_;
00632       
00634       long            CurrentWindow_;
00635       
00637       long            WindowOffset_;
00638       
00640       int             Areamin_;
00641       
00643       double          Difsim_;
00644       
00646       int             Nlin_;
00647       
00649       int             Ncol_;
00650       
00652       int             Nban_;
00653       
00655       int             ilin_;
00656        
00658       int flin_;
00659       
00661       int             icol_;
00662       
00664       int fcol_;
00665       
00667       int             sizelin_;
00668        
00670       int sizecol_;    
00671       
00673       CellStack cellStack;
00674     
00680       void ResetState( const TePDIParameters& params );    
00681      
00687       bool RunImplementation();
00688       
00696       TePDIRGSCell* MergeCells( TePDIRGSCell *c1, TePDIRGSCell *c2 );
00697   
00703           bool InitWindow();
00704   
00708       void Adjust();
00709   
00719       bool MergeSmallCells( long area, int linmax);
00720   
00730       bool MergeSimilarCells();
00731   
00741       bool MergeMutuallyClosestCells();
00742   
00749       bool Resort();      
00750   };
00751   
00756 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines