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.

819 lines
24 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /************************************************************************
  2. BlobResult.cpp
  3. FUNCIONALITAT: Implementaci� de la classe CBlobResult
  4. AUTOR: Inspecta S.L.
  5. MODIFICACIONS (Modificaci�, Autor, Data):
  6. **************************************************************************/
  7. #if _MSC_VER > 1000
  8. // deactivate warning (identifier was truncated to '255' characters in the
  9. // debug information) is a problem of msvc6.5
  10. #pragma warning(disable: 4786)
  11. #endif // _MSC_VER > 1000
  12. #include "BlobResult.h"
  13. #include <limits.h>
  14. #include <stdio.h>
  15. #include <functional>
  16. #include <algorithm>
  17. //! Show errors functions: only works for windows releases
  18. #ifdef _SHOW_ERRORS
  19. #include <afx.h> //suport per a CStrings
  20. #include <afxwin.h> //suport per a AfxMessageBox
  21. #endif
  22. /**************************************************************************
  23. Constructors / Destructors
  24. **************************************************************************/
  25. /**
  26. - FUNCI�: CBlobResult
  27. - FUNCIONALITAT: Constructor estandard.
  28. - PAR�METRES:
  29. - RESULTAT:
  30. - Crea un CBlobResult sense cap blob
  31. - RESTRICCIONS:
  32. - AUTOR: Ricard Borr�s
  33. - DATA DE CREACI�: 20-07-2004.
  34. - MODIFICACI�: Data. Autor. Descripci�.
  35. */
  36. /**
  37. - FUNCTION: CBlobResult
  38. - FUNCTIONALITY: Standard constructor
  39. - PARAMETERS:
  40. - RESULT:
  41. - creates an empty set of blobs
  42. - RESTRICTIONS:
  43. - AUTHOR: Ricard Borr�s
  44. - CREATION DATE: 25-05-2005.
  45. - MODIFICATION: Date. Author. Description.
  46. */
  47. CBlobResult::CBlobResult()
  48. {
  49. m_blobs = Blob_vector();
  50. }
  51. /**
  52. - FUNCI�: CBlobResult
  53. - FUNCIONALITAT: Constructor a partir d'una imatge. Inicialitza la seq��ncia de blobs
  54. amb els blobs resultants de l'an�lisi de blobs de la imatge.
  55. - PAR�METRES:
  56. - source: imatge d'on s'extreuran els blobs
  57. - mask: m�scara a aplicar. Nom�s es calcularan els blobs on la m�scara sigui
  58. diferent de 0. Els blobs que toquin a un pixel 0 de la m�scara seran
  59. considerats exteriors.
  60. - threshold: llindar que s'aplicar� a la imatge source abans de calcular els blobs
  61. - findmoments: indica si s'han de calcular els moments de cada blob
  62. - blackBlobs: true per buscar blobs negres a la binaritzazi� (it will join all extern white blobs).
  63. false per buscar blobs negres a la binaritzazi� (it will join all extern black blobs).
  64. - RESULTAT:
  65. - objecte CBlobResult amb els blobs de la imatge source
  66. - RESTRICCIONS:
  67. - AUTOR: Ricard Borr�s
  68. - DATA DE CREACI�: 25-05-2005.
  69. - MODIFICACI�: Data. Autor. Descripci�.
  70. */
  71. /**
  72. - FUNCTION: CBlob
  73. - FUNCTIONALITY: Constructor from an image. Fills an object with all the blobs in
  74. the image
  75. - PARAMETERS:
  76. - source: image to extract the blobs from
  77. - mask: optional mask to apply. The blobs will be extracted where the mask is
  78. not 0. All the neighbouring blobs where the mask is 0 will be extern blobs
  79. - threshold: threshold level to apply to the image before computing blobs
  80. - findmoments: true to calculate the blob moments (slower) (needed to calculate elipses!)
  81. - blackBlobs: true to search for black blobs in the binarization (it will join all extern white blobs).
  82. false to search for white blobs in the binarization (it will join all extern black blobs).
  83. - RESULT:
  84. - object with all the blobs in the image. It throws an EXCEPCIO_CALCUL_BLOBS
  85. if some error appears in the BlobAnalysis function
  86. - RESTRICTIONS:
  87. - AUTHOR: Ricard Borr�s
  88. - CREATION DATE: 25-05-2005.
  89. - MODIFICATION: Date. Author. Description.
  90. */
  91. CBlobResult::CBlobResult(IplImage *source, IplImage *mask, uchar backgroundColor )
  92. {
  93. bool success;
  94. try
  95. {
  96. success = ComponentLabeling( source, mask, backgroundColor, m_blobs );
  97. }
  98. catch(...)
  99. {
  100. success = false;
  101. }
  102. if( !success ) throw EXCEPCIO_CALCUL_BLOBS;
  103. }
  104. /**
  105. - FUNCI�: CBlobResult
  106. - FUNCIONALITAT: Constructor de c�pia. Inicialitza la seq��ncia de blobs
  107. amb els blobs del par�metre.
  108. - PAR�METRES:
  109. - source: objecte que es copiar�
  110. - RESULTAT:
  111. - objecte CBlobResult amb els blobs de l'objecte source
  112. - RESTRICCIONS:
  113. - AUTOR: Ricard Borr�s
  114. - DATA DE CREACI�: 25-05-2005.
  115. - MODIFICACI�: Data. Autor. Descripci�.
  116. */
  117. /**
  118. - FUNCTION: CBlobResult
  119. - FUNCTIONALITY: Copy constructor
  120. - PARAMETERS:
  121. - source: object to copy
  122. - RESULT:
  123. - RESTRICTIONS:
  124. - AUTHOR: Ricard Borr�s
  125. - CREATION DATE: 25-05-2005.
  126. - MODIFICATION: Date. Author. Description.
  127. */
  128. CBlobResult::CBlobResult( const CBlobResult &source )
  129. {
  130. m_blobs = Blob_vector( source.GetNumBlobs() );
  131. // creem el nou a partir del passat com a par�metre
  132. m_blobs = Blob_vector( source.GetNumBlobs() );
  133. std::copy( source.m_blobs.begin(), source.m_blobs.end(), m_blobs.begin() );
  134. }
  135. /**
  136. - FUNCI�: ~CBlobResult
  137. - FUNCIONALITAT: Destructor estandard.
  138. - PAR�METRES:
  139. - RESULTAT:
  140. - Allibera la mem�ria reservada de cadascun dels blobs de la classe
  141. - RESTRICCIONS:
  142. - AUTOR: Ricard Borr�s
  143. - DATA DE CREACI�: 25-05-2005.
  144. - MODIFICACI�: Data. Autor. Descripci�.
  145. */
  146. /**
  147. - FUNCTION: ~CBlobResult
  148. - FUNCTIONALITY: Destructor
  149. - PARAMETERS:
  150. - RESULT:
  151. - RESTRICTIONS:
  152. - AUTHOR: Ricard Borr�s
  153. - CREATION DATE: 25-05-2005.
  154. - MODIFICATION: Date. Author. Description.
  155. */
  156. CBlobResult::~CBlobResult()
  157. {
  158. ClearBlobs();
  159. }
  160. /**************************************************************************
  161. Operadors / Operators
  162. **************************************************************************/
  163. /**
  164. - FUNCI�: operador =
  165. - FUNCIONALITAT: Assigna un objecte source a l'actual
  166. - PAR�METRES:
  167. - source: objecte a assignar
  168. - RESULTAT:
  169. - Substitueix els blobs actuals per els de l'objecte source
  170. - RESTRICCIONS:
  171. - AUTOR: Ricard Borr�s
  172. - DATA DE CREACI�: 25-05-2005.
  173. - MODIFICACI�: Data. Autor. Descripci�.
  174. */
  175. /**
  176. - FUNCTION: Assigment operator
  177. - FUNCTIONALITY:
  178. - PARAMETERS:
  179. - RESULT:
  180. - RESTRICTIONS:
  181. - AUTHOR: Ricard Borr�s
  182. - CREATION DATE: 25-05-2005.
  183. - MODIFICATION: Date. Author. Description.
  184. */
  185. CBlobResult& CBlobResult::operator=(const CBlobResult& source)
  186. {
  187. // si ja s�n el mateix, no cal fer res
  188. if (this != &source)
  189. {
  190. // free previous blobs
  191. m_blobs.clear();
  192. // create a copy of source blobs
  193. m_blobs = Blob_vector( source.GetNumBlobs() );
  194. std::copy(source.m_blobs.begin(), source.m_blobs.end(), m_blobs.begin());
  195. }
  196. return *this;
  197. }
  198. /**
  199. - FUNCI�: operador +
  200. - FUNCIONALITAT: Concatena els blobs de dos CBlobResult
  201. - PAR�METRES:
  202. - source: d'on s'agafaran els blobs afegits a l'actual
  203. - RESULTAT:
  204. - retorna un nou CBlobResult amb els dos CBlobResult concatenats
  205. - RESTRICCIONS:
  206. - AUTOR: Ricard Borr�s
  207. - DATA DE CREACI�: 25-05-2005.
  208. - NOTA: per la implementaci�, els blobs del par�metre es posen en ordre invers
  209. - MODIFICACI�: Data. Autor. Descripci�.
  210. */
  211. /**
  212. - FUNCTION: + operator
  213. - FUNCTIONALITY: Joins the blobs in source with the current ones
  214. - PARAMETERS:
  215. - source: object to copy the blobs
  216. - RESULT:
  217. - object with the actual blobs and the source blobs
  218. - RESTRICTIONS:
  219. - AUTHOR: Ricard Borr�s
  220. - CREATION DATE: 25-05-2005.
  221. - MODIFICATION: Date. Author. Description.
  222. */
  223. CBlobResult CBlobResult::operator+( const CBlobResult& source ) const
  224. {
  225. // create a copy of current blobs
  226. CBlobResult result( *this );
  227. int numblobs = result.GetNumBlobs();
  228. // allocate space for new blobs
  229. result.m_blobs.resize( result.GetNumBlobs() + source.GetNumBlobs() );
  230. // copy new blobs
  231. std::copy( source.m_blobs.begin(), source.m_blobs.end(), result.m_blobs.begin() + numblobs );
  232. return result;
  233. }
  234. /**************************************************************************
  235. Operacions / Operations
  236. **************************************************************************/
  237. /**
  238. - FUNCI�: AddBlob
  239. - FUNCIONALITAT: Afegeix un blob al conjunt
  240. - PAR�METRES:
  241. - blob: blob a afegir
  242. - RESULTAT:
  243. - modifica el conjunt de blobs actual
  244. - RESTRICCIONS:
  245. - AUTOR: Ricard Borr�s
  246. - DATA DE CREACI�: 2006/03/01
  247. - MODIFICACI�: Data. Autor. Descripci�.
  248. */
  249. void CBlobResult::AddBlob( BlobPtr blob )
  250. {
  251. m_blobs.push_back( blob );
  252. }
  253. #ifdef MATRIXCV_ACTIU
  254. /**
  255. - FUNCI�: GetResult
  256. - FUNCIONALITAT: Calcula el resultat especificat sobre tots els blobs de la classe
  257. - PAR�METRES:
  258. - evaluador: Qualsevol objecte derivat de COperadorBlob
  259. - RESULTAT:
  260. - Retorna un array de float's amb el resultat per cada blob
  261. - RESTRICCIONS:
  262. - AUTOR: Ricard Borr�s
  263. - DATA DE CREACI�: 25-05-2005.
  264. - MODIFICACI�: Data. Autor. Descripci�.
  265. */
  266. /**
  267. - FUNCTION: GetResult
  268. - FUNCTIONALITY: Computes the function evaluador on all the blobs of the class
  269. and returns a vector with the result
  270. - PARAMETERS:
  271. - evaluador: function to apply to each blob (any object derived from the
  272. COperadorBlob class )
  273. - RESULT:
  274. - vector with all the results in the same order as the blobs
  275. - RESTRICTIONS:
  276. - AUTHOR: Ricard Borr�s
  277. - CREATION DATE: 25-05-2005.
  278. - MODIFICATION: Date. Author. Description.
  279. */
  280. double_vector CBlobResult::GetResult( funcio_calculBlob *evaluador ) const
  281. {
  282. if( GetNumBlobs() <= 0 )
  283. {
  284. return double_vector();
  285. }
  286. // definim el resultat
  287. double_vector result = double_vector( GetNumBlobs() );
  288. // i iteradors sobre els blobs i el resultat
  289. double_vector::iterator itResult = result.GetIterator();
  290. Blob_vector::const_iterator itBlobs = m_blobs.begin();
  291. // avaluem la funci� en tots els blobs
  292. while( itBlobs != m_blobs.end() )
  293. {
  294. *itResult = (*evaluador).GetOperatorResult(**itBlobs);
  295. itBlobs++;
  296. itResult++;
  297. }
  298. return result;
  299. }
  300. #endif
  301. /**
  302. - FUNCI�: GetSTLResult
  303. - FUNCIONALITAT: Calcula el resultat especificat sobre tots els blobs de la classe
  304. - PAR�METRES:
  305. - evaluador: Qualsevol objecte derivat de COperadorBlob
  306. - RESULTAT:
  307. - Retorna un array de float's STL amb el resultat per cada blob
  308. - RESTRICCIONS:
  309. - AUTOR: Ricard Borr�s
  310. - DATA DE CREACI�: 25-05-2005.
  311. - MODIFICACI�: Data. Autor. Descripci�.
  312. */
  313. /**
  314. - FUNCTION: GetResult
  315. - FUNCTIONALITY: Computes the function evaluador on all the blobs of the class
  316. and returns a vector with the result
  317. - PARAMETERS:
  318. - evaluador: function to apply to each blob (any object derived from the
  319. COperadorBlob class )
  320. - RESULT:
  321. - vector with all the results in the same order as the blobs
  322. - RESTRICTIONS:
  323. - AUTHOR: Ricard Borr�s
  324. - CREATION DATE: 25-05-2005.
  325. - MODIFICATION: Date. Author. Description.
  326. */
  327. double_stl_vector CBlobResult::GetSTLResult( funcio_calculBlob *evaluador ) const
  328. {
  329. if( GetNumBlobs() <= 0 )
  330. {
  331. return double_stl_vector();
  332. }
  333. // definim el resultat
  334. double_stl_vector result = double_stl_vector( GetNumBlobs() );
  335. // i iteradors sobre els blobs i el resultat
  336. double_stl_vector::iterator itResult = result.begin();
  337. Blob_vector::const_iterator itBlobs = m_blobs.begin();
  338. // avaluem la funci� en tots els blobs
  339. while( itBlobs != m_blobs.end() )
  340. {
  341. *itResult = (*evaluador).GetOperatorResult(**itBlobs);
  342. itBlobs++;
  343. itResult++;
  344. }
  345. return result;
  346. }
  347. /**
  348. - FUNCI�: GetNumber
  349. - FUNCIONALITAT: Calcula el resultat especificat sobre un �nic blob de la classe
  350. - PAR�METRES:
  351. - evaluador: Qualsevol objecte derivat de COperadorBlob
  352. - indexblob: n�mero de blob del que volem calcular el resultat.
  353. - RESULTAT:
  354. - Retorna un float amb el resultat
  355. - RESTRICCIONS:
  356. - AUTOR: Ricard Borr�s
  357. - DATA DE CREACI�: 25-05-2005.
  358. - MODIFICACI�: Data. Autor. Descripci�.
  359. */
  360. /**
  361. - FUNCTION: GetNumber
  362. - FUNCTIONALITY: Computes the function evaluador on a blob of the class
  363. - PARAMETERS:
  364. - indexBlob: index of the blob to compute the function
  365. - evaluador: function to apply to each blob (any object derived from the
  366. COperadorBlob class )
  367. - RESULT:
  368. - RESTRICTIONS:
  369. - AUTHOR: Ricard Borr�s
  370. - CREATION DATE: 25-05-2005.
  371. - MODIFICATION: Date. Author. Description.
  372. */
  373. float CBlobResult::GetNumber( int indexBlob, funcio_calculBlob *evaluador ) const
  374. {
  375. if( indexBlob < 0 || indexBlob >= GetNumBlobs() )
  376. RaiseError( EXCEPTION_BLOB_OUT_OF_BOUNDS );
  377. return (*evaluador).GetOperatorResult( *m_blobs[indexBlob] );
  378. }
  379. /////////////////////////// FILTRAT DE BLOBS ////////////////////////////////////
  380. /**
  381. - FUNCI�: Filter
  382. - FUNCIONALITAT: Filtra els blobs de la classe i deixa el resultat amb nom�s
  383. els blobs que han passat el filtre.
  384. El filtrat es basa en especificar condicions sobre un resultat dels blobs
  385. i seleccionar (o excloure) aquells blobs que no compleixen una determinada
  386. condicio
  387. - PAR�METRES:
  388. - dst: variable per deixar els blobs filtrats
  389. - filterAction: acci� de filtrat. Incloure els blobs trobats (B_INCLUDE),
  390. o excloure els blobs trobats (B_EXCLUDE)
  391. - evaluador: Funci� per evaluar els blobs (qualsevol objecte derivat de COperadorBlob
  392. - Condition: tipus de condici� que ha de superar la mesura (FilterType)
  393. sobre cada blob per a ser considerat.
  394. B_EQUAL,B_NOT_EQUAL,B_GREATER,B_LESS,B_GREATER_OR_EQUAL,
  395. B_LESS_OR_EQUAL,B_INSIDE,B_OUTSIDE
  396. - LowLimit: valor num�ric per a la comparaci� (Condition) de la mesura (FilterType)
  397. - HighLimit: valor num�ric per a la comparaci� (Condition) de la mesura (FilterType)
  398. (nom�s t� sentit per a aquelles condicions que tenen dos valors
  399. (B_INSIDE, per exemple).
  400. - RESULTAT:
  401. - Deixa els blobs resultants del filtrat a destination
  402. - RESTRICCIONS:
  403. - AUTOR: Ricard Borr�s
  404. - DATA DE CREACI�: 25-05-2005.
  405. - MODIFICACI�: Data. Autor. Descripci�.
  406. */
  407. /**
  408. - FUNCTION: Filter
  409. - FUNCTIONALITY: Get some blobs from the class based on conditions on measures
  410. of the blobs.
  411. - PARAMETERS:
  412. - dst: where to store the selected blobs
  413. - filterAction: B_INCLUDE: include the blobs which pass the filter in the result
  414. B_EXCLUDE: exclude the blobs which pass the filter in the result
  415. - evaluador: Object to evaluate the blob
  416. - Condition: How to decide if the result returned by evaluador on each blob
  417. is included or not. It can be:
  418. B_EQUAL,B_NOT_EQUAL,B_GREATER,B_LESS,B_GREATER_OR_EQUAL,
  419. B_LESS_OR_EQUAL,B_INSIDE,B_OUTSIDE
  420. - LowLimit: numerical value to evaluate the Condition on evaluador(blob)
  421. - HighLimit: numerical value to evaluate the Condition on evaluador(blob).
  422. Only useful for B_INSIDE and B_OUTSIDE
  423. - RESULT:
  424. - It returns on dst the blobs that accomplish (B_INCLUDE) or discards (B_EXCLUDE)
  425. the Condition on the result returned by evaluador on each blob
  426. - RESTRICTIONS:
  427. - AUTHOR: Ricard Borr�s
  428. - CREATION DATE: 25-05-2005.
  429. - MODIFICATION: Date. Author. Description.
  430. */
  431. void CBlobResult::Filter(CBlobResult &dst,
  432. int filterAction,
  433. funcio_calculBlob *evaluador,
  434. int condition,
  435. float lowLimit, float highLimit /*=0*/)
  436. {
  437. //int numBlobs = GetNumBlobs();
  438. // do the job
  439. DoFilter(dst, filterAction, evaluador, condition, &dst == this, lowLimit, highLimit );
  440. // inline operation: remove previous blobs
  441. /*if( &dst == this )
  442. {
  443. // delete original blob in inline operations ( it will be repeated if they pass the filter )
  444. m_blobs.erase( m_blobs.begin(), m_blobs.begin()+numBlobs );
  445. }*/
  446. }
  447. //! Does the Filter method job
  448. void CBlobResult::DoFilter(CBlobResult &dst, int filterAction, funcio_calculBlob *evaluador,
  449. int condition, bool inlineFilter, float lowLimit, float highLimit/* = 0*/)
  450. {
  451. int i, numBlobs, numBlobsErased;
  452. bool resultavaluacio = false, selectedBlob;
  453. double_stl_vector avaluacioBlobs;
  454. double_stl_vector::iterator itavaluacioBlobs;
  455. BlobPtr newblob;
  456. if( GetNumBlobs() <= 0 ) return;
  457. if( !evaluador ) return;
  458. //evaluate blobs using specified operator
  459. avaluacioBlobs = GetSTLResult(evaluador);
  460. itavaluacioBlobs = avaluacioBlobs.begin();
  461. numBlobs = GetNumBlobs();
  462. numBlobsErased = 0;
  463. for(i=0;i<numBlobs;i++, itavaluacioBlobs++)
  464. {
  465. switch(condition)
  466. {
  467. case B_EQUAL:
  468. resultavaluacio= *itavaluacioBlobs == lowLimit;
  469. break;
  470. case B_NOT_EQUAL:
  471. resultavaluacio= *itavaluacioBlobs != lowLimit;
  472. break;
  473. case B_GREATER:
  474. resultavaluacio= *itavaluacioBlobs > lowLimit;
  475. break;
  476. case B_LESS:
  477. resultavaluacio= *itavaluacioBlobs < lowLimit;
  478. break;
  479. case B_GREATER_OR_EQUAL:
  480. resultavaluacio= *itavaluacioBlobs >= lowLimit;
  481. break;
  482. case B_LESS_OR_EQUAL:
  483. resultavaluacio= *itavaluacioBlobs <= lowLimit;
  484. break;
  485. case B_INSIDE:
  486. resultavaluacio=( *itavaluacioBlobs >= lowLimit) && ( *itavaluacioBlobs <= highLimit);
  487. break;
  488. case B_OUTSIDE:
  489. resultavaluacio=( *itavaluacioBlobs < lowLimit) || ( *itavaluacioBlobs > highLimit);
  490. break;
  491. }
  492. // blob is selected?
  493. selectedBlob = ( resultavaluacio && filterAction == B_INCLUDE ) ||
  494. ( !resultavaluacio && filterAction == B_EXCLUDE );
  495. // add to dst if not inline operation (for inline operation we don't have to do nothing)
  496. if( !inlineFilter && selectedBlob )
  497. {
  498. dst.AddBlob( m_blobs[i-numBlobsErased] );
  499. }
  500. // remove from origin for inline operation (for not inline we don't have to do nothing)
  501. if(inlineFilter && !selectedBlob )
  502. {
  503. m_blobs.erase(m_blobs.begin() + (i - numBlobsErased) );
  504. numBlobsErased++;
  505. }
  506. }
  507. }
  508. /**
  509. - FUNCI�: GetBlob
  510. - FUNCIONALITAT: Retorna un blob si aquest existeix (index != -1)
  511. - PAR�METRES:
  512. - indexblob: index del blob a retornar
  513. - RESULTAT:
  514. - RESTRICCIONS:
  515. - AUTOR: Ricard Borr�s
  516. - DATA DE CREACI�: 25-05-2005.
  517. - MODIFICACI�: Data. Autor. Descripci�.
  518. */
  519. /*
  520. - FUNCTION: GetBlob
  521. - FUNCTIONALITY: Gets the n-th blob (without ordering the blobs)
  522. - PARAMETERS:
  523. - indexblob: index in the blob array
  524. - RESULT:
  525. - RESTRICTIONS:
  526. - AUTHOR: Ricard Borr�s
  527. - CREATION DATE: 25-05-2005.
  528. - MODIFICATION: Date. Author. Description.
  529. */
  530. CBlob CBlobResult::GetBlob(int indexblob) const
  531. {
  532. if( indexblob < 0 || indexblob >= GetNumBlobs() )
  533. RaiseError( EXCEPTION_BLOB_OUT_OF_BOUNDS );
  534. return *m_blobs[indexblob];
  535. }
  536. BlobPtr CBlobResult::GetBlobPtr(int indexblob)
  537. {
  538. if( indexblob < 0 || indexblob >= GetNumBlobs() )
  539. RaiseError( EXCEPTION_BLOB_OUT_OF_BOUNDS );
  540. return m_blobs[indexblob];
  541. }
  542. /**
  543. - FUNCI�: GetNthBlob
  544. - FUNCIONALITAT: Retorna l'en�ssim blob segons un determinat criteri
  545. - PAR�METRES:
  546. - criteri: criteri per ordenar els blobs (objectes derivats de COperadorBlob)
  547. - nBlob: index del blob a retornar
  548. - dst: on es retorna el resultat
  549. - RESULTAT:
  550. - retorna el blob nBlob a dst ordenant els blobs de la classe segons el criteri
  551. en ordre DESCENDENT. Per exemple, per obtenir el blob major:
  552. GetNthBlob( CBlobGetArea(), 0, blobMajor );
  553. GetNthBlob( CBlobGetArea(), 1, blobMajor ); (segon blob m�s gran)
  554. - RESTRICCIONS:
  555. - AUTOR: Ricard Borr�s
  556. - DATA DE CREACI�: 25-05-2005.
  557. - MODIFICACI�: Data. Autor. Descripci�.
  558. */
  559. /*
  560. - FUNCTION: GetNthBlob
  561. - FUNCTIONALITY: Gets the n-th blob ordering first the blobs with some criteria
  562. - PARAMETERS:
  563. - criteri: criteria to order the blob array
  564. - nBlob: index of the returned blob in the ordered blob array
  565. - dst: where to store the result
  566. - RESULT:
  567. - RESTRICTIONS:
  568. - AUTHOR: Ricard Borr�s
  569. - CREATION DATE: 25-05-2005.
  570. - MODIFICATION: Date. Author. Description.
  571. */
  572. void CBlobResult::GetNthBlob( funcio_calculBlob *criteri, int nBlob, CBlob &dst ) const
  573. {
  574. // verifiquem que no estem accedint fora el vector de blobs
  575. if( nBlob < 0 || nBlob >= GetNumBlobs() )
  576. {
  577. //RaiseError( EXCEPTION_BLOB_OUT_OF_BOUNDS );
  578. dst = CBlob();
  579. return;
  580. }
  581. double_stl_vector avaluacioBlobs, avaluacioBlobsOrdenat;
  582. float valorEnessim;
  583. //avaluem els blobs amb la funci� pertinent
  584. avaluacioBlobs = GetSTLResult(criteri);
  585. avaluacioBlobsOrdenat = double_stl_vector( GetNumBlobs() );
  586. // obtenim els nBlob primers resultats (en ordre descendent)
  587. std::partial_sort_copy( avaluacioBlobs.begin(),
  588. avaluacioBlobs.end(),
  589. avaluacioBlobsOrdenat.begin(),
  590. avaluacioBlobsOrdenat.end(),
  591. std::greater<float>() );
  592. valorEnessim = avaluacioBlobsOrdenat[nBlob];
  593. // busquem el primer blob que t� el valor n-ssim
  594. double_stl_vector::const_iterator itAvaluacio = avaluacioBlobs.begin();
  595. bool trobatBlob = false;
  596. int indexBlob = 0;
  597. while( itAvaluacio != avaluacioBlobs.end() && !trobatBlob )
  598. {
  599. if( *itAvaluacio == valorEnessim )
  600. {
  601. trobatBlob = true;
  602. dst = CBlob( GetBlob(indexBlob));
  603. }
  604. itAvaluacio++;
  605. indexBlob++;
  606. }
  607. }
  608. /**
  609. - FUNCI�: ClearBlobs
  610. - FUNCIONALITAT: Elimina tots els blobs de l'objecte
  611. - PAR�METRES:
  612. - RESULTAT:
  613. - Allibera tota la mem�ria dels blobs
  614. - RESTRICCIONS:
  615. - AUTOR: Ricard Borr�s Navarra
  616. - DATA DE CREACI�: 25-05-2005.
  617. - MODIFICACI�: Data. Autor. Descripci�.
  618. */
  619. /*
  620. - FUNCTION: ClearBlobs
  621. - FUNCTIONALITY: Clears all the blobs from the object and releases all its memory
  622. - PARAMETERS:
  623. - RESULT:
  624. - RESTRICTIONS:
  625. - AUTHOR: Ricard Borr�s
  626. - CREATION DATE: 25-05-2005.
  627. - MODIFICATION: Date. Author. Description.
  628. */
  629. void CBlobResult::ClearBlobs()
  630. {
  631. m_blobs.clear();
  632. }
  633. /**
  634. - FUNCI�: RaiseError
  635. - FUNCIONALITAT: Funci� per a notificar errors al l'usuari (en debug) i llen�a
  636. les excepcions
  637. - PAR�METRES:
  638. - errorCode: codi d'error
  639. - RESULTAT:
  640. - Ensenya un missatge a l'usuari (en debug) i llen�a una excepci�
  641. - RESTRICCIONS:
  642. - AUTOR: Ricard Borr�s Navarra
  643. - DATA DE CREACI�: 25-05-2005.
  644. - MODIFICACI�: Data. Autor. Descripci�.
  645. */
  646. /*
  647. - FUNCTION: RaiseError
  648. - FUNCTIONALITY: Error handling function
  649. - PARAMETERS:
  650. - errorCode: reason of the error
  651. - RESULT:
  652. - in _SHOW_ERRORS version, shows a message box with the error. In release is silent.
  653. In both cases throws an exception with the error.
  654. - RESTRICTIONS:
  655. - AUTHOR: Ricard Borr�s
  656. - CREATION DATE: 25-05-2005.
  657. - MODIFICATION: Date. Author. Description.
  658. */
  659. void CBlobResult::RaiseError(const int errorCode) const
  660. {
  661. //! Do we need to show errors?
  662. #ifdef _SHOW_ERRORS
  663. CString msg, format = "Error en CBlobResult: %s";
  664. switch (errorCode)
  665. {
  666. case EXCEPTION_BLOB_OUT_OF_BOUNDS:
  667. msg.Format(format, "Intentant accedir a un blob no existent");
  668. break;
  669. default:
  670. msg.Format(format, "Codi d'error desconegut");
  671. break;
  672. }
  673. AfxMessageBox(msg);
  674. #endif
  675. throw errorCode;
  676. }
  677. /**************************************************************************
  678. Auxiliars / Auxiliary functions
  679. **************************************************************************/
  680. /**
  681. - FUNCI�: PrintBlobs
  682. - FUNCIONALITAT: Escriu els par�metres (�rea, per�metre, exterior, mitjana)
  683. de tots els blobs a un fitxer.
  684. - PAR�METRES:
  685. - nom_fitxer: path complet del fitxer amb el resultat
  686. - RESULTAT:
  687. - RESTRICCIONS:
  688. - AUTOR: Ricard Borr�s
  689. - DATA DE CREACI�: 25-05-2005.
  690. - MODIFICACI�: Data. Autor. Descripci�.
  691. */
  692. /*
  693. - FUNCTION: PrintBlobs
  694. - FUNCTIONALITY: Prints some blob features in an ASCII file
  695. - PARAMETERS:
  696. - nom_fitxer: full path + filename to generate
  697. - RESULT:
  698. - RESTRICTIONS:
  699. - AUTHOR: Ricard Borr�s
  700. - CREATION DATE: 25-05-2005.
  701. - MODIFICATION: Date. Author. Description.
  702. */
  703. void CBlobResult::PrintBlobs( char *nom_fitxer ) const
  704. {
  705. double_stl_vector area, /*perimetre,*/ exterior, compacitat, longitud,
  706. externPerimeter, perimetreConvex, perimetre;
  707. int i;
  708. FILE *fitxer_sortida;
  709. area = GetSTLResult( CBlobGetArea());
  710. perimetre = GetSTLResult( CBlobGetPerimeter());
  711. exterior = GetSTLResult( CBlobGetExterior());
  712. compacitat = GetSTLResult(CBlobGetCompactness());
  713. longitud = GetSTLResult( CBlobGetLength());
  714. externPerimeter = GetSTLResult( CBlobGetExternPerimeter());
  715. perimetreConvex = GetSTLResult( CBlobGetHullPerimeter());
  716. fitxer_sortida = fopen( nom_fitxer, "w" );
  717. for(i=0; i<GetNumBlobs(); i++)
  718. {
  719. fprintf( fitxer_sortida, "blob %d ->\t a=%7.0f\t p=%8.2f (%8.2f extern)\t pconvex=%8.2f\t ext=%.0f\t c=%3.2f\t l=%8.2f\n",
  720. i, area[i], perimetre[i], externPerimeter[i], perimetreConvex[i], exterior[i], compacitat[i], longitud[i] );
  721. }
  722. fclose( fitxer_sortida );
  723. }
  724. /*
  725. - FUNCTION: RemoveProperty
  726. - FUNCTIONALITY: Remove a property from al contained blobs to allow recalculation
  727. - PARAMETERS:
  728. - name: property name (CBlobOperador name)
  729. - RESULT:
  730. - RESTRICTIONS:
  731. - AUTHOR: Ricard Borr�s
  732. - CREATION DATE: 19-11-2009.
  733. - MODIFICATION: Date. Author. Description.
  734. */
  735. void CBlobResult::RemoveProperty( const std::string &name )
  736. {
  737. Blob_vector::iterator pBlobs = m_blobs.begin();
  738. while( pBlobs != m_blobs.end() )
  739. {
  740. (*pBlobs)->ResetProperty(name);
  741. pBlobs++;
  742. }
  743. }