forked from EdlinOrg/prominentcolor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans.go
453 lines (360 loc) · 11.7 KB
/
kmeans.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
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
// Copyright 2016 Carl Asman. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package prominentcolor finds the K most dominant/prominent colors in an image
package prominentcolor
import (
"fmt"
"image"
"image/color"
"log"
"math/rand"
"sort"
"time"
"github.com/lucasb-eyer/go-colorful"
)
const (
// ArgumentDefault default settings
ArgumentDefault int = 0
// ArgumentSeedRandom randomly pick initial values (instead of K-means++)
ArgumentSeedRandom = 1 << iota
// ArgumentAverageMean take the mean value when determining the centroid color (instead of median)
ArgumentAverageMean
// ArgumentNoCropping do not crop background that is considered "white"
ArgumentNoCropping
// ArgumentLAB (experimental, it seems to be buggy in some cases): uses LAB instead of RGB when measuring distance
ArgumentLAB
// ArgumentDebugImage saves a tmp file in /tmp/ where the area that has been cut away by the mask is marked pink
// useful when figuring out what values to pick for the masks
ArgumentDebugImage
)
const (
// DefaultK is the k used as default
DefaultK = 3
// DefaultSize is the default size images are re-sized to
DefaultSize = 80
)
var (
// MaskWhite "constant" for white mask (for ease of re-use for other mask arrays)
MaskWhite = ColorBackgroundMask{R: true, G: true, B: true, Treshold: uint32(0xc000)}
// MaskBlack "constant" for black mask (for ease of re-use for other mask arrays)
MaskBlack = ColorBackgroundMask{R: false, G: false, B: false, Treshold: uint32(0x5000)}
// MaskGreen "constant" for green mask (for ease of re-use for other mask arrays)
MaskGreen = ColorBackgroundMask{R: false, G: true, B: false, PercDiff: 0.9}
)
// ErrNoPixelsFound is returned when no non-alpha pixels are found in the provided image
var ErrNoPixelsFound = fmt.Errorf("Failed, no non-alpha pixels found (either fully transparent image, or the ColorBackgroundMask removed all pixels)")
// ColorRGB contains the color values
type ColorRGB struct {
R, G, B uint32
}
// ColorItem contains color and have many occurrences of this color found
type ColorItem struct {
Color ColorRGB
Cnt int
}
// AsString gives back the color in hex as 6 character string
func (c *ColorItem) AsString() string {
return fmt.Sprintf("%.2X%.2X%.2X", c.Color.R, c.Color.G, c.Color.B)
}
// createColor returns ColorItem struct unless it was a transparent color
func createColor(c color.Color) (ColorItem, bool) {
r, g, b, a := c.RGBA()
if a == 0 {
// transparent pixels are ignored
return ColorItem{}, true
}
divby := uint32(256.0)
return ColorItem{Color: ColorRGB{R: r / divby, G: g / divby, B: b / divby}}, false
}
// IsBitSet check if "lookingfor" is set in "bitset"
func IsBitSet(bitset int, lookingfor int) bool {
return lookingfor == (bitset & lookingfor)
}
// GetDefaultMasks returns the masks that are used for the default settings
func GetDefaultMasks() []ColorBackgroundMask {
return []ColorBackgroundMask{MaskWhite, MaskBlack, MaskGreen}
}
// Kmeans uses the default: k=3, Kmeans++, Median, crop center, resize to 80 pixels, mask out white/black/green backgrounds
// It returns an array of ColorItem which are three centroids, sorted according to dominance (most frequent first).
func Kmeans(orgimg image.Image) (centroids []ColorItem, err error) {
return KmeansWithAll(DefaultK, orgimg, ArgumentDefault, DefaultSize, GetDefaultMasks())
}
// KmeansWithArgs takes arguments which consists of the bits, see constants Argument*
func KmeansWithArgs(arguments int, orgimg image.Image) (centroids []ColorItem, err error) {
return KmeansWithAll(DefaultK, orgimg, arguments, DefaultSize, GetDefaultMasks())
}
// KmeansWithAll takes additional arguments to define k, arguments (see constants Argument*), size to resize and masks to use
func KmeansWithAll(k int, orgimg image.Image, arguments int, imageReSize uint, bgmasks []ColorBackgroundMask) ([]ColorItem, error) {
img := prepareImg(arguments, bgmasks, imageReSize, orgimg)
allColors, _ := extractColorsAsArray(img)
numColors := len(allColors)
if numColors == 0 {
return nil, ErrNoPixelsFound
}
if numColors == 1 {
return allColors, nil
}
if numColors <= k {
sortCentroids(allColors)
return allColors, nil
}
centroids, err := kmeansSeed(k, allColors, arguments)
if err != nil {
return nil, err
}
cent := make([][]ColorItem, k)
//initialize
cent[0] = allColors
for i := 1; i < k; i++ {
cent[i] = []ColorItem{}
}
//rounds is a safety net to make sure we terminate if its a bug in our distance function (or elsewhere) that makes k-means not terminate
rounds := 0
maxRounds := 5000
changes := 1
for changes > 0 && rounds < maxRounds {
changes = 0
tmpCent := make([][]ColorItem, k)
for i := 0; i < k; i++ {
tmpCent[i] = []ColorItem{}
}
for i := 0; i < k; i++ {
for _, aColor := range cent[i] {
closestCentroid := findClosest(arguments, aColor, centroids)
tmpCent[closestCentroid] = append(tmpCent[closestCentroid], aColor)
if closestCentroid != i {
changes++
}
}
}
cent = tmpCent
centroids = calculateCentroids(cent, arguments)
rounds++
}
if rounds >= maxRounds {
log.Println("Warning: terminated k-means due to max number of iterations")
}
sortCentroids(centroids)
return centroids, nil
}
// ByColorCnt makes the ColorItem sortable
type byColorCnt []ColorItem
func (a byColorCnt) Len() int { return len(a) }
func (a byColorCnt) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byColorCnt) Less(i, j int) bool {
if a[i].Cnt == a[j].Cnt {
return a[i].AsString() < a[j].AsString()
}
return a[i].Cnt < a[j].Cnt
}
// sortCentroids sorts them from most dominant color descending
func sortCentroids(centroids []ColorItem) {
sort.Sort(sort.Reverse(byColorCnt(centroids)))
}
func calculateCentroids(cent [][]ColorItem, arguments int) []ColorItem {
var centroids []ColorItem
for _, colors := range cent {
var meanColor ColorItem
if IsBitSet(arguments, ArgumentAverageMean) {
meanColor = mean(colors)
} else {
meanColor = median(colors)
}
centroids = append(centroids, meanColor)
}
return centroids
}
// mean calculate the mean color values from an array of colors
func mean(colors []ColorItem) ColorItem {
var r, g, b float64
r, g, b = 0.0, 0.0, 0.0
cntInThisBucket := 0
for _, aColor := range colors {
cntInThisBucket += aColor.Cnt
r += float64(aColor.Color.R)
g += float64(aColor.Color.G)
b += float64(aColor.Color.B)
}
theSize := float64(len(colors))
return ColorItem{Cnt: cntInThisBucket, Color: ColorRGB{R: uint32(r / theSize), G: uint32(g / theSize), B: uint32(b / theSize)}}
}
// median calculate the median color from an array of colors
func median(colors []ColorItem) ColorItem {
var rValues, gValues, bValues []int
cntInThisBucket := 0
for _, aColor := range colors {
cntInThisBucket += aColor.Cnt
rValues = append(rValues, int(aColor.Color.R))
gValues = append(gValues, int(aColor.Color.G))
bValues = append(bValues, int(aColor.Color.B))
}
retR := 0
if 0 != len(rValues) {
sort.Ints(rValues)
retR = rValues[int(len(rValues)/2)]
}
retG := 0
if 0 != len(gValues) {
sort.Ints(gValues)
retG = gValues[int(len(gValues)/2)]
}
retB := 0
if 0 != len(bValues) {
sort.Ints(bValues)
retB = bValues[int(len(bValues)/2)]
}
return ColorItem{Cnt: cntInThisBucket, Color: ColorRGB{R: uint32(retR), G: uint32(retG), B: uint32(retB)}}
}
// extractColorsAsArray counts the number of occurrences of each color in the image, returns array and numPixels
func extractColorsAsArray(img image.Image) ([]ColorItem, int) {
m, numPixels := extractColors(img)
v := make([]ColorItem, len(m))
idx := 0
for _, value := range m {
v[idx] = value
idx++
}
return v, numPixels
}
// extractColors counts the number of occurrences of each color in the image, returns map
func extractColors(img image.Image) (map[string]ColorItem, int) {
m := make(map[string]ColorItem)
numPixels := 0
data := img.Bounds()
for x := data.Min.X; x < data.Max.X; x++ {
for y := data.Min.Y; y < data.Max.Y; y++ {
colorAt := img.At(x, y)
colorItem, ignore := createColor(colorAt)
if ignore {
continue
}
numPixels++
asString := colorItem.AsString()
value, ok := m[asString]
if ok {
value.Cnt++
m[asString] = value
} else {
colorItem.Cnt = 1
m[asString] = colorItem
}
}
}
return m, numPixels
}
// findClosest returns the index of the closest centroid to the color "c"
func findClosest(arguments int, c ColorItem, centroids []ColorItem) int {
centLen := len(centroids)
closestIdx := 0
closestDistance := distance(arguments, c, centroids[0])
for i := 1; i < centLen; i++ {
distance := distance(arguments, c, centroids[i])
if distance < closestDistance {
closestIdx = i
closestDistance = distance
}
}
return closestIdx
}
// distance returns the distance between two colors
func distance(arguments int, c ColorItem, p ColorItem) float64 {
if IsBitSet(arguments, ArgumentLAB) {
return distanceLAB(c, p)
}
return distanceRGB(c, p)
}
func distanceLAB(c ColorItem, p ColorItem) float64 {
errmsg := "Warning: LAB failed, fallback to RGB"
a, err := colorful.Hex("#" + c.AsString())
if err != nil {
log.Fatal(err)
log.Println(errmsg)
return distanceRGB(c, p)
}
b, err2 := colorful.Hex("#" + p.AsString())
if err2 != nil {
log.Fatal(err2)
log.Println(errmsg)
return distanceRGB(c, p)
}
return a.DistanceLab(b)
}
func distanceRGB(c ColorItem, p ColorItem) float64 {
r := c.Color.R
g := c.Color.G
b := c.Color.B
r2 := p.Color.R
g2 := p.Color.G
b2 := p.Color.B
//sqrt not needed since we just want to compare distances to each other
return float64((r-r2)*(r-r2) + (g-g2)*(g-g2) + (b-b2)*(b-b2))
}
// kmeansSeed calculates the initial cluster centroids
func kmeansSeed(k int, allColors []ColorItem, arguments int) ([]ColorItem, error) {
if k > len(allColors) {
return nil, fmt.Errorf("Failed, k larger than len(allColors): %d vs %d\n", k, len(allColors))
}
rand.Seed(time.Now().UnixNano())
if IsBitSet(arguments, ArgumentSeedRandom) {
return kmeansSeedRandom(k, allColors), nil
}
return kmeansPlusPlusSeed(k, arguments, allColors), nil
}
// kmeansSeedRandom picks k random points as initial centroids
func kmeansSeedRandom(k int, allColors []ColorItem) []ColorItem {
var centroids []ColorItem
taken := make(map[int]bool)
for i := 0; i < k; i++ {
idx := rand.Intn(len(allColors))
//check if we already taken this one
_, ok := taken[idx]
if ok {
i--
continue
}
taken[idx] = true
centroids = append(centroids, allColors[idx])
}
return centroids
}
// kmeansPlusPlusSeed picks initial centroids using K-Means++
func kmeansPlusPlusSeed(k int, arguments int, allColors []ColorItem) []ColorItem {
var centroids []ColorItem
taken := make(map[int]bool)
initIdx := rand.Intn(len(allColors))
centroids = append(centroids, allColors[initIdx])
taken[initIdx] = true
for kk := 1; kk < k; kk++ {
totaldistances := 0.0
var point2distance []float64
for j := 0; j < len(allColors); j++ {
_, ok := taken[j]
if ok {
point2distance = append(point2distance, 0.0)
continue
}
minDistanceToCluster := -1.0
for i := 0; i < len(centroids); i++ {
d := distance(arguments, centroids[i], allColors[j])
if minDistanceToCluster == -1.0 || d < minDistanceToCluster {
minDistanceToCluster = d
}
}
squareDistance := minDistanceToCluster * minDistanceToCluster
totaldistances += squareDistance
point2distance = append(point2distance, squareDistance)
}
rndpoint := rand.Float64() * totaldistances
sofar := 0.0
for j := 0; j < len(point2distance); j++ {
if rndpoint <= sofar {
centroids = append(centroids, allColors[j])
taken[j] = true
break
}
sofar += point2distance[j]
}
}
return centroids
}