This is a study & experiment about various formulas for optimizing median cut algorithm heuristics. In particular, this is an experiment on which logic is the most appropriate for next box selection and axis cut selection.
For more context, see http://blog.pkh.me/p/39-improving-color-quantization-heuristics.html
Note: the scripts depend on Python3, matplotlib
and PIL
pal.py
: core lib where all the important code isconvert.py
: simple script to quantize an image, see--help
for more infogen_results.py
: generate results, see--help
for more infoshow_results.py
: show or export the results in an image, see--help
for more infoshow_pal.py
: tool to display a palette stored in a 16×16 file in sRGB, Linear and OkLabboxdbg.py
: a tool to generate a demonstration video of how the algorithm works, see--help
for more infosamples
: some sample files
Each algo follows the <axis-selection>_<operator>_<measurement>
naming, where:
axis-selection
:xer2
: cut the axis with the maximum error squaredxlen
: cut the axis with the maximum length
operator
:max
: maximum measurement out of all the channelsmul
: product of the measurements of all the channelssum
: sum of the measurements of all the channels
measurement
:er2absW
: error squared, honoring weightser2noW
: error squared, not honoring weightser2normW
: error squared, honoring weights, normalizedlen
: length of the axis
The MSE metrics providing the best overall results is xer2_max_er2absW
(followed by xer2_sum_er2absW
), which means:
- Overall, the box that has the axis with the largest non-normalized weighted sum of the squared error is the best candidate in the box selection algorithm
- Overall, cutting the axis with the largest weighted sum of squared error is the best axis cut selection algorithm