Color quantization: Difference between revisions
(Updated D entry) |
(Updated D entry) |
||
Line 137: | Line 137: | ||
enum Axis { R, G, B } |
enum Axis { R, G, B } |
||
enum round = (in float x) pure nothrow => cast(int)floor(x + 0.5); |
|||
return cast(int)floor(x + 0.5); |
|||
⚫ | |||
enum roundRGB = (in Col c) pure nothrow => RGB(cast(ubyte)round(c.r), |
|||
cast(ubyte)round(c.g), |
|||
cast(ubyte)round(c. |
cast(ubyte)round(c.b)); |
||
cast(ubyte)round(c.b)); |
|||
⚫ | |||
} |
|||
⚫ | |||
Col meanRGB(in Col[] pxList) pure nothrow { |
Col meanRGB(in Col[] pxList) pure nothrow { |
||
immutable tot = reduce!addRGB(Col(0, 0, 0), pxList); |
|||
⚫ | |||
⚫ | |||
} |
|||
immutable Col tot = reduce!addRGB(Col(0, 0, 0), pxList); |
|||
⚫ | |||
return Col(tot.r / n, tot.g / n, tot.b / n); |
return Col(tot.r / n, tot.g / n, tot.b / n); |
||
} |
} |
||
⚫ | |||
⚫ | |||
enum maxC = (in Col c1, in Col c2) pure nothrow => |
|||
⚫ | |||
Tuple!(Col, Col) extrems(in Col[] lst) pure nothrow { |
Tuple!(Col, Col) extrems(in Col[] lst) pure nothrow { |
||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
} |
|||
enum FI = float.infinity; |
enum FI = float.infinity; |
||
auto mmRGB = typeof(return)(Col(FI, FI, FI), Col(-FI, -FI, -FI)); |
auto mmRGB = typeof(return)(Col(FI, FI, FI), Col(-FI, -FI, -FI)); |
||
return reduce!( |
return reduce!(minC, maxC)(mmRGB, lst); |
||
} |
} |
||
Line 177: | Line 173: | ||
Cluster makeCluster(Col[] pixelList) pure nothrow { |
Cluster makeCluster(Col[] pixelList) pure nothrow { |
||
immutable vol_dims = |
immutable vol_dims = pixelList.volumeAndDims; |
||
immutable int len = pixelList.length; |
immutable int len = pixelList.length; |
||
return |
return typeof(return)(pixelList.meanRGB, |
||
len * vol_dims[0], |
len * vol_dims[0], |
||
vol_dims[1], |
vol_dims[1], |
||
pixelList); |
pixelList); |
||
} |
} |
||
enum fCmp = (in float a, in float b) pure nothrow => |
|||
(a > b) ? 1 : (a < b ? -1 : 0); |
|||
Axis largestAxis(in Col c) pure nothrow { |
Axis largestAxis(in Col c) pure nothrow { |
||
immutable int r1 = fCmp(c.r, c.g); |
|||
immutable int r2 = fCmp(c.r, c.b); |
|||
⚫ | |||
} |
|||
if (r1 == -1 && r2 == 1) return Axis.G; |
|||
if (r1 == 1 && r2 == -1) return Axis.B; |
|||
return (fCmp(c.g, c.b) == 1) ? Axis.G : Axis.B; |
|||
⚫ | |||
if (r1 == 1 && r2 == -1) return Axis.B; |
|||
⚫ | |||
} |
} |
||
Line 200: | Line 196: | ||
in Col vol, Col[] pixels) |
in Col vol, Col[] pixels) |
||
pure nothrow { |
pure nothrow { |
||
bool delegate(immutable Col |
bool delegate(immutable Col) pure nothrow partFunc; |
||
final switch (largestAxis(vol)) { |
final switch (largestAxis(vol)) { |
||
case Axis.R: partFunc = c1 => c1.r < c.r; break; |
case Axis.R: partFunc = c1 => c1.r < c.r; break; |
||
Line 206: | Line 202: | ||
case Axis.B: partFunc = c1 => c1.b < c.b; break; |
case Axis.B: partFunc = c1 => c1.b < c.b; break; |
||
} |
} |
||
auto px2 = pixels.partition!partFunc; |
|||
auto px1 = pixels[0 .. $ - px2.length]; |
|||
return typeof(return)(px1.makeCluster, px2.makeCluster); |
return typeof(return)(px1.makeCluster, px2.makeCluster); |
||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
Image!RGB colorQuantize(in Image!RGB img, in int n) pure nothrow { |
Image!RGB colorQuantize(in Image!RGB img, in int n) pure nothrow { |
||
immutable |
immutable width = img.nx; |
||
immutable |
immutable height = img.ny; |
||
auto cols = new Col[width * height]; |
auto cols = new Col[width * height]; |
||
Line 229: | Line 232: | ||
clusters = [cl[].subdivide[]] ~ |
clusters = [cl[].subdivide[]] ~ |
||
clusters.remove!(c => c == cl, SwapStrategy.unstable); |
clusters.remove!(c => c == cl, SwapStrategy.unstable); |
||
} |
|||
⚫ | |||
return c.r | (c.g << 8) | (c.b << 16); |
|||
} |
} |
||
Line 238: | Line 237: | ||
ubyte[4] u4a, u4b; |
ubyte[4] u4a, u4b; |
||
foreach (const cluster; clusters) { |
foreach (const cluster; clusters) { |
||
immutable ubyteMean = |
immutable ubyteMean = cluster[0].roundRGB.RGB2uint; |
||
foreach (immutable col; cluster[3]) |
foreach (immutable col; cluster[3]) |
||
pixMap[ |
pixMap[col.roundRGB.RGB2uint] = ubyteMean; |
||
} |
} |
||
auto result = new Image!RGB; |
auto result = new Image!RGB; |
||
result.allocate(height, width); |
result.allocate(height, width); |
||
⚫ | |||
return RGB( c & 0xFF, |
|||
(c >> 8) & 0xFF, |
|||
⚫ | |||
} |
|||
foreach (immutable i, immutable p; img.image) { |
foreach (immutable i, immutable p; img.image) { |
||
immutable u3a = p.tupleof.RGB; |
immutable u3a = p.tupleof.RGB; |
||
result.image[i] = |
result.image[i] = pixMap[RGB2uint(u3a)].uintToRGB; |
||
} |
} |
||
Revision as of 20:47, 13 September 2013
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Color quantization is the process of reducing number of colors used in an image while trying to maintain the visual appearance of the original image. In general, it is a form of cluster analysis, if each RGB color value is considered as a coordinate triple in the 3D colorspace. There are some well know algorithms [1], each with its own advantages and drawbacks.
Task: Take an RGB color image and reduce its colors to some smaller number (< 256). For this task, use the frog as input and reduce colors to 16, and output the resulting colors. The chosen colors should be adaptive to the input image, meaning you should not use a fixed palette such as Web colors or Windows system palette. Dithering is not required.
Note: the funny color bar on top of the frog image is intentional.
C
Using an octree to store colors. Here are only the relevant parts. For full C code see Color_quantization/C. It's different from the standard octree method in that:
- Each node can both be leaf node and have child nodes;
- Leaf nodes are not folded until all pixels are in. This removes the possibility of early pixels completely bias the tree. And child nodes are reduced one at a time instead of typical all or nothing approach.
- Node folding priorities are tracked by a binary heap instead of typical linked list.
The output image is better at preserving textures of the original than Gimp, though it obviously depends on the input image. This particular frog image has the color bar added at the top specifically to throw off some early truncation algorithms, which Gimp is suseptible to. <lang c>typedef struct oct_node_t oct_node_t, *oct_node; struct oct_node_t{ /* sum of all colors represented by this node. 64 bit in case of HUGE image */ uint64_t r, g, b; int count, heap_idx; oct_node kids[8], parent; unsigned char n_kids, kid_idx, flags, depth; };
/* cmp function that decides the ordering in the heap. This is how we determine
which octree node to fold next, the heart of the algorithm. */
inline int cmp_node(oct_node a, oct_node b) { if (a->n_kids < b->n_kids) return -1; if (a->n_kids > b->n_kids) return 1;
int ac = a->count * (1 + a->kid_idx) >> a->depth; int bc = b->count * (1 + b->kid_idx) >> b->depth; return ac < bc ? -1 : ac > bc; }
/* adding a color triple to octree */ oct_node node_insert(oct_node root, unsigned char *pix) {
- define OCT_DEPTH 8
/* 8: number of significant bits used for tree. It's probably good enough for most images to use a value of 5. This affects how many nodes eventually end up in the tree and heap, thus smaller values helps with both speed and memory. */
unsigned char i, bit, depth = 0; for (bit = 1 << 7; ++depth < OCT_DEPTH; bit >>= 1) { i = !!(pix[1] & bit) * 4 + !!(pix[0] & bit) * 2 + !!(pix[2] & bit); if (!root->kids[i]) root->kids[i] = node_new(i, depth, root);
root = root->kids[i]; }
root->r += pix[0]; root->g += pix[1]; root->b += pix[2]; root->count++; return root; }
/* remove a node in octree and add its count and colors to parent node. */ oct_node node_fold(oct_node p) { if (p->n_kids) abort(); oct_node q = p->parent; q->count += p->count;
q->r += p->r; q->g += p->g; q->b += p->b; q->n_kids --; q->kids[p->kid_idx] = 0; return q; }
/* traverse the octree just like construction, but this time we replace the pixel
color with color stored in the tree node */
void color_replace(oct_node root, unsigned char *pix) { unsigned char i, bit;
for (bit = 1 << 7; bit; bit >>= 1) { i = !!(pix[1] & bit) * 4 + !!(pix[0] & bit) * 2 + !!(pix[2] & bit); if (!root->kids[i]) break; root = root->kids[i]; }
pix[0] = root->r; pix[1] = root->g; pix[2] = root->b; }
/* Building an octree and keep leaf nodes in a bin heap. Afterwards remove first node
in heap and fold it into its parent node (which may now be added to heap), until heap contains required number of colors. */
void color_quant(image im, int n_colors) { int i; unsigned char *pix = im->pix; node_heap heap = { 0, 0, 0 };
oct_node root = node_new(0, 0, 0), got; for (i = 0; i < im->w * im->h; i++, pix += 3) heap_add(&heap, node_insert(root, pix));
while (heap.n > n_colors + 1) heap_add(&heap, node_fold(pop_heap(&heap)));
double c; for (i = 1; i < heap.n; i++) { got = heap.buf[i]; c = got->count; got->r = got->r / c + .5; got->g = got->g / c + .5; got->b = got->b / c + .5; printf("%2d | %3llu %3llu %3llu (%d pixels)\n", i, got->r, got->g, got->b, got->count); }
for (i = 0, pix = im->pix; i < im->w * im->h; i++, pix += 3) color_replace(root, pix);
node_free(); free(heap.buf); }</lang>
D
This code retains the style of the original OCaML code, and uses the bitmap module from the Bitmap Task. <lang d>import core.stdc.stdio, std.stdio, std.algorithm, std.typecons,
std.math, std.range, std.conv, std.string, bitmap;
struct Col { float r, g, b; } alias Cluster = Tuple!(Col, float, Col, Col[]); enum Axis { R, G, B }
enum round = (in float x) pure nothrow => cast(int)floor(x + 0.5);
enum roundRGB = (in Col c) pure nothrow => RGB(cast(ubyte)round(c.r),
cast(ubyte)round(c.g), cast(ubyte)round(c.b));
enum addRGB = (in Col c1, in Col c2) pure nothrow =>
Col(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b);
Col meanRGB(in Col[] pxList) pure nothrow {
immutable tot = reduce!addRGB(Col(0, 0, 0), pxList); immutable n = pxList.length; return Col(tot.r / n, tot.g / n, tot.b / n);
}
enum minC = (in Col c1, in Col c2) pure nothrow =>
Col(min(c1.r, c2.r), min(c1.g, c2.g), min(c1.b, c2.b));
enum maxC = (in Col c1, in Col c2) pure nothrow =>
Col(max(c1.r, c2.r), max(c1.g, c2.g), max(c1.b, c2.b));
Tuple!(Col, Col) extrems(in Col[] lst) pure nothrow {
enum FI = float.infinity; auto mmRGB = typeof(return)(Col(FI, FI, FI), Col(-FI, -FI, -FI)); return reduce!(minC, maxC)(mmRGB, lst);
}
Tuple!(float, Col) volumeAndDims(in Col[] lst) pure nothrow {
immutable e = lst.extrems; immutable r = Col(e[1].r - e[0].r, e[1].g - e[0].g, e[1].b - e[0].b); return typeof(return)(r.r * r.g * r.b, r);
}
Cluster makeCluster(Col[] pixelList) pure nothrow {
immutable vol_dims = pixelList.volumeAndDims; immutable int len = pixelList.length; return typeof(return)(pixelList.meanRGB, len * vol_dims[0], vol_dims[1], pixelList);
}
enum fCmp = (in float a, in float b) pure nothrow =>
(a > b) ? 1 : (a < b ? -1 : 0);
Axis largestAxis(in Col c) pure nothrow {
immutable int r1 = fCmp(c.r, c.g); immutable int r2 = fCmp(c.r, c.b); if (r1 == 1 && r2 == 1) return Axis.R; if (r1 == -1 && r2 == 1) return Axis.G; if (r1 == 1 && r2 == -1) return Axis.B; return (fCmp(c.g, c.b) == 1) ? Axis.G : Axis.B;
}
Tuple!(Cluster, Cluster) subdivide(in Col c, in float nVolProd,
in Col vol, Col[] pixels)
pure nothrow {
bool delegate(immutable Col) pure nothrow partFunc; final switch (largestAxis(vol)) { case Axis.R: partFunc = c1 => c1.r < c.r; break; case Axis.G: partFunc = c1 => c1.g < c.g; break; case Axis.B: partFunc = c1 => c1.b < c.b; break; } auto px2 = pixels.partition!partFunc; auto px1 = pixels[0 .. $ - px2.length]; return typeof(return)(px1.makeCluster, px2.makeCluster);
}
uint RGB2uint(in RGB c) pure nothrow {
return c.r | (c.g << 8) | (c.b << 16);
}
enum uintToRGB = (in uint c) pure nothrow =>
RGB(c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF);
Image!RGB colorQuantize(in Image!RGB img, in int n) pure nothrow {
immutable width = img.nx; immutable height = img.ny;
auto cols = new Col[width * height]; foreach (immutable i, ref c; img.image) cols[i] = Col(c.tupleof);
immutable dumb = Col(0, 0, 0); Cluster unused= Cluster(dumb, -float.infinity, dumb, (Col[]).init);
auto clusters = [cols.makeCluster]; while (clusters.length < n) { // Cluster cl = clusters.reduce!(max!q{ a[1] })(unused); Cluster cl = reduce!((c1, c2) => c1[1] > c2[1] ? c1 : c2) (unused, clusters); clusters = [cl[].subdivide[]] ~ clusters.remove!(c => c == cl, SwapStrategy.unstable); }
uint[uint] pixMap; // Faster than RGB[RGB]. ubyte[4] u4a, u4b; foreach (const cluster; clusters) { immutable ubyteMean = cluster[0].roundRGB.RGB2uint; foreach (immutable col; cluster[3]) pixMap[col.roundRGB.RGB2uint] = ubyteMean; }
auto result = new Image!RGB; result.allocate(height, width);
foreach (immutable i, immutable p; img.image) { immutable u3a = p.tupleof.RGB; result.image[i] = pixMap[RGB2uint(u3a)].uintToRGB; }
return result;
}
void main(in string[] args) {
string fileName; int nCols; switch (args.length) { case 1: fileName = "quantum_frog.ppm"; nCols = 16; break; case 3: fileName = args[1]; nCols = args[2].to!int; break; default: "Usage: color_quantization image.ppm ncolors".writeln; return; }
auto im = new Image!RGB; im.loadPPM6(fileName); const imq = colorQuantize(im, nCols); imq.savePPM6("quantum_frog_quantized.ppm");
}</lang>
Go
A very basic median cut algorithm. <lang go>package main
import (
"container/heap" "image" "image/color" "image/png" "log" "math" "os" "sort"
)
func main() {
f, err := os.Open("Quantum_frog.png") if err != nil { log.Fatal(err) } img, err := png.Decode(f) f.Close() if err != nil { log.Fatal(err) } fq, err := os.Create("frog256.png") if err != nil { log.Fatal(err) } err = png.Encode(fq, quant(img, 256)) if err != nil { log.Fatal(err) }
}
// Organize quatization in some logical steps. func quant(img image.Image, nq int) image.Image {
qz := newQuantizer(img, nq) // set up a work space qz.cluster() // cluster pixels by color return qz.Paletted() // generate paletted image from clusters
}
// A workspace with members that can be accessed by methods. type quantizer struct {
img image.Image // original image cs []cluster // len is the desired number of colors px []point // list of all points in the image ch chValues // buffer for computing median eq []point // additional buffer used when splitting cluster
}
type cluster struct {
px []point // list of points in the cluster widestCh int // rx, gx, bx const for channel with widest value range chRange uint32 // value range (vmax-vmin) of widest channel index int // heap index
}
type point struct{ x, y int } type chValues []uint32 type queue []*cluster
const (
rx = iota gx bx
)
func newQuantizer(img image.Image, nq int) *quantizer {
b := img.Bounds() npx := (b.Max.X - b.Min.X) * (b.Max.Y - b.Min.Y) // Create work space. qz := &quantizer{ img: img, ch: make(chValues, npx), cs: make([]cluster, nq), } // Populate initial cluster with all pixels from image. c := &qz.cs[0] px := make([]point, npx) c.px = px i := 0 for y := b.Min.Y; y < b.Max.Y; y++ { for x := b.Min.X; x < b.Max.X; x++ { px[i].x = x px[i].y = y i++ } } return qz
}
func (qz *quantizer) cluster() {
// Cluster by repeatedly splitting clusters. // Use a heap as priority queue for picking clusters to split. // The rule will be to spilt the cluster with the most pixels. // Terminate when the desired number of clusters has been populated // or when clusters cannot be further split. pq := new(queue) // Initial cluster. populated at this point, but not analyzed. c := &qz.cs[0] for i := 1; ; { qz.setColorRange(c) // Cluster cannot be split if all pixels are the same color. // Only enqueue clusters that can be split. if c.chRange > 0 { heap.Push(pq, c) // add new cluster to queue } // If no clusters have any color variation, mark the end of the // cluster list and quit early. if len(*pq) == 0 { qz.cs = qz.cs[:i] break } s := heap.Pop(pq).(*cluster) // get cluster to split c = &qz.cs[i] // set c to new cluster i++ m := qz.Median(s) qz.Split(s, c, m) // split s into c and s // If that was the last cluster, we're done. if i == len(qz.cs) { break } qz.setColorRange(s) if s.chRange > 0 { heap.Push(pq, s) // return to queue } }
}
func (q *quantizer) setColorRange(c *cluster) {
// Find extents of color values in each channel. var maxR, maxG, maxB uint32 minR := uint32(math.MaxUint32) minG := uint32(math.MaxUint32) minB := uint32(math.MaxUint32) for _, p := range c.px { r, g, b, _ := q.img.At(p.x, p.y).RGBA() if r < minR { minR = r } if r > maxR { maxR = r } if g < minG { minG = g } if g > maxG { maxG = g } if b < minB { minB = b } if b > maxB { maxB = b } } // See which channel had the widest range. s := gx min := minG max := maxG if maxR-minR > max-min { s = rx min = minR max = maxR } if maxB-minB > max-min { s = bx min = minB max = maxB } c.widestCh = s c.chRange = max - min // also store the range of that channel
}
func (q *quantizer) Median(c *cluster) uint32 {
px := c.px ch := q.ch[:len(px)] // Copy values from appropriate channel to buffer for computing median. switch c.widestCh { case rx: for i, p := range c.px { ch[i], _, _, _ = q.img.At(p.x, p.y).RGBA() } case gx: for i, p := range c.px { _, ch[i], _, _ = q.img.At(p.x, p.y).RGBA() } case bx: for i, p := range c.px { _, _, ch[i], _ = q.img.At(p.x, p.y).RGBA() } } // Median algorithm. sort.Sort(ch) half := len(ch) / 2 m := ch[half] if len(ch)%2 == 0 { m = (m + ch[half-1]) / 2 } return m
}
func (q *quantizer) Split(s, c *cluster, m uint32) {
px := s.px var v uint32 i := 0 lt := 0 gt := len(px) - 1 eq := q.eq[:0] // reuse any existing buffer for i <= gt { // Get pixel value of appropriate channel. r, g, b, _ := q.img.At(px[i].x, px[i].y).RGBA() switch s.widestCh { case rx: v = r case gx: v = g case bx: v = b } // Categorize each pixel as either <, >, or == median. switch { case v < m: px[lt] = px[i] lt++ i++ case v > m: px[gt], px[i] = px[i], px[gt] gt-- default: eq = append(eq, px[i]) i++ } } // Handle values equal to the median. if len(eq) > 0 { copy(px[lt:], eq) // move them back between the lt and gt values. // Then, if the number of gt values is < the number of lt values, // fix up i so that the split will include the eq values with // the gt values. if len(px)-i < lt { i = lt } q.eq = eq // squirrel away (possibly expanded) buffer for reuse } // Split the pixel list. s.px = px[:i] c.px = px[i:]
}
func (qz *quantizer) Paletted() *image.Paletted {
cp := make(color.Palette, len(qz.cs)) pi := image.NewPaletted(qz.img.Bounds(), cp) for i := range qz.cs { px := qz.cs[i].px // Average values in cluster to get palette color. var rsum, gsum, bsum int64 for _, p := range px { r, g, b, _ := qz.img.At(p.x, p.y).RGBA() rsum += int64(r) gsum += int64(g) bsum += int64(b) } n64 := int64(len(px)) cp[i] = color.NRGBA64{ uint16(rsum / n64), uint16(gsum / n64), uint16(bsum / n64), 0xffff, } // set image pixels for _, p := range px { pi.SetColorIndex(p.x, p.y, uint8(i)) } } return pi
}
// Implement sort.Interface for sort in median algorithm. func (c chValues) Len() int { return len(c) } func (c chValues) Less(i, j int) bool { return c[i] < c[j] } func (c chValues) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
// Implement heap.Interface for priority queue of clusters. func (q queue) Len() int { return len(q) }
// Less implements rule to select cluster with greatest number of pixels. func (q queue) Less(i, j int) bool {
return len(q[j].px) < len(q[i].px)
}
func (q queue) Swap(i, j int) {
q[i], q[j] = q[j], q[i] q[i].index = i q[j].index = j
} func (pq *queue) Push(x interface{}) {
c := x.(*cluster) c.index = len(*pq) *pq = append(*pq, c)
} func (pq *queue) Pop() interface{} {
q := *pq n := len(q) - 1 c := q[n] *pq = q[:n] return c
}</lang>
Mathematica
<lang Mathematica>ColorQuantize[Import["http://rosettacode.org/mw/images/3/3f/Quantum_frog.png"],16,Dithering->False]</lang>
OCaml
Here we use a simplified method inspired from this paper: www.leptonica.com/papers/mediancut.pdf
<lang ocaml>let rem_from rem from =
List.filter ((<>) rem) from
let float_rgb (r,g,b) = (* prevents int overflow *)
(float r, float g, float b)
let round x =
int_of_float (floor (x +. 0.5))
let int_rgb (r,g,b) =
(round r, round g, round b)
let rgb_add (r1,g1,b1) (r2,g2,b2) =
(r1 +. r2, g1 +. g2, b1 +. b2)
let rgb_mean px_list =
let n = float (List.length px_list) in let r, g, b = List.fold_left rgb_add (0.0, 0.0, 0.0) px_list in (r /. n, g /. n, b /. n)
let extrems lst =
let min_rgb = (infinity, infinity, infinity) and max_rgb = (neg_infinity, neg_infinity, neg_infinity) in List.fold_left (fun ((sr,sg,sb), (mr,mg,mb)) (r,g,b) -> ((min sr r), (min sg g), (min sb b)), ((max mr r), (max mg g), (max mb b)) ) (min_rgb, max_rgb) lst
let volume_and_dims lst =
let (sr,sg,sb), (br,bg,bb) = extrems lst in let dr, dg, db = (br -. sr), (bg -. sg), (bb -. sb) in (dr *. dg *. db), (dr, dg, db)
let make_cluster pixel_list =
let vol, dims = volume_and_dims pixel_list in let len = float (List.length pixel_list) in (rgb_mean pixel_list, len *. vol, dims, pixel_list)
type axis = R | G | B let largest_axis (r,g,b) =
match compare r g, compare r b with | 1, 1 -> R | -1, 1 -> G | 1, -1 -> B | _ -> match compare g b with | 1 -> G | _ -> B
let subdivide ((mr,mg,mb), n_vol_prod, vol, pixels) =
let part_func = match largest_axis vol with | R -> (fun (r,_,_) -> r < mr) | G -> (fun (_,g,_) -> g < mg) | B -> (fun (_,_,b) -> b < mb) in let px1, px2 = List.partition part_func pixels in (make_cluster px1, make_cluster px2)
let color_quant img n =
let width, height = get_dims img in let clusters = let lst = ref [] in for x = 0 to pred width do for y = 0 to pred height do let rgb = float_rgb (get_pixel_unsafe img x y) in lst := rgb :: !lst done; done; ref [make_cluster !lst] in while (List.length !clusters) < n do let dumb = (0.0,0.0,0.0) in let unused = (dumb, neg_infinity, dumb, []) in let select ((_,v1,_,_) as c1) ((_,v2,_,_) as c2) = if v1 > v2 then c1 else c2 in let cl = List.fold_left (fun c1 c2 -> select c1 c2) unused !clusters in let cl1, cl2 = subdivide cl in clusters := cl1 :: cl2 :: (rem_from cl !clusters) done; let module PxMap = Map.Make (struct type t = float * float * float let compare = compare end) in let m = List.fold_left (fun m (mean, _, _, pixel_list) -> let int_mean = int_rgb mean in List.fold_left (fun m px -> PxMap.add px int_mean m) m pixel_list ) PxMap.empty !clusters in let res = new_img ~width ~height in for y = 0 to pred height do for x = 0 to pred width do let rgb = float_rgb (get_pixel_unsafe img x y) in let mean_rgb = PxMap.find rgb m in put_pixel_unsafe res mean_rgb x y; done; done; (res)</lang>
J
Here, we use a simplistic averaging technique to build an initial set of colors and then use k-means clustering to refine them.
<lang j>kmcL=:4 :0
C=. /:~ 256 #.inv ,y NB. colors G=. x (i.@] <.@* %) #C NB. groups (initial) Q=. _ NB. quantized list of colors (initial whilst.-. Q-:&<.&(x&*)Q0 do. Q0=. Q Q=. /:~C (+/ % #)/.~ G G=. (i. <./)"1 C +/&.:*: .- |:Q end.Q
)</lang>
The left argument is the number of colors desired.
The right argument is the image, with pixels represented as bmp color integers (base 256 numbers).
The result is the colors represented as pixel triples (blue, green, red). They are shown here as fractional numbers, but they should be either rounded to the nearest integer in the range 0..255 (and possibly converted back to bmp integer form) or scaled so they are floating point triples in the range 0..1.
<lang j> 16 kmcL img 7.52532 22.3347 0.650468 8.20129 54.4678 0.0326828 33.1132 69.8148 0.622265 54.2232 125.682 2.67713 56.7064 99.5008 3.04013 61.2135 136.42 4.2015 68.1246 140.576 6.37512 74.6006 143.606 7.57854 78.9101 150.792 10.2563 89.5873 148.621 14.6202 98.9523 154.005 25.7583 114.957 159.697 47.6423 145.816 178.136 33.8845 164.969 199.742 67.0467 179.849 207.594 109.973 209.229 221.18 204.513</lang>
PureBasic
<lang PureBasic>
- ColorQuantization.pb
Structure bestA_ ; table for our histogram nn.i ; 16,32,... rc.i ; red count within (0,1,...,255)/(number of colors) gc.i ; green count within (0,1,...,255)/(number of colors) bc.i ; blue count within (0,1,...,255)/(number of colors) EndStructure
- these two functions appear to be rather self-explanatory
UsePNGImageDecoder() UsePNGImageEncoder()
Procedure.i ColorQuantization(Filename$,ncol) Protected x,y,c
- load our original image or leave the procedure
If not LoadImage(0,Filename$) :ProcedureReturn 0:endif
- we are not going to actually draw on the original image...
- but we need to use the drawing library to load up
- the pixel information into our arrays...
- if we can't do that, what's the point of going any further?
- so then we would be wise to just leave the procedure [happy fred?]
If not StartDrawing(ImageOutput(0)):ProcedureReturn 0:endif
iw=ImageWidth(0) ih=ImageHeight(0)
dim cA(iw,ih) ; color array to hold at a given (x,y) dim rA(iw,ih) ; red array to hold at a given (x,y) dim gA(iw,ih) ; green array to hold at a given (x,y) dim bA(iw,ih) ; blue array to hold at a given (x,y) dim tA(iw,ih) ; temp array to hold at a given (x,y)
- map each pixel from the original image to our arrays
- don't overrun the ranges ie. use {ih-1,iw-1}
for y=0 to ih-1
for x=0 to iw-1 c = Point(x,y) cA(x,y)=c rA(x,y)=Red(c) gA(x,y)=Green(c) bA(x,y)=Blue(c) next
next
StopDrawing() ; don't forget to... StopDrawing()
N=ih*iw
- N is the total number if pixels
if not N:ProcedureReturn 0:endif ; to avoid a division by zero
- stuctured array ie. a table to hold the frequency distribution
dim bestA.bestA_(ncol)
- the "best" red,green,blue based upon frequency
dim rbestA(ncol/3) dim gbestA(ncol/3) dim bbestA(ncol/3)
- split the (0..255) range up
xoff=256/ncol ;256/16=16 xrng=xoff ;xrng=16
- store these values in our table
- bestA(i)\nn= 16,32,...
for i=1 to ncol xrng+xoff bestA(i)\nn=xrng next
- scan by row [y]
for y=0 to ih-1
- scan by col [x]
for x=0 to iw-1
- retrieve the rgb values from each pixel
r=rA(x,y) g=gA(x,y) b=bA(x,y)
- sum up the numbers that fall within our subdivisions of (0..255)
for i=1 to ncol if r>=bestA(i)\nn and r<bestA(i+1)\nn:bestA(i)\rc+1:endif if g>=bestA(i)\nn and g<bestA(i+1)\nn:bestA(i)\gc+1:endif if b>=bestA(i)\nn and b<bestA(i+1)\nn:bestA(i)\bc+1:endif next next next
- option and type to
- Sort our Structured Array
opt=#PB_Sort_Descending typ=#PB_Sort_Integer
- sort to get most frequent reds
off=OffsetOf(bestA_\rc) SortStructuredArray(bestA(),opt, off, typ,1, ncol)
- save the best [ for number of colors =16 this is int(16/3)=5 ] reds
for i=1 to ncol/3 rbestA(i)=bestA(i)\nn next
- sort to get most frequent greens
off=OffsetOf(bestA_\gc) SortStructuredArray(bestA(),opt, off, typ,1, ncol)
- save the best [ for number of colors =16 this is int(16/3)=5 ] greens
for i=1 to ncol/3 gbestA(i)=bestA(i)\nn next
- sort to get most frequent blues
off=OffsetOf(bestA_\bc) SortStructuredArray(bestA(),opt, off, typ,1, ncol)
- save the best [ for number of colors =16 this is int(16/3)=5 ] blues
for i=1 to ncol/3 bbestA(i)=bestA(i)\nn next
- reset the best low value to 15 and high value to 240
- this helps to ensure there is some contrast when the statistics bunch up
- ie. when a single color tends to predominate... such as perhaps green?
rbestA(1)=15:rbestA(ncol/3)=240 gbestA(1)=15:gbestA(ncol/3)=240 bbestA(1)=15:bbestA(ncol/3)=240
- make a copy of our original image or leave the procedure
If not CopyImage(0,1) :ProcedureReturn 0:endif
- draw on that copy of our original image or leave the procedure
If not StartDrawing(ImageOutput(1)):ProcedureReturn 0:endif
for y=0 to ih-1 for x=0 to iw-1 c = Point(x,y)
- get the rgb value from our arrays
rt=rA(x,y) gt=gA(x,y) bt=bA(x,y)
- given a particular red value say 123 at point x,y
- which of our rbestA(i's) is closest?
- then for green and blue?
- ==============================
r=255 for i=1 to ncol/3 rdiff=abs(rbestA(i)-rt) if rdiff<=r:ri=i:r=rdiff:endif next
g=255 for i=1 to ncol/3 gdiff=abs(gbestA(i)-gt) if gdiff<=g:gi=i:g=gdiff:endif next
b=255 for i=1 to ncol/3 bdiff=abs(bbestA(i)-bt) if bdiff<=b:bi=i:b=bdiff:endif next
- ==============================
- get the color value so we can plot it at that pixel
Color=RGB(rbestA(ri),gbestA(gi),bbestA(bi))
- plot it at that pixel
Plot(x,y,Color)
- save that info to tA(x,y) for our comparison image
tA(x,y)=Color
next next StopDrawing() ; don't forget to... StopDrawing()
- create a comparison image of our original vs 16-color or leave the procedure
If not CreateImage(2,iw*2,ih) :ProcedureReturn 0:endif
- draw on that image both our original image and our 16-color image or leave the procedure
If not StartDrawing(ImageOutput(2)):ProcedureReturn 0:endif
- plot original image
- 0,0 .... 511,0
- .
- .
- 511,0 .. 511,511
for y=0 to ih-1
for x=0 to iw-1 c = cA(x,y) Plot(x,y,c) next next
- plot 16-color image to the right of original image
- 512,0 .... 1023,0
- .
- .
- 512,511 .. 1023,511
for y=0 to ih-1
for x=0 to iw-1 c = tA(x,y) Plot(x+iw,y,c) next next
StopDrawing() ; don't forget to... StopDrawing()
- save the single 16-color image
SaveImage(1, "_single_"+str(ncol)+"_"+Filename$,#PB_ImagePlugin_PNG )
- save the comparison image
SaveImage(2, "_compare_"+str(ncol)+"_"+Filename$,#PB_ImagePlugin_PNG ) ProcedureReturn 1 EndProcedure
ColorQuantization("Quantum_frog.png",16)
</lang>
Racket
<lang Racket>
- lang racket/base
(require racket/class
racket/draw)
- This is an implementation of the Octree Quantization algorithm. This implementation
- follows the sketch in
- Dean Clark. Color Quantization using Octrees. Dr. Dobbs Portal, January 1, 1996.
- http://www.ddj.com/184409805
- This code is adapted from the color quantizer in the implementation of Racket's
- file/gif standard library.
- To view an example of the quantizer, run the following test submodule
- in DrRacket
(module+ test
(require racket/block net/url) (define frog (block (define url (string->url "http://rosettacode.org/mw/images/3/3f/Quantum_frog.png")) (define frog-ip (get-pure-port url)) (define bitmap (make-object bitmap% frog-ip)) (close-input-port frog-ip) bitmap))
;; Display the original: (print frog) ;; And the quantized version (16 colors): (print (quantize-bitmap frog 16)))
- quantize-bitmap
- bitmap positive-number -> bitmap
- Given a bitmap, returns a new bitmap quantized to, at most, n colors.
(define (quantize-bitmap bm n)
(let* ([width (send bm get-width)] [height (send bm get-height)] [len (* width height 4)] [source-buffer (make-bytes len)] [_ (send bm get-argb-pixels 0 0 width height source-buffer)] [an-octree (make-octree-from-argb source-buffer n)] [dest-buffer (make-bytes len)]) (let quantize-bitmap-loop ([i 0]) (when (< i len) (let* ([i+1 (+ i 1)] [i+2 (+ i 2)] [i+3 (+ i 3)] [a (bytes-ref source-buffer i)] [r (bytes-ref source-buffer i+1)] [g (bytes-ref source-buffer i+2)] [b (bytes-ref source-buffer i+3)]) (cond [(alpha-opaque? a) (let-values ([(new-r new-g new-b) (octree-lookup an-octree r g b)]) (bytes-set! dest-buffer i 255) (bytes-set! dest-buffer i+1 new-r) (bytes-set! dest-buffer i+2 new-g) (bytes-set! dest-buffer i+3 new-b))] [else (bytes-set! dest-buffer i 0) (bytes-set! dest-buffer i+1 0) (bytes-set! dest-buffer i+2 0) (bytes-set! dest-buffer i+3 0)])) (quantize-bitmap-loop (+ i 4)))) (let* ([new-bm (make-object bitmap% width height)] [dc (make-object bitmap-dc% new-bm)]) (send dc set-argb-pixels 0 0 width height dest-buffer) (send dc set-bitmap #f) new-bm)))
- make-octree-from-argb
- bytes positive-number -> octree
- Constructs an octree ready to quantize the colors from an-argb.
(define (make-octree-from-argb an-argb n)
(unless (> n 0) (raise-type-error 'make-octree-from-argb "positive number" n)) (let ([an-octree (new-octree)] [len (bytes-length an-argb)]) (let make-octree-loop ([i 0]) (when (< i len) (let ([a (bytes-ref an-argb i)] [r (bytes-ref an-argb (+ i 1))] [g (bytes-ref an-argb (+ i 2))] [b (bytes-ref an-argb (+ i 3))]) (when (alpha-opaque? a) (octree-insert-color! an-octree r g b) (let reduction-loop () (when (> (octree-leaf-count an-octree) n) (octree-reduce! an-octree) (reduction-loop))))) (make-octree-loop (+ i 4)))) (octree-finalize! an-octree) an-octree))
- alpha-opaque? byte -> boolean
- Returns true if the alpha value is considered opaque.
(define (alpha-opaque? a)
(>= a 128))
- The maximum level height of an octree.
(define MAX-LEVEL 7)
- A color is a (vector byte byte byte)
- An octree is a
(define-struct octree (root ; node
leaf-count ; number reduction-heads ; (vectorof (or/c node #f)) palette) ; (vectorof (or/c color #f)) #:mutable)
- reduction-heads is used to accelerate the search for a reduction candidate.
- A subtree node is a
(define-struct node (leaf? ; bool
npixels ; number -- number of pixels this subtree node represents redsum ; number greensum ; number bluesum ; number children ; (vectorof (or/c #f node)) next ; (or/c #f node) palette-index) ; (or/c #f byte?) #:mutable)
- node-next is used to accelerate the search for a reduction candidate.
- new-octree
- -> octree
(define (new-octree)
(let* ([root-node (make-node #f ;; not a leaf 0 ;; no pixels under us yet 0 ;; red sum 0 ;; green sum 0 ;; blue sum (make-vector 8 #f) ;; no children so far #f ;; next #f ;; palette-index )] [an-octree (make-octree root-node 0 ; no leaves so far (make-vector (add1 MAX-LEVEL) #f) ; no reductions so far (make-vector 256 #(0 0 0)))]) ; the palette ;; Although we'll almost never reduce to this level, initialize the first ;; reducible node to the root, for completeness sake. (vector-set! (octree-reduction-heads an-octree) 0 root-node) an-octree))
- rgb->index
- natural-number byte byte byte -> octet
- Given a level and an (r,g,b) triplet, returns an octet that can be used
- as an index into our octree structure.
(define (rgb->index level r g b)
(bitwise-ior (bitwise-and 4 (arithmetic-shift r (- level 5))) (bitwise-and 2 (arithmetic-shift g (- level 6))) (bitwise-and 1 (arithmetic-shift b (- level 7)))))
- octree-insert-color!
- octree byte byte byte -> void
- Accumulates a new r,g,b triplet into the octree.
(define (octree-insert-color! an-octree r g b)
(node-insert-color! (octree-root an-octree) an-octree r g b 0))
- node-insert-color!
- node octree byte byte byte natural-number -> void
- Adds a color to the node subtree. While we hit #f, we create new nodes.
- If we hit an existing leaf, we accumulate our color into it.
(define (node-insert-color! a-node an-octree r g b level)
(let insert-color-loop ([a-node a-node] [level level]) (cond [(node-leaf? a-node) ;; update the leaf with the new color (set-node-npixels! a-node (add1 (node-npixels a-node))) (set-node-redsum! a-node (+ (node-redsum a-node) r)) (set-node-greensum! a-node (+ (node-greensum a-node) g)) (set-node-bluesum! a-node (+ (node-bluesum a-node) b))] [else ;; create the child node if necessary (let ([index (rgb->index level r g b)]) (unless (vector-ref (node-children a-node) index) (let ([new-node (make-node (= level MAX-LEVEL) ; leaf? 0 ; npixels 0 ; redsum 0 ; greensum 0 ; bluesum (make-vector 8 #f) ; no children yet #f ; and no next node yet #f ; or palette index )]) (vector-set! (node-children a-node) index new-node) (cond [(= level MAX-LEVEL) ;; If we added a leaf, mark it in the octree. (set-octree-leaf-count! an-octree (add1 (octree-leaf-count an-octree)))] [else ;; Attach the node as a reducible node if it's interior. (set-node-next! new-node (vector-ref (octree-reduction-heads an-octree) (add1 level))) (vector-set! (octree-reduction-heads an-octree) (add1 level) new-node)]))) ;; and recur on the child node. (insert-color-loop (vector-ref (node-children a-node) index) (add1 level)))])))
- octree-reduce!
- octree -> void
- Reduces one of the subtrees, collapsing the children into a single node.
(define (octree-reduce! an-octree)
(node-reduce! (pop-reduction-candidate! an-octree) an-octree))
- node-reduce!
- node octree -> void
- Reduces the interior node.
(define (node-reduce! a-node an-octree)
(for ([child (in-vector (node-children a-node))] #:when child) (set-node-npixels! a-node (+ (node-npixels a-node) (node-npixels child))) (set-node-redsum! a-node (+ (node-redsum a-node) (node-redsum child))) (set-node-greensum! a-node (+ (node-greensum a-node) (node-greensum child))) (set-node-bluesum! a-node (+ (node-bluesum a-node) (node-bluesum child))) (set-octree-leaf-count! an-octree (sub1 (octree-leaf-count an-octree)))) (set-node-leaf?! a-node #t) (set-octree-leaf-count! an-octree (add1 (octree-leaf-count an-octree))))
- find-reduction-candidate!
- octree -> node
- Returns a bottom-level interior node for reduction. Also takes the
- candidate out of the conceptual queue of reduction candidates.
(define (pop-reduction-candidate! an-octree)
(let loop ([i MAX-LEVEL]) (cond [(vector-ref (octree-reduction-heads an-octree) i) => (lambda (candidate-node) (when (> i 0) (vector-set! (octree-reduction-heads an-octree) i (node-next candidate-node))) candidate-node)] [else (loop (sub1 i))])))
- octree-finalize!
- octree -> void
- Finalization does a few things
- * Walks through the octree and reduces any interior nodes with just one leaf child.
- Optimizes future lookups.
- * Fills in the palette of the octree and the palette indexes of the leaf nodes.
- * Note
- palette index 0 is always reserved for the transparent color.
(define (octree-finalize! an-octree)
;; Collapse one-leaf interior nodes. (let loop ([a-node (octree-root an-octree)]) (for ([child (in-vector (node-children a-node))] #:when (and child (not (node-leaf? child)))) (loop child) (when (interior-node-one-leaf-child? a-node) (node-reduce! a-node an-octree))))
;; Attach palette entries. (let ([current-palette-index 1]) (let loop ([a-node (octree-root an-octree)]) (cond [(node-leaf? a-node) (let ([n (node-npixels a-node)]) (vector-set! (octree-palette an-octree) current-palette-index (vector (quotient (node-redsum a-node) n) (quotient (node-greensum a-node) n) (quotient (node-bluesum a-node) n))) (set-node-palette-index! a-node current-palette-index) (set! current-palette-index (add1 current-palette-index)))] [else (for ([child (in-vector (node-children a-node))] #:when child) (loop child))]))))
- interior-node-one-leaf-child?
- node -> boolean
(define (interior-node-one-leaf-child? a-node)
(let ([child-list (filter values (vector->list (node-children a-node)))]) (and (= (length child-list) 1) (node-leaf? (car child-list)))))
- octree-lookup
- octree byte byte byte -> (values byte byte byte)
- Returns the palettized color.
(define (octree-lookup an-octree r g b)
(let* ([index (node-lookup-index (octree-root an-octree) an-octree r g b 0)] [vec (vector-ref (octree-palette an-octree) index)]) (values (vector-ref vec 0) (vector-ref vec 1) (vector-ref vec 2))))
- node-lookup-index
- node byte byte byte natural-number -> byte
- Returns the palettized color index.
(define (node-lookup-index a-node an-octree r g b level)
(let loop ([a-node a-node] [level level]) (if (node-leaf? a-node) (node-palette-index a-node) (let ([child (vector-ref (node-children a-node) (rgb->index level r g b))]) (unless child (error 'node-lookup-index "color (~a, ~a, ~a) not previously inserted" r g b)) (loop child (add1 level))))))
</lang>
Tcl
<lang tcl>package require Tcl 8.6 package require Tk
proc makeCluster {pixels} {
set rmin [set rmax [lindex $pixels 0 0]] set gmin [set gmax [lindex $pixels 0 1]] set bmin [set bmax [lindex $pixels 0 2]] set rsum [set gsum [set bsum 0]] foreach p $pixels {
lassign $p r g b if {$r<$rmin} {set rmin $r} elseif {$r>$rmax} {set rmax $r} if {$g<$gmin} {set gmin $g} elseif {$g>$gmax} {set gmax $g} if {$b<$bmin} {set bmin $b} elseif {$b>$bmax} {set bmax $b} incr rsum $r incr gsum $g incr bsum $b
} set n [llength $pixels] list [expr {double($n)*($rmax-$rmin)*($gmax-$gmin)*($bmax-$bmin)}] \
[list [expr {$rsum/$n}] [expr {$gsum/$n}] [expr {$bsum/$n}]] \ [list [expr {$rmax-$rmin}] [expr {$gmax-$gmin}] [expr {$bmax-$bmin}]] \ $pixels }
proc colorQuant {img n} {
set width [image width $img] set height [image height $img] # Extract the pixels from the image for {set x 0} {$x < $width} {incr x} {
for {set y 0} {$y < $height} {incr y} { lappend pixels [$img get $x $y] }
} # Divide pixels into clusters for {set cs [list [makeCluster $pixels]]} {[llength $cs] < $n} {} {
set cs [lsort -real -index 0 $cs] lassign [lindex $cs end] score centroid volume pixels lassign $centroid cr cg cb lassign $volume vr vg vb while 1 { set p1 [set p2 {}] if {$vr>$vg && $vr>$vb} { foreach p $pixels { if {[lindex $p 0]<$cr} {lappend p1 $p} {lappend p2 $p} } } elseif {$vg>$vb} { foreach p $pixels { if {[lindex $p 1]<$cg} {lappend p1 $p} {lappend p2 $p} } } else { foreach p $pixels { if {[lindex $p 2]<$cb} {lappend p1 $p} {lappend p2 $p} } } if {[llength $p1] && [llength $p2]} break # Partition failed! Perturb partition point away from the centroid and try again set cr [expr {$cr + 20*rand() - 10}] set cg [expr {$cg + 20*rand() - 10}] set cb [expr {$cb + 20*rand() - 10}] } set cs [lreplace $cs end end [makeCluster $p1] [makeCluster $p2]]
} # Produce map from pixel values to quantized values foreach c $cs {
set centroid [format "#%02x%02x%02x" {*}[lindex $c 1]] foreach p [lindex $c end] { set map($p) $centroid }
} # Remap the source image set newimg [image create photo -width $width -height $height] for {set x 0} {$x < $width} {incr x} {
for {set y 0} {$y < $height} {incr y} { $newimg put $map([$img get $x $y]) -to $x $y }
} return $newimg
}</lang> Demonstration code: <lang tcl>set src [image create photo -file quantum_frog.png] set dst [colorQuant $src 16]
- Save as GIF now that quantization is done, then exit explicitly (no GUI desired)
$dst write quantum_frog_compressed.gif exit</lang>