New kmeans color reduction

Announcements pertaining to ImageMagick, or ImageMagick related software. This list is moderated. No discussions here, instead post to the users group instead.
Post Reply
User avatar
fmw42
Posts: 25944
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

New kmeans color reduction

Post by fmw42 »

IM 7.0.9.10 introduces kmeans iterative color reduction.

The kmeans algorithm starts with some initial set of colors. It then classifies each pixel in the image with the closest color (by color distance). Then it separately averages all the pixels of each color class, one class at a time, to find a new set of colors. It repeats this process until it either reaches the specified number of iterations or the specified convergence threshold.

The syntax is:

-kmeans colors{xiterations}{+convergence}

Code: Select all

For example:
-kmeans 5
-kmeans 5x100
-kmeans 5x100+0.01
Colors is the desired number of colors. Initial colors are found using color quantization. Iterations is the stopping number of iterations (default=100). Convergence is the stopping threshold on the color change between iterations (default=0.01). Processing finishes, if either iterations or convergence are reached. Use -define kmeans:seed-colors=color-list to initialize the colors, where color-list is a semicolon delimited list of seed colors (e.g. -define kmeans:seed-colors="red;sRGB(19,167,254);#00ffff). A color list overrides the color quantization. A non-empty list of colors overrides the number of colors. Any unassigned initial colors are assigned random colors from the image. Random seed colors may be reproduced by including -seed XXX in the command line. Different results may occur depending upon the initial colors and how long it iterates before stopping.

Examples:

Input:
Image

Code: Select all

magick flower_blue2.jpg -kmeans 2 flower_blue2_2colors.gif
Image

Code: Select all

magick flower_blue2.jpg -kmeans 3 flower_blue2_3colors.gif
Image

Code: Select all

magick flower_blue2.jpg -kmeans 4 flower_blue2_4colors.gif
Image

Code: Select all

magick flower_blue2.jpg -kmeans 5 flower_blue2_5colors.gif
Image

Code: Select all

magick flower_blue2.jpg -seed 10 -define kmeans:seed-colors="" -kmeans 5 flower_blue2_5empty_color_list.gif
Image

Code: Select all

magick flower_blue2.jpg -define kmeans:seed-colors="black;brown;green1;LightSlateBlue;white" -kmeans 5 flower_blue2_5color_list.gif
Image

Post Reply