Class: KvgCharacterRecognition::Preprocessor
- Inherits:
-
Object
- Object
- KvgCharacterRecognition::Preprocessor
- Includes:
- Normalization
- Defined in:
- lib/kvg_character_recognition/preprocessor.rb
Instance Attribute Summary collapse
-
#interpolate_distance ⇒ Object
Returns the value of attribute interpolate_distance.
-
#number_of_points ⇒ Object
Returns the value of attribute number_of_points.
-
#size ⇒ Object
Returns the value of attribute size.
-
#smooth(stroke) ⇒ Object
A simple smooth method using the following formula p’(i) = (w(-M)*p(i-M) + … + w(0)*p(i) + … + w(M)*p(i+M)) / S where the smoothed point is a weighted average of its adjacent points.
-
#smooth_weights ⇒ Object
Returns the value of attribute smooth_weights.
Instance Method Summary collapse
-
#initialize(interpolate_distance, size, smooth = true, smooth_weights = [1,2,3,2,1]) ⇒ Preprocessor
constructor
A new instance of Preprocessor.
-
#interpolate(stroke) ⇒ Object
This method interpolates points into a stroke with given distance The algorithm is taken from the paper preprocessing techniques for online character recognition.
-
#preprocess(strokes) ⇒ Object
preprocess steps bi moment size normalization, smooth and interpolate.
Methods included from Normalization
#bi_moment_normalize, #line_density_normalize, #point_density_normalize
Constructor Details
#initialize(interpolate_distance, size, smooth = true, smooth_weights = [1,2,3,2,1]) ⇒ Preprocessor
5 6 7 8 9 10 11 |
# File 'lib/kvg_character_recognition/preprocessor.rb', line 5 def initialize interpolate_distance, size, smooth=true, smooth_weights=[1,2,3,2,1] @smooth = smooth @smooth_weights = smooth_weights #@downsample_rate = downsample_rate @interpolate_distance = interpolate_distance @size = size end |
Instance Attribute Details
#interpolate_distance ⇒ Object
Returns the value of attribute interpolate_distance.
4 5 6 |
# File 'lib/kvg_character_recognition/preprocessor.rb', line 4 def interpolate_distance @interpolate_distance end |
#number_of_points ⇒ Object
Returns the value of attribute number_of_points.
4 5 6 |
# File 'lib/kvg_character_recognition/preprocessor.rb', line 4 def number_of_points @number_of_points end |
#size ⇒ Object
Returns the value of attribute size.
4 5 6 |
# File 'lib/kvg_character_recognition/preprocessor.rb', line 4 def size @size end |
#smooth(stroke) ⇒ Object
A simple smooth method using the following formula p’(i) = (w(-M)*p(i-M) + … + w(0)*p(i) + … + w(M)*p(i+M)) / S where the smoothed point is a weighted average of its adjacent points. Only the user input should be smoothed, it is not necessary for kvg data. Params:
stroke-
array of points i.e [[x1, y1], [x2, y2] …]
27 28 29 |
# File 'lib/kvg_character_recognition/preprocessor.rb', line 27 def smooth @smooth end |
#smooth_weights ⇒ Object
Returns the value of attribute smooth_weights.
4 5 6 |
# File 'lib/kvg_character_recognition/preprocessor.rb', line 4 def smooth_weights @smooth_weights end |
Instance Method Details
#interpolate(stroke) ⇒ Object
This method interpolates points into a stroke with given distance The algorithm is taken from the paper preprocessing techniques for online character recognition
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/kvg_character_recognition/preprocessor.rb', line 50 def interpolate stroke current = stroke.first new_stroke = [current] index = 1 last_index = 0 while index < stroke.length do point = stroke[index] #only consider point with greater than d distance to current point if Math.euclidean_distance(current, point) < @interpolate_distance index += 1 else #calculate new point coordinate new_point = [] if point[0].round(2) == current[0].round(2) # x2 == x1 if point[1] > current[1] # y2 > y1 new_point = [current[0], current[1] + @interpolate_distance] else # y2 < y1 new_point = [current[0], current[1] - @interpolate_distance] end else # x2 != x1 slope = (point[1] - current[1]) / (point[0] - current[0]).to_f if point[0] > current[0] # x2 > x1 new_point[0] = current[0] + Math.sqrt(@interpolate_distance**2 / (slope**2 + 1)) else # x2 < x1 new_point[0] = current[0] - Math.sqrt(@interpolate_distance**2 / (slope**2 + 1)) end new_point[1] = slope * new_point[0] + point[1] - (slope * point[0]) end new_point = new_point.map{ |num| num.round(2) } if current != new_point new_stroke << new_point current = new_point end last_index += ((index - last_index) / 2).floor index = last_index + 1 end end new_stroke end |
#preprocess(strokes) ⇒ Object
preprocess steps bi moment size normalization, smooth and interpolate
14 15 16 17 18 19 |
# File 'lib/kvg_character_recognition/preprocessor.rb', line 14 def preprocess strokes bi_moment_normalize(strokes).map do |stroke| stroke = smooth(stroke) if @smooth smooth(interpolate(stroke)) end end |