Skip to content

Nykakin/quantize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

quantize

This package reimplements hierarchical quantization described in this tutorial in Go programming language. Gonum was used instead of OpenCV, then latter replaced with simpler in-home matrix types and eigenvalue decomposition algorithm adapred from Java JAML package. This allowed to reduce ammount of needed dependencies. The effect and comparission with different Go packages can be found in this repository. 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.

package main

import (
    "image"
    "image/color"
    _ "image/jpeg"
    _ "image/png"
    "os"

    "github.com/Nykakin/quantize"
    "github.com/joshdk/preview"
)   

func main() {
    f, err := os.Open("test.jpg")
    if err != nil {
        panic(err)
    }
    defer f.Close()
    img, _, err := image.Decode(f)
    if err != nil {
        panic(err)
    }

    quantizer := quantize.NewHierarhicalQuantizer()
    colors, err := quantizer.Quantize(img, 5)
    if err != nil {
        panic(err)
    }    

    palette := make([]color.Color, len(colors))
    for index, clr := range colors {
    	palette[index] = clr
    }

    // Display our new palette
    preview.Show(palette)
}

About

Image quantization in Golang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages