├── .gitignore ├── LICENSE ├── README.md ├── hierarchical.go └── matrix.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.dll 4 | *.so 5 | *.dylib 6 | 7 | # Test binary, build with `go test -c` 8 | *.test 9 | 10 | # Output of the go coverage tool, specifically when used with LiteIDE 11 | *.out 12 | 13 | # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 14 | .glide/ 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Nykakin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # quantize 2 | 3 | This package reimplements hierarchical quantization described in [this tutorial](http://aishack.in/tutorials/dominant-color/) in Go programming language. [Gonum](https://github.com/gonum/gonum) was used instead of OpenCV, then latter replaced with simpler in-home matrix types and [eigenvalue decomposition algorithm](https://github.com/Nykakin/eigenvalues) adapred from Java [JAML](https://math.nist.gov/javanumerics/jama/) package. This allowed to reduce ammount of needed dependencies. The effect and comparission with different Go packages can be found in [this repository](https://github.com/Nykakin/QuantizationTournament). Described eigenvalue method, while correct, in practice appears to be much slower than alternative methods, mostly based on some sort of k-means clustering. Therefore it doesn't really seem to be a good choice for a production code. It does a better job with detecting dominant background colors than some other competitors, though. 4 | 5 | ```go 6 | package main 7 | 8 | import ( 9 | "image" 10 | "image/color" 11 | _ "image/jpeg" 12 | _ "image/png" 13 | "os" 14 | 15 | "github.com/Nykakin/quantize" 16 | "github.com/joshdk/preview" 17 | ) 18 | 19 | func main() { 20 | f, err := os.Open("test.jpg") 21 | if err != nil { 22 | panic(err) 23 | } 24 | defer f.Close() 25 | img, _, err := image.Decode(f) 26 | if err != nil { 27 | panic(err) 28 | } 29 | 30 | quantizer := quantize.NewHierarhicalQuantizer() 31 | colors, err := quantizer.Quantize(img, 5) 32 | if err != nil { 33 | panic(err) 34 | } 35 | 36 | palette := make([]color.Color, len(colors)) 37 | for index, clr := range colors { 38 | palette[index] = clr 39 | } 40 | 41 | // Display our new palette 42 | preview.Show(palette) 43 | } 44 | ``` 45 | -------------------------------------------------------------------------------- /hierarchical.go: -------------------------------------------------------------------------------- 1 | package quantize 2 | 3 | import ( 4 | "image" 5 | "image/color" 6 | "math" 7 | "sort" 8 | 9 | "github.com/Nykakin/eigenvalues" 10 | ) 11 | 12 | type colorNode struct { 13 | mean vec3x1 14 | cov mat3x3 15 | classid uint8 16 | count uint64 17 | 18 | left *colorNode 19 | right *colorNode 20 | } 21 | 22 | func newColorNode(classid uint8) *colorNode { 23 | return &colorNode{ 24 | classid: classid, 25 | mean: newVec3x1(), 26 | cov: newMat3x3(), 27 | } 28 | } 29 | 30 | type Quantizer interface { 31 | Quantize(img image.Image, count int) ([]color.RGBA, error) 32 | } 33 | 34 | type hierarhicalQuantizer struct { 35 | tmp3x3 mat3x3 36 | tmp3x1 vec3x1 37 | tmp1x3 vec1x3 38 | } 39 | 40 | func NewHierarhicalQuantizer() hierarhicalQuantizer { 41 | return hierarhicalQuantizer{ 42 | tmp3x3: newMat3x3(), 43 | tmp3x1: newVec3x1(), 44 | tmp1x3: newVec1x3(), 45 | } 46 | } 47 | 48 | func (hq hierarhicalQuantizer) Quantize(img image.Image, count int) ([]color.RGBA, error) { 49 | bounds := img.Bounds() 50 | pixelCount := bounds.Max.X * bounds.Max.Y 51 | 52 | classes := make([]uint8, pixelCount, pixelCount) 53 | for i := range classes { 54 | classes[i] = 1 55 | } 56 | root := newColorNode(1) 57 | 58 | hq.getClassMeanCov(img, classes, root) 59 | for i := 0; i < count-1; i++ { 60 | next, err := hq.getMaxEigenvalueNode(root) 61 | if err != nil { 62 | return nil, err 63 | } 64 | err = hq.partitionClass(img, classes, getNextClassid(root), next) 65 | if err != nil { 66 | return nil, err 67 | } 68 | hq.getClassMeanCov(img, classes, next.left) 69 | hq.getClassMeanCov(img, classes, next.right) 70 | } 71 | return getDominantColors(root), nil 72 | } 73 | 74 | func convertColor(col color.Color) (color []float64, isTransparent bool) { 75 | r, g, b, a := col.RGBA() 76 | // TODO: handle transparency more smartly 77 | if a == 0 { 78 | return nil, true 79 | } 80 | 81 | return []float64{float64(r) / 65535.0, float64(g) / 65535.0, float64(b) / 65535.0}, false 82 | } 83 | 84 | func (hq hierarhicalQuantizer) getClassMeanCov(img image.Image, classes []uint8, node *colorNode) { 85 | bounds := img.Bounds() 86 | 87 | node.mean.set(0) 88 | node.cov.set(0) 89 | pixcount := 0 90 | 91 | for y := 0; y < bounds.Max.Y; y++ { 92 | for x := 0; x < bounds.Max.X; x++ { 93 | if classes[y*bounds.Max.X+x] != node.classid { 94 | continue 95 | } 96 | 97 | color, isTransparent := convertColor(img.At(x, y)) 98 | if isTransparent { 99 | continue 100 | } 101 | hq.tmp3x1.setVec(color) 102 | node.mean.add(node.mean, hq.tmp3x1) 103 | hq.tmp1x3.t(hq.tmp3x1) 104 | hq.tmp3x3.mul3x1And1x3(hq.tmp3x1, hq.tmp1x3) 105 | node.cov.add(node.cov, hq.tmp3x3) 106 | pixcount += 1 107 | } 108 | } 109 | 110 | hq.tmp1x3.t(node.mean) 111 | hq.tmp3x3.mul(node.mean, hq.tmp1x3) 112 | node.cov.apply(func(i, j int, v float64) float64 { 113 | return v - hq.tmp3x3.at(j, i)/float64(pixcount) 114 | }, node.cov) 115 | node.mean.setVec([]float64{ 116 | node.mean.atVec(0) / float64(pixcount), 117 | node.mean.atVec(1) / float64(pixcount), 118 | node.mean.atVec(2) / float64(pixcount), 119 | }) 120 | } 121 | 122 | func (hq hierarhicalQuantizer) getMaxEigenvalueNode(current *colorNode) (*colorNode, error) { 123 | maxEigen := float64(-1) 124 | queue := []*colorNode{current} 125 | var node *colorNode 126 | ret := current 127 | 128 | if current.left == nil && current.right == nil { 129 | return current, nil 130 | } 131 | 132 | LOOP: 133 | for len(queue) > 0 { 134 | node, queue = queue[len(queue)-1], queue[:len(queue)-1] 135 | 136 | if node.left != nil && node.right != nil { 137 | queue = append(queue, node.left) 138 | queue = append(queue, node.right) 139 | continue 140 | } 141 | 142 | for i := 0; i < 3; i++ { 143 | for j := 0; j < 3; j++ { 144 | if math.IsNaN(node.cov.at(j, i)) { 145 | continue LOOP 146 | } 147 | } 148 | } 149 | 150 | r := eigenvalues.NewEigenvalueDecomposition(node.cov) 151 | val := r.EigenvaluesReal()[0] 152 | 153 | if val > maxEigen { 154 | maxEigen = val 155 | ret = node 156 | } 157 | } 158 | return ret, nil 159 | } 160 | 161 | func (hq hierarhicalQuantizer) partitionClass(img image.Image, classes []uint8, nextid uint8, node *colorNode) error { 162 | cmpValue := newMat3x3() 163 | thisValue := newMat3x3() 164 | 165 | bounds := img.Bounds() 166 | newidleft := nextid 167 | newidright := nextid + 1 168 | 169 | r := eigenvalues.NewEigenvalueDecomposition(node.cov) 170 | eig := newVec3x1() 171 | eig.setVec(r.Eigenvector()[0]) 172 | cmpValue.mul(eig, node.mean) 173 | 174 | node.left = newColorNode(newidleft) 175 | node.right = newColorNode(newidright) 176 | 177 | for y := 0; y < bounds.Max.Y; y++ { 178 | for x := 0; x < bounds.Max.X; x++ { 179 | pos := y*bounds.Max.X + x 180 | if classes[pos] != node.classid { 181 | continue 182 | } 183 | 184 | color, isTransparent := convertColor(img.At(x, y)) 185 | if isTransparent { 186 | continue 187 | } 188 | hq.tmp3x1.setVec(color) 189 | thisValue.mul(eig, hq.tmp3x1) 190 | 191 | if thisValue.at(0, 0) <= cmpValue.at(0, 0) { 192 | node.left.count++ 193 | classes[pos] = newidleft 194 | } else { 195 | node.right.count++ 196 | classes[pos] = newidright 197 | } 198 | } 199 | } 200 | 201 | return nil 202 | } 203 | 204 | func getDominantColors(root *colorNode) []color.RGBA { 205 | ret := []color.RGBA{} 206 | for _, leave := range getLeaves(root) { 207 | c := color.RGBA{ 208 | uint8(leave.mean.atVec(0) * float64(255.0)), 209 | uint8(leave.mean.atVec(1) * float64(255.0)), 210 | uint8(leave.mean.atVec(2) * float64(255.0)), 211 | 255, 212 | } 213 | ret = append(ret, c) 214 | } 215 | return ret 216 | } 217 | 218 | func getLeaves(root *colorNode) []*colorNode { 219 | ret := []*colorNode{} 220 | queue := []*colorNode{root} 221 | var current *colorNode 222 | for len(queue) > 0 { 223 | current, queue = queue[len(queue)-1], queue[:len(queue)-1] 224 | if current.left != nil && current.right != nil { 225 | queue = append(queue, current.left) 226 | queue = append(queue, current.right) 227 | continue 228 | } 229 | ret = append(ret, current) 230 | } 231 | sort.Sort(sort.Reverse(ByCount(ret))) 232 | return ret 233 | } 234 | 235 | func getNextClassid(root *colorNode) uint8 { 236 | maxid := uint8(0) 237 | 238 | queue := []*colorNode{root} 239 | var current *colorNode 240 | for len(queue) > 0 { 241 | current, queue = queue[len(queue)-1], queue[:len(queue)-1] 242 | 243 | if current.classid > maxid { 244 | maxid = current.classid 245 | } 246 | if current.left != nil { 247 | queue = append(queue, current.left) 248 | } 249 | if current.right != nil { 250 | queue = append(queue, current.right) 251 | } 252 | } 253 | 254 | return maxid + 1 255 | } 256 | 257 | type ByCount []*colorNode 258 | 259 | func (c ByCount) Len() int { return len(c) } 260 | func (c ByCount) Swap(i, j int) { c[i], c[j] = c[j], c[i] } 261 | func (c ByCount) Less(i, j int) bool { return c[i].count < c[j].count } 262 | -------------------------------------------------------------------------------- /matrix.go: -------------------------------------------------------------------------------- 1 | package quantize 2 | 3 | type matrix interface { 4 | set(val float64) 5 | add(r, l matrix) 6 | sub(r, l matrix) 7 | rcount() int 8 | ccount() int 9 | at(i, j int) float64 10 | } 11 | 12 | type mat3x3 [][]float64 13 | 14 | func newMat3x3() mat3x3 { 15 | v := make([][]float64, 3) 16 | for i := range v { 17 | v[i] = make([]float64, 3) 18 | } 19 | return mat3x3(v) 20 | } 21 | 22 | func (m mat3x3) at(r, c int) float64 { 23 | return m[r][c] 24 | } 25 | 26 | func (m mat3x3) rcount() int { 27 | return 3 28 | } 29 | 30 | func (m mat3x3) ccount() int { 31 | return 3 32 | } 33 | 34 | func (m mat3x3) set(val float64) { 35 | for i := 0; i < 3; i++ { 36 | for j := 0; j < 3; j++ { 37 | m[i][j] = val 38 | } 39 | } 40 | } 41 | 42 | func (m mat3x3) mul(l, r matrix) { 43 | m.set(0.0) 44 | for i := 0; i < l.rcount(); i++ { 45 | for j := 0; j < r.ccount(); j++ { 46 | for k := 0; k < l.ccount(); k++ { 47 | m[i][j] += l.at(i, k) * r.at(k, j) 48 | } 49 | } 50 | } 51 | } 52 | 53 | func (m mat3x3) mul3x1And1x3(l vec3x1, r vec1x3) { 54 | for i := 0; i < 3; i++ { 55 | for j := 0; j < 3; j++ { 56 | m[i][j] = l[i][0] * r[0][j] 57 | } 58 | } 59 | } 60 | 61 | func (m mat3x3) add(l, r matrix) { 62 | for i := 0; i < 3; i++ { 63 | for j := 0; j < 3; j++ { 64 | m[i][j] = l.at(i, j) + r.at(i, j) 65 | } 66 | } 67 | } 68 | 69 | func (m mat3x3) sub(l, r matrix) { 70 | for i := 0; i < 3; i++ { 71 | for j := 0; j < 3; j++ { 72 | m[i][j] = l.at(i, j) - r.at(i, j) 73 | } 74 | } 75 | } 76 | 77 | func (m mat3x3) apply(f func(i, j int, v float64) float64, r mat3x3) { 78 | for i := 0; i < 3; i++ { 79 | for j := 0; j < 3; j++ { 80 | m[i][j] = f(i, j, r[i][j]) 81 | } 82 | } 83 | } 84 | 85 | type vec3x1 [][]float64 86 | 87 | func (m vec3x1) at(r, c int) float64 { 88 | return m[r][c] 89 | } 90 | 91 | func newVec3x1() vec3x1 { 92 | v := make([][]float64, 3) 93 | for i := range v { 94 | v[i] = make([]float64, 1) 95 | } 96 | return vec3x1(v) 97 | } 98 | 99 | func (m vec3x1) rcount() int { 100 | return 3 101 | } 102 | 103 | func (m vec3x1) ccount() int { 104 | return 1 105 | } 106 | 107 | func (m vec3x1) set(val float64) { 108 | for i := 0; i < 3; i++ { 109 | m[i][0] = val 110 | } 111 | } 112 | 113 | func (m vec3x1) setVec(vals []float64) { 114 | for i := 0; i < 3; i++ { 115 | m[i][0] = vals[i] 116 | } 117 | } 118 | 119 | func (m vec3x1) add(l, r matrix) { 120 | for i := 0; i < 3; i++ { 121 | m[i][0] = l.at(i, 0) + r.at(i, 0) 122 | } 123 | } 124 | 125 | func (m vec3x1) sub(l, r matrix) { 126 | for i := 0; i < 3; i++ { 127 | m[i][0] = l.at(i, 0) - r.at(i, 0) 128 | } 129 | } 130 | 131 | func (m vec3x1) atVec(i int) float64 { 132 | return m[i][0] 133 | } 134 | 135 | func (m vec3x1) t(v vec1x3) { 136 | m[0][0] = v[0][0] 137 | m[1][0] = v[0][1] 138 | m[2][0] = v[0][2] 139 | } 140 | 141 | type vec1x3 [][]float64 142 | 143 | func (m vec1x3) at(r, c int) float64 { 144 | return m[r][c] 145 | } 146 | 147 | func newVec1x3() vec1x3 { 148 | v := make([][]float64, 1) 149 | for i := range v { 150 | v[i] = make([]float64, 3) 151 | } 152 | return vec1x3(v) 153 | } 154 | 155 | func (m vec1x3) rcount() int { 156 | return 1 157 | } 158 | 159 | func (m vec1x3) ccount() int { 160 | return 3 161 | } 162 | 163 | func (m vec1x3) set(val float64) { 164 | for i := 0; i < 3; i++ { 165 | m[0][i] = val 166 | } 167 | } 168 | 169 | func (m vec1x3) setVec(vals []float64) { 170 | for i := 0; i < 3; i++ { 171 | m[0][i] = vals[i] 172 | } 173 | } 174 | 175 | func (m vec1x3) add(l, r matrix) { 176 | for i := 0; i < 3; i++ { 177 | m[0][i] = l.at(0, i) + r.at(0, i) 178 | } 179 | } 180 | 181 | func (m vec1x3) sub(l, r matrix) { 182 | for i := 0; i < 3; i++ { 183 | m[0][i] = l.at(0, i) - r.at(0, i) 184 | } 185 | } 186 | 187 | func (m vec1x3) atVec(i int) float64 { 188 | return m[0][i] 189 | } 190 | 191 | func (m vec1x3) t(v vec3x1) { 192 | m[0][0] = v[0][0] 193 | m[0][1] = v[1][0] 194 | m[0][2] = v[2][0] 195 | } 196 | --------------------------------------------------------------------------------