mean

package
v0.0.0-...-92869c5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2015 License: MIT, Apache-2.0 Imports: 5 Imported by: 0

README

Mean

A simple color quantizer. Similar to a median cut algorithm execept it uses the mean rather than the median. While the median seems technically correct the mean seems good enough and is easier to compute. This implementation is also simplified over traditional median cut algorithms by replacing the priority queue with a simple linear search. For a typical number of colors (256) a linear search is fast enough and does not represent a significant inefficiency.

A bit of sophistication added though is a two stage clustering process. The first stage takes a stab at clipping tails off the distributions of colors by pixel population, with the goal of smoother transitions in larger areas of lower color density. The second stage attempts to allocate remaining palette entries more uniformly. It prioritizes by a combination of pixel population and color range and splits clusters at mean values.

Documentation

Overview

Mean is a simple color quantizer. The algorithm successively divides the color space much like a median cut algorithm, but a mean statistic is used rather than a median. In another simplification, there is no priority queue to order color blocks; linear search is used instead.

An added sopphistication though, is that division proceeds in two stages, with somewhat different criteria used for the earlier cuts than for the later cuts.

Motivation for using the mean is the observation that in a two stage algorithm, cuts are offset from the computed average so having the logically "correct" value of the median must not be that important. Motivation for the linear search is that the number of blocks to search is limited to the target number of colors in the palette, which is small and typically limited to 256. If n is 256, O(log n) and O(n) both become O(1).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Quantizer

type Quantizer int

Quantizer methods implement mean cut color quantization.

The value is the target number of colors. Methods do not require pointer receivers, simply construct Quantizer objects with a type conversion.

The type satisfies both quant.Quantizer and draw.Quantizer interfaces.

func (Quantizer) Image

func (q Quantizer) Image(img image.Image) *image.Paletted

Image performs color quantization and returns a paletted image.

Returned is a paletted image with no more than q colors. Note though that image.Paletted is limited to 256 colors.

func (Quantizer) Palette

func (q Quantizer) Palette(img image.Image) quant.Palette

Palette performs color quantization and returns a quant.Palette object.

Returned is a palette with no more than q colors. Q may be > 256.

func (Quantizer) Quantize

func (Quantizer) Quantize(p color.Palette, m image.Image) color.Palette

Quantize performs color quantization and returns a color.Palette.

Following the behavior documented with the draw.Quantizer interface, "Quantize appends up to cap(p) - len(p) colors to p and returns the updated palette...." This method does not limit the number of colors to 256. Cap(p) or the quantity cap(p) - len(p) may be > 256. Also for this method the value of the Quantizer object is ignored.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL