You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
5.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /************************************************************************
  2. Blob.h
  3. FUNCIONALITAT: Definici� de la classe CBlob
  4. AUTOR: Inspecta S.L.
  5. MODIFICACIONS (Modificaci�, Autor, Data):
  6. FUNCTIONALITY: Definition of the CBlob class and some helper classes to perform
  7. some calculations on it
  8. AUTHOR: Inspecta S.L.
  9. MODIFICATIONS (Modification, Author, Date):
  10. **************************************************************************/
  11. #ifndef CBLOB_INSPECTA_INCLUDED
  12. #define CBLOB_INSPECTA_INCLUDED
  13. #ifdef _MSC_VER
  14. //! Disable warnings referred to 255 character truncation for the std:map
  15. #pragma warning( disable : 4786 )
  16. #endif
  17. #include <opencv/cv.h>
  18. #include "BlobLibraryConfiguration.h"
  19. #include "BlobProperties.h"
  20. #include <map>
  21. #ifdef BLOB_OBJECT_FACTORY
  22. //! Object factory pattern implementation
  23. #include "..\inspecta\DesignPatterns\ObjectFactory.h"
  24. #endif
  25. using namespace std;
  26. //! Type of labelled images
  27. typedef unsigned int t_labelType;
  28. struct mapComparator{
  29. bool operator()(const string &s1, const string &s2) const {
  30. return s1.compare(s2) > 0;
  31. };
  32. };
  33. //! Type of blob properties: a map with variant types
  34. typedef map<string, float, mapComparator> t_properties;
  35. //! Blob class
  36. class CBlob: public CBlobProperties {
  37. public:
  38. CBlob();
  39. CBlob( t_labelType id, CvPoint startPoint, CvSize originalImageSize );
  40. ~CBlob();
  41. //! Copy constructor
  42. CBlob( const CBlob &src );
  43. CBlob( const CBlob *src );
  44. //! Operador d'assignaci�
  45. //! Assigment operator
  46. CBlob& operator=(const CBlob &src );
  47. //! Adds a new internal contour to the blob
  48. void AddInternalContour( const CBlobContour &newContour );
  49. //! Retrieves contour in Freeman's chain code
  50. CBlobContour *GetExternalContour()
  51. {
  52. return &m_externalContour;
  53. }
  54. //! Retrieves blob storage
  55. CvMemStorage *GetStorage()
  56. {
  57. return m_storage;
  58. }
  59. //! Get label ID
  60. t_labelType GetID()
  61. {
  62. return m_id;
  63. }
  64. //! > 0 for extern blobs, 0 if not
  65. int Exterior( IplImage *mask,
  66. bool xBorderLeft = true, bool xBorderRight = true,
  67. bool yBorderTop = true , bool yBorderBottom = true );
  68. //! Compute blob's area
  69. float Area();
  70. //! Compute blob's perimeter
  71. float Perimeter();
  72. //! Compute blob's moment (p,q up to MAX_CALCULATED_MOMENTS)
  73. float Moment(int p, int q);
  74. //! Compute extern perimeter
  75. float ExternPerimeter( IplImage *mask,
  76. bool xBorderLeft = true, bool xBorderRight = true,
  77. bool yBorderTop = true , bool yBorderBottom = true );
  78. //! Get mean grey color
  79. float Mean( IplImage *image );
  80. //! Get standard deviation grey color
  81. float StdDev( IplImage *image );
  82. //! Indica si el blob est� buit ( no t� cap info associada )
  83. //! Shows if the blob has associated information
  84. bool IsEmpty();
  85. //! Retorna el poligon convex del blob
  86. //! Calculates the convex hull of the blob
  87. t_PointList GetConvexHull();
  88. //! Pinta l'interior d'un blob d'un color determinat
  89. //! Paints the blob in an image
  90. void FillBlob( IplImage *imatge, CvScalar color );
  91. //! Join a blob to current one (add's contour
  92. void JoinBlob( CBlob *blob );
  93. //! Get bounding box
  94. CvRect GetBoundingBox();
  95. //! Get bounding ellipse
  96. CvBox2D GetEllipse();
  97. //! Minimun X
  98. float MinX()
  99. {
  100. return GetBoundingBox().x;
  101. }
  102. //! Minimun Y
  103. float MinY()
  104. {
  105. return GetBoundingBox().y;
  106. }
  107. //! Maximun X
  108. float MaxX()
  109. {
  110. return GetBoundingBox().x + GetBoundingBox().width;
  111. }
  112. //! Maximun Y
  113. float MaxY()
  114. {
  115. return GetBoundingBox().y + GetBoundingBox().height;
  116. }
  117. //! Get mutable blob properties
  118. t_properties *GetProperties()
  119. {
  120. return &m_properties;
  121. }
  122. //! Remove calculated property from results
  123. void ResetProperty( const std::string &name )
  124. {
  125. m_properties.erase( name );
  126. }
  127. private:
  128. static int cmp_func( const void* _a, const void* _b, void* userdata )
  129. {
  130. CvPoint* a = (CvPoint*)_a;
  131. CvPoint* b = (CvPoint*)_b;
  132. int y_diff = a->y - b->y;
  133. int x_diff = a->x - b->x;
  134. return y_diff ? y_diff : x_diff;
  135. }
  136. //! Calculates mean and std deviation of blob in input image
  137. void GetMeanAndStdDev( IplImage *image, float &mean, float &stddev );
  138. //! Deallocates all contours
  139. void ClearContours();
  140. //////////////////////////////////////////////////////////////////////////
  141. // Blob contours
  142. //////////////////////////////////////////////////////////////////////////
  143. //! Contour storage memory
  144. CvMemStorage *m_storage;
  145. //! External contour of the blob (crack codes)
  146. //CBlobContour m_externalContour;
  147. //! Internal contours (crack codes)
  148. //t_contourList m_internalContours;
  149. //////////////////////////////////////////////////////////////////////////
  150. // Blob features
  151. //////////////////////////////////////////////////////////////////////////
  152. // all calculated blob properties
  153. t_properties m_properties;
  154. //! Label number
  155. t_labelType m_id;
  156. //! Bounding box
  157. CvRect m_boundingBox;
  158. //! Sizes from image where blob is extracted
  159. CvSize m_originalImageSize;
  160. };
  161. #endif //CBLOB_INSPECTA_INCLUDED