Color quantization: Difference between revisions

From Rosetta Code
Content added Content deleted
(sample indexed color by gimp)
(J)
Line 6: Line 6:


Note: the funny color bar on top of the frog image is intentional.
Note: the funny color bar on top of the frog image is intentional.

=={{header|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>kmc=:4 :0
K=. x
img=. y
C=. 256 #.inv ,img
G=. K (i.@] <.@* %) #C
M=. _
whilst.-. M-:&<.&(k&*)M0 do.
M0=. M
M=. /:~C (+/ % #)&.:*:/.~ G
G=. (i. <./)"1 C +/&.:*: .- |:M
end.
M
)</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. They are shown here as fractional numbers, but they should be either rounded to the nearest integer in the range 0..255 or scaled so they are floating point triples in the range 0..1.

<lang j> 16 kmc img
14.5636 53.3597 2.33875
57.5988 131.857 4.15525
58.5179 95.6464 5.80455
74.3796 144.683 8.96188
90.1449 152.701 17.5318
96.8655 122.289 58.9191
100.095 157.416 38.817
118.095 163.199 76.2121
129.679 161.837 19.2454
148.401 184.187 40.3014
151.128 170.16 126.952
164.774 180.797 206.901
168.343 203.405 68.5922
184.498 214.185 103.206
204.996 225.25 155.394
230.069 234.23 240.694</lang>

Revision as of 23:24, 12 August 2011

Color quantization is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
full color
Example: Gimp 16 color

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.

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>kmc=:4 :0

 K=. x
 img=. y
 C=. 256 #.inv ,img
 G=. K (i.@] <.@* %) #C
 M=. _
 whilst.-. M-:&<.&(k&*)M0 do.
   M0=. M
   M=. /:~C (+/ % #)&.:*:/.~ G
   G=. (i. <./)"1 C +/&.:*: .- |:M
 end.
 M

)</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. They are shown here as fractional numbers, but they should be either rounded to the nearest integer in the range 0..255 or scaled so they are floating point triples in the range 0..1.

<lang j> 16 kmc img 14.5636 53.3597 2.33875 57.5988 131.857 4.15525 58.5179 95.6464 5.80455 74.3796 144.683 8.96188 90.1449 152.701 17.5318 96.8655 122.289 58.9191 100.095 157.416 38.817 118.095 163.199 76.2121 129.679 161.837 19.2454 148.401 184.187 40.3014 151.128 170.16 126.952 164.774 180.797 206.901 168.343 203.405 68.5922 184.498 214.185 103.206 204.996 225.25 155.394 230.069 234.23 240.694</lang>