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.

514 lines
15 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // deactivation of warning 4786
  2. // (identifier was truncated to '255' characters in the debug information)
  3. #if _MSC_VER > 1000
  4. #pragma warning(disable: 4786)
  5. #pragma warning(disable: 4503)
  6. #endif
  7. #include <limits.h>
  8. #include "BlobOperators.h"
  9. /***************************************************************************
  10. Implementaci� de les classes per al c�lcul de caracter�stiques sobre el blob
  11. Implementation of the helper classes to perform operations on blobs
  12. **************************************************************************/
  13. /**
  14. - FUNCTION: Moment
  15. - FUNCTIONALITY: Calculates the pq moment of the blob
  16. - PARAMETERS:
  17. - RESULT:
  18. - returns the pq moment or 0 if the moment it is not implemented
  19. - RESTRICTIONS:
  20. - Currently, implemented moments up to 3
  21. - AUTHOR: Ricard Borr�s
  22. - CREATION DATE: 20-07-2004.
  23. - MODIFICATION: Date. Author. Description.
  24. */
  25. float CBlobGetMoment::operator()(CBlob &blob)
  26. {
  27. return blob.Moment(m_p, m_q);
  28. }
  29. /**
  30. - FUNCI�: HullPerimeter
  31. - FUNCIONALITAT: Calcula la longitud del perimetre convex del blob.
  32. Fa servir la funci� d'OpenCV cvConvexHull2 per a
  33. calcular el perimetre convex.
  34. - PAR�METRES:
  35. - RESULTAT:
  36. - retorna la longitud del per�metre convex del blob. Si el blob no t� coordenades
  37. associades retorna el per�metre normal del blob.
  38. - RESTRICCIONS:
  39. - AUTOR: Ricard Borr�s
  40. - DATA DE CREACI�: 20-07-2004.
  41. - MODIFICACI�: Data. Autor. Descripci�.
  42. */
  43. /**
  44. - FUNCTION: CBlobGetHullPerimeter
  45. - FUNCTIONALITY: Calculates the convex hull perimeter of the blob
  46. - PARAMETERS:
  47. - RESULT:
  48. - returns the convex hull perimeter of the blob or the perimeter if the
  49. blob edges could not be retrieved
  50. - RESTRICTIONS:
  51. - AUTHOR: Ricard Borr�s
  52. - CREATION DATE: 25-05-2005.
  53. - MODIFICATION: Date. Author. Description.
  54. */
  55. float CBlobGetHullPerimeter::operator()(CBlob &blob)
  56. {
  57. CvSeq *convexHull;
  58. float perimeter;
  59. convexHull = blob.GetConvexHull();
  60. if( convexHull )
  61. perimeter = fabs(cvArcLength(convexHull,CV_WHOLE_SEQ,1));
  62. else
  63. return 0;
  64. cvClearSeq(convexHull);
  65. return perimeter;
  66. }
  67. float CBlobGetHullArea::operator()(CBlob &blob)
  68. {
  69. CvSeq *convexHull;
  70. float area;
  71. convexHull = blob.GetConvexHull();
  72. if( convexHull )
  73. area = fabs(cvContourArea(convexHull));
  74. else
  75. return 0;
  76. cvClearSeq(convexHull);
  77. return area;
  78. }
  79. /**
  80. - FUNCTION: CBlobGetMinXatMinY
  81. - FUNCTIONALITY: Calculates the minimum X on the minimum Y
  82. - PARAMETERS:
  83. - RESULT:
  84. - RESTRICTIONS:
  85. - AUTHOR: Ricard Borr�s
  86. - CREATION DATE: 25-05-2005.
  87. - MODIFICATION: Date. Author. Description.
  88. */
  89. float CBlobGetMinXatMinY::operator()(CBlob &blob)
  90. {
  91. float result = LONG_MAX;
  92. CvSeqReader reader;
  93. CvPoint actualPoint;
  94. t_PointList externContour;
  95. externContour = blob.GetExternalContour()->GetContourPoints();
  96. if( !externContour ) return result;
  97. cvStartReadSeq( externContour, &reader);
  98. for( int i=0; i< externContour->total; i++)
  99. {
  100. CV_READ_SEQ_ELEM( actualPoint, reader);
  101. if( (actualPoint.y == blob.MinY()) && (actualPoint.x < result) )
  102. {
  103. result = actualPoint.x;
  104. }
  105. }
  106. return result;
  107. }
  108. /**
  109. - FUNCTION: CBlobGetMinXatMinY
  110. - FUNCTIONALITY: Calculates the minimum Y on the maximum X
  111. - PARAMETERS:
  112. - RESULT:
  113. - RESTRICTIONS:
  114. - AUTHOR: Ricard Borr�s
  115. - CREATION DATE: 25-05-2005.
  116. - MODIFICATION: Date. Author. Description.
  117. */
  118. float CBlobGetMinYatMaxX::operator()(CBlob &blob)
  119. {
  120. float result = LONG_MAX;
  121. CvSeqReader reader;
  122. CvPoint actualPoint;
  123. t_PointList externContour;
  124. externContour = blob.GetExternalContour()->GetContourPoints();
  125. if( !externContour ) return result;
  126. cvStartReadSeq( externContour, &reader);
  127. for( int i=0; i< externContour->total; i++)
  128. {
  129. CV_READ_SEQ_ELEM( actualPoint, reader);
  130. if( (actualPoint.x == blob.MaxX()) && (actualPoint.y < result) )
  131. {
  132. result = actualPoint.y;
  133. }
  134. }
  135. return result;
  136. }
  137. /**
  138. - FUNCTION: CBlobGetMaxXatMaxY
  139. - FUNCTIONALITY: Calculates the maximum X on the maximum Y
  140. - PARAMETERS:
  141. - RESULT:
  142. - RESTRICTIONS:
  143. - AUTHOR: Ricard Borr�s
  144. - CREATION DATE: 25-05-2005.
  145. - MODIFICATION: Date. Author. Description.
  146. */
  147. float CBlobGetMaxXatMaxY::operator()(CBlob &blob)
  148. {
  149. float result = LONG_MIN;
  150. CvSeqReader reader;
  151. CvPoint actualPoint;
  152. t_PointList externContour;
  153. externContour = blob.GetExternalContour()->GetContourPoints();
  154. if( !externContour ) return result;
  155. cvStartReadSeq( externContour, &reader);
  156. for( int i=0; i< externContour->total; i++)
  157. {
  158. CV_READ_SEQ_ELEM( actualPoint, reader);
  159. if( (actualPoint.y == blob.MaxY()) && (actualPoint.x > result) )
  160. {
  161. result = actualPoint.x;
  162. }
  163. }
  164. return result;
  165. }
  166. /**
  167. - FUNCTION: CBlobGetMaxYatMinX
  168. - FUNCTIONALITY: Calculates the maximum Y on the minimum X
  169. - PARAMETERS:
  170. - RESULT:
  171. - RESTRICTIONS:
  172. - AUTHOR: Ricard Borr�s
  173. - CREATION DATE: 25-05-2005.
  174. - MODIFICATION: Date. Author. Description.
  175. */
  176. float CBlobGetMaxYatMinX::operator()(CBlob &blob)
  177. {
  178. float result = LONG_MIN;
  179. CvSeqReader reader;
  180. CvPoint actualPoint;
  181. t_PointList externContour;
  182. externContour = blob.GetExternalContour()->GetContourPoints();
  183. if( !externContour ) return result;
  184. cvStartReadSeq( externContour, &reader);
  185. for( int i=0; i< externContour->total; i++)
  186. {
  187. CV_READ_SEQ_ELEM( actualPoint, reader);
  188. if( (actualPoint.x == blob.MinX()) && (actualPoint.y > result) )
  189. {
  190. result = actualPoint.y;
  191. }
  192. }
  193. return result;
  194. }
  195. /**
  196. - FUNCTION: CBlobGetElongation
  197. - FUNCTIONALITY: Calculates the elongation of the blob ( length/breadth )
  198. - PARAMETERS:
  199. - RESULT:
  200. - RESTRICTIONS:
  201. - See below to see how the lenght and the breadth are aproximated
  202. - AUTHOR: Ricard Borr�s
  203. - CREATION DATE: 25-05-2005.
  204. - MODIFICATION: Date. Author. Description.
  205. */
  206. float CBlobGetElongation::operator()(CBlob &blob)
  207. {
  208. float ampladaC,longitudC,amplada,longitud;
  209. float tmp;
  210. if( blob.Area() == 0 )
  211. return 0;
  212. tmp = blob.Perimeter()*blob.Perimeter() - 16*blob.Area();
  213. if( tmp > 0.0 )
  214. ampladaC = (float) (blob.Perimeter()+sqrt(tmp))/4;
  215. // error intr�nsec en els c�lculs de l'�rea i el per�metre
  216. else
  217. ampladaC = (float) (blob.Perimeter())/4;
  218. if(ampladaC<=0.0) return 0;
  219. longitudC=(float) blob.Area()/ampladaC;
  220. longitud=MAX( longitudC , ampladaC );
  221. amplada=MIN( longitudC , ampladaC );
  222. return (float) longitud/amplada;
  223. }
  224. /**
  225. Retorna la compacitat del blob
  226. */
  227. /**
  228. - FUNCTION: CBlobGetCompactness
  229. - FUNCTIONALITY: Calculates the compactness of the blob
  230. ( maximum for circle shaped blobs, minimum for the rest)
  231. - PARAMETERS:
  232. - RESULT:
  233. - RESTRICTIONS:
  234. - AUTHOR: Ricard Borr�s
  235. - CREATION DATE: 25-05-2005.
  236. - MODIFICATION: Date. Author. Description.
  237. */
  238. float CBlobGetCompactness::operator()(CBlob &blob)
  239. {
  240. if( blob.Area() != 0.0 )
  241. return (float) pow(blob.Perimeter(),2)/(4*CV_PI*blob.Area());
  242. else
  243. return 0.0;
  244. }
  245. /**
  246. Retorna la rugositat del blob
  247. */
  248. /**
  249. - FUNCTION: CBlobGetRoughness
  250. - FUNCTIONALITY: Calculates the roughness of the blob
  251. ( ratio between perimeter and convex hull perimeter)
  252. - PARAMETERS:
  253. - RESULT:
  254. - RESTRICTIONS:
  255. - AUTHOR: Ricard Borr�s
  256. - CREATION DATE: 25-05-2005.
  257. - MODIFICATION: Date. Author. Description.
  258. */
  259. float CBlobGetRoughness::operator()(CBlob &blob)
  260. {
  261. CBlobGetHullPerimeter getHullPerimeter = CBlobGetHullPerimeter();
  262. float hullPerimeter = getHullPerimeter(blob);
  263. if( hullPerimeter != 0.0 )
  264. return blob.Perimeter() / hullPerimeter;//HullPerimeter();
  265. return 0.0;
  266. }
  267. /**
  268. Retorna la longitud del blob
  269. */
  270. /**
  271. - FUNCTION: CBlobGetLength
  272. - FUNCTIONALITY: Calculates the lenght of the blob (the biggest axis of the blob)
  273. - PARAMETERS:
  274. - RESULT:
  275. - RESTRICTIONS:
  276. - The lenght is an aproximation to the real lenght
  277. - AUTHOR: Ricard Borr�s
  278. - CREATION DATE: 25-05-2005.
  279. - MODIFICATION: Date. Author. Description.
  280. */
  281. float CBlobGetLength::operator()(CBlob &blob)
  282. {
  283. float ampladaC,longitudC;
  284. float tmp;
  285. tmp = blob.Perimeter()*blob.Perimeter() - 16*blob.Area();
  286. if( tmp > 0.0 )
  287. ampladaC = (float) (blob.Perimeter()+sqrt(tmp))/4;
  288. // error intr�nsec en els c�lculs de l'�rea i el per�metre
  289. else
  290. ampladaC = (float) (blob.Perimeter())/4;
  291. if(ampladaC<=0.0) return 0;
  292. longitudC=(float) blob.Area()/ampladaC;
  293. return MAX( longitudC , ampladaC );
  294. }
  295. /**
  296. Retorna l'amplada del blob
  297. */
  298. /**
  299. - FUNCTION: CBlobGetBreadth
  300. - FUNCTIONALITY: Calculates the breadth of the blob (the smallest axis of the blob)
  301. - PARAMETERS:
  302. - RESULT:
  303. - RESTRICTIONS:
  304. - The breadth is an aproximation to the real breadth
  305. - AUTHOR: Ricard Borr�s
  306. - CREATION DATE: 25-05-2005.
  307. - MODIFICATION: Date. Author. Description.
  308. */
  309. float CBlobGetBreadth::operator()(CBlob &blob)
  310. {
  311. float ampladaC,longitudC;
  312. float tmp;
  313. tmp = blob.Perimeter()*blob.Perimeter() - 16*blob.Area();
  314. if( tmp > 0.0 )
  315. ampladaC = (float) (blob.Perimeter()+sqrt(tmp))/4;
  316. // error intr�nsec en els c�lculs de l'�rea i el per�metre
  317. else
  318. ampladaC = (float) (blob.Perimeter())/4;
  319. if(ampladaC<=0.0) return 0;
  320. longitudC = (float) blob.Area()/ampladaC;
  321. return MIN( longitudC , ampladaC );
  322. }
  323. /**
  324. Calcula la dist�ncia entre un punt i el centre del blob
  325. */
  326. /**
  327. - FUNCTION: CBlobGetDistanceFromPoint
  328. - FUNCTIONALITY: Calculates the euclidean distance between the blob center and
  329. the point specified in the constructor
  330. - PARAMETERS:
  331. - RESULT:
  332. - RESTRICTIONS:
  333. - AUTHOR: Ricard Borr�s
  334. - CREATION DATE: 25-05-2005.
  335. - MODIFICATION: Date. Author. Description.
  336. */
  337. float CBlobGetDistanceFromPoint::operator()(CBlob &blob)
  338. {
  339. float xmitjana, ymitjana;
  340. CBlobGetXCenter getXCenter;
  341. CBlobGetYCenter getYCenter;
  342. xmitjana = m_x - getXCenter( blob );
  343. ymitjana = m_y - getYCenter( blob );
  344. return sqrt((xmitjana*xmitjana)+(ymitjana*ymitjana));
  345. }
  346. /**
  347. - FUNCTION: BlobGetXYInside
  348. - FUNCTIONALITY: Calculates whether a point is inside the
  349. rectangular bounding box of a blob
  350. - PARAMETERS:
  351. - RESULT:
  352. - returns 1 if it is inside; o if not
  353. - RESTRICTIONS:
  354. - AUTHOR: Francesc Pinyol Margalef
  355. - CREATION DATE: 16-01-2006.
  356. - MODIFICATION: Date. Author. Description.
  357. */
  358. float CBlobGetXYInside::operator()(CBlob &blob)
  359. {
  360. if( blob.GetExternalContour()->GetContourPoints() )
  361. {
  362. return cvPointPolygonTest( blob.GetExternalContour()->GetContourPoints(), m_p,0) >= 0;
  363. }
  364. return 0;
  365. }
  366. #ifdef BLOB_OBJECT_FACTORY
  367. /**
  368. - FUNCI�: RegistraTotsOperadors
  369. - FUNCIONALITAT: Registrar tots els operadors definits a blob.h
  370. - PAR�METRES:
  371. - fabricaOperadorsBlob: f�brica on es registraran els operadors
  372. - RESULTAT:
  373. - Modifica l'objecte fabricaOperadorsBlob
  374. - RESTRICCIONS:
  375. - Nom�s es registraran els operadors de blob.h. Si se'n volen afegir, cal afegir-los amb
  376. el m�tode Register de la f�brica.
  377. - AUTOR: rborras
  378. - DATA DE CREACI�: 2006/05/18
  379. - MODIFICACI�: Data. Autor. Descripci�.
  380. */
  381. void RegistraTotsOperadors( t_OperadorBlobFactory &fabricaOperadorsBlob )
  382. {
  383. // blob shape
  384. fabricaOperadorsBlob.Register( CBlobGetArea().GetNom(), Type2Type<CBlobGetArea>());
  385. fabricaOperadorsBlob.Register( CBlobGetRelativeArea().GetNom(), Type2Type<CBlobGetRelativeArea>());
  386. fabricaOperadorsBlob.Register( CBlobGetBreadth().GetNom(), Type2Type<CBlobGetBreadth>());
  387. fabricaOperadorsBlob.Register( CBlobGetCompactness().GetNom(), Type2Type<CBlobGetCompactness>());
  388. fabricaOperadorsBlob.Register( CBlobGetElongation().GetNom(), Type2Type<CBlobGetElongation>());
  389. fabricaOperadorsBlob.Register( CBlobGetExterior().GetNom(), Type2Type<CBlobGetExterior>());
  390. fabricaOperadorsBlob.Register( CBlobGetLength().GetNom(), Type2Type<CBlobGetLength>());
  391. fabricaOperadorsBlob.Register( CBlobGetPerimeter().GetNom(), Type2Type<CBlobGetPerimeter>());
  392. fabricaOperadorsBlob.Register( CBlobGetRoughness().GetNom(), Type2Type<CBlobGetRoughness>());
  393. // blob color
  394. fabricaOperadorsBlob.Register( CBlobGetMean(NULL).GetNom(), Type2Type<CBlobGetMean>());
  395. fabricaOperadorsBlob.Register( CBlobGetReferencedMean(NULL,0).GetNom(), Type2Type<CBlobGetReferencedMean>());
  396. fabricaOperadorsBlob.Register( CBlobGetStdDev(NULL).GetNom(), Type2Type<CBlobGetStdDev>());
  397. // extern pixels
  398. fabricaOperadorsBlob.Register( CBlobGetExternPerimeterRatio().GetNom(), Type2Type<CBlobGetExternPerimeterRatio>());
  399. fabricaOperadorsBlob.Register( CBlobGetExternHullPerimeterRatio().GetNom(), Type2Type<CBlobGetExternHullPerimeterRatio>());
  400. fabricaOperadorsBlob.Register( CBlobGetExternPerimeter().GetNom(), Type2Type<CBlobGetExternPerimeter>());
  401. // hull
  402. fabricaOperadorsBlob.Register( CBlobGetHullPerimeter().GetNom(), Type2Type<CBlobGetHullPerimeter>());
  403. fabricaOperadorsBlob.Register( CBlobGetHullArea().GetNom(), Type2Type<CBlobGetHullArea>());
  404. // elipse info
  405. fabricaOperadorsBlob.Register( CBlobGetMajorAxisLength().GetNom(), Type2Type<CBlobGetMajorAxisLength>());
  406. fabricaOperadorsBlob.Register( CBlobGetMinorAxisLength().GetNom(), Type2Type<CBlobGetMinorAxisLength>());
  407. fabricaOperadorsBlob.Register( CBlobGetAxisRatio().GetNom(), Type2Type<CBlobGetAxisRatio>());
  408. fabricaOperadorsBlob.Register( CBlobGetOrientation().GetNom(), Type2Type<CBlobGetOrientation>());
  409. fabricaOperadorsBlob.Register( CBlobGetOrientationCos().GetNom(), Type2Type<CBlobGetOrientationCos>());
  410. fabricaOperadorsBlob.Register( CBlobGetAreaElipseRatio().GetNom(), Type2Type<CBlobGetAreaElipseRatio>());
  411. // min an max
  412. fabricaOperadorsBlob.Register( CBlobGetMaxX().GetNom(), Type2Type<CBlobGetMaxX>());
  413. fabricaOperadorsBlob.Register( CBlobGetMaxY().GetNom(), Type2Type<CBlobGetMaxY>());
  414. fabricaOperadorsBlob.Register( CBlobGetMinX().GetNom(), Type2Type<CBlobGetMinX>());
  415. fabricaOperadorsBlob.Register( CBlobGetMinY().GetNom(), Type2Type<CBlobGetMinY>());
  416. fabricaOperadorsBlob.Register( CBlobGetMaxXatMaxY().GetNom(), Type2Type<CBlobGetMaxXatMaxY>());
  417. fabricaOperadorsBlob.Register( CBlobGetMaxYatMinX().GetNom(), Type2Type<CBlobGetMaxYatMinX>());
  418. fabricaOperadorsBlob.Register( CBlobGetMinXatMinY().GetNom(), Type2Type<CBlobGetMinXatMinY>());
  419. fabricaOperadorsBlob.Register( CBlobGetMinYatMaxX().GetNom(), Type2Type<CBlobGetMinYatMaxX>());
  420. // coordinate info
  421. fabricaOperadorsBlob.Register( CBlobGetXYInside().GetNom(), Type2Type<CBlobGetXYInside>());
  422. fabricaOperadorsBlob.Register( CBlobGetDiffY().GetNom(), Type2Type<CBlobGetDiffY>());
  423. fabricaOperadorsBlob.Register( CBlobGetDiffX().GetNom(), Type2Type<CBlobGetDiffX>());
  424. fabricaOperadorsBlob.Register( CBlobGetXCenter().GetNom(), Type2Type<CBlobGetXCenter>());
  425. fabricaOperadorsBlob.Register( CBlobGetYCenter().GetNom(), Type2Type<CBlobGetYCenter>());
  426. fabricaOperadorsBlob.Register( CBlobGetDistanceFromPoint().GetNom(), Type2Type<CBlobGetDistanceFromPoint>());
  427. // moments
  428. fabricaOperadorsBlob.Register( CBlobGetMoment().GetNom(), Type2Type<CBlobGetMoment>());
  429. }
  430. #endif //BLOB_OBJECT_FACTORY