imgbase.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. //////////////////////////////////////////////////////////
  2. // imgBase.cpp : basic image processing functions
  3. //////////////////////////////////////////////////////////
  4. #include "img.h"
  5. #include "math.h"
  6. #include <fstream>
  7. int _cdecl blob_ord2(const void *, const void *);
  8. // Function : img
  9. // Description : Object constructor
  10. // Creation date : 08/04
  11. // Author : CJ
  12. // Inputs :
  13. // Outputs :
  14. img::img()
  15. {
  16. dimh = 256;
  17. dimv = 256;
  18. nbpix = dimh * dimv;
  19. itype = TYPE_UCHAR;
  20. init = 0;
  21. }
  22. // Function : ~img
  23. // Description : Object destructor
  24. // Creation date : 08/04
  25. // Author : CJ
  26. // Inputs :
  27. // Outputs :
  28. img::~img()
  29. {
  30. if ((data != 0) && (init == 1))
  31. {
  32. delete [] ((unsigned char *)data);
  33. }
  34. init = 0;
  35. }
  36. // Function : Del
  37. // Description : Free the image data
  38. // Creation date : 08/04
  39. // Author : CJ
  40. // Inputs :
  41. // Outputs :
  42. void img::Del()
  43. {
  44. if ((data != 0) && (init == 1))
  45. {
  46. delete [] ((unsigned char *)data);
  47. }
  48. init = 0;
  49. }
  50. // Function : Create
  51. // Description : Allocate the pixel data for the image
  52. // Creation date : 08/04
  53. // Author : CJ
  54. // Inputs :
  55. // Outputs : Return 1 if OK, 0 if default
  56. int img::Create(int it, int dh , int dv)
  57. {
  58. itype = it;
  59. dimh = dh;
  60. dimv = dv;
  61. nbpix = dimh * dimv;
  62. if (init == 0)
  63. {
  64. // Image 8 bits unsigned
  65. if (itype == TYPE_UCHAR)
  66. {
  67. data = (unsigned char *) new unsigned char[nbpix];
  68. }
  69. // Image 8 bits signed
  70. else if (itype == TYPE_SCHAR)
  71. {
  72. data = (char *) new char[nbpix];
  73. }
  74. // Image 16 bits unsigned
  75. else if (itype == TYPE_USHORT)
  76. {
  77. data = (unsigned short int *) new unsigned short[nbpix];
  78. }
  79. // Image 16 bits signed
  80. else if (itype == TYPE_SSHORT)
  81. {
  82. data = (short int*) new short int[nbpix];
  83. }
  84. // Image 32 bits of int
  85. else if (itype == TYPE_INT)
  86. {
  87. data = (int *) new int[nbpix];
  88. }
  89. // Image 64 bits unsigned
  90. else if (itype == TYPE_ULINT)
  91. {
  92. data = (unsigned long int *) new unsigned long int[nbpix];
  93. }
  94. // Image 32 bits of floats
  95. else if (itype == TYPE_FLOAT)
  96. {
  97. data = (float *) new float [nbpix];
  98. }
  99. // Image of doubles
  100. else if (itype == TYPE_DOUBLE)
  101. {
  102. data = (double *) new double[nbpix];
  103. }
  104. }
  105. // Allocation problem
  106. if (data == 0)
  107. {
  108. init = 0;
  109. }
  110. else
  111. {
  112. init = 1;
  113. }
  114. return init;
  115. }
  116. // Function : copyto
  117. // Description : Copy the current image to the image img2 (Image may have different types)
  118. // Creation date : 08/04
  119. // Author : CJ
  120. // Inputs :
  121. // Outputs :
  122. void img::CopyTo(img *img2)
  123. {
  124. if (itype == TYPE_UCHAR)
  125. {
  126. unsigned char *cadd;
  127. cadd = (unsigned char *) data;
  128. if (img2->itype == TYPE_UCHAR)
  129. {
  130. unsigned char *cadd2, *vcadd;
  131. cadd2 = (unsigned char *) img2->data;
  132. vcadd = (unsigned char *) memcpy(cadd2, cadd, (size_t) (nbpix * sizeof(unsigned char)));
  133. }
  134. else if (img2->itype == TYPE_USHORT)
  135. {
  136. unsigned short *sadd, *smax;
  137. sadd = (unsigned short *) img2->data;
  138. smax = sadd + img2->nbpix;
  139. while (sadd < smax)
  140. {
  141. *sadd = *cadd;
  142. cadd++;
  143. sadd++;
  144. }
  145. }
  146. }
  147. else if (itype == TYPE_USHORT)
  148. {
  149. unsigned short *cadd;
  150. cadd = (unsigned short *) data;
  151. if (img2->itype == TYPE_USHORT)
  152. {
  153. unsigned short *cadd2, *vcadd;
  154. cadd2 = (unsigned short *) img2->data;
  155. vcadd = (unsigned short *) memcpy(cadd2, cadd, (size_t) (nbpix * sizeof(unsigned short)));
  156. }
  157. else if (img2->itype == TYPE_UCHAR)
  158. {
  159. unsigned short *amax;
  160. unsigned char *cadd2;
  161. cadd2 = (unsigned char *) img2->data;
  162. amax = cadd + nbpix;
  163. while (cadd < amax)
  164. {
  165. *cadd2 = (unsigned char) *cadd;
  166. cadd++;
  167. cadd2++;
  168. }
  169. }
  170. }
  171. else if (itype == TYPE_INT)
  172. {
  173. int *cadd;
  174. cadd = (int *) data;
  175. if (img2->itype == TYPE_INT)
  176. {
  177. int *cadd2, *vcadd;
  178. cadd2 = (int *) img2->data;
  179. vcadd = (int *) memcpy(cadd2, cadd, (size_t) (nbpix * sizeof(char)));
  180. }
  181. else if (img2->itype == TYPE_UCHAR)
  182. {
  183. int *amax;
  184. unsigned char *cadd2;
  185. cadd2 = (unsigned char *) img2->data;
  186. amax = cadd + nbpix;
  187. while (cadd < amax)
  188. {
  189. *cadd2 = (unsigned char) *cadd;
  190. cadd++;
  191. cadd2++;
  192. }
  193. }
  194. else if (img2->itype == TYPE_USHORT)
  195. {
  196. int *amax;
  197. unsigned short *cadd2;
  198. cadd2 = (unsigned short *) img2->data;
  199. amax = cadd + nbpix;
  200. while (cadd < amax)
  201. {
  202. *cadd2 = (unsigned short) *cadd;
  203. cadd++;
  204. cadd2++;
  205. }
  206. }
  207. }
  208. }
  209. // Function : GetValue
  210. // Description : Get the value of a pixel
  211. // Creation date : 08/04
  212. // Author : CJ
  213. // Inputs :
  214. // Outputs :
  215. int img::GetValue(int x, int y)
  216. {
  217. int ng = 0;
  218. if ((x > 0) && (y > 0) && (x < dimh) && (y < dimv))
  219. {
  220. if ( itype == TYPE_UCHAR )
  221. {
  222. unsigned char *dadd;
  223. dadd = (unsigned char *) data;
  224. dadd += x + y * dimh;
  225. ng = *dadd;
  226. }
  227. else if ( itype == TYPE_USHORT )
  228. {
  229. unsigned short *dadd;
  230. dadd = (unsigned short *) data;
  231. dadd += x + y * dimh;
  232. ng = *dadd;
  233. }
  234. else if ( itype == TYPE_INT )
  235. {
  236. int *dadd;
  237. dadd = (int *) data;
  238. dadd += x + y * dimh;
  239. ng = *dadd;
  240. }
  241. }
  242. return(ng);
  243. }
  244. // Function : SetValue
  245. // Description : Set the value of a pixel
  246. // Creation date : 08/04
  247. // Author : CJ
  248. // Inputs :
  249. // Outputs :
  250. void img::SetValue(int x, int y, int value)
  251. {
  252. if ((x > 0) && (y > 0) && (x < dimh) && (y < dimv))
  253. {
  254. if ( itype == TYPE_UCHAR )
  255. {
  256. unsigned char *dadd;
  257. dadd = (unsigned char *) data;
  258. dadd += x + y * dimh;
  259. *dadd = (unsigned char) value;
  260. }
  261. else if ( itype == TYPE_USHORT )
  262. {
  263. unsigned short *dadd;
  264. dadd = (unsigned short *) data;
  265. dadd += x + y * dimh;
  266. *dadd = (unsigned short) value;
  267. }
  268. else if ( itype == TYPE_INT )
  269. {
  270. int *dadd;
  271. dadd = (int *) data;
  272. dadd += x + y * dimh;
  273. *dadd = value;
  274. }
  275. }
  276. }
  277. // Function : Equalize
  278. // Description : histogram equalization
  279. // Creation date : 08/04
  280. // Author : CJ
  281. // Inputs :
  282. // Outputs :
  283. void img::Equalize(int ngray)
  284. {
  285. double his[SIZEHISTO];
  286. double hissom = 0.0;
  287. int i = 0;
  288. int x = 0;
  289. int y = 0;
  290. int ng1 = 0;
  291. int ng2 = 0;
  292. for (i = 0; i < SIZEHISTO; i++) his[i] = 0;
  293. // fill the histogram
  294. for (x = 0; x < dimh; x++)
  295. {
  296. for (y = 0; y < dimv ;y++)
  297. {
  298. ng1 = GetValue(x, y);
  299. if ( ng1 < SIZEHISTO )
  300. {
  301. his[ng1]++;
  302. hissom++;
  303. }
  304. }
  305. }
  306. // cumulative histogram
  307. for (i = 1; i < SIZEHISTO; i++)
  308. {
  309. his[i] += his[i-1];
  310. }
  311. // normalisation
  312. for (i = 0; i < SIZEHISTO; i++)
  313. {
  314. his[i] = his[i] / hissom;
  315. }
  316. // Equalization
  317. for (x = 0; x < dimh; x++)
  318. {
  319. for (y = 0; y < dimv; y++)
  320. {
  321. ng1 = GetValue(x, y);
  322. if ( ng1 < SIZEHISTO )
  323. {
  324. ng2 = (int) ( ( (double) ( ngray - 1.0 ) * his[ng1] ) );
  325. if ( ng2 < SIZEHISTO )
  326. {
  327. SetValue(x, y, ng2);
  328. }
  329. }
  330. }
  331. }
  332. }
  333. // Function : Fill
  334. // Description : Fill all the image with a given gray level value
  335. // Creation date : 08/04
  336. // Author : CJ
  337. // Inputs : value of the gry level
  338. // Outputs :
  339. void img::Fill(int value)
  340. {
  341. if ( itype == TYPE_UCHAR )
  342. {
  343. unsigned char *dadd, *amax;
  344. dadd = (unsigned char *) data;
  345. amax = dadd + nbpix;
  346. while ( dadd < amax )
  347. {
  348. *dadd = (unsigned char) value;
  349. dadd++;
  350. }
  351. }
  352. else if ( itype == TYPE_USHORT )
  353. {
  354. unsigned short *dadd, *amax;
  355. dadd = (unsigned short *) data;
  356. amax = dadd + nbpix;
  357. while ( dadd < amax )
  358. {
  359. *dadd = (unsigned short) value;
  360. dadd++;
  361. }
  362. }
  363. else if ( itype == TYPE_INT )
  364. {
  365. int *dadd, *amax;
  366. dadd = (int *) data;
  367. amax = dadd + nbpix;
  368. while ( dadd < amax )
  369. {
  370. *dadd = value;
  371. dadd++;
  372. }
  373. }
  374. else if (itype == TYPE_DOUBLE)
  375. {
  376. double *dadd, *amax;
  377. dadd = (double *) data;
  378. amax = dadd + nbpix;
  379. while ( dadd < amax )
  380. {
  381. *dadd = value;
  382. dadd++;
  383. }
  384. }
  385. }
  386. //-------------------------------------------------
  387. // Basic functions of the mathematical morphology
  388. //-------------------------------------------------
  389. // Function : BinaryErosion
  390. // Description : mathematical morphological : binary erosion
  391. // Creation date : 08/04
  392. // Author : CJ
  393. // Inputs :
  394. // Outputs :
  395. void img::BinaryErosion(img *res)
  396. {
  397. unsigned short *dadd0;
  398. unsigned short *dadd;
  399. unsigned short *amax;
  400. unsigned short *dadd2;
  401. int i;
  402. int x;
  403. int y;
  404. int offset[8];
  405. int dx[8];
  406. int dy[8];
  407. dx[0] = -1; dy[0] = 1;
  408. dx[1] = 0; dy[1] = 1;
  409. dx[2] = 1; dy[2] = 1;
  410. dx[3] = -1; dy[3] = 0;
  411. dx[4] = 1; dy[4] = 0;
  412. dx[5] = -1; dy[5] = -1;
  413. dx[6] = 0; dy[6] = -1;
  414. dx[7] = 1; dy[7] = -1;
  415. offset[0] = -dimh - 1;
  416. offset[1] = -dimh;
  417. offset[2] = -dimh + 1;
  418. offset[3] = -1;
  419. offset[4] = 1;
  420. offset[5] = dimh - 1;
  421. offset[6] = dimh;
  422. offset[7] = dimh + 1;
  423. CopyTo(res);
  424. dadd0 = (unsigned short *) data;
  425. dadd = dadd0;
  426. amax = dadd0 + nbpix;
  427. dadd2 = (unsigned short *) res->data;
  428. x = 0;
  429. y = dimv -1;
  430. while ( dadd < amax )
  431. {
  432. if ( *dadd == 1 )
  433. {
  434. for(i = 0; i < 8; i++)
  435. {
  436. // Overflow
  437. if ( ( ( x + dx[i] ) < 0 ) || ( ( x + dx[i] ) > ( dimh - 1 ) )
  438. || ( ( y + dy[i] ) < 0 ) || ( ( y + dy[i] ) > ( dimv - 1 ) ) )
  439. *dadd2 = 0;
  440. else {
  441. if ( *( dadd + offset[i] ) == 0 ) {
  442. *dadd2 = 0;
  443. }
  444. }
  445. }
  446. }
  447. x++;
  448. if ( x >= dimh )
  449. {
  450. x = 0;
  451. y--;
  452. }
  453. dadd++;
  454. dadd2++;
  455. }
  456. }
  457. // Function : BinaryDilatation
  458. // Description : mathematical morphological : binary dilatation
  459. // Creation date : 08/04
  460. // Author : CJ
  461. // Inputs :
  462. // Outputs :
  463. void img::BinaryDilatation(img *res)
  464. {
  465. unsigned short *dadd0;
  466. unsigned short *dadd;
  467. unsigned short *amax;
  468. unsigned short *dadd2;
  469. int x = 0;
  470. int y = 0;
  471. int i = 0;
  472. int offset[9];
  473. int dx[8];
  474. int dy[8];
  475. offset[0] = -dimh - 1;
  476. offset[1] = -dimh;
  477. offset[2] = -dimh + 1;
  478. offset[3] = -1;
  479. offset[4] = 1;
  480. offset[5] = dimh - 1;
  481. offset[6] = dimh;
  482. offset[7] = dimh + 1;
  483. offset[8] = 0;
  484. dx[0] = -1; dy[0] = 1;
  485. dx[1] = 0; dy[1] = 1;
  486. dx[2] = 1; dy[2] = 1;
  487. dx[3] = -1; dy[3] = 0;
  488. dx[4] = 1; dy[4] = 0;
  489. dx[5] = -1; dy[5] = -1;
  490. dx[6] = 0; dy[6] = -1;
  491. dx[7] = 1; dy[7] = -1;
  492. dx[8] = 0; dy[8] = 0;
  493. CopyTo(res);
  494. dadd0 = (unsigned short *) data;
  495. dadd = dadd0 + 1 + dimh;
  496. amax = dadd0 + nbpix - 1 - dimh;
  497. dadd2 = (unsigned short *) res->data;
  498. dadd2+= 1 + dimh;
  499. x = 0;
  500. y = dimv - 1;
  501. while ( dadd < amax )
  502. {
  503. for(i = 0; i < 9; i++)
  504. {
  505. // Overflow
  506. if ( ( ( x + dx[i] ) < 0 ) || ( ( x + dx[i] ) > ( dimh - 1 ) )
  507. || ( ( y + dy[i] ) < 0 ) || ( ( y + dy[i] ) > ( dimv - 1 ) ) )
  508. *dadd2 = 0;
  509. else
  510. {
  511. if ( *( dadd + offset[i] ) == 1 )
  512. {
  513. *dadd2 = 1;
  514. }
  515. }
  516. }
  517. x++;
  518. if ( x > dimh - 1 )
  519. {
  520. x = 0;
  521. y--;
  522. }
  523. dadd++;
  524. dadd2++;
  525. }
  526. }
  527. // Function : BinaryOpening
  528. // Description : mathematical morphological : binary opening
  529. // Creation date : 08/04
  530. // Author : CJ
  531. // Inputs :
  532. // Outputs :
  533. void img::BinaryOpening(img *tamp)
  534. {
  535. img tamp1;
  536. BinaryErosion(tamp);
  537. tamp1.Create(itype, dimh, dimv);
  538. tamp->CopyTo(&tamp1);
  539. tamp1.BinaryDilatation(tamp);
  540. tamp1.Del();
  541. }
  542. // Function : BinaryClosing
  543. // Description : mathematical morphological : binary closing
  544. // Creation date : 08/04
  545. // Author : CJ
  546. // Inputs :
  547. // Outputs :
  548. void img::BinaryClosing(img *tamp)
  549. {
  550. img tamp1;
  551. BinaryDilatation(tamp);
  552. tamp1.Create(itype, dimh, dimv);
  553. tamp->CopyTo(&tamp1);
  554. tamp1.BinaryErosion(tamp);
  555. tamp1.Del();
  556. }
  557. void img::RemplaceCouleur(int a, int b)
  558. {
  559. int *dadd, *amax;
  560. dadd = (int *) data;
  561. amax = dadd + nbpix;
  562. while (dadd < amax)
  563. {
  564. if (*dadd == a)
  565. {
  566. *dadd = b;
  567. }
  568. dadd++;
  569. }
  570. }
  571. // Fonction de comparaison pour la fonction de quick sort qsort
  572. int _cdecl blob_ord2(const void *arg1, const void *arg2)
  573. {
  574. struct blob *bl1, *bl2;
  575. bl1 = (struct blob *) arg1;
  576. bl2 = (struct blob *) arg2;
  577. if (bl1->size == bl2->size)
  578. {
  579. return 0;
  580. }
  581. else if (bl1->size < bl2->size)
  582. {
  583. return 1;
  584. }
  585. else
  586. {
  587. return -1;
  588. }
  589. }
  590. // Function : BlobColoring
  591. // Description : Algorithme du Blob Coloring
  592. // Creation date : 05/01/2010
  593. // Author : CJ
  594. // Inputs :
  595. // Outputs : Renvoie -1 si on dépasse MAXBLOB
  596. int img::BlobColoring(blob tblob[MAXBLOB], img *img1)
  597. {
  598. int i, j, ic, lastblob, nblob, vsup, vleft;
  599. unsigned char *dadd, *amax;
  600. unsigned int *badd, *badd0, *apix, *apix2, *aleft, *asup, *bmax;
  601. int tlabel[MAXBLOB+1];
  602. // Génére une image binaire 0/1
  603. // seuillage(seuil);
  604. dadd = (unsigned char *) (data);
  605. badd0 = (unsigned int *) (img1->data);
  606. badd = badd0;
  607. amax = dadd + nbpix;
  608. while (dadd < amax)
  609. {
  610. *badd = *dadd;
  611. dadd++;
  612. badd++;
  613. }
  614. for (i = 0; i < MAXBLOB; i++)
  615. {
  616. tblob[i].bind = 0;
  617. tblob[i].used = 0;
  618. tblob[i].size = 0;
  619. tblob[i].first = 0;
  620. }
  621. for (i = 0; i < MAXBLOB+1; i++)
  622. {
  623. tlabel[i] = 0;
  624. }
  625. lastblob = 0;
  626. nblob = 0;
  627. // Point en haut à gauche
  628. apix = badd0;
  629. if (*apix != 0)
  630. {
  631. lastblob++;
  632. nblob++;
  633. *apix = lastblob;
  634. if (lastblob > MAXBLOB)
  635. {
  636. return -1;
  637. }
  638. tblob[lastblob].bind = lastblob;
  639. tblob[lastblob].used = 1;
  640. tblob[lastblob].size = 1;
  641. tblob[lastblob].first = apix;
  642. }
  643. // Première colonne
  644. for (i=1; i<dimh; i++)
  645. {
  646. aleft = apix++;
  647. if (*apix != 0)
  648. {
  649. if (*aleft == 0)
  650. {
  651. lastblob++;
  652. nblob++;
  653. *apix = lastblob;
  654. if (lastblob > MAXBLOB)
  655. {
  656. return -1;
  657. }
  658. tblob[lastblob].bind = lastblob;
  659. tblob[lastblob].used = 1;
  660. tblob[lastblob].size = 1;
  661. tblob[lastblob].first = apix;
  662. }
  663. else
  664. {
  665. *apix = *aleft;
  666. tblob[*apix].size++;
  667. }
  668. }
  669. }
  670. asup = apix - dimh;
  671. // Colonne 1 à dimv
  672. for (i = 1; i < dimv; i++)
  673. {
  674. aleft = apix++;
  675. asup++;
  676. // Première colonne
  677. if (*apix != 0)
  678. {
  679. if (*asup == 0)
  680. {
  681. lastblob++;
  682. nblob++;
  683. *apix = lastblob;
  684. if (lastblob > MAXBLOB)
  685. {
  686. return -1;
  687. }
  688. tblob[lastblob].bind = lastblob;
  689. tblob[lastblob].used = 1;
  690. tblob[lastblob].size = 1;
  691. tblob[lastblob].first = apix;
  692. }
  693. else
  694. {
  695. *apix = *asup;
  696. tblob[*apix].size++;
  697. }
  698. }
  699. // Colonne 1 à dimh
  700. for (ic = 1 ;ic < dimh ; ic++)
  701. {
  702. aleft = apix++;
  703. asup++;
  704. if (*apix != 0)
  705. {
  706. if (*aleft == 0)
  707. { // case left == 0
  708. if (*asup == 0)
  709. {
  710. lastblob++; nblob++;
  711. *apix = lastblob;
  712. if (lastblob > MAXBLOB)
  713. {
  714. return -1;
  715. }
  716. tblob[lastblob].bind = lastblob;
  717. tblob[lastblob].used = 1;
  718. tblob[lastblob].size = 1;
  719. tblob[lastblob].first = apix;
  720. }
  721. else
  722. {
  723. *apix = *asup;
  724. tblob[*apix].size++;
  725. }
  726. }
  727. else
  728. { // case left != 0
  729. *apix = *aleft;
  730. tblob[*apix].size++;
  731. if ((*asup != 0) && (*asup != *aleft))
  732. { // fusion
  733. nblob--;
  734. vsup = *asup;
  735. vleft = *aleft;
  736. if (tblob[vleft].size < tblob[vsup].size)
  737. {
  738. tblob[vsup].size += tblob[vleft].size;
  739. tblob[vleft].used = 0;
  740. apix2 = tblob[vleft].first;
  741. while (apix2 <= apix)
  742. {
  743. if (*apix2 == (unsigned int) vleft) *apix2 = (int) vsup;
  744. apix2++;
  745. }
  746. }
  747. else
  748. {
  749. tblob[vleft].size += tblob[vsup].size;
  750. tblob[vsup].used = 0;
  751. apix2 = tblob[vsup].first;
  752. while (apix2 <= apix)
  753. {
  754. if (*apix2 == (unsigned int) vsup) *apix2 = vleft;
  755. apix2++;
  756. }
  757. }
  758. }
  759. }
  760. }
  761. }
  762. }
  763. // Tri des blobs par taille
  764. qsort((char *)tblob, lastblob + 1, sizeof(blob), blob_ord2);
  765. if (nblob > MAXBLOB)
  766. {
  767. return -1;
  768. }
  769. j = 0;
  770. for (i = 0; i <= lastblob; i++)
  771. {
  772. if (tblob[i].used == 1)
  773. {
  774. tlabel[tblob[i].bind] = j++;
  775. }
  776. }
  777. // recoloriage (en tenant compte des blobs utilisés)
  778. badd = badd0;
  779. bmax = badd + nbpix;
  780. while (badd < bmax)
  781. {
  782. if ((*badd != 0) && (*badd < MAXBLOB))
  783. {
  784. *badd = 1 + tlabel[*badd];
  785. }
  786. badd++;
  787. }
  788. return (nblob);
  789. }
  790. // Renvoie la surface du blob d'indice nblob
  791. // L'image des blobs est en int
  792. long img::GetBlobSize(int nblob)
  793. {
  794. unsigned int *dadd, *amax;
  795. long sblob;
  796. sblob = 0;
  797. dadd = (unsigned int *) data;
  798. amax = dadd + nbpix;
  799. while (dadd < amax)
  800. {
  801. if (*dadd == (unsigned int) nblob)
  802. {
  803. sblob++;
  804. }
  805. dadd++;
  806. }
  807. return (sblob);
  808. }
  809. void img::SaveImgAsRaw()
  810. {
  811. std::ofstream ofs( "C:\\Img.raw", std::ios_base::out | std::ios_base::binary );
  812. if ( ofs.good() )
  813. {
  814. ofs.write( (char*) data, nbpix );
  815. ofs.close();
  816. }
  817. }