Class: Camalian::Quantization::KMeans

Inherits:
Object
  • Object
show all
Defined in:
lib/camalian/quantization/k_means.rb

Overview

:nodoc:

Constant Summary collapse

INIT_TRIES =
10

Instance Method Summary collapse

Instance Method Details

#process(colors, count) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/camalian/quantization/k_means.rb', line 8

def process(colors, count)
  # Its faster to extract unique colors once
  colors = colors.uniq
  means = initial_means(colors, count)

  done = false

  until done
    groups = group_by_means(colors, means)
    new_means = groups.map do |group|
      Palette.new(group).average_color
    end
    common = means & new_means

    done = common.size == means.size
    means = new_means
  end

  Palette.new(means)
end