/
slic.go
391 lines (332 loc) · 9.03 KB
/
slic.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
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
package slic
import (
"image"
"image/color"
"image/draw"
"math"
"github.com/kurige/SLIC/lab"
)
/*
* TODO:
* - More accurate LAB color diffing
* - "Upgrade" to SLICO (or make it an option)
* - Use all avaialble cores
* - Perturb superpixels during seeding
* - Support hexgrid seeding(?)
*/
type SuperPixel struct {
label int
L, A, B float64
X, Y float64
}
type SLIC struct {
image lab.Image
compactness float64
step int
distvec []float64
Superpixels []*SuperPixel
XStrips int
YStrips int
Labels []int
labelCount int
}
func SuperPixelSizeForCount(width, height, count int) int {
return int(0.5 + float64(width*height)/float64(count))
}
func MakeSlic(image image.Image, compactness float64, supsz int) *SLIC {
var (
w = image.Bounds().Size().X
h = image.Bounds().Size().Y
sz = w * h
step = int(math.Sqrt(float64(supsz)) + 0.5)
)
x_strips := int(0.5 + float64(w)/float64(step))
y_strips := int(0.5 + float64(h)/float64(step))
x_err := w - step*x_strips
if x_err < 0 {
x_strips--
x_err = w - step*x_strips
}
y_err := h - step*y_strips
if y_err < 0 {
y_strips--
y_err = h - step*y_strips
}
labels := make([]int, sz)
for i := 0; i < sz; i++ {
labels[i] = -1
}
distvec := make([]float64, sz)
// Overwrite user selected superpixel count if necessary.
supsz = x_strips * y_strips
superpixels := make([]*SuperPixel, supsz)
img := lab.ImageToLab(image)
slic := &SLIC{
img,
compactness,
step,
distvec,
superpixels,
x_strips,
y_strips,
labels,
0,
}
x_err_per_strip := float64(x_err) / float64(x_strips)
y_err_per_strip := float64(y_err) / float64(y_strips)
x_offset := step / 2
y_offset := step / 2
label := 0
for y := 0; y < y_strips; y++ {
ye := y * int(y_err_per_strip)
for x := 0; x < x_strips; x++ {
var (
xe = x * int(x_err_per_strip)
seedx = x*step + x_offset + xe
seedy = y*step + y_offset + ye
c = img.At(seedx, seedy).(lab.Color)
)
superpixels[label] = &SuperPixel{label, c.L, c.A, c.B, float64(seedx), float64(seedy)}
label++
}
}
return slic
}
func (slic *SLIC) Run(iterations int) {
if iterations <= 0 {
iterations = 1
}
for i := 0; i < iterations; i++ {
slic.resetDistances()
slic.labelPixels()
slic.recalculateCentroids()
}
label_count, new_labels := slic.enforceLabelConnectivity()
slic.labelCount = label_count
size := slic.image.Bounds().Size()
sz := size.X * size.Y
for i := 0; i < sz; i++ {
slic.Labels[i] = new_labels[i]
}
}
func (slic *SLIC) resetDistances() {
for index := range slic.distvec {
slic.distvec[index] = math.MaxFloat64
}
}
func (slic *SLIC) labelPixels() {
for n := range slic.Superpixels {
slic.labelPixelsInSuperpixel(slic.Superpixels[n])
}
}
func (slic *SLIC) labelPixelsInSuperpixel(s *SuperPixel) {
fstep := float64(slic.step)
invwt := 1.0 / ((fstep / slic.compactness) * (fstep / slic.compactness))
size := slic.image.Bounds().Size()
width, height := size.X, size.Y
y1 := int(math.Max(0.0, s.Y-fstep))
y2 := int(math.Min(float64(height), s.Y+fstep))
x1 := int(math.Max(0.0, s.X-fstep))
x2 := int(math.Min(float64(width), s.X+fstep))
supL, supA, supB := s.L, s.A, s.B
supX, supY := s.X, s.Y
for y := y1; y < y2; y++ {
for x := x1; x < x2; x++ {
c := slic.image.At(x, y).(lab.Color)
X, Y := float64(x), float64(y)
var distc float64 = (c.L-supL)*(c.L-supL) + (c.A-supA)*(c.A-supA) + (c.B-supB)*(c.B-supB)
var distxy float64 = (X-supX)*(X-supX) + (Y-supY)*(Y-supY)
dist := math.Sqrt(distc) + math.Sqrt(distxy*invwt)
i := y*width + x
if dist < slic.distvec[i] {
slic.distvec[i] = dist
slic.Labels[i] = s.label
}
}
}
}
func (slic *SLIC) AverageColors() (lvec, avec, bvec []float64) {
lvec = make([]float64, slic.labelCount)
avec = make([]float64, slic.labelCount)
bvec = make([]float64, slic.labelCount)
count := make([]int, slic.labelCount)
size := slic.image.Bounds().Size()
width, height := size.X, size.Y
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
i := y*width + x
label := slic.Labels[i]
c := slic.image.At(x, y).(lab.Color)
lvec[label] += c.L
avec[label] += c.A
bvec[label] += c.B
count[label]++
}
}
for i := 0; i < slic.labelCount; i++ {
count := float64(count[i])
lvec[i] = lvec[i] / count
avec[i] = avec[i] / count
bvec[i] = bvec[i] / count
}
return
}
func (slic *SLIC) recalculateCentroids() {
supsz := len(slic.Superpixels)
sigma_l := make([]float64, supsz)
sigma_a := make([]float64, supsz)
sigma_b := make([]float64, supsz)
sigma_x := make([]float64, supsz)
sigma_y := make([]float64, supsz)
clustersize := make([]float64, supsz)
size := slic.image.Bounds().Size()
width, height := size.X, size.Y
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
i := y*width + x
label := slic.Labels[i]
// This needs to be handled better...
if label == -1 {
continue
}
c := slic.image.At(x, y).(lab.Color)
sigma_l[label] += c.L
sigma_a[label] += c.A
sigma_b[label] += c.B
sigma_x[label] += float64(x)
sigma_y[label] += float64(y)
clustersize[label] += 1.0
}
}
for n := 0; n < supsz; n++ {
if clustersize[n] <= 0 {
clustersize[n] = 1.0
}
superpixel := slic.Superpixels[n]
superpixel.L = sigma_l[n] / clustersize[n]
superpixel.A = sigma_a[n] / clustersize[n]
superpixel.B = sigma_b[n] / clustersize[n]
superpixel.X = sigma_x[n] / clustersize[n]
superpixel.Y = sigma_y[n] / clustersize[n]
}
}
func (slic *SLIC) enforceLabelConnectivity() (int, []int) {
size := slic.image.Bounds().Size()
width, height := size.X, size.Y
sz := width * height
target_supsz := sz / (slic.step * slic.step)
SUPSZ := sz / target_supsz
dx4 := [...]int{-1, 0, 1, 0}
dy4 := [...]int{0, -1, 0, 1}
xvec := make([]int, sz)
yvec := make([]int, sz)
oindex := 0
adjlabel := 0
label := 0
nlabels := make([]int, sz)
for i := 0; i < sz; i++ {
nlabels[i] = -1
}
for j := 0; j < height; j++ {
for k := 0; k < width; k++ {
if 0 > nlabels[oindex] {
nlabels[oindex] = label
// Start a new segment
xvec[0] = k
yvec[0] = j
// Quickly find an adjacent label for use later if needed
for n := 0; n < 4; n++ {
x := xvec[0] + dx4[n]
y := yvec[0] + dy4[n]
if (x >= 0 && x < width) && (y >= 0 && y < height) {
nindex := y*width + x
if nlabels[nindex] >= 0 {
adjlabel = nlabels[nindex]
}
}
}
count := 1
for c := 0; c < count; c++ {
for n := 0; n < 4; n++ {
x := xvec[c] + dx4[n]
y := yvec[c] + dy4[n]
if (x >= 0 && x < width) && (y >= 0 && y < height) {
nindex := y*width + x
if 0 > nlabels[nindex] && slic.Labels[oindex] == slic.Labels[nindex] {
xvec[count] = x
yvec[count] = y
nlabels[nindex] = label
count++
}
}
}
}
// If segment size is less than the limit, assign an adjacent label
// found before, and decrement label count.
if count <= SUPSZ>>2 {
for c := 0; c < count; c++ {
ind := yvec[c]*width + xvec[c]
nlabels[ind] = adjlabel
}
label--
}
label++
}
oindex++
}
}
return label, nlabels
}
func (slic *SLIC) DrawEdgesToImage(img image.Image) image.Image {
// Create new RGBA image from source
b := img.Bounds()
canvas := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(canvas, canvas.Bounds(), img, b.Min, draw.Src)
dx8 := []int{-1, -1, 0, 1, 1, 1, 0, -1}
dy8 := []int{0, -1, -1, -1, 0, 1, 1, 1}
size := slic.image.Bounds().Size()
width, height := size.X, size.Y
sz := width * height
contourx := make([]int, sz)
contoury := make([]int, sz)
istaken := make([]bool, sz)
mainindex := 0
cind := 0
black := color.RGBA{0, 0, 0, 254}
red := color.RGBA{254, 0, 0, 254}
for j := 0; j < height; j++ {
for k := 0; k < width; k++ {
if slic.Labels[mainindex] == -1 {
canvas.Set(k, j, red)
mainindex++
continue
}
np := 0
for i := 0; i < 8; i++ {
x := k + dx8[i]
y := j + dy8[i]
if (x >= 0 && x < width) && (y >= 0 && y < height) {
index := y*width + x
if !istaken[index] {
if slic.Labels[mainindex] != slic.Labels[index] {
np++
}
}
}
}
if np > 1 {
contourx[cind] = k
contoury[cind] = j
istaken[mainindex] = true
cind++
}
mainindex++
}
}
for j := 0; j < cind; j++ {
x := contourx[j]
y := contoury[j]
canvas.Set(x, y, black)
}
return canvas
}