00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00031
00032
00033
00034
00035 #include "image.h"
00036 #include "reggr3d.h"
00037 #include <stdlib.h>
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #define ETMAX 17000000
00048
00049
00050 #define ABSOL(x,y) (((x) > (y))? abs(x-y) : abs(y-x))
00051 #define MAX(x,y) (((x)>(y))? (x):(y))
00052 #define MIN(x,y) (((x)<(y))? (x):(y))
00053 #define MIN3(x,y,z) ( ( ((x)<(y))&&((x)<(z)) )?(x):( ( ((x)>(y))&&((y)<(z)) )?y:z ) )
00054
00055
00056
00057
00058
00059
00060
00061
00071 param* reggr3d_lect(reggr3d_t* des, param* ptp, char* debq)
00072 {
00073 char question[500];
00074
00075 sprintf(question, "%s nb d'iteration ", debq);
00076 lec_param(question, ptp);
00077 des->nb_loop = atoi(ptp->rep);
00078 ptp = ptp->next;
00079
00080 sprintf(question, "%s seuil de depart aggreg de 2 regions ", debq);
00081 lec_param(question, ptp);
00082 des->seuil0 = atoi(ptp->rep);
00083 ptp = ptp->next;
00084
00085 sprintf(question, "%s incrementation du seuil ", debq);
00086 lec_param(question, ptp);
00087 des->step = atoi(ptp->rep);
00088 ptp = ptp->next;
00089
00090
00091
00092
00093
00094
00095 return(ptp);
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00117 int reggr3d_init(reggr3d_t* des, ima3Du1* im0, ima3Du4* imres)
00118 {
00119
00120
00121 imres->dimx=im0->dimx;
00122 imres->dimy=im0->dimy;
00123 imres->dimz=im0->dimz;
00124 imres->lgtete= im0->lgtete;
00125 imres->dept= im0->dept;
00126 allouer_ima3Du4 (imres);
00127
00128
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 unsigned long int equivalence_2_label( unsigned long int *pt_etiq,
00148 unsigned long int a,
00149 unsigned long int b){
00150
00151 unsigned long int minimum;
00152 unsigned long int beta;
00153 unsigned long int tmp;
00154
00155 minimum = MIN(a,b);
00156
00157
00158 beta = a;
00159 while( pt_etiq[beta]!= minimum){
00160 tmp = pt_etiq[beta];
00161 pt_etiq[beta] = minimum;
00162 beta = tmp;
00163 }
00164
00165 beta = b;
00166 while( pt_etiq[beta]!= minimum){
00167 tmp = pt_etiq[beta];
00168 pt_etiq[beta] = minimum;
00169 beta = tmp;
00170 }
00171
00172 return( minimum);
00173
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 unsigned long int equivalence_3_label( unsigned long int *pt_etiq,
00185 unsigned long int a,
00186 unsigned long int b,
00187 unsigned long int c){
00188
00189 unsigned long int minimum;
00190 unsigned long int beta;
00191 unsigned long int tmp;
00192
00193
00194 minimum = MIN3( a, b, c);
00195
00196 beta = a;
00197 while( pt_etiq[beta]!= minimum){
00198 tmp = pt_etiq[beta];
00199 pt_etiq[beta] = minimum;
00200 beta = tmp;
00201 }
00202
00203 beta = b;
00204 while( pt_etiq[beta]!= minimum){
00205 tmp = pt_etiq[beta];
00206 pt_etiq[beta] = minimum;
00207 beta = tmp;
00208 }
00209
00210 beta = c;
00211 while( pt_etiq[beta]!= minimum){
00212 tmp = pt_etiq[beta];
00213 pt_etiq[beta] = minimum;
00214 beta = tmp;
00215 }
00216 return( minimum);
00217
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 void label_management( unsigned char cas,
00315 unsigned long int haut,
00316 unsigned long int gauche,
00317 unsigned long int avant,
00318 unsigned int *courant,
00319
00320 unsigned long int *pt_etiq,
00321
00322 unsigned long int *nb_etiq
00323 )
00324 {
00325
00326
00327 if(cas>=64) printf("PROBLEME\n");
00328
00329 switch (cas) {
00330
00331 case 0:
00332 case 8:
00333 case 16:
00334 case 24:
00335 case 32:
00336 case 40:
00337 case 48:
00338 case 56:
00339
00340 {
00341 *nb_etiq +=1;
00342 if(*nb_etiq>ETMAX-1) *nb_etiq=ETMAX-1;
00343 *courant = *nb_etiq;
00344 return;
00345 }
00346
00347 case 1:
00348 case 33:
00349
00350 {
00351 *courant = pt_etiq[avant];
00352 return;
00353 }
00354
00355 case 2:
00356 case 18:
00357
00358 {
00359 *courant = pt_etiq[haut];
00360 return;
00361 }
00362
00363 case 4 :
00364 case 12:
00365
00366 {
00367 *courant = pt_etiq[gauche];
00368 return;
00369 }
00370
00371 case 3:
00372 case 9:
00373 case 10:
00374 case 11:
00375
00376 {
00377 *courant = equivalence_2_label( pt_etiq, avant, haut);
00378 return;
00379 }
00380
00381
00382 case 5:
00383 case 17:
00384 case 20:
00385 case 21:
00386
00387 {
00388 *courant = equivalence_2_label(pt_etiq, avant, gauche);
00389 return;
00390 }
00391
00392 case 6:
00393 case 34:
00394 case 36:
00395 case 38:
00396
00397 {
00398 *courant = equivalence_2_label(pt_etiq, haut, gauche);
00399 return;
00400 }
00401
00402 default:
00403
00404 {
00405 *courant = equivalence_3_label(pt_etiq, haut, gauche, avant);
00406
00407 return;
00408 }
00409 }
00410
00411 }
00412
00413
00414
00415
00416
00417
00418
00419 void
00420 reetiquetage( ima3Du1* ima_ori,
00421 ima3Du4* ima_etiq,
00422 unsigned long int *pt_etiq,
00423 unsigned long int *pt_nb_etiq,
00424 unsigned long int *pt_nb_real_etiq)
00425 {
00426 register unsigned int x,y,z;
00427 unsigned long int i;
00428 unsigned long int label_effectif_max =0;
00429 unsigned long int nb_label_effectif =1;
00430 unsigned long int nb_label_effectif2 =0;
00431 unsigned long int tmp;
00432 unsigned long int *pt_nbel;
00433 float *pt_moy;
00434
00435
00436 for (i = 0; i <= (*pt_nb_etiq); i++) {
00437 tmp = i;
00438 while (pt_etiq[tmp]!=tmp){
00439 tmp = pt_etiq[tmp];
00440 }
00441 pt_etiq[i] = tmp;
00442 }
00443
00444
00445
00446 pt_nbel = (unsigned long int *)calloc( (*pt_nb_etiq)+1, sizeof(*pt_nbel));
00447
00448 pt_moy = (float *)calloc( (*pt_nb_etiq)+1, sizeof(*pt_moy));
00449 if( pt_nbel == NULL || pt_moy == NULL){
00450 printf("Probleme d'allocation memoire\n");
00451 exit(1);
00452 }
00453
00454
00455 for(i=0; i<=(*pt_nb_etiq); i++){
00456 pt_nbel[i] = 0;
00457 pt_moy[i] = 0.0;
00458 }
00459
00460
00461
00462 for( z=0; z<ima_etiq->dimz; z++){
00463 for( x=0; x<ima_etiq->dimx; x++){
00464 for( y=0; y<ima_etiq->dimy; y++){
00465 ima_etiq->data[z][y][x] = pt_etiq[ ima_etiq->data[z][y][x] ];
00466 pt_nbel[ima_etiq->data[z][y][x]] = pt_nbel[ima_etiq->data[z][y][x]] + 1.0;
00467 }
00468 }
00469 }
00470
00471
00472
00473 for( z=0; z<ima_etiq->dimz; z++)
00474 for( x=0; x<ima_etiq->dimx; x++)
00475 for( y=0; y<ima_etiq->dimy; y++)
00476 pt_moy[ima_etiq->data[z][y][x]] +=
00477 (float)(ima_ori->data[z][y][x]) / (float)(pt_nbel[ima_etiq->data[z][y][x]]);
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488 for( i=0; i<= (*pt_nb_etiq); i++){
00489 if( pt_nbel[i]!= 0.0){
00490 ++nb_label_effectif2;
00491 }
00492 }
00493
00494
00495
00496 for( z=0; z<ima_etiq->dimz; z++)
00497 for( x=0; x<ima_etiq->dimx; x++)
00498 for( y=0; y<ima_etiq->dimy; y++)
00499 ima_ori->data[z][y][x] = (unsigned char)(pt_moy[ ima_etiq->data[z][y][x] ]);
00500
00501 *pt_nb_real_etiq = nb_label_effectif2;
00502 printf("nb_label_effectif: %d\n", nb_label_effectif2);
00503
00504
00505
00506 free(pt_nbel);
00507 free(pt_moy);
00508 }
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526 void
00527 etiquetage(ima3Du1* ima_in,
00528 ima3Du4* ima_etiq,
00529 unsigned char seuil,
00530 unsigned long int *pt_nb_etiq,
00531 unsigned long int *nb_real_etiq)
00532 {
00533 int cas;
00534 register int x,y,z;
00535 unsigned long int i;
00536 unsigned long int *pt_etiq;
00537 unsigned long int nb_etiq;
00538
00539
00540
00541
00542 pt_etiq = (unsigned long int *)calloc(ETMAX, sizeof(unsigned long int));
00543 if(pt_etiq == NULL){
00544 printf("Probleme d'allocation mémoire (pt_etiq)\n");
00545 exit(1);
00546 }
00547
00548
00549
00550 for( i=0; i<ETMAX ; i++)
00551 pt_etiq[i] = i;
00552 nb_etiq = 0;
00553
00554
00555
00556 ima_etiq->data[0][0][0] = 0;
00557
00558
00559
00560 x = 0;
00561 z = 0;
00562 for( y=1; y<ima_in->dimy; y++){
00563 cas = 0;
00564
00565 if( ABSOL( ima_in->data[z][y][x], ima_in->data[z][y-1][x]) <= seuil)
00566 cas += 4;
00567 label_management( cas,
00568 0,
00569 ima_etiq->data[z][y-1][x],
00570 0,
00571 &ima_etiq->data[z][y][x],
00572 pt_etiq,
00573 &nb_etiq
00574 );
00575 }
00576
00577
00578
00579
00580
00581
00582 z = 0;
00583 for( x=1; x<ima_in->dimx; x++){
00584 for( y=0; y<ima_in->dimy; y++){
00585 cas = 0;
00586
00587
00588 if( ABSOL( ima_in->data[z][y][x], ima_in->data[z][y][x-1]) <= seuil)
00589 cas += 2;
00590
00591
00592
00593 if( y!=0){
00594
00595 if( ABSOL( ima_in->data[z][y][x], ima_in->data[z][y-1][x]) <= seuil)
00596 cas += 4;
00597 if( ABSOL( ima_in->data[z][y-1][x], ima_in->data[z][y][x-1]) <= seuil)
00598 cas += 32;
00599 label_management( cas,
00600 ima_etiq->data[z][y][x-1] ,
00601 ima_etiq->data[z][y-1][x],
00602 0,
00603 &ima_etiq->data[z][y][x],
00604 pt_etiq,
00605 &nb_etiq
00606 );
00607 }
00608 else{
00609 label_management( cas,
00610 ima_etiq->data[z][y][x-1] ,
00611 0,
00612 0,
00613 &ima_etiq->data[z][y][x],
00614 pt_etiq,
00615 &nb_etiq
00616 );
00617 }
00618 }
00619 }
00620
00621
00622
00623 for( z=1; z<ima_in->dimz; z++){
00624 for( x=0; x<ima_in->dimx; x++){
00625 for( y=0; y<ima_in->dimy; y++){
00626 cas = 0;
00627
00628
00629 if( ABSOL( ima_in->data[z][y][x], ima_in->data[z-1][y][x]) <= seuil)
00630 cas += 1;
00631
00632
00633
00634 if(x==0){
00635 if(y!=0){
00636 if( ABSOL( ima_in->data[z][y][x], ima_in->data[z][y-1][x]) <= seuil)
00637 cas += 4;
00638 if( ABSOL( ima_in->data[z][y-1][x], ima_in->data[z-1][y][x]) <= seuil)
00639 cas += 16;
00640 label_management( cas,
00641 0 ,
00642 ima_etiq->data[z][y-1][x] ,
00643 ima_etiq->data[z-1][y][x] ,
00644 &ima_etiq->data[z][y][x],
00645 pt_etiq,
00646 &nb_etiq
00647 );
00648
00649 }
00650 else{
00651 label_management( cas,
00652 0 ,
00653 0 ,
00654 ima_etiq->data[z-1][y][x] ,
00655 &ima_etiq->data[z][y][x],
00656 pt_etiq,
00657 &nb_etiq
00658 );
00659
00660 }
00661 }
00662 else{
00663
00664
00665 if( ABSOL( ima_in->data[z][y][x], ima_in->data[z][y][x-1]) <= seuil)
00666 cas += 2;
00667 if( ABSOL( ima_in->data[z][y][x-1], ima_in->data[z-1][y][x]) <= seuil)
00668 cas += 8;
00669
00670 if(y!=0){
00671 if( ABSOL( ima_in->data[z][y][x], ima_in->data[z][y-1][x]) <= seuil)
00672 cas += 4;
00673 if( ABSOL( ima_in->data[z][y-1][x], ima_in->data[z-1][y][x]) <= seuil)
00674 cas += 16;
00675 if( ABSOL( ima_in->data[z][y-1][x], ima_in->data[z][y][x-1]) <= seuil)
00676 cas += 32;
00677 label_management( cas,
00678 ima_etiq->data[z][y][x-1] ,
00679 ima_etiq->data[z][y-1][x] ,
00680 ima_etiq->data[z-1][y][x] ,
00681 &ima_etiq->data[z][y][x],
00682 pt_etiq,
00683 &nb_etiq
00684 );
00685 }
00686 else{
00687 label_management( cas,
00688 ima_etiq->data[z][y][x-1] ,
00689 0 ,
00690 ima_etiq->data[z-1][y][x] ,
00691 &ima_etiq->data[z][y][x],
00692 pt_etiq,
00693 &nb_etiq
00694 );
00695 }
00696 }
00697
00698
00699 }
00700 }
00701 }
00702
00703
00704 printf("nb_etiq = %d (max %d)\n", nb_etiq+1, ETMAX);
00705 if( nb_etiq+1 == ETMAX) {
00706 printf("-----DANGER Pas assez d'etiquettes !!!----\n");
00707 }
00708
00709 *pt_nb_etiq = nb_etiq+1;
00710
00711
00712 reetiquetage( ima_in,
00713 ima_etiq,
00714 pt_etiq,
00715 &nb_etiq,
00716 nb_real_etiq);
00717
00718
00719 free( pt_etiq);
00720 }
00721
00722
00723
00724
00725
00726
00727
00738 int reggr3d_calc(reggr3d_t *des, ima3Du1* im0, ima3Du4 *imres)
00739 {
00740
00741 unsigned char i;
00742
00743 for( i=0; i<des->nb_loop; i++){
00744 printf("-------Etiquettage %d --- seuil %d -----\n", i, des->seuil0 + (i*des->step));
00745 etiquetage( im0,
00746 imres,
00747 des->seuil0 + (i*des->step),
00748 &(des->nb_etiq),
00749 &(des->nb_label)
00750 );
00751 }
00752
00753
00754
00755 }