-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmeans.go
76 lines (63 loc) · 1.71 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
package main
import (
"alexkmeans/alexkmeans"
"flag"
"fmt"
"image"
"image/color"
"image/png"
"math"
"os"
"strings"
)
// A simple program to sort pixels into the correct colours via K-means clustering
// This was written as a sample project to practice GoLang
// K means clustering wikipedia article: https://en.wikipedia.org/wiki/K-means_clustering
// Information on colour quantization can be found here: https://en.wikipedia.org/wiki/Color_quantization
var (
input string
output string
clusters int
passes int
)
func init() {
flag.IntVar(&clusters, "clusters", 3, "the amount of clusters to be used")
flag.IntVar(&passes, "passes", 3, "the amount of passes to be used")
flag.StringVar(&input, "input", "", "the input file to be used")
flag.StringVar(&output, "output", "", "the output file to be used")
flag.Parse()
if !strings.HasSuffix(output, ".png") {
output += ".png"
}
}
func main() {
data, err := alexkmeans.LoadImage(input)
if err != nil {
panic(err)
}
results := alexkmeans.CalculateKMeansGrouping(data, passes, clusters)
drawResults(results)
}
func drawResults(colours []alexkmeans.Vector) {
fmt.Printf("\n Saving output colours... \n")
im := image.NewRGBA(image.Rectangle{Max: image.Point{X: 250, Y: 50}})
var blocks float64 = (250.00 / float64(len(colours)))
start := 0
for c := range colours {
for x := start; x < start+int(math.Ceil(blocks)); x++ {
for y := 0; y < 50; y++ {
im.Set(x, y, color.RGBA{uint8(colours[c].X), uint8(colours[c].Y), uint8(colours[c].Y), 255})
}
}
start += int(math.Ceil(blocks))
}
imgfile, err := os.Create(output)
if err != nil {
panic(err)
}
defer imgfile.Close()
err = png.Encode(imgfile, im)
if err != nil {
panic(err)
}
}