-
Notifications
You must be signed in to change notification settings - Fork 0
/
seg.c
186 lines (148 loc) · 4.22 KB
/
seg.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
#include <stdio.h>
#include "seg.h"
#include "unionfind.h"
#include "image.h"
#include "oracle.h"
void SegRec_aux(Image*, size_t, size_t, size_t,Oracle *,Forest);
static inline unsigned int max(unsigned int a, unsigned int b) {
if (a>b) return a;
return b;
}
Forest SegIter(Image* I, Oracle * O){
size_t i, j;
size_t w = I->w;
size_t h = I->h;
size_t sz = w*h;
float* pData = I->data;
Forest forest = (Forest) malloc(sz*sizeof(Node*));
for(i=0; i<sz; i++) {
forest[i] = Node_New(NULL, O->Sig_New(&pData[i]));
}
Node* left;
Node* top;
Node* curr;
for(j=1; j<w; j++){ // First line
left = Find(forest[(j-1)]);
curr = Find(forest[j]);
if(O->Apply(O,left->Sig, curr->Sig)) Union(left, curr);
}
for(j=0; j<w; j++) Find(forest[j]);
for(i=1; i<h; i++){
top = Find(forest[(i-1)*w]);
curr = Find(forest[i*w]);
if(O->Apply(O,top->Sig, curr->Sig)) Union(top, curr);
for(j=1; j<w; j++){
left = Find(forest[i*w + (j-1)]);
curr = Find(forest[i*w + j]);
if(O->Apply(O,left->Sig, curr->Sig)) Union(left, curr);
top = Find(forest[(i-1)*w + j]);
curr = Find(forest[i*w + j]);
if(O->Apply(O,top->Sig, curr->Sig)) Union(top, curr);
}
for(j=0; j<w; j++) Find(forest[i*w + j]);
}
return forest;
}
Forest SegRec(Image* I, Oracle * O){
size_t i;
size_t w = I->w;
size_t h = I->h;
size_t sz = w*h;
float* pData = I->data;
Forest forest = (Forest) malloc(sz*sizeof(Node*));
for(i=0; i<sz; i++) {
forest[i] = Node_New(NULL, O->Sig_New(&pData[i]));
}
// w = 2^k
SegRec_aux(I,0,0,w,O,forest);
return forest;
}
void SegRec_aux(Image* I, size_t itl, size_t jtl, size_t pow2k,Oracle * O, Forest forest){
if (pow2k>1) { // k > 0
size_t i,j;
Node* left;
Node* right;
Node* top;
Node* bottom;
size_t hplus = pow2k>>1;
size_t hmoins = hplus-1;
SegRec_aux(I, itl, jtl, hplus,O,forest); // NO
SegRec_aux(I, itl, jtl+hplus, hplus,0,forest); // NE
SegRec_aux(I, itl+hplus, jtl+hplus, hplus,0,forest); // SE
SegRec_aux(I, itl+hplus, jtl, hplus,0,forest); // SO
for(i=0; i<pow2k; i++) {
left = Find(forest[(itl+i)*I->w + (jtl+hmoins)]);
right = Find(forest[(itl+i)*I->w + (jtl+hplus)]);
if(O->Apply(O,left->Sig, right->Sig)) Union(left, right);
}
for(j=0; j<pow2k; j++) {
top = Find(forest[(itl+hmoins)*I->w + (jtl+j)]);
bottom = Find(forest[(itl+hplus) *I->w + (jtl+j)]);
if(O->Apply(O,top->Sig, bottom->Sig)) Union(top, bottom);
}
}
}
Forest SegIter2(Image* I, Oracle * O){
unsigned int i=0,j=0,k=0,pow2k=0;
unsigned int hmoins=0,hplus=0;
size_t w = I->w;
size_t h = I->h;
unsigned int sz = w*h;
float* pData = I->data;
Forest forest = (Forest) malloc(sz*sizeof(Node*));
for(i=0; i<sz; i++) {
forest[i] = Node_New(NULL, O->Sig_New(&pData[i]));
}
Node* left = NULL;
Node* right = NULL;
Node* top = NULL;
Node* bottom = NULL;
for (pow2k=2;pow2k < 2*max(w,h);pow2k=pow2k*2) {
hplus = pow2k/2;
hmoins = hplus-1;
for (j=0; j<w; j+=pow2k) {
for (k=0;k<h;k++) {
left = Find(forest[k*w + (j+hmoins)]);
right = Find(forest[k*w + (j+hplus)]);
if(O->Apply(O,left->Sig, right->Sig)) Union(left, right);
}
}
for (i=0; i<h; i+=pow2k) {
for (k=0;k<w;k++) {
top = Find(forest[(i+hmoins)*w + k]);
bottom = Find(forest[(i+hplus)*w + k]);
if(O->Apply(O,top->Sig, bottom->Sig)) Union(top, bottom);
}
}
}
return forest;
}
Image * colorize(Node ** forest, size_t w, size_t h) {
Image* imageout = malloc(sizeof(Image));
imageout->w = w;
imageout->h = h;
float * img = malloc(3 * w * h * sizeof(float));
float *img_r = img;
float *img_g = img + w * h;
float *img_b = img + 2 * w *h;
size_t k,reg=0;
for(k=0; k<w*h; k++){
Node * curr = Find(forest[k]);
if (curr->Label == NULL) {
float * col = (float *) malloc(3*sizeof(float));
col[0] = (float) (rand() % 256);
col[1] = (float) (rand() % 256);
col[2] = (float) (rand() % 256);
curr->Label = (void*)col;
reg++;
}
img_r[k] = ((float *) curr->Label)[0];
img_g[k] = ((float *) curr->Label)[1];
img_b[k] = ((float *)curr->Label)[2];
}
for(k=0; k<w*h; k++)
if (isRoot(forest[k]) == 1) free(forest[k]->Label);
printf("Nombre de régions : %zu\n",reg);
imageout->data=img;
return imageout;
}