-
Notifications
You must be signed in to change notification settings - Fork 11
/
lwshedtopo.c
1048 lines (951 loc) · 30.6 KB
/
lwshedtopo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright ESIEE (2009)
m.couprie@esiee.fr
This software is an image processing library whose purpose is to be
used primarily for research and teaching.
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
*/
/*
Ligne de partage des eaux topologique (nouvelle version)
Ref: CBN04
Michel Couprie - septembre 2003
Laurent Najman - mai 2005
Updates:
MC - cor. bug mem. alloc. - 12 juillet 2005
MC - cor. bug dans Watershed - 24 septembre 2007
*/
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <math.h>
#include <mcimage.h>
#include <mccodimage.h>
#include <mcfahsalembier.h>
#include <mccomptree.h>
#include <mcutil.h>
#include <mcindic.h>
#include <lwshedtopo.h>
#include <assert.h>
static void compressTree(ctree *CT, int32_t *CM, int32_t *newCM, int32_t N);
static void reconsTree(ctree *CT, int32_t *CM, int32_t *newCM, int32_t N, uint8_t *G);
#define EN_FAHS 0
#define WATERSHED 1
#define MASSIF 2
#define MODIFIE 4
#define LCA1 0x08
//#define VERBOSE
//#define _DEBUG_
//#define PARANO
// If you want the first version (slower),
// uncomment the following line
//#define OLDVERSION
// If you want the first version of lwshedtopobin (slower, BUT WHICH WORKS),
// uncomment the following line
//#define OLDVERSIONBIN
// If you want to compute slowly the LCA in the watershed, uncomment the following line
//#define LCASLOW
/* ==================================== */
static int32_t TrouveComposantes(int32_t x, uint8_t *F, int32_t rs, int32_t ps, int32_t N, int32_t connex,
int32_t *CM, int32_t *tabcomp)
/* ==================================== */
// variante sans simplification
// place la plus haute composante (ou l'une des plus hautes) en premier
{
int32_t k, y, n = 1, maxval = F[x], first = 1;
switch (connex)
{
case 4:
for (k = 0; k < 8; k += 2) // parcourt les c-voisins y de x d'un niveau > F[x]
{ y = voisin(x, k, rs, N);
if ((y != -1) && (F[y] > maxval)) maxval = F[y];
} /* for (k = 0; k < 8; k += 2) */
if (maxval == F[x]) return 0;
for (k = 0; k < 8; k += 2) // parcourt les c-voisins y de x d'un niveau > F[x]
{ y = voisin(x, k, rs, N);
if ((y != -1) && (F[y] > F[x]))
{ if (first && (F[y] == maxval)) { tabcomp[0] = CM[y]; first = 0; }
else { tabcomp[n] = CM[y]; n++; }
}
} break;
case 8:
for (k = 0; k < 8; k += 1) // parcourt les c-voisins y de x d'un niveau > F[x]
{ y = voisin(x, k, rs, N);
if ((y != -1) && (F[y] > maxval)) maxval = F[y];
} /* for (k = 0; k < 8; k += 1) */
if (maxval == F[x]) return 0;
for (k = 0; k < 8; k += 1) // parcourt les c-voisins y de x d'un niveau > F[x]
{ y = voisin(x, k, rs, N);
if ((y != -1) && (F[y] > F[x]))
{ if (first && (F[y] == maxval)) { tabcomp[0] = CM[y]; first = 0; }
else { tabcomp[n] = CM[y]; n++; }
}
} break;
case 6:
for (k = 0; k <= 10; k += 2) // parcourt les c-voisins y de x d'un niveau > F[x]
{ y = voisin6(x, k, rs, ps, N);
if ((y != -1) && (F[y] > maxval)) maxval = F[y];
}
if (maxval == F[x]) return 0;
for (k = 0; k <= 10; k += 2) // parcourt les c-voisins y de x d'un niveau > F[x]
{ y = voisin6(x, k, rs, ps, N);
if ((y != -1) && (F[y] > F[x]))
{ if (first && (F[y] == maxval)) { tabcomp[0] = CM[y]; first = 0; }
else { tabcomp[n] = CM[y]; n++; }
}
} break;
case 18:
for (k = 0; k < 18; k += 1) // parcourt les c-voisins y de x d'un niveau > F[x]
{ y = voisin18(x, k, rs, ps, N);
if ((y != -1) && (F[y] > maxval)) maxval = F[y];
}
if (maxval == F[x]) return 0;
for (k = 0; k < 18; k += 1) // parcourt les c-voisins y de x d'un niveau > F[x]
{ y = voisin18(x, k, rs, ps, N);
if ((y != -1) && (F[y] > F[x]))
{ if (first && (F[y] == maxval)) { tabcomp[0] = CM[y]; first = 0; }
else { tabcomp[n] = CM[y]; n++; }
}
} break;
case 26:
for (k = 0; k < 26; k += 1) // parcourt les c-voisins y de x d'un niveau > F[x]
{ y = voisin26(x, k, rs, ps, N);
if ((y != -1) && (F[y] > maxval)) maxval = F[y];
}
if (maxval == F[x]) return 0;
for (k = 0; k < 26; k += 1) // parcourt les c-voisins y de x d'un niveau > F[x]
{ y = voisin26(x, k, rs, ps, N);
if ((y != -1) && (F[y] > F[x]))
{ if (first && (F[y] == maxval)) { tabcomp[0] = CM[y]; first = 0; }
else { tabcomp[n] = CM[y]; n++; }
}
} break;
} // switch (connex)
return n;
} // TrouveComposantes()
#ifdef __GNUC__
static int32_t LowComAncSlow(ctree * CT, int32_t c1, int32_t c2) __attribute__ ((unused));
#endif
/* ==================================== */
static int32_t LowComAncSlow(
ctree * CT,
int32_t c1,
int32_t c2)
/* Retourne le plus proche commun ancetre des cellules c1,c2
Utilise le champ "flags".
*/
/* ==================================== */
#undef F_NAME
#define F_NAME "LowComAncSlow"
{
int32_t x, lca = -1;
x = c1; do
{
CT->flags[x] |= LCA1; /* marque LCA1 tous les ancetres de x */
x = CT->tabnodes[x].father;
} while (x != -1);
x = c2; do
{ /* remonte les ancetres de x */
if (CT->flags[x] & LCA1) { lca = x; break; }
x = CT->tabnodes[x].father;
} while (x != -1);
x = c1; do
{ /* derniere remontee: demarque */
CT->flags[x] &= ~LCA1;
x = CT->tabnodes[x].father;
} while (x != -1);
#ifdef PARANO
if (lca == -1)
{
fprintf(stderr, "%s: lca not found\n", F_NAME);
exit(0);
}
#endif
return lca;
} // LowComAncSlow()
/////////////////////////////////////////
// lca : Nearest (Lowest) Common Ancestor
//
// From: The LCA Problem Revisited
// M.A. Bender - M. Farach-Colton
//
// Depth-first preprocessing
int32_t LCApreprocessDepthFirst(ctree *CT, int32_t node, int32_t depth, int32_t *nbr, int32_t *rep, int32_t *Euler, int32_t *Represent, int32_t *Depth, int32_t *Number)
{
int32_t son;
soncell *sc;
if (CT->tabnodes[node].nbsons > -1) {
(*nbr)++;
Euler[*nbr] = node;
Number[node] = *nbr;
Depth[node] = depth;
Represent[*nbr] = node;
//Represent[(*rep)++] = *nbr;
(*rep)++;
for (sc = CT->tabnodes[node].sonlist; sc != NULL; sc = sc->next) {
son = sc->son;
LCApreprocessDepthFirst(CT, son, depth+1, nbr, rep, Euler, Represent, Depth, Number);
Euler[++(*nbr)] = node;
}
}
return *nbr;
}
int32_t ** LCApreprocess(ctree *CT, int32_t *Euler, int32_t *Depth, int32_t *Represent, int32_t *Number, int32_t *nbR, int32_t *lognR)
#undef F_NAME
#define F_NAME "LCApreprocess"
{
//O(n.log(n)) preprocessing
int32_t nbr, rep, nbNodes;
int32_t nbRepresent;
int32_t logn;
int32_t i,j,k1,k2;
int32_t *minim;
int32_t **Minim;
nbr = -1; // Initialization number of euler nodes
rep = 0;
nbr = LCApreprocessDepthFirst(CT, CT->root, 0, &nbr, &rep, Euler, Represent, Depth, Number);
#ifdef _DEBUG_
ComponentTreePrint(CT);
#endif
nbNodes = rep;
// Check that the number of nodes in the tree was correct
#ifdef _DEBUG_
printf("rep = %d, nbr = %d, nbnodes = %d, 2*nbnodes = %d\n", rep, nbr, nbNodes, 2*nbNodes);
#endif
assert((nbr+1) == (2*nbNodes-1));
nbRepresent = 2*nbNodes-1;
logn = (int32_t)(ceil(log((double)(nbRepresent))/log(2.0)));
*nbR = nbRepresent;
*lognR = logn;
minim = (int32_t *)calloc(logn*nbRepresent, sizeof(int32_t));
Minim = (int32_t **)calloc(logn, sizeof(int32_t*));
if ((minim == NULL) || (Minim == NULL)) {
fprintf(stderr, "%s : malloc failed\n", F_NAME);
return NULL;
}
Minim[0] = minim;
for (i=0; i<nbRepresent-1; i++) {
if (Depth[Euler[i]] < Depth[Euler[i+1]]) {
Minim[0][i] = i;
} else {
Minim[0][i] = i+1;
}
}
Minim[0][nbRepresent-1] = nbRepresent-1;
for (j=1; j<logn; j++) {
k1 = 1<<(j-1);
k2 = k1<<1;
Minim[j] = &minim[j*nbRepresent];
for (i=0; i<nbRepresent; i++) {
if ((i+ k2) >= nbRepresent) {
Minim[j][i] = nbRepresent-1;
} else {
if (Depth[Euler[Minim[j-1][i]]] <= Depth[Euler[Minim[j-1][i+k1]]]) {
Minim[j][i] = Minim[j-1][i];
} else {
Minim[j][i] = Minim[j-1][i+k1];
}
}
}
}
#ifdef _DEBUG_
for (i=0; i<logn; i++) {
for (j=0; j<nbRepresent; j++)
printf("M[%d][%d] = %d - ", i, j, Minim[i][j]);
printf("\n");
}
#endif
return Minim;
}
int32_t LowComAncFast(int32_t n1, int32_t n2, int32_t *Euler, int32_t *Number, int32_t *Depth, int32_t **Minim)
#undef F_NAME
#define F_NAME "LowComAncFast"
{
int32_t ii, jj, kk, k;
ii = Number[n1];
jj = Number[n2];
if (ii == jj)
return ii;
if (ii > jj) {
kk = jj;
jj = ii;
ii = kk;
}
k = (int32_t)(log((double)(jj - ii))/log(2.));
if (Depth[Euler[Minim[k][ii]]] < Depth[Euler[Minim[k][jj-(1<<(k))]]]) {
return Number[Euler[Minim[k][ii]]];
} else {
return Number[Euler[Minim[k][jj-(1<<k)]]];
}
}
/* ==================================== */
static void W_Constructible(int32_t x, uint8_t *F, int32_t rs, int32_t ps, int32_t N, int32_t connex,
ctree *CT, int32_t *CM, int32_t *tabcomp,
int32_t *c, int32_t *lcalevel
#ifndef LCASLOW
, int32_t *Euler, int32_t *Represent, int32_t *Depth, int32_t *Number, int32_t **Minim
#endif
)
/* ==================================== */
// Si x est W-construcible, le couple [c, lcalevel] représente la composante (avec son niveau)
// à laquelle x peut être ajouté.
// Sinon la valeur retournée dans c est -1
{
int32_t c1, k, ncomp = TrouveComposantes(x, F, rs, ps, N, connex, CM, tabcomp);
if (ncomp > 0)
{
if (ncomp == 1) *c = tabcomp[0];
else
{
*c = tabcomp[0];
for (k = 1; k < ncomp; k++)
{
#ifdef LCASLOW
c1 = LowComAncSlow(CT, *c, tabcomp[k]);
#else
c1 = Represent[LowComAncFast(*c, tabcomp[k], Euler, Number, Depth, Minim)];
#endif
if (c1 != tabcomp[k]) *c = c1;
}
}
*lcalevel = CT->tabnodes[*c].data;
if (*lcalevel <= F[x]) *c = -1;
}
else *c = -1;
} // W_Constructible()
/* ==================================== */
static void Watershed(struct xvimage *image, int32_t connex,
Fahs * FAHS, int32_t *CM, ctree * CT)
/* ==================================== */
//
// inondation a partir des voisins des maxima, suivant les ndg decroissants
// nouvelle (08/03) caracterisation des points destructibles
// nouvelle (09/03) construction de l'arbre des composantes
// Nouvelle (05/05) lca en temps constant + qq optimisation de l'article JMIV (najmanl)
#undef F_NAME
#define F_NAME "Watershed"
{
uint8_t *F = UCHARDATA(image);
int32_t rs = rowsize(image);
int32_t ps = rs * colsize(image);
int32_t N = ps * depth(image);
int32_t i, k, x, y;
int32_t c; /* une composante */
int32_t tabcomp[26]; /* liste de composantes */
int32_t nbelev; /* nombre d'elevations effectuees */
int32_t lcalevel; /* niveau du lca */
int32_t incr_vois;
#ifndef LCASLOW
int32_t logn, nbRepresent;
int32_t *Euler, *Depth, *Represent, *Number, **Minim;
#endif
// INITIALISATIONS
FahsFlush(FAHS); // Re-initialise la FAHS
#ifndef LCASLOW
Euler = (int32_t *)calloc(2*CT->nbnodes-1, sizeof(int32_t));
Represent = (int32_t *)calloc(CT->nbnodes, sizeof(int32_t));
Depth = (int32_t *)calloc(CT->nbnodes, sizeof(int32_t));
Number = (int32_t *)calloc(CT->nbnodes, sizeof(int32_t));
if ((Euler == NULL) || (Represent == NULL)
|| (Depth == NULL) || (Number == NULL)) {
fprintf(stderr, "%s : malloc failed\n", F_NAME);
return;
}
Minim = LCApreprocess(CT, Euler, Depth, Represent, Number, &nbRepresent, &logn);
#ifdef _DEBUG_
printf("Comparison Slow/Fast lca\n");
{
int32_t i,j, anc;
int32_t nbErrors = 0;
for (i=0; i<CT->nbnodes; i++)
for (j=0; j<CT->nbnodes; j++) {
if ((CT->tabnodes[i].nbsons > -1) && (CT->tabnodes[j].nbsons > -1)) {
anc = Represent[LowComAncFast(i,j,Euler,Number,Depth,Minim)];
if (anc != LowComAncSlow(CT,i,j)) {
nbErrors++;
printf("Error node lca(%d, %d) = %d ou %d ?\n", i, j, anc, LowComAncSlow(CT,i,j));
} else {
printf("Ok node lca(%d, %d) = %d\n", i, j, anc);
}
}
}
printf("Nb errors = %d\n",nbErrors);
}
fflush(stdout);
#endif // _DEBUG_
#endif //ifndef LCASLOW
switch (connex)
{
case 4: incr_vois = 2; break;
case 8: incr_vois = 1; break;
} /* switch (connex) */
// etiquetage des c-maxima (doit pouvoir se faire au vol lors de la construction de l'arbre)
for (i = 0; i < N; i++)
{
c = CM[i];
if (CT->tabnodes[c].nbsons == 0) {
Set(i,MASSIF);
}
} // for (i = 0; i < N; i++)
// empile les points
for (i = 0; i < N; i++)
{
#ifdef OLDVERSION
// empile tous les points
Set(i,EN_FAHS);
FahsPush(FAHS, i, NDG_MAX - F[i]);
#else
// empile les points voisins d'un minima
char flag=0;
switch (connex)
{
case 4:
case 8:
for (k = 0; (k < 8) && (flag == 0); k += incr_vois)
{ y = voisin(i, k, rs, N);
if ((y != -1) && (IsSet(y,MASSIF)))
{ flag = 1; }
} break;
case 6:
for (k = 0; k <= 10; k += 2)
{ y = voisin6(i, k, rs, ps, N);
if ((y != -1) && (IsSet(y,MASSIF)))
{ flag = 1; }
} break;
case 18:
for (k = 0; k < 18; k += 1)
{ y = voisin18(i, k, rs, ps, N);
if ((y != -1) && (IsSet(y,MASSIF)))
{ flag = 1; }
} break;
case 26:
for (k = 0; k < 26; k += 1)
{ y = voisin26(i, k, rs, ps, N);
if ((y != -1) && (IsSet(y,MASSIF)))
{ flag = 1; }
} break;
} /* switch (connex) */
if (flag) {
Set(i,EN_FAHS); FahsPush(FAHS, i, NDG_MAX - F[i]);
}
#endif
} // for (i = 0; i < N; i++)
// BOUCLE PRINCIPALE
nbelev = 0;
while (!FahsVide(FAHS))
{
x = FahsPop(FAHS);
UnSet(x,EN_FAHS);
W_Constructible(x, F, rs, ps, N, connex, CT, CM, tabcomp, &c, &lcalevel
#ifndef LCASLOW
, Euler, Represent, Depth, Number, Minim
#endif
);
if (c != -1)
{
nbelev++;
F[x] = lcalevel; // eleve le niveau du point x
CM[x] = c; // maj pointeur image -> composantes
Set(x,MODIFIE);
if (CT->tabnodes[c].nbsons == 0) // feuille
{
Set(x,MASSIF);
} // if feuille
else
if (CT->tabnodes[c].nbsons > 1) // noeud
{
}
#ifdef PARANO
else
printf("%s : ERREUR COMPOSANTE BRANCHE!!!\n", F_NAME);
#endif
// empile les c-voisins de x non marques MASSIF ni EN_FAHS
switch (connex)
{
case 4:
case 8:
for (k = 0; k < 8; k += incr_vois)
{ y = voisin(x, k, rs, N);
if ((y != -1) && (!IsSet(y,MASSIF)) && (!IsSet(y,EN_FAHS)))
{ Set(y,EN_FAHS); FahsPush(FAHS, y, NDG_MAX - F[y]); }
} break;
case 6:
for (k = 0; k <= 10; k += 2)
{ y = voisin6(x, k, rs, ps, N);
if ((y != -1) && (!IsSet(y,MASSIF)) && (!IsSet(y,EN_FAHS)))
{ Set(y,EN_FAHS); FahsPush(FAHS, y, NDG_MAX - F[y]); }
} break;
case 18:
for (k = 0; k < 18; k += 1)
{ y = voisin18(x, k, rs, ps, N);
if ((y != -1) && (!IsSet(y,MASSIF)) && (!IsSet(y,EN_FAHS)))
{ Set(y,EN_FAHS); FahsPush(FAHS, y, NDG_MAX - F[y]); }
} break;
case 26:
for (k = 0; k < 26; k += 1)
{ y = voisin26(x, k, rs, ps, N);
if ((y != -1) && (!IsSet(y,MASSIF)) && (!IsSet(y,EN_FAHS)))
{ Set(y,EN_FAHS); FahsPush(FAHS, y, NDG_MAX - F[y]); }
} break;
} /* switch (connex) */
} // if (c != -1)}
} // while (!FahsVide(FAHS))
#ifdef VERBOSE
printf("Nombre d'elevations %d\n", nbelev);
#endif
#ifndef LCASLOW
free(Euler);
free(Represent);
free(Depth);
free(Number);
free(Minim[0]);
free(Minim);
#endif //LCASLOW
} // Watershed()
/* ==================================== */
int32_t lwshedtopo_lwshedtopo(struct xvimage *image, int32_t connex)
/* ==================================== */
/*! \fn int32_t lwshedtopo(struct xvimage *image, int32_t connex)
\param image (entrée/sortie) : une image 2D ndg
\param connex (entrée) : 4 ou 8 (2D), 6, 18 ou 26 (3D)
\return code erreur : 0 si échec, 1 sinon
\brief ligne de partage des eaux "topologique" (algo MC, GB, LN)
*/
#undef F_NAME
#define F_NAME "lwshedtopo_lwshedtopo"
{
register int32_t i; /* index muet */
int32_t rs = rowsize(image); /* taille ligne */
int32_t cs = colsize(image); /* taille colonne */
int32_t ds = depth(image); /* nb plans */
int32_t ps = rs * cs; /* taille plan */
int32_t N = ps * ds; /* taille image */
uint8_t *F = UCHARDATA(image); /* l'image de depart */
Fahs * FAHS; /* la file d'attente hierarchique */
int32_t *CM, *newCM; /* etat d'un pixel */
ctree * CT; /* resultat : l'arbre des composantes */
FAHS = CreeFahsVide(N);
if (FAHS == NULL)
{ fprintf(stderr, "%s() : CreeFahsVide failed\n", F_NAME);
return 0;
}
if ((connex == 4) || (connex == 8))
{
if (!ComponentTree(F, rs, N, connex, &CT, &CM))
{ fprintf(stderr, "%s() : ComponentTree failed\n", F_NAME);
return 0;
}
}
else if ((connex == 6) || (connex == 18) || (connex == 26))
{
if (!ComponentTree3d(F, rs, ps, N, connex, &CT, &CM))
{ fprintf(stderr, "%s() : ComponentTree failed\n", F_NAME);
return 0;
}
}
else
{ fprintf(stderr, "%s() : bad value for connex : %d\n", F_NAME, connex);
return 0;
}
#ifndef OLDVERSION
newCM = (int32_t *)calloc(CT->nbnodes, sizeof(int32_t));
if (newCM == NULL) {
fprintf(stderr, "%s : malloc failed\n", F_NAME);
return 0;
}
compressTree(CT, CM, newCM, N);
free(newCM);
#endif
IndicsInit(N);
Watershed(image, connex, FAHS, CM, CT);
for (i=0; i<N; i++)
F[i] = CT->tabnodes[CM[i]].data;
/* ================================================ */
/* UN PEU DE MENAGE */
/* ================================================ */
IndicsTermine();
FahsTermine(FAHS);
ComponentTreeFree(CT);
free(CM);
return(1);
} /* lwshedtopo_lwshedtopo() */
/* ==================================== */
static void Reconstruction(struct xvimage *g, struct xvimage *f, int32_t *CM, ctree * CT)
/* ==================================== */
#undef F_NAME
#define F_NAME "Reconstruction"
{
uint8_t *F = UCHARDATA(f);
uint8_t *G = UCHARDATA(g);
int32_t rs = rowsize(f); /* taille ligne */
int32_t cs = colsize(f); /* taille colonne */
int32_t ds = depth(f); /* nb plans */
int32_t ps = rs * cs; /* taille plan */
int32_t N = ps * ds; /* taille image */
int32_t i, c, d;
for (i = 0; i < N; i++) if (G[i] >= F[i]) CT->flags[CM[i]] = 1; // marque les feuilles
for (d = 0; d < CT->nbnodes; d++)
if (CT->flags[d] == 1)
{ // pour toutes les feuilles marquees
c = CT->tabnodes[d].father;
while ((c != -1) && (CT->flags[c] == 0))
{
CT->flags[c] = 1; // marque tous les ancetres de c
c = CT->tabnodes[c].father;
}
}
for (i = 0; i < N; i++) // AMELIORATION POSSIBLE !!!
{
c = CM[i];
while ((CT->flags[c] == 0) && (CT->tabnodes[c].father != -1))
{
c = CT->tabnodes[c].father;
}
G[i] = CT->tabnodes[c].data;
}
} // Reconstruction()
/* ==================================== */
int32_t lwshedtopo_lreconsdilat(
struct xvimage *g,
struct xvimage *f,
int32_t connex)
/* reconstruction de g sous f */
/* g : image marqueur */
/* f : image masque */
/* connex : 4 ou 8 */
/* resultat dans g */
/* ==================================== */
{
#undef F_NAME
#define F_NAME "lwshedtopo_lreconsdilat"
uint8_t *F = UCHARDATA(f);
int32_t rs = rowsize(f); /* taille ligne */
int32_t cs = colsize(f); /* taille colonne */
int32_t ds = depth(f); /* nb plans */
int32_t N = rs * cs * ds;
int32_t *CM; // component mapping
ctree * CT; // component tree
if ((rowsize(g) != rs) || (colsize(g) != cs) || (depth(g) != ds))
{
fprintf(stderr, "%s: incompatible sizes\n", F_NAME);
return 0;
}
ComponentTree(F, rs, N, connex, &CT, &CM);
Reconstruction(g, f, CM, CT);
ComponentTreeFree(CT);
free(CM);
return(1);
} /* lwshedtopo_lreconsdilat() */
/* ==================================== */
int32_t lreconseros(
struct xvimage *g,
struct xvimage *f,
int32_t connex)
/* reconstruction de g sur f */
/* g : image marqueur */
/* f : image masque */
/* connex : 4 ou 8 */
/* resultat dans g */
/* ==================================== */
{
#undef F_NAME
#define F_NAME "lreconseros"
int32_t ret, i;
int32_t rs = rowsize(f); /* taille ligne */
int32_t cs = colsize(f); /* taille colonne */
int32_t ds = depth(f); /* nb plans */
int32_t N = rs * cs * ds;
uint8_t *F = UCHARDATA(f);
uint8_t *G = UCHARDATA(g);
if ((rowsize(g) != rs) || (colsize(g) != cs) || (depth(g) != ds))
{
fprintf(stderr, "%s: incompatible sizes\n", F_NAME);
return 0;
}
for (i = 0; i < N; i++) F[i] = NDG_MAX - F[i];
for (i = 0; i < N; i++) G[i] = NDG_MAX - G[i];
ret = lwshedtopo_lreconsdilat(g, f, connex);
for (i = 0; i < N; i++) G[i] = NDG_MAX - G[i];
return(ret);
} /* lreconseros() */
static void reconsTree(ctree *CT, int32_t *CM, int32_t *newCM, int32_t N, uint8_t *G)
{
int32_t d, c, e, i;
#ifdef _DEBUG_
printf("Reconstruction - Marquage\n");
#endif
for (d = 0; d < CT->nbnodes; d++) CT->flags[d] = 0; // utile ???
for (i = 0; i < N; i++) if (G[i]) CT->flags[CM[i]] = 1; // marque les feuilles
for (d = 0; d < CT->nbnodes; d++)
if ((CT->tabnodes[d].nbsons == 0) && (CT->flags[d] == 1))
{ // pour toutes les feuilles marquees
c = CT->tabnodes[d].father;
while ((c != -1) && (CT->flags[c] == 0))
{
CT->flags[c] = 1; // marque tous les ancetres de c
c = CT->tabnodes[c].father;
}
}
#ifdef _DEBUG_
printf("Reconstruction - Elimination arbre\n");
#endif
// elimine les feuilles non marquees
for (d = 0; d < CT->nbnodes; d++) {
if ((CT->tabnodes[d].nbsons == 0) && (CT->flags[d] == 0)) {
// pour toutes les feuilles non marquees
c = CT->tabnodes[d].father; // récupère le père marqué
while (CT->flags[c] == 0)
{
c = CT->tabnodes[c].father;
}
#ifdef PARANO
if (c < 0) {
printf("ERROR : Father is lower than 0\n");
}
#endif
e=d;
while ((e != c) && (CT->tabnodes[e].nbsons != -2)) {
if (CT->tabnodes[e].father == c) { // remove the son which is elevated to the father
soncell *sc, *prev = NULL;
for (sc = CT->tabnodes[c].sonlist; sc != NULL; sc = sc->next) {
if (sc->son == e) {
CT->tabnodes[c].nbsons--;
if (prev) {
prev->next = sc->next;
} else {
CT->tabnodes[c].sonlist = sc->next;
}
break;
}
prev = sc;
}
}
// Save the number of the node to which e has been moved
newCM[e] = c;
CT->tabnodes[e].nbsons = -2;
e = CT->tabnodes[e].father;
}
} // if ((CT->tabnodes[d].nbsons == 0) && (CT->flags[d] == 0))
} // for (d = 0; d < CT->nbnodes; d++)
// change the component mapping BUT NOT THE GRAY VALUE of the original image
// That allows to place the watershed line on the original contour
// while having done the reconstruction on the component tree only.
// LNA 16Septembre 2005
// Well apparently we have to reconstruct the original image
// It is done that later in the main procedure
// I do not fully understand why it is the case
for (i=0; i<N; i++) {
if (CT->tabnodes[CM[i]].nbsons == -2) {
CM[i] = newCM[CM[i]];
// However, we can build the reconstructed image.
G[i] = CT->tabnodes[CM[i]].data;
}
}
}
static void compressTree(ctree *CT, int32_t *CM, int32_t *newCM, int32_t N)
{
// Compress the component tree
// suppress all nodes that have only one son
int32_t i, d, c, e, f;
//ComponentTreePrint(CT);
for (d = 0; d < CT->nbnodes; d++) CT->flags[d] = 0; // utile !!
for (d = 0; d < CT->nbnodes; d++) {
if (CT->tabnodes[d].nbsons == 0) { // C'est une feuille
c = CT->tabnodes[d].father;
//while ((CT->tabnodes[c].father != -1) && (CT->flags[c]==0)) {
while ((c!=-1) && (CT->flags[c]==0)) {
if ((CT->tabnodes[c].nbsons == 1) && (CT->tabnodes[c].father != -1)) {
soncell *sc = NULL;
f = CT->tabnodes[c].father;
e = CT->tabnodes[c].sonlist->son;
for (sc = CT->tabnodes[f].sonlist; sc != NULL; sc = sc->next)
{
if (sc->son == c)
{
sc->son = e;
break;
}
}
#ifdef PARANO
if (sc != NULL)
if (sc->son != e) printf("Compress Erreur %d != %d\n", sc->son, e);
#endif
CT->tabnodes[e].father = f;
CT->tabnodes[c].nbsons = -3;
newCM[c] = e;
if (CT->tabnodes[c].father == -1)
CT->root = e;
}
CT->flags[c] = 1;
c = CT->tabnodes[c].father;
}
}
}
// Change the component mapping BUT NOT THE GRAY VALUE
// That allows to place the watershed line on the original contour
// while having done the reconstruction on the component tree only.
for (i=0; i<N; i++) {
if (CT->tabnodes[CM[i]].nbsons == -3) {
CM[i] = newCM[CM[i]];
}
}
}
/* ==================================== */
int32_t lwshedtopobin(struct xvimage *image, struct xvimage *marqueur, int32_t connex)
/* ==================================== */
/*! \fn int32_t lwshedtopobin(struct xvimage *image, struct xvimage *marqueur, int32_t connex)
\param image (entrée/sortie) : une image ndg
\param marqueur (entrée/sortie) : une image binaire
\param connex (entrée) : 4 ou 8 (2D), 6, 18 ou 26 (3D)
\return code erreur : 0 si échec, 1 sinon
\brief ligne de partage des eaux "topologique" binaire (algo MC, GB, LN)
*/
#undef F_NAME
#define F_NAME "lwshedtopobin"
{
register int32_t i, x; /* index muet */
int32_t rs = rowsize(image); /* taille ligne */
int32_t cs = colsize(image); /* taille colonne */
int32_t ds = depth(image); /* nb plans */
int32_t ps = rs * cs; /* taille plan */
int32_t N = ps * ds; /* taille image */
uint8_t *F = UCHARDATA(image);
uint8_t *G = UCHARDATA(marqueur);
Fahs * FAHS; /* la file d'attente hierarchique */
int32_t *CM, *newCM; /* etat d'un pixel */
ctree * CT; /* resultat : l'arbre des composantes */
if ((datatype(image) != VFF_TYP_1_BYTE) || (datatype(marqueur) != VFF_TYP_1_BYTE))
{
fprintf(stderr, "%s: image and marker must be both byte\n", F_NAME);
return 0;
}
if ((rowsize(marqueur) != rs) || (colsize(marqueur) != cs) || (depth(marqueur) != ds))
{
fprintf(stderr, "%s: incompatible sizes\n", F_NAME);
return 0;
}
FAHS = CreeFahsVide(N);
if (FAHS == NULL)
{ fprintf(stderr, "%s() : CreeFahsVide failed\n", F_NAME);
return 0;
}
IndicsInit(N);
// imposition des maxima
for (i = 0; i < N; i++) if (G[i]) F[i] = G[i] = NDG_MAX;
#ifdef _DEBUG_
printf("Component tree\n");
#endif
if ((connex == 4) || (connex == 8))
{
if (!ComponentTree(F, rs, N, connex, &CT, &CM))
{ fprintf(stderr, "%s() : ComponentTree failed\n", F_NAME);
return 0;
}
}
else if ((connex == 6) || (connex == 18) || (connex == 26))
{
if (!ComponentTree3d(F, rs, ps, N, connex, &CT, &CM))
{ fprintf(stderr, "%s() : ComponentTree failed\n", F_NAME);
return 0;
}
}
else
{ fprintf(stderr, "%s() : bad value for connex : %d\n", F_NAME, connex);
return 0;
}
#ifdef OLDVERSIONBIN
#ifdef _DEBUG_
printf("Reconstruction \n");
#endif
Reconstruction(marqueur, image, CM, CT);
ComponentTreeFree(CT); // AMELIORATION POSSIBLE: modification du CT et reutilisation pour la suite
free(CM);
#ifdef _DEBUG_
printf("Component tree \n");
#endif
if ((connex == 4) || (connex == 8))
{
if (!ComponentTree(G, rs, N, connex, &CT, &CM))
{ fprintf(stderr, "%s() : ComponentTree failed\n", F_NAME);
return 0;
}
}
else
{
if (!ComponentTree3d(G, rs, ps, N, connex, &CT, &CM))
{ fprintf(stderr, "%s() : ComponentTree failed\n", F_NAME);
return 0;
}
}
Watershed(marqueur, connex, FAHS, CM, CT);
#endif // #ifdef OLDVERSIONBIN
#ifndef OLDVERSIONBIN
#ifdef _DEBUG_
printf("Reconstruction - par arbre\n");
#endif
newCM = (int32_t *)calloc(CT->nbnodes, sizeof(int32_t));
if (newCM == NULL) {
fprintf(stderr, "%s : malloc failed\n", F_NAME);
return 0;
}