-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
304 lines (250 loc) · 5.92 KB
/
label.go
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
package tda
import (
"image"
"github.com/theodesp/unionfind"
)
// Label finds the connected components in a binary image.
type Label struct {
// The dimensions of the image that is being processed
rows int
cols int
// The binary image (coded 0/1) used to define the connected
// regions.
mask []uint8
// This is used to track labels of regions that need to be
// merged
uf *unionfind.UnionFind
// The labels of the connected regions
labels []int
// The number of components, including the background. The
// greatest component label is ncomp-1.
ncomp int
}
// NewLabel finds the connected components of a given binary image
// (mask), which is rectangular with the given number of rows. buf is
// an optional memory buffer having the same length as mask. Use the
// methods of the returned Label value to obtain information about the
// labels.
//
// The algorithm implemented here is the run-based algorithm of He et
// al. (2008), IEEE Transactions on Image Processing, 17:5.
// https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4472694
func NewLabel(mask []uint8, rows int, buf []int) *Label {
cols := len(mask) / rows
if rows*cols != len(mask) {
panic("Invalid number of rows")
}
la := &Label{
rows: rows,
cols: cols,
mask: mask,
labels: buf,
}
la.init()
la.label()
return la
}
func (la *Label) init() {
r := la.rows
c := la.cols
la.uf = unionfind.New(r * c)
// Blank out the first and last row
for j := 0; j < c; j++ {
la.mask[j] = 0
la.mask[(r-1)*c+j] = 0
}
// Blank out the first and last column
for i := 0; i < r; i++ {
la.mask[i*c] = 0
la.mask[i*c+c-1] = 0
}
if cap(la.labels) < r*c {
la.labels = make([]int, r*c)
} else {
la.labels = la.labels[0 : r*c]
for i := range la.labels {
la.labels[i] = 0
}
}
}
// nextRun finds the next run of 1's in row i of the image, starting
// from column j1. The image has c columns. The returned values [j1,
// j2) span the run.
func (la *Label) nextRun(i, j1, c int) (int, int) {
// Find the beginning of a run
var j2 int
for ; j1 < c && la.mask[i*c+j1] == 0; j1++ {
}
// No run was found
if j1 == c {
return -1, -1
}
// Find the end of the run
for j2 = j1 + 1; j2 < c && la.mask[i*c+j2] == 1; j2++ {
}
return j1, j2
}
func (la *Label) label() {
la.labelPass1()
la.labelPass2()
la.labelPass3()
}
// NumComponents returns the number of components, including the
// background component. The maximum component label is one less than
// the number of components.
func (la *Label) NumComponents() int {
return la.ncomp
}
// Use a row-scanning algorithm to identify candidate labels. Labels
// that need to be merged are stored in a union-find data structure,
// but the actual labels remain unadjusted on exit from this function.
func (la *Label) labelPass1() {
c := la.cols
var j1, j2, k1, k2 int
var vu int = 1
for i := 1; i < la.rows; i++ {
j1 = 0
for {
j1, j2 = la.nextRun(i, j1, c)
if j1 == -1 {
break
}
// Find all runs in the previous row that
// overlap with the current run
k1 = j1 - 1
first := true
var vf int
for k1 < c {
k1, k2 = la.nextRun(i-1, k1, c)
if k1 == -1 || k1 > j2 {
break
}
if first {
vf = la.labels[(i-1)*c+k1]
for j := j1; j < j2; j++ {
la.labels[i*c+j] = vf
}
first = false
} else {
la.uf.Union(la.labels[(i-1)*c+k1], vf)
}
k1 = k2
}
if first {
// Starting a new region
for j := j1; j < j2; j++ {
la.labels[i*c+j] = vu
}
vu++
}
j1 = j2
}
}
}
// Merge adjacent components under a single label.
func (la *Label) labelPass2() {
r := la.rows
c := la.cols
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
y := la.mask[i*c+j]
if y != 0 {
la.labels[i*c+j] = la.uf.Find(la.labels[i*c+j])
}
}
}
}
// Renumber the components so there are no gaps.
func (la *Label) labelPass3() {
cnt := make([]int, 0, 1000)
for _, v := range la.labels {
for len(cnt) < v+1 {
cnt = append(cnt, 0)
}
cnt[v]++
}
// mp defines a mapping from old labels to new labels
ncomp := 0
mp := make([]int, len(cnt))
for j := range cnt {
if cnt[j] > 0 {
mp[j] = ncomp
ncomp++
}
}
la.ncomp = ncomp
// Update the labels
for i := range la.labels {
la.labels[i] = mp[la.labels[i]]
}
}
// Sizes returns the sizes (number of pixels) in every labeled
// component of the array. The size of the component with label k is
// held in position k of the returned slice. The provided buffer will
// be used if large enough.
func (la *Label) Sizes(buf []int) []int {
if cap(buf) < la.ncomp {
buf = make([]int, la.ncomp)
} else {
buf = buf[0:la.ncomp]
for i := range buf {
buf[i] = 0
}
}
for _, v := range la.labels {
buf[v]++
}
return buf
}
// Bboxes returns the bounding boxes for every labeled component. The
// bounding box for the component with label k is held in position k
// of the returned slice. The provided slice is used if large enough.
func (la *Label) Bboxes(buf []image.Rectangle) []image.Rectangle {
buf = buf[0:0]
var bf []bool
for i, v := range la.labels {
row := i / la.cols
col := i % la.cols
for len(buf) < v+1 {
buf = append(buf, image.Rectangle{})
bf = append(bf, false)
}
if !bf[v] {
buf[v] = image.Rect(col, row, col+1, row+1)
bf[v] = true
} else {
r := buf[v]
if col < r.Min.X {
r.Min.X = col
}
if col+1 > r.Max.X {
r.Max.X = col + 1
}
if row < r.Min.Y {
r.Min.Y = row
}
if row+1 > r.Max.Y {
r.Max.Y = row + 1
}
buf[v] = r
}
}
return buf
}
// Labels returns the component labels.
func (la *Label) Labels() []int {
return la.labels
}
// Mask returns the image that is being labeled.
func (la *Label) Mask() []uint8 {
return la.mask
}
// Rows returns the number of rows in the image that is being labeled.
func (la *Label) Rows() int {
return la.rows
}
// Cols returns the number of columns in the image that is being
// labeled.
func (la *Label) Cols() int {
return la.cols
}