You are on page 1of 30

Color Quantization

1
Color Quantization
• The problem of reducing the number of
colors in an image with minimal impact on
appearance
– Extreme case: 24 bit color to black and white
– Less extreme: 24 bit color to 256 colors, or 256
grays

2
Why do we care?
• It is critical for displaying images on devices
that can only display a limited number of
colors, usually due to memory limitations.

• It enables efficient compression of certain


types of images.

3
A bar in the
big images is a
hair on the
zebra’s nose;
in smaller
images, a
stripe; in the
smallest, the
animal’s nose

4
from Wikipedia.org
Color Quantization

5
from Wikipedia.org
Color Quantization
• Color quantization can be viewed as a subset of the
field of vector quantization.

• Vector quantization is the problem of selecting K


vectors in some N dimensional space to represent N
vectors from that space where K<<N and the total
error incurred by the quantization is minimized.

• Color quantization is then vector quantization in a 3-


Dimensional space (RGB, CIE, HLS, ...).

6
Color Quantization
• The process of color image quantization is often
broken into 3 phases.
1. Sampling the original image for color statistics.
2. Choosing a color map based on those statistics.
3. Mapping the colors to their representative in the color map.

• In particular the method used for phases 1 and 2 will


determine the best method for accomplishing phase 3.

7
Color Quantization
• Color quantization can be broken into two categories:
Uniform and Non-Uniform.
– Uniform: the color space is broken into equal sized regions
where the number of regions which is less than or equal to K.
– Non-Uniform: the manner in which the color space is
divided is dependent on the distribution of colors in the
image.
• 4 different algorithms: Uniform, Popularity, Median
Cut, and Octree.
• Throughout the discussions the color space is assumed
to be RGB and K = 256 although none of the
algorithms are dependent on these assumptions.
8
Uniform Quantization
• Each axis is then divided into equal sized segments.
• The number of these segments is dependent on the
scheme used for dividing the color space.
• One possible scheme is to divide the red and green
axis into 8 segments each and the blue axis into 4
resulting in 256 regions.
• Another possibility is dividing the red and blue into 6
and the green into 7 segments resulting in 252
regions.
• Each one of these regions generated by 3 segments
will produce a color for the color map.
9
Uniform Quantization
• Once the color space has been divided, each of
the original colors is then mapped to the region
which it falls in.
• The representative color for each region is then
the average of all the colors mapped to that
region (or the fixed center of the region).
• While this algorithm is quick and easy to
implement, it does not yield very good results.

10
Uniform Quantization
• Often region in the color space will not have
any colors mapped to them resulting in color
map entries to be wasted.
• This algorithm can also be applied in a non-
uniform manner if the axis are broken on a
logarithmic scale instead of linear.

11
Uniform Quantization
• 256 colors in this
image
• Note that it does
very poorly on
smooth gradients
• It’s hard to get
right if lots of
similar colors
appear very close
together.

12
from Wikipedia.org
Uniform Quantization

8 colors 64 colors

512 colors Original

13
Uniform Quantization

Original 4096
colors

14
Popularity Algorithms
• Popularity algorithms are another form of
uniform quantization.

• Instead of dividing the color space into 256


regions, these algorithms break the color space
into much smaller, and consequently many
more regions.

• One possible implementation is to divide the


space into regions (64x64x64 = 262,144
regions).

15
Popularity Algorithms
• The original colors are again mapped to the
region they fall in.

• The representative color for each region is


the average of the colors mapped to it.

• The color map is selected by taking the


representative colors of the 256 most popular
regions (the regions that have the most colors
(i.e., pixels) mapped to them).

16
Popularity Algorithms
• These algorithms are also easy to implement
and yield better results than the uniform
quantization algorithm.
• They do, however, take slightly longer to
execute and can have a significantly larger
storage requirement depending on the size of
regions.
• Also depending on the image characteristics
this may not produce a good result.
17
Popularity Algorithm
• Build a color histogram: count the number
of times each color appears
• Choose the n most commonly occurring
colors
• Map other colors to the closest chosen color
• Problem: May completely ignore under-
represented but important colors

18
Popularity Algorithm
• the most popular
256 colors in
this image

• Note that blue


wasn’t very
popular, so the
crystal ball is
now the same
color as the floor

19
from Wikipedia.org
Median Cut Algorithms
• In contrast to uniform sub-division, these algorithms
divide the color space based on the distribution of the
original colors. The process is as follows:

1. Find the smallest box which contains all the colors in the
image.
2. Find the longest axis of the box.
3. Split the box into 2 regions at median.
4. Repeat the above process until the original color space has
been divided into 256 regions.

20
Median Cut Algorithms
• The algorithm then divides the color space in a
manner depicted below.
– Look at distribution of colors
– Recursively:
• Find the “longest” dimension (r, g, b are
dimensions)
• Choose the median of the long dimension as a
color to use
• Split along the median plane, and recurse on
both halves

21
Median Cut in Action
Original colors Median in long Recurse
in color space dimension

22
Median Cut
• The representative colors are found by
averaging the colors in each box.

• Because these algorithms use image


information in dividing the color space, this
class of algorithms consistently produce
good results while having memory and time
complexity no worse than popularity
algorithms.

23
Median Cut
• 256 colors in
this image
• Now we get the
blue
• Median cut
works so well
because it
divides up the
color space in
the “most
useful” way
24
from Wikipedia.org
Octree Algorithms
• An octree is a tree data structure in which each
internal node has exactly eight children.
• Octrees are most often used to partition a three
dimensional space by recursively subdividing it
into eight octants.
• Octrees are the three-dimensional analog of
quadtrees (in 2D)
• The name is formed from oct + tree, and normally
written "octree", not "octtree".

25
Octree Algorithms

26
from Wikipedia.org
Color Quantization
• There are many different algorithms for color
quantization.
• Which one is best is dependent on the
requirements of the system it is to be used in.
• The uniform quantization algorithms offer a
quick and dirty means but can result in very
poor quality depending on image
characteristics.
• The non-uniform algorithms offer better
consistent results at the cost of increased
memory and time complexities
27
Quantization Error
• A way of measuring the quality of
quantization
• Define an error for each color, c, in the
original image: d(c,c’), where c’ is the color
c maps to under the quantization
– Common is to use squared distance in RGB
space
• Sum up the error over all the pixels

28
Quantization Errors

Sum up the error over all the pixels

8 colors Original

29
Optimization Algorithms
• The quantization problem can be phrased as
optimization
– Find the set of colors and mapping that result in
the lowest quantization error
• Several methods to solve the problem, but
of limited use unless the number of colors
to be chosen is small
– It’s expensive to compute the optimum
– It’s also a poorly behaved optimization

30

You might also like