Nykakin / quantize

Image quantization in Golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

License:MIT License


Languages

Language:Go 100.0%