Blob.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00017 #pragma warning( disable : 4786 )
00018
00019 #ifndef CBLOB_INSPECTA_INCLUDED
00020 #define CBLOB_INSPECTA_INCLUDED
00021
00022 #include <cxcore.h>
00023 #include <BlobLibraryConfiguration.h>
00024 #include <functional>
00025 #include <vector>
00026 #include <algorithm>
00027
00028
00029 #ifdef BLOB_OBJECT_FACTORY
00031 #include "..\inspecta\DesignPatterns\ObjectFactory.h"
00032 #endif
00033
00034
00036 #define DEGREE2RAD (CV_PI / 180.0)
00037
00043 class CBlob
00044 {
00045 public:
00048 CBlob();
00051 CBlob( const CBlob &src );
00052 CBlob( const CBlob *src );
00053
00056 ~CBlob();
00057
00060 CBlob& operator=(const CBlob &src );
00061
00064 bool IsEmpty() const
00065 {
00066 return (area == 0.0 && perimeter == 0.0 );
00067 };
00068
00071 void ClearEdges();
00074 void CopyEdges( CBlob &destination ) const;
00077 bool GetConvexHull( CvSeq **dst ) const;
00080 CvBox2D GetEllipse() const;
00081
00084 void FillBlob( IplImage *imatge, CvScalar color, int offsetX = 0, int offsetY = 0 ) const;
00085
00088
00089 inline int Label() const { return etiqueta; }
00090 inline int Parent() const { return parent; }
00091 inline double Area() const { return area; }
00092 inline double Perimeter() const { return perimeter; }
00093 inline double ExternPerimeter() const { return externPerimeter; }
00094 inline int Exterior() const { return exterior; }
00095 inline double Mean() const { return mean; }
00096 inline double StdDev() const { return stddev; }
00097 inline double MinX() const { return minx; }
00098 inline double MinY() const { return miny; }
00099 inline double MaxX() const { return maxx; }
00100 inline double MaxY() const { return maxy; }
00101 inline CvSeq *Edges() const { return edges; }
00102 inline double SumX() const { return sumx; }
00103 inline double SumY() const { return sumy; }
00104 inline double SumXX() const { return sumxx; }
00105 inline double SumYY() const { return sumyy; }
00106 inline double SumXY() const { return sumxy; }
00107
00110 int etiqueta;
00113 int exterior;
00116 double area;
00119 double perimeter;
00122 double externPerimeter;
00125 int parent;
00127 double sumx;
00128 double sumy;
00129 double sumxx;
00130 double sumyy;
00131 double sumxy;
00133 double minx;
00134 double maxx;
00135 double miny;
00136 double maxy;
00137
00140 double mean;
00143 double stddev;
00144
00147 CvMemStorage *m_storage;
00150 CvSeq *edges;
00151
00152
00154 typedef std::vector<CvPoint> vectorPunts;
00155
00157 struct comparaCvPoint : public std::binary_function<CvPoint, CvPoint, bool>
00158 {
00160 bool operator()(CvPoint a, CvPoint b)
00161 {
00162 if( a.y == b.y )
00163 return a.x < b.x;
00164 else
00165 return a.y < b.y;
00166 }
00167 };
00168 };
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00181 class COperadorBlob
00182 {
00183 public:
00184 virtual ~COperadorBlob(){};
00185
00187 virtual double operator()(const CBlob &blob) const = 0;
00189 virtual const char *GetNom() const = 0;
00190
00191 operator COperadorBlob*() const
00192 {
00193 return (COperadorBlob*)this;
00194 }
00195 };
00196
00197 typedef COperadorBlob funcio_calculBlob;
00198
00199 #ifdef BLOB_OBJECT_FACTORY
00200
00203 struct functorComparacioIdOperador
00204 {
00205 bool operator()(const char* s1, const char* s2) const
00206 {
00207 return strcmp(s1, s2) < 0;
00208 }
00209 };
00210
00212 typedef ObjectFactory<COperadorBlob, const char *, functorComparacioIdOperador > t_OperadorBlobFactory;
00213
00215 void RegistraTotsOperadors( t_OperadorBlobFactory &fabricaOperadorsBlob );
00216
00217 #endif
00218
00221 class CBlobGetArea : public COperadorBlob
00222 {
00223 public:
00224 double operator()(const CBlob &blob) const
00225 {
00226 return blob.Area();
00227 }
00228 const char *GetNom() const
00229 {
00230 return "CBlobGetArea";
00231 }
00232 };
00233
00236 class CBlobGetPerimeter: public COperadorBlob
00237 {
00238 public:
00239 double operator()(const CBlob &blob) const
00240 {
00241 return blob.Perimeter();
00242 }
00243 const char *GetNom() const
00244 {
00245 return "CBlobGetPerimeter";
00246 }
00247 };
00248
00251 class CBlobGetExterior: public COperadorBlob
00252 {
00253 public:
00254 double operator()(const CBlob &blob) const
00255 {
00256 return blob.Exterior();
00257 }
00258 const char *GetNom() const
00259 {
00260 return "CBlobGetExterior";
00261 }
00262 };
00263
00266 class CBlobGetMean: public COperadorBlob
00267 {
00268 public:
00269 double operator()(const CBlob &blob) const
00270 {
00271 return blob.Mean();
00272 }
00273 const char *GetNom() const
00274 {
00275 return "CBlobGetMean";
00276 }
00277 };
00278
00281 class CBlobGetStdDev: public COperadorBlob
00282 {
00283 public:
00284 double operator()(const CBlob &blob) const
00285 {
00286 return blob.StdDev();
00287 }
00288 const char *GetNom() const
00289 {
00290 return "CBlobGetStdDev";
00291 }
00292 };
00293
00296 class CBlobGetCompactness: public COperadorBlob
00297 {
00298 public:
00299 double operator()(const CBlob &blob) const;
00300 const char *GetNom() const
00301 {
00302 return "CBlobGetCompactness";
00303 }
00304 };
00305
00308 class CBlobGetLength: public COperadorBlob
00309 {
00310 public:
00311 double operator()(const CBlob &blob) const;
00312 const char *GetNom() const
00313 {
00314 return "CBlobGetLength";
00315 }
00316 };
00317
00320 class CBlobGetBreadth: public COperadorBlob
00321 {
00322 public:
00323 double operator()(const CBlob &blob) const;
00324 const char *GetNom() const
00325 {
00326 return "CBlobGetBreadth";
00327 }
00328 };
00329
00331 class CBlobGetDiffX: public COperadorBlob
00332 {
00333 public:
00334 double operator()(const CBlob &blob) const
00335 {
00336 return blob.maxx - blob.minx;
00337 }
00338 const char *GetNom() const
00339 {
00340 return "CBlobGetDiffX";
00341 }
00342 };
00343
00345 class CBlobGetDiffY: public COperadorBlob
00346 {
00347 public:
00348 double operator()(const CBlob &blob) const
00349 {
00350 return blob.maxy - blob.miny;
00351 }
00352 const char *GetNom() const
00353 {
00354 return "CBlobGetDiffY";
00355 }
00356 };
00357
00360 class CBlobGetMoment: public COperadorBlob
00361 {
00362 public:
00365 CBlobGetMoment()
00366 {
00367 m_p = m_q = 0;
00368 }
00371 CBlobGetMoment( int p, int q )
00372 {
00373 m_p = p;
00374 m_q = q;
00375 };
00376 double operator()(const CBlob &blob) const;
00377 const char *GetNom() const
00378 {
00379 return "CBlobGetMoment";
00380 }
00381
00382 private:
00384 int m_p, m_q;
00385 };
00386
00389 class CBlobGetHullPerimeter: public COperadorBlob
00390 {
00391 public:
00392 double operator()(const CBlob &blob) const;
00393 const char *GetNom() const
00394 {
00395 return "CBlobGetHullPerimeter";
00396 }
00397 };
00398
00401 class CBlobGetHullArea: public COperadorBlob
00402 {
00403 public:
00404 double operator()(const CBlob &blob) const;
00405 const char *GetNom() const
00406 {
00407 return "CBlobGetHullArea";
00408 }
00409 };
00410
00413 class CBlobGetMinXatMinY: public COperadorBlob
00414 {
00415 public:
00416 double operator()(const CBlob &blob) const;
00417 const char *GetNom() const
00418 {
00419 return "CBlobGetMinXatMinY";
00420 }
00421 };
00422
00425 class CBlobGetMinYatMaxX: public COperadorBlob
00426 {
00427 public:
00428 double operator()(const CBlob &blob) const;
00429 const char *GetNom() const
00430 {
00431 return "CBlobGetMinYatMaxX";
00432 }
00433 };
00434
00437 class CBlobGetMaxXatMaxY: public COperadorBlob
00438 {
00439 public:
00440 double operator()(const CBlob &blob) const;
00441 const char *GetNom() const
00442 {
00443 return "CBlobGetMaxXatMaxY";
00444 }
00445 };
00446
00449 class CBlobGetMaxYatMinX: public COperadorBlob
00450 {
00451 public:
00452 double operator()(const CBlob &blob) const;
00453 const char *GetNom() const
00454 {
00455 return "CBlobGetMaxYatMinX";
00456 }
00457 };
00458
00461 class CBlobGetMinX: public COperadorBlob
00462 {
00463 public:
00464 double operator()(const CBlob &blob) const
00465 {
00466 return blob.MinX();
00467 }
00468 const char *GetNom() const
00469 {
00470 return "CBlobGetMinX";
00471 }
00472 };
00473
00476 class CBlobGetMaxX: public COperadorBlob
00477 {
00478 public:
00479 double operator()(const CBlob &blob) const
00480 {
00481 return blob.MaxX();
00482 }
00483 const char *GetNom() const
00484 {
00485 return "CBlobGetMaxX";
00486 }
00487 };
00488
00491 class CBlobGetMinY: public COperadorBlob
00492 {
00493 public:
00494 double operator()(const CBlob &blob) const
00495 {
00496 return blob.MinY();
00497 }
00498 const char *GetNom() const
00499 {
00500 return "CBlobGetMinY";
00501 }
00502 };
00503
00506 class CBlobGetMaxY: public COperadorBlob
00507 {
00508 public:
00509 double operator()(const CBlob &blob) const
00510 {
00511 return blob.MaxY();
00512 }
00513 const char *GetNom() const
00514 {
00515 return "CBlobGetMax";
00516 }
00517 };
00518
00519
00522 class CBlobGetElongation: public COperadorBlob
00523 {
00524 public:
00525 double operator()(const CBlob &blob) const;
00526 const char *GetNom() const
00527 {
00528 return "CBlobGetElongation";
00529 }
00530 };
00531
00534 class CBlobGetRoughness: public COperadorBlob
00535 {
00536 public:
00537 double operator()(const CBlob &blob) const;
00538 const char *GetNom() const
00539 {
00540 return "CBlobGetRoughness";
00541 }
00542 };
00543
00546 class CBlobGetDistanceFromPoint: public COperadorBlob
00547 {
00548 public:
00550 CBlobGetDistanceFromPoint()
00551 {
00552 m_x = m_y = 0.0;
00553 }
00555 CBlobGetDistanceFromPoint( const double x, const double y )
00556 {
00557 m_x = x;
00558 m_y = y;
00559 }
00560
00561 double operator()(const CBlob &blob) const;
00562 const char *GetNom() const
00563 {
00564 return "CBlobGetDistanceFromPoint";
00565 }
00566
00567 private:
00568
00569 double m_x, m_y;
00570 };
00571
00574 class CBlobGetExternPerimeter: public COperadorBlob
00575 {
00576 public:
00577 double operator()(const CBlob &blob) const
00578 {
00579 return blob.ExternPerimeter();
00580 }
00581 const char *GetNom() const
00582 {
00583 return "CBlobGetExternPerimeter";
00584 }
00585 };
00586
00591 class CBlobGetExternPerimeterRatio: public COperadorBlob
00592 {
00593 public:
00594 double operator()(const CBlob &blob) const
00595 {
00596 if( blob.Perimeter() != 0 )
00597 return blob.ExternPerimeter() / blob.Perimeter();
00598 else
00599 return blob.ExternPerimeter();
00600 }
00601 const char *GetNom() const
00602 {
00603 return "CBlobGetExternPerimeterRatio";
00604 }
00605 };
00606
00611 class CBlobGetExternHullPerimeterRatio: public COperadorBlob
00612 {
00613 public:
00614 double operator()(const CBlob &blob) const
00615 {
00616 CBlobGetHullPerimeter getHullPerimeter;
00617 double hullPerimeter;
00618
00619 if( (hullPerimeter = getHullPerimeter( blob ) ) != 0 )
00620 return blob.ExternPerimeter() / hullPerimeter;
00621 else
00622 return blob.ExternPerimeter();
00623 }
00624 const char *GetNom() const
00625 {
00626 return "CBlobGetExternHullPerimeterRatio";
00627 }
00628 };
00629
00632 class CBlobGetXCenter: public COperadorBlob
00633 {
00634 public:
00635 double operator()(const CBlob &blob) const
00636 {
00637 return blob.MinX() + (( blob.MaxX() - blob.MinX() ) / 2.0);
00638 }
00639 const char *GetNom() const
00640 {
00641 return "CBlobGetXCenter";
00642 }
00643 };
00644
00647 class CBlobGetYCenter: public COperadorBlob
00648 {
00649 public:
00650 double operator()(const CBlob &blob) const
00651 {
00652 return blob.MinY() + (( blob.MaxY() - blob.MinY() ) / 2.0);
00653 }
00654 const char *GetNom() const
00655 {
00656 return "CBlobGetYCenter";
00657 }
00658 };
00659
00662 class CBlobGetMajorAxisLength: public COperadorBlob
00663 {
00664 public:
00665 double operator()(const CBlob &blob) const
00666 {
00667 CvBox2D elipse = blob.GetEllipse();
00668
00669 return elipse.size.width;
00670 }
00671 const char *GetNom() const
00672 {
00673 return "CBlobGetMajorAxisLength";
00674 }
00675 };
00676
00679 class CBlobGetAreaElipseRatio: public COperadorBlob
00680 {
00681 public:
00682 double operator()(const CBlob &blob) const
00683 {
00684 if( blob.Area()==0.0 ) return 0.0;
00685
00686 CvBox2D elipse = blob.GetEllipse();
00687 double ratioAreaElipseAreaTaca = ( (elipse.size.width/2.0)
00688 *
00689 (elipse.size.height/2.0)
00690 *CV_PI
00691 )
00692 /
00693 blob.Area();
00694
00695 return ratioAreaElipseAreaTaca;
00696 }
00697 const char *GetNom() const
00698 {
00699 return "CBlobGetAreaElipseRatio";
00700 }
00701 };
00702
00705 class CBlobGetMinorAxisLength: public COperadorBlob
00706 {
00707 public:
00708 double operator()(const CBlob &blob) const
00709 {
00710 CvBox2D elipse = blob.GetEllipse();
00711
00712 return elipse.size.height;
00713 }
00714 const char *GetNom() const
00715 {
00716 return "CBlobGetMinorAxisLength";
00717 }
00718 };
00719
00722 class CBlobGetOrientation: public COperadorBlob
00723 {
00724 public:
00725 double operator()(const CBlob &blob) const
00726 {
00727 CvBox2D elipse = blob.GetEllipse();
00728
00729 if( elipse.angle > 180.0 )
00730 return (( elipse.angle - 180.0 )* DEGREE2RAD);
00731 else
00732 return ( elipse.angle * DEGREE2RAD);
00733
00734 }
00735 const char *GetNom() const
00736 {
00737 return "CBlobGetOrientation";
00738 }
00739 };
00740
00743 class CBlobGetOrientationCos: public COperadorBlob
00744 {
00745 public:
00746 double operator()(const CBlob &blob) const
00747 {
00748 CBlobGetOrientation getOrientation;
00749 return fabs( cos( getOrientation(blob) ));
00750 }
00751 const char *GetNom() const
00752 {
00753 return "CBlobGetOrientationCos";
00754 }
00755 };
00756
00757
00760 class CBlobGetAxisRatio: public COperadorBlob
00761 {
00762 public:
00763 double operator()(const CBlob &blob) const
00764 {
00765 CvBox2D elipse = blob.GetEllipse();
00766
00767 return elipse.size.height / elipse.size.width;
00768 }
00769 const char *GetNom() const
00770 {
00771 return "CBlobGetAxisRatio";
00772 }
00773 };
00774
00775
00778 class CBlobGetXYInside: public COperadorBlob
00779 {
00780 public:
00783 CBlobGetXYInside()
00784 {
00785 m_p = cvPoint(0,0);
00786 }
00789 CBlobGetXYInside( CvPoint p )
00790 {
00791 m_p = p;
00792 };
00793 double operator()(const CBlob &blob) const;
00794 const char *GetNom() const
00795 {
00796 return "CBlobGetXYInside";
00797 }
00798
00799 private:
00802 CvPoint m_p;
00803 };
00804
00805 #endif //CBLOB_INSPECTA_INCLUDED