Home > Software engineering >  Small white to ask about a color image quantization (mass-tone attune to extract) algorithm, a code,
Small white to ask about a color image quantization (mass-tone attune to extract) algorithm, a code,

Time:09-18

https://github.com/obskyr/colorgram.py
https://github.com/darosh/colorgram-js
The two is the same algorithm, a python is js language,

If bosses is not convenient to see a lot of, I attached the python to write several functions of the main, or I here are simple to write about my own understanding,
 def sample (image) : 
Top_two_bits=0 b11000000

Sides=1 & lt; <2 # Left by the number of bits, informs the Left
* * 7 # cubes cubes sides of==4 ^ 7

Samples=array. The array (ARRAY_DATATYPE, (0 for _ in range (cubes)))
Width, height=image size

Pixels=image. The load ()
For y in range (height) :
For the x in the range (width) :
# Pack the top two bits of all 6 values into 12 bits.
R, g, b=pixels (x, y) [3] :
H, s, l=an HSL (r, g, and b) # an HSL function is to convert RGB to an HSL, function content posted
I don't# Standard constants for converting RGB to relative luminance.
Y=int (r + g + b * 0.7152 * 0.0722 * 0.2126)

# Everything 's shifted into place from the top two
# bits' the original position - that is, bits 7-8.
The packed=(Y & amp; Top_two_bits)
The packed |=(h & amp; Top_two_bits) & gt;> 2
The packed |=(l & amp; Top_two_bits) & gt;> 4

Samples [the packed] +=r
[the packed samples + 1] +=g
[the packed samples + 2] +=b
[the packed samples + 3) +=1
The return samples

Def pick_used (samples) : # extract used in the chart color
2=[]
For I in range (0, len (samples), 4) :
Count=samples + 3 [I]
If the count:
2. Append ((the count, I))
Return 2

Def get_colors (samples, informs, number_of_colors) : # extraction image color, number_of_colors colors
Pixels=0
Colors=[]
Number_of_colors=min (number_of_colors, len (2))

For the count, the index in 2 [: number_of_colors] :
Pixels +=count

Color=color (
Samples [index]//count, # RGB average
Samples [index + 1]//count,
Samples [index + 2]//count,
Count
)

Colors. Append (color)
For color in colors:
Color. The proportion/=pixels
The return on colors


I understand is that the code with Y (gray) h (hue), s (saturation) binary were taken before two, behind of eight binary composition YYhhss, plus two (in order to store the RGB and the count, that is, four for a group of) the YYhhssxx , as the index, which in turn is stored pixels of r, g, b, count frequency, finally, the accumulation of r, g, b divided by frequency get , don't know if I have the clear,
Is my personal feeling to the naked eye can effect, saturation integral partial ordinary (?) But no big effect, this is I took a simple scenery figure running as a result, only took 6 clock (theory can extract 256?)


Information search many are k - means, a variety of clustering, the algorithm is too simple, I don't really understand there are two
1. Why do you want to use the Yhs to locate (I tried an HSL, RGB, and so on combined effect is very poor)
2. Why can simple and crude to RGB accumulation and then averaged

Please excuse me big to guide! Or do you have the related literature? Key words? Look, can I go! I just can't search to information...
  • Related