Class: Iguvium::Labeler
- Inherits:
-
Object
- Object
- Iguvium::Labeler
- Defined in:
- lib/iguvium/labeler.rb
Overview
Clusterizes connected pixels using two-pass connected component labelling algorithm (Hoshen-Kopelman), 8-connectivity is used. Line-like groups are then flattened using simplified dispersion ratio
Instance Method Summary collapse
-
#clusters ⇒ Array<Array>
Coordinates of connected pixels grouped together.
-
#initialize(image) ⇒ Labeler
constructor
A new instance of Labeler.
-
#lines ⇒ Hash
Vertical and horizontal lines detected.
Constructor Details
#initialize(image) ⇒ Labeler
Returns a new instance of Labeler.
15 16 17 18 19 20 21 |
# File 'lib/iguvium/labeler.rb', line 15 def initialize(image) @image = image rows = image.count cols = image.first.count @labels = Array.new(rows) { Array.new(cols) } @equalities = [] end |
Instance Method Details
#clusters ⇒ Array<Array>
Returns coordinates of connected pixels grouped together.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/iguvium/labeler.rb', line 29 def clusters accumulator = Hash.new { |h, k| h[k] = [] } label.each_index do |row| labels[row].each_index do |column| pix = labels[row][column] next unless pix accumulator[pix] << [row, column] end end accumulator.values end |
#lines ⇒ Hash
Returns vertical and horizontal lines detected.
24 25 26 |
# File 'lib/iguvium/labeler.rb', line 24 def lines clusters.map { |cluster| flatten_cluster cluster }.compact end |