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.

257 lines
5.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "BlobContour.h"
  2. CBlobContour::CBlobContour()
  3. {
  4. m_startPoint.x = 0;
  5. m_startPoint.y = 0;
  6. m_area = -1;
  7. m_perimeter = -1;
  8. m_contourPoints = NULL;
  9. m_moments.m00 = -1;
  10. m_contour = NULL;
  11. m_parentStorage = NULL;
  12. }
  13. CBlobContour::CBlobContour(CvPoint startPoint, CvMemStorage *storage )
  14. {
  15. m_startPoint.x = startPoint.x;
  16. m_startPoint.y = startPoint.y;
  17. m_area = -1;
  18. m_perimeter = -1;
  19. m_moments.m00 = -1;
  20. m_parentStorage = storage;
  21. m_contourPoints = NULL;
  22. // contour sequence: must be compatible with opencv functions
  23. m_contour = cvCreateSeq( CV_SEQ_ELTYPE_CODE | CV_SEQ_KIND_CURVE | CV_SEQ_FLAG_CLOSED,
  24. sizeof(CvContour),
  25. sizeof(t_chainCode),m_parentStorage);
  26. }
  27. //! Copy constructor
  28. CBlobContour::CBlobContour( CBlobContour *source )
  29. {
  30. if (source != NULL )
  31. {
  32. *this = *source;
  33. }
  34. }
  35. CBlobContour::~CBlobContour()
  36. {
  37. // let parent blob deallocate all contour and contour point memory
  38. m_contour = NULL;
  39. m_contourPoints = NULL;
  40. }
  41. //! Copy operator
  42. CBlobContour& CBlobContour::operator=( const CBlobContour &source )
  43. {
  44. if( this != &source )
  45. {
  46. m_startPoint = source.m_startPoint;
  47. m_parentStorage = source.m_parentStorage;
  48. if (m_contour)
  49. {
  50. cvClearSeq( m_contour );
  51. }
  52. if (source.m_contour)
  53. {
  54. m_contour = cvCloneSeq( source.m_contour, m_parentStorage);
  55. }
  56. if( source.m_contourPoints )
  57. {
  58. if( m_contourPoints )
  59. cvClearSeq( m_contourPoints );
  60. m_contourPoints = cvCloneSeq( source.m_contourPoints, m_parentStorage);
  61. }
  62. m_area = source.m_area;
  63. m_perimeter = source.m_area;
  64. m_moments = source.m_moments;
  65. }
  66. return *this;
  67. }
  68. /**
  69. - FUNCI�: AddChainCode
  70. - FUNCIONALITAT: Add chain code to contour
  71. - PAR�METRES:
  72. -
  73. - RESULTAT:
  74. -
  75. - RESTRICCIONS:
  76. -
  77. - AUTOR: rborras
  78. - DATA DE CREACI�: 2008/05/06
  79. - MODIFICACI�: Data. Autor. Descripci�.
  80. */
  81. void CBlobContour::AddChainCode(t_chainCode chaincode)
  82. {
  83. cvSeqPush(m_contour, &chaincode);
  84. }
  85. //! Clears chain code contour and points
  86. void CBlobContour::ResetChainCode()
  87. {
  88. if( m_contour )
  89. {
  90. cvClearSeq( m_contour );
  91. m_contour = NULL;
  92. }
  93. if( m_contourPoints )
  94. {
  95. cvClearSeq( m_contourPoints );
  96. m_contourPoints = NULL;
  97. }
  98. }
  99. /**
  100. - FUNCI�: GetPerimeter
  101. - FUNCIONALITAT: Get perimeter from chain code. Diagonals sum sqrt(2) and horizontal and vertical codes 1
  102. - PAR�METRES:
  103. -
  104. - RESULTAT:
  105. -
  106. - RESTRICCIONS:
  107. -
  108. - AUTOR: rborras
  109. - DATA DE CREACI�: 2008/04/30
  110. - MODIFICACI�: Data. Autor. Descripci�.
  111. - NOTA: Algorithm derived from "Methods to estimate area and perimeters of blob-like objects: A comparison", L.Yang
  112. */
  113. float CBlobContour::GetPerimeter()
  114. {
  115. // is calculated?
  116. if (m_perimeter != -1)
  117. {
  118. return m_perimeter;
  119. }
  120. if( IsEmpty() )
  121. return 0;
  122. m_perimeter = cvContourPerimeter( GetContourPoints() );
  123. return m_perimeter;
  124. }
  125. /**
  126. - FUNCI�: GetArea
  127. - FUNCIONALITAT: Computes area from chain code
  128. - PAR�METRES:
  129. -
  130. - RESULTAT:
  131. - May give negative areas for clock wise contours
  132. - RESTRICCIONS:
  133. -
  134. - AUTOR: rborras
  135. - DATA DE CREACI�: 2008/04/30
  136. - MODIFICACI�: Data. Autor. Descripci�.
  137. - NOTA: Algorithm derived from "Properties of contour codes", G.R. Wilson
  138. */
  139. float CBlobContour::GetArea()
  140. {
  141. // is calculated?
  142. if (m_area != -1)
  143. {
  144. return m_area;
  145. }
  146. if( IsEmpty() )
  147. return 0;
  148. m_area = fabs( cvContourArea( GetContourPoints() ));
  149. return m_area;
  150. }
  151. //! Get contour moment (p,q up to MAX_CALCULATED_MOMENTS)
  152. float CBlobContour::GetMoment(int p, int q)
  153. {
  154. // is a valid moment?
  155. if ( p < 0 || q < 0 || p > MAX_MOMENTS_ORDER || q > MAX_MOMENTS_ORDER )
  156. {
  157. return -1;
  158. }
  159. if( IsEmpty() )
  160. return 0;
  161. // it is calculated?
  162. if( m_moments.m00 == -1)
  163. {
  164. cvMoments( GetContourPoints(), &m_moments );
  165. }
  166. return cvGetSpatialMoment( &m_moments, p, q );
  167. }
  168. //! Calculate contour points from crack codes
  169. t_PointList CBlobContour::GetContourPoints()
  170. {
  171. // it is calculated?
  172. if( m_contourPoints != NULL )
  173. return m_contourPoints;
  174. if ( m_contour == NULL || m_contour->total <= 0 )
  175. {
  176. return NULL;
  177. }
  178. CvSeq *tmpPoints;
  179. CvSeqReader reader;
  180. CvSeqWriter writer;
  181. CvPoint actualPoint;
  182. CvRect boundingBox;
  183. // if aproximation is different than simple extern perimeter will not work
  184. tmpPoints = cvApproxChains( m_contour, m_parentStorage, CV_CHAIN_APPROX_NONE);
  185. // apply an offset to contour points to recover real coordinates
  186. cvStartReadSeq( tmpPoints, &reader);
  187. m_contourPoints = cvCreateSeq( tmpPoints->flags, tmpPoints->header_size, tmpPoints->elem_size, m_parentStorage );
  188. cvStartAppendToSeq(m_contourPoints, &writer );
  189. // also calculate bounding box of the contour to allow cvPointPolygonTest
  190. // work correctly on the generated polygon
  191. boundingBox.x = boundingBox.y = 10000;
  192. boundingBox.width = boundingBox.height = 0;
  193. for( int i=0; i< tmpPoints->total; i++)
  194. {
  195. CV_READ_SEQ_ELEM( actualPoint, reader);
  196. actualPoint.x += m_startPoint.x;
  197. actualPoint.y += m_startPoint.y;
  198. boundingBox.x = MIN( boundingBox.x, actualPoint.x );
  199. boundingBox.y = MIN( boundingBox.y, actualPoint.y );
  200. boundingBox.width = MAX( boundingBox.width, actualPoint.x );
  201. boundingBox.height = MAX( boundingBox.height, actualPoint.y );
  202. CV_WRITE_SEQ_ELEM( actualPoint, writer );
  203. }
  204. cvEndWriteSeq( &writer );
  205. cvClearSeq( tmpPoints );
  206. // assign calculated bounding box
  207. ((CvContour*)m_contourPoints)->rect = boundingBox;
  208. return m_contourPoints;
  209. }