Module: OpenCVColor

Includes:
OpenCV
Defined in:
lib/opencv-color.rb,
lib/opencv-color/version.rb

Defined Under Namespace

Classes: Colors

Constant Summary collapse

VERSION =
"1.0.2"

Class Method Summary collapse

Class Method Details

.color_name(path) ⇒ Object



59
60
61
# File 'lib/opencv-color.rb', line 59

def color_name(path)
  File.basename(path).downcase.gsub(/[^a-z_]/, '_')
end

.learn(dir) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opencv-color.rb', line 35

def learn(dir)
  Hash[samples(dir).map do |color_dir, files|
    group = [[], [], []]
    files.map(&method(:load_image_colors)).each do |colors|
      colors.each do |c|
        3.times do |i|
          group[i] << c[i]
        end
      end
    end
    range = group.map(&:to_scale).each_with_index.inject({low: [], high: [], mean: [], sd: []}) do |memo, d|
      g, index = d
      sd = g.sd
      mean = g.mean
      memo[:low] << ([mean - 3 * sd, min_value].max).floor
      memo[:high] << ([mean + 3 * sd, max_value(index)].min).ceil
      memo[:mean] << mean
      memo[:sd] << sd
      memo
    end
    [color_name(color_dir), range]
  end]
end

.load_image_colors(file) ⇒ Object



72
73
74
75
# File 'lib/opencv-color.rb', line 72

def load_image_colors(file)
  img = IplImage.load(file, OpenCV::CV_LOAD_IMAGE_ANYCOLOR | OpenCV::CV_LOAD_IMAGE_ANYDEPTH)
  Colors.new(img.smooth(:median, 3).BGR2HSV)
end

.max_value(i) ⇒ Object



67
68
69
70
# File 'lib/opencv-color.rb', line 67

def max_value(i)
  # H => 0
  i == 0 ? 179 : 255
end

.min_valueObject



63
64
65
# File 'lib/opencv-color.rb', line 63

def min_value
  0
end

.samples(dir) ⇒ Object



29
30
31
32
33
# File 'lib/opencv-color.rb', line 29

def samples(dir)
  Dir["#{dir}/*"].map do |color|
    [color, Dir["#{color}/*"]]
  end
end