D:/users/ricard/src/utilitats/cpp/cvblobslib/blob.h

Go to the documentation of this file.
00001 /************************************************************************
00002                         Blob.h
00003                         
00004 FUNCIONALITAT: Definició de la classe CBlob
00005 AUTOR: Inspecta S.L.
00006 MODIFICACIONS (Modificació, Autor, Data):
00007 
00008 FUNCTIONALITY: Definition of the CBlob class and some helper classes to perform
00009                            some calculations on it
00010 AUTHOR: Inspecta S.L.
00011 MODIFICATIONS (Modification, Author, Date):
00012 
00013 **************************************************************************/
00014 
00016 #pragma warning( disable : 4786 ) 
00017 
00018 #ifndef CBLOB_INSPECTA_INCLUDED
00019 #define CBLOB_INSPECTA_INCLUDED
00020 
00021 #include "cxcore.h"
00022 #include <functional>
00023 #include <vector>
00024 #include <algorithm>
00026 #include "..\inspecta\DesignPatterns\ObjectFactory.h"
00027 
00028 
00030 #define DEGREE2RAD              (CV_PI / 180.0)
00031 
00037 class CBlob
00038 {
00039 public:
00042         CBlob();
00045         CBlob( const CBlob &src );
00046         CBlob( const CBlob *src );
00047 
00050         ~CBlob();
00051         
00054         CBlob& operator=(const CBlob &src );
00055 
00058         bool IsEmpty() const
00059         {
00060                 return (area == 0.0 && perimeter == 0.0 );
00061         };
00062 
00065         void ClearEdges();
00068         void CopyEdges( CBlob &destination ) const;
00071         bool GetConvexHull( CvSeq **dst ) const;
00074         CvBox2D GetEllipse() const;
00075 
00078         void FillBlob( IplImage *imatge, CvScalar color, int offsetX = 0, int offsetY = 0 ) const;
00079 
00082 
00083         inline int Label() const        { return etiqueta; }
00084         inline int Parent() const       { return parent; }
00085         inline double Area() const { return area; }
00086         inline double Perimeter() const { return perimeter; }
00087         inline double ExternPerimeter() const { return externPerimeter; }
00088         inline int        Exterior() const { return exterior; }
00089         inline double Mean() const { return mean; }
00090         inline double StdDev() const { return stddev; }
00091         inline double MinX() const { return minx; }
00092         inline double MinY() const { return miny; }
00093         inline double MaxX() const { return maxx; }
00094         inline double MaxY() const { return maxy; }
00095         inline CvSeq *Edges() const { return edges; }
00096         inline double SumX() const { return sumx; }
00097         inline double SumY() const { return sumy; }
00098         inline double SumXX() const { return sumxx; }
00099         inline double SumYY() const { return sumyy; }
00100         inline double SumXY() const { return sumxy; }
00101         
00104         int etiqueta;
00107         int exterior;
00110         double area;
00113         double perimeter;
00116         double externPerimeter;
00119         int parent;
00121         double sumx;
00122         double sumy;
00123         double sumxx;
00124         double sumyy;
00125         double sumxy;
00127         double minx;
00128         double maxx;
00129         double miny;
00130         double maxy;
00131         
00134         double mean;
00137         double stddev;
00138 
00141         CvMemStorage *m_storage;
00144         CvSeq *edges;
00145         
00146 
00148         typedef std::vector<CvPoint> vectorPunts;
00149 
00151         struct comparaCvPoint : public std::binary_function<CvPoint, CvPoint, bool> 
00152         {
00154                 bool operator()(CvPoint a, CvPoint b) 
00155                 { 
00156                         if( a.y == b.y ) 
00157                                 return a.x < b.x;
00158                         else 
00159                                 return a.y < b.y; 
00160                 }
00161         };
00162 };
00163 
00164 
00165 
00166 /**************************************************************************
00167                 Definició de les classes per a fer operacions sobre els blobs
00168 
00169                 Helper classes to perform operations on blobs
00170 **************************************************************************/
00171 
00172 
00175 class COperadorBlob
00176 {
00177 public:
00178         virtual ~COperadorBlob(){};
00179 
00181         virtual double operator()(const CBlob &blob) const = 0;
00183         virtual const char *GetNom() const = 0;
00184 
00185         operator COperadorBlob*() const
00186         {
00187                 return (COperadorBlob*)this;
00188         }
00189 };
00190 
00191 typedef COperadorBlob funcio_calculBlob;
00192 
00193 
00197 struct functorComparacioIdOperador
00198 {
00199   bool operator()(const char* s1, const char* s2) const
00200   {
00201     return strcmp(s1, s2) < 0;
00202   }
00203 };
00204 
00206 typedef ObjectFactory<COperadorBlob, const char *, functorComparacioIdOperador > t_OperadorBlobFactory;
00207 
00208 
00211 class CBlobGetArea : public COperadorBlob
00212 {
00213 public:
00214     double operator()(const CBlob &blob) const
00215         { 
00216                 return blob.Area(); 
00217         }
00218         const char *GetNom() const
00219         {
00220                 return "CBlobGetArea";
00221         }
00222 };
00223 
00226 class CBlobGetPerimeter: public COperadorBlob
00227 {
00228 public:
00229     double operator()(const CBlob &blob) const
00230         { 
00231                 return blob.Perimeter(); 
00232         }
00233         const char *GetNom() const
00234         {
00235                 return "CBlobGetPerimeter";
00236         }
00237 };
00238 
00241 class CBlobGetExterior: public COperadorBlob
00242 {
00243 public:
00244     double operator()(const CBlob &blob) const
00245         { 
00246                 return blob.Exterior(); 
00247         }
00248         const char *GetNom() const
00249         {
00250                 return "CBlobGetExterior";
00251         }
00252 };
00253 
00256 class CBlobGetMean: public COperadorBlob
00257 {
00258 public:
00259     double operator()(const CBlob &blob) const
00260         { 
00261                 return blob.Mean(); 
00262         }
00263         const char *GetNom() const
00264         {
00265                 return "CBlobGetMean";
00266         }
00267 };
00268 
00271 class CBlobGetStdDev: public COperadorBlob
00272 {
00273 public:
00274     double operator()(const CBlob &blob) const
00275         { 
00276                 return blob.StdDev(); 
00277         }
00278         const char *GetNom() const
00279         {
00280                 return "CBlobGetStdDev";
00281         }
00282 };
00283 
00286 class CBlobGetCompactness: public COperadorBlob
00287 {
00288 public:
00289     double operator()(const CBlob &blob) const;
00290         const char *GetNom() const
00291         {
00292                 return "CBlobGetCompactness";
00293         }
00294 };
00295 
00298 class CBlobGetLength: public COperadorBlob
00299 {
00300 public:
00301     double operator()(const CBlob &blob) const;
00302         const char *GetNom() const
00303         {
00304                 return "CBlobGetLength";
00305         }
00306 };
00307 
00310 class CBlobGetBreadth: public COperadorBlob
00311 {
00312 public:
00313     double operator()(const CBlob &blob) const;
00314         const char *GetNom() const
00315         {
00316                 return "CBlobGetBreadth";
00317         }
00318 };
00319 
00321 class CBlobGetDiffX: public COperadorBlob
00322 {
00323 public:
00324     double operator()(const CBlob &blob) const
00325         {
00326                 return blob.maxx - blob.minx;
00327         }
00328         const char *GetNom() const
00329         {
00330                 return "CBlobGetDiffX";
00331         }
00332 };
00333 
00335 class CBlobGetDiffY: public COperadorBlob
00336 {
00337 public:
00338     double operator()(const CBlob &blob) const
00339         {
00340                 return blob.maxy - blob.miny;
00341         }
00342         const char *GetNom() const
00343         {
00344                 return "CBlobGetDiffY";
00345         }
00346 };
00347 
00350 class CBlobGetMoment: public COperadorBlob
00351 {
00352 public:
00355         CBlobGetMoment()
00356         {
00357                 m_p = m_q = 0;
00358         }
00361         CBlobGetMoment( int p, int q )
00362         {
00363                 m_p = p;
00364                 m_q = q;
00365         };
00366         double operator()(const CBlob &blob) const;
00367         const char *GetNom() const
00368         {
00369                 return "CBlobGetMoment";
00370         }
00371 
00372 private:
00374         int m_p, m_q;
00375 };
00376 
00379 class CBlobGetHullPerimeter: public COperadorBlob
00380 {
00381 public:
00382     double operator()(const CBlob &blob) const;
00383         const char *GetNom() const
00384         {
00385                 return "CBlobGetHullPerimeter";
00386         }
00387 };
00388 
00391 class CBlobGetHullArea: public COperadorBlob
00392 {
00393 public:
00394     double operator()(const CBlob &blob) const;
00395         const char *GetNom() const
00396         {
00397                 return "CBlobGetHullArea";
00398         }
00399 };
00400 
00403 class CBlobGetMinXatMinY: public COperadorBlob
00404 {
00405 public:
00406     double operator()(const CBlob &blob) const;
00407         const char *GetNom() const
00408         {
00409                 return "CBlobGetMinXatMinY";
00410         }
00411 };
00412 
00415 class CBlobGetMinYatMaxX: public COperadorBlob
00416 {
00417 public:
00418     double operator()(const CBlob &blob) const;
00419         const char *GetNom() const
00420         {
00421                 return "CBlobGetMinYatMaxX";
00422         }
00423 };
00424 
00427 class CBlobGetMaxXatMaxY: public COperadorBlob
00428 {
00429 public:
00430     double operator()(const CBlob &blob) const;
00431         const char *GetNom() const
00432         {
00433                 return "CBlobGetMaxXatMaxY";
00434         }
00435 };
00436 
00439 class CBlobGetMaxYatMinX: public COperadorBlob
00440 {
00441 public:
00442     double operator()(const CBlob &blob) const;
00443         const char *GetNom() const
00444         {
00445                 return "CBlobGetMaxYatMinX";
00446         }
00447 };
00448 
00451 class CBlobGetMinX: public COperadorBlob
00452 {
00453 public:
00454     double operator()(const CBlob &blob) const
00455         {
00456                 return blob.MinX();
00457         }
00458         const char *GetNom() const
00459         {
00460                 return "CBlobGetMinX";
00461         }
00462 };
00463 
00466 class CBlobGetMaxX: public COperadorBlob
00467 {
00468 public:
00469     double operator()(const CBlob &blob) const
00470         {
00471                 return blob.MaxX();
00472         }
00473         const char *GetNom() const
00474         {
00475                 return "CBlobGetMaxX";
00476         }
00477 };
00478 
00481 class CBlobGetMinY: public COperadorBlob
00482 {
00483 public:
00484     double operator()(const CBlob &blob) const
00485         {
00486                 return blob.MinY();
00487         }
00488         const char *GetNom() const
00489         {
00490                 return "CBlobGetMinY";
00491         }
00492 };
00493 
00496 class CBlobGetMaxY: public COperadorBlob
00497 {
00498 public:
00499     double operator()(const CBlob &blob) const
00500         {
00501                 return blob.MaxY();
00502         }
00503         const char *GetNom() const
00504         {
00505                 return "CBlobGetMax";
00506         }
00507 };
00508 
00509 
00512 class CBlobGetElongation: public COperadorBlob
00513 {
00514 public:
00515     double operator()(const CBlob &blob) const;
00516         const char *GetNom() const
00517         {
00518                 return "CBlobGetElongation";
00519         }
00520 };
00521 
00524 class CBlobGetRoughness: public COperadorBlob
00525 {
00526 public:
00527     double operator()(const CBlob &blob) const;
00528         const char *GetNom() const
00529         {
00530                 return "CBlobGetRoughness";
00531         }
00532 };
00533 
00536 class CBlobGetDistanceFromPoint: public COperadorBlob
00537 {
00538 public:
00540         CBlobGetDistanceFromPoint()
00541         {
00542                 m_x = m_y = 0.0;
00543         }
00545         CBlobGetDistanceFromPoint( const double x, const double y )
00546         {
00547                 m_x = x;
00548                 m_y = y;
00549         }
00550 
00551     double operator()(const CBlob &blob) const;
00552         const char *GetNom() const
00553         {
00554                 return "CBlobGetDistanceFromPoint";
00555         }
00556 
00557 private:
00558         // coordenades del punt on volem calcular la distància
00559         double m_x, m_y;
00560 };
00561 
00564 class CBlobGetExternPerimeter: public COperadorBlob
00565 {
00566 public:
00567     double operator()(const CBlob &blob) const
00568         {
00569                 return blob.ExternPerimeter();
00570         }
00571         const char *GetNom() const
00572         {
00573                 return "CBlobGetExternPerimeter";
00574         }
00575 };
00576 
00581 class CBlobGetExternPerimeterRatio: public COperadorBlob
00582 {
00583 public:
00584     double operator()(const CBlob &blob) const
00585         {
00586                 if( blob.Perimeter() != 0 )
00587                         return blob.ExternPerimeter() / blob.Perimeter();
00588                 else
00589                         return blob.ExternPerimeter();
00590         }
00591         const char *GetNom() const
00592         {
00593                 return "CBlobGetExternPerimeterRatio";
00594         }
00595 };
00596 
00601 class CBlobGetExternHullPerimeterRatio: public COperadorBlob
00602 {
00603 public:
00604     double operator()(const CBlob &blob) const
00605         {
00606                 CBlobGetHullPerimeter getHullPerimeter;
00607                 double hullPerimeter;
00608 
00609                 if( (hullPerimeter = getHullPerimeter( blob ) ) != 0 )
00610                         return blob.ExternPerimeter() / hullPerimeter;
00611                 else
00612                         return blob.ExternPerimeter();
00613         }
00614         const char *GetNom() const
00615         {
00616                 return "CBlobGetExternHullPerimeterRatio";
00617         }
00618 };
00619 
00622 class CBlobGetXCenter: public COperadorBlob
00623 {
00624 public:
00625     double operator()(const CBlob &blob) const
00626         {
00627                 return blob.MinX() + (( blob.MaxX() - blob.MinX() ) / 2.0);
00628         }
00629         const char *GetNom() const
00630         {
00631                 return "CBlobGetXCenter";
00632         }
00633 };
00634 
00637 class CBlobGetYCenter: public COperadorBlob
00638 {
00639 public:
00640     double operator()(const CBlob &blob) const
00641         {
00642                 return blob.MinY() + (( blob.MaxY() - blob.MinY() ) / 2.0);
00643         }
00644         const char *GetNom() const
00645         {
00646                 return "CBlobGetYCenter";
00647         }
00648 };
00649 
00652 class CBlobGetMajorAxisLength: public COperadorBlob
00653 {
00654 public:
00655     double operator()(const CBlob &blob) const
00656         {
00657                 CvBox2D elipse = blob.GetEllipse();
00658 
00659                 return elipse.size.width;
00660         }
00661         const char *GetNom() const
00662         {
00663                 return "CBlobGetMajorAxisLength";
00664         }
00665 };
00666 
00669 class CBlobGetAreaElipseRatio: public COperadorBlob
00670 {
00671 public:
00672     double operator()(const CBlob &blob) const
00673         {
00674                 if( blob.Area()==0.0 ) return 0.0;
00675 
00676                 CvBox2D elipse = blob.GetEllipse();
00677                 double ratioAreaElipseAreaTaca = ( (elipse.size.width/2.0)
00678                                                                                    *
00679                                                                                    (elipse.size.height/2.0)
00680                                                                        *CV_PI
00681                                                                  )
00682                                                                              /
00683                                                                              blob.Area();
00684 
00685                 return ratioAreaElipseAreaTaca;
00686         }
00687         const char *GetNom() const
00688         {
00689                 return "CBlobGetAreaElipseRatio";
00690         }
00691 };
00692 
00695 class CBlobGetMinorAxisLength: public COperadorBlob
00696 {
00697 public:
00698     double operator()(const CBlob &blob) const
00699         {
00700                 CvBox2D elipse = blob.GetEllipse();
00701 
00702                 return elipse.size.height;
00703         }
00704         const char *GetNom() const
00705         {
00706                 return "CBlobGetMinorAxisLength";
00707         }
00708 };
00709 
00712 class CBlobGetOrientation: public COperadorBlob
00713 {
00714 public:
00715     double operator()(const CBlob &blob) const
00716         {
00717                 CvBox2D elipse = blob.GetEllipse();
00718 
00719                 if( elipse.angle > 180.0 )
00720                         return (( elipse.angle - 180.0 )* DEGREE2RAD);
00721                 else
00722                         return ( elipse.angle * DEGREE2RAD);
00723 
00724         }
00725         const char *GetNom() const
00726         {
00727                 return "CBlobGetOrientation";
00728         }
00729 };
00730 
00733 class CBlobGetOrientationCos: public COperadorBlob
00734 {
00735 public:
00736     double operator()(const CBlob &blob) const
00737         {
00738                 CBlobGetOrientation getOrientation;
00739                 return fabs( cos( getOrientation(blob) ));
00740         }
00741         const char *GetNom() const
00742         {
00743                 return "CBlobGetOrientationCos";
00744         }
00745 };
00746 
00747 
00750 class CBlobGetAxisRatio: public COperadorBlob
00751 {
00752 public:
00753     double operator()(const CBlob &blob) const
00754         {
00755                 CvBox2D elipse = blob.GetEllipse();
00756 
00757                 return elipse.size.height / elipse.size.width;
00758         }
00759         const char *GetNom() const
00760         {
00761                 return "CBlobGetAxisRatio";
00762         }
00763 };
00764 
00765 
00768 class CBlobGetXYInside: public COperadorBlob
00769 {
00770 public:
00773         CBlobGetXYInside()
00774         {
00775                 m_p = cvPoint(0,0);
00776         }
00779         CBlobGetXYInside( CvPoint p )
00780         {
00781                 m_p = p;
00782         };
00783         double operator()(const CBlob &blob) const;
00784         const char *GetNom() const
00785         {
00786                 return "CBlobGetXYInside";
00787         }
00788 
00789 private:
00792         CvPoint m_p;
00793 };
00794 
00796 void RegistraTotsOperadors( t_OperadorBlobFactory &fabricaOperadorsBlob );
00797 
00798 #endif //CBLOB_INSPECTA_INCLUDED

Generated on Mon Nov 13 13:32:49 2006 for cvBlobsLib by  doxygen 1.5.1-p1