Module: KvgCharacterRecognition::Normalization

Included in:
Preprocessor
Defined in:
lib/kvg_character_recognition/normalization.rb

Overview

This module contains various normalization methods

Instance Method Summary collapse

Instance Method Details

#bi_moment_normalize(strokes) ⇒ Object

This methods normalizes the strokes using bi moment Params:

strokes
[[x1, y1], [x2, y2], …], [[x1, y1], …]
slant_correction

boolean whether a slant correction should be performed

returns normed_strokes, normed_strokes_with_slant_correction



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kvg_character_recognition/normalization.rb', line 9

def bi_moment_normalize strokes
  means, diffs = means_and_diffs strokes

  #calculating delta values
  delta = Proc.new do |diff, operator|
    #d_x or d_y
    #operator: >= or <
    accum = 0
    counter = 0

    diff.each do |d|
      if d.send operator, 0
        accum += d ** 2
        counter += 1
      end
    end
    accum / counter
  end

  new_strokes = []

  strokes.each do |stroke|
    new_stroke = []
    stroke.each do |point|
      x = point[0]
      y = point[1]

      if x - means[0] >= 0
        new_x = ( @size * (x - means[0]) / (4 * Math.sqrt(delta.call(diffs[0], :>=))).round(2) ) + @size/2
      else
        new_x = ( @size * (x - means[0]) / (4 * Math.sqrt(delta.call(diffs[0], :<))).round(2) ) + @size/2
      end

      if y - means[1] >= 0
        new_y = ( @size * (y - means[1]) / (4 * Math.sqrt(delta.call(diffs[1], :>=))).round(2) ) + @size/2
      else
        new_y = ( @size * (y - means[1]) / (4 * Math.sqrt(delta.call(diffs[1], :<))).round(2) ) + @size/2
      end

      if new_x >= 0 && new_x <= @size && new_y >= 0 && new_y <= @size
        new_stroke << [new_x.round(3), new_y.round(3)]
      end
    end
    new_strokes << new_stroke unless new_stroke.empty?
  end
  new_strokes
end

#line_density_normalize(strokes) ⇒ Object

line density equalization strokes must be scaled to 109x109



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kvg_character_recognition/normalization.rb', line 59

def line_density_normalize strokes
  hist_x, hist_y = line_density_histogram strokes
  strokes.map do |stroke|
    stroke.map do |point|
      if point[0] < 109 && point[1] < 109
        [@size * hist_x[point[0].floor] / hist_x.last, @size * hist_y[point[1].floor] / hist_y.last]
      else
        point
      end
    end
  end
end

#point_density_normalize(strokes) ⇒ Object

point density normalization



73
74
75
76
77
78
79
80
81
# File 'lib/kvg_character_recognition/normalization.rb', line 73

def point_density_normalize strokes
  points = strokes.flatten(1)
  h_x, h_y = accumulated_histogram strokes
  strokes.map do |stroke|
    stroke.map do |point|
      [(@size * h_x[point[0].round] / points.length.to_f).round(2), (@size * h_y[point[1].round] / points.length.to_f).round(2)]
    end
  end
end