-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnected_components.h
More file actions
235 lines (201 loc) · 4.78 KB
/
connected_components.h
File metadata and controls
235 lines (201 loc) · 4.78 KB
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
class ConnectedComponents
{
public:
typedef enum
{
FOUR,
EIGHT,
} Connectivity;
protected:
const unsigned int w;
const unsigned int h;
Connectivity conn;
bool labelMatch (
const unsigned int ref,
const unsigned int trg,
const unsigned int *label)
{
return (label[ref] == label[trg]);
}
bool imageMatch (
const unsigned int ref,
const unsigned int trg,
const unsigned char *image)
{
// return (image[ref] == image[trg]);
return (abs ((float)image[ref]-(float)image[trg]) < 3.1f);
}
void minmaxLabel (
const unsigned int l1,
const unsigned int l2,
const unsigned int *label,
unsigned int& mn,
unsigned int& mx)
{
if (label[l1] < label[l2])
{
mn = l1;
mx = l2;
}
else
{
mx = l1;
mn = l2;
}
}
public:
ConnectedComponents (const unsigned int width, const unsigned int height, Connectivity connectivity = FOUR)
: w (width), h (height), conn (connectivity)
{}
virtual ~ConnectedComponents ()
{}
const unsigned int produceLabeling (
unsigned char *image,
unsigned int *label)
{
const unsigned int NO_LABEL = 0xFFFFFFFF;
unsigned int x, y, i;
unsigned int c, r; // count of labels
unsigned int *eqs, *cnt, *meq;
unsigned int l, j;//, lmn, lmx;
unsigned int src, trg[3];
c = 0;
r = 0;
eqs = new unsigned int[w*h];
meq = new unsigned int[w*h];
cnt = new unsigned int[w*h];
for (i=0; i<w*h; i++)
{
label[i] = NO_LABEL;
eqs[i] = NO_LABEL;
meq[i] = NO_LABEL;
cnt[i] = 0;
}
for (y=0; y<h; y++)
{
for (x=0; x<w; x++)
{
src = (y*w)+x;
trg[0] = (y*w)+(x-1); // simple 4
trg[1] = ((y-1)*w)+x; // simple 4
trg[2] = ((y-1)*w)+(x-1); // simple 8
// horizontal
if ((x>0) && (imageMatch (src, trg[0], image)))
{
label[src] = label[trg[0]];
}
// vertical
if ((y>0) && (imageMatch (src, trg[1], image)) && (!labelMatch (src, trg[1], label)))
{
if (label[trg[1]] < label[src])
{
if (label[src] != NO_LABEL)
eqs[label[src]] = label[trg[1]];
label[src] = label[trg[1]];
}
else
{
if (label[trg[1]] != NO_LABEL)
eqs[label[trg[1]]] = label[src];
}
}
// diagonal
if ((x>0) && (y>0) && (imageMatch (src, trg[2], image)) && (!labelMatch (src, trg[2], label)))
{
if (label[trg[2]] < label[src])
{
if (label[src] != NO_LABEL)
eqs[label[src]] = label[trg[2]];
label[src] = label[trg[2]];
}
else
{
if (label[trg[2]] != NO_LABEL)
eqs[label[trg[2]]] = label[src];
}
}
// request a new label
if (label[src] == NO_LABEL)
{
label[src] = c++;
}
// track the count
cnt[label[src]]++;
}
}
// rename the labels
// build the list of minimum equivalences
for (i=0; i<w*h; i++)
{
// l = eqs[i];
l = i;
while (eqs[l] != NO_LABEL)
{
l = eqs[l];
}
meq[i] = l;
}
// for each pixel, find the min equivalence
for (i=0; i<w*h; i++)
{
//l = label[i];
//cnt[l]--;
//while (eqs[l] != NO_LABEL)
//{
// l = eqs[l];
//}
//label[i] = l;
//cnt[l]++;
cnt[label[i]]--;
label[i] = meq[label[i]];
cnt[label[i]]++;
}
fprintf (stdout, "c: %i", c);
// replace the empty labels with good labels
unsigned int mn, mx;
for (i=0; i<c; i++)
{
if (cnt[i] == 0)
{
// replace the last non-empty label with i
for (j=c, r=0; j>i; j--)
{
if (cnt[j] > 0)
{
r = j;
break;
}
}
if (r==0)
break;
// replace the label in the image
for (j=0; j<w*h; j++)
{
if (label[j] == r)
label[j] = i;
}
cnt[i] = cnt[r];
cnt[r] = 0;
}
}
// count the number of components
mx = 0;
mn = 0xFFFFFFFF;
for (i=0, c=0; i<w*h; i++)
{
if (cnt[i] > 0)
{
c++;
if (mx<cnt[i])
mx=cnt[i];
if (mn>cnt[i])
mn=cnt[i];
}
}
fprintf (stdout, ", nc: %i [%i -> %i]\n", c, mn, mx);
delete[] eqs;
delete[] meq;
delete[] cnt;
return c;
}
};