Class: Iguvium::Labeler

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(image) ⇒ Labeler

Returns a new instance of Labeler.

Parameters:

  • image (Array<Boolean>)

    should be an Array, binarized image w/ falsy elements as background and anything truthy as pixels



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

#clustersArray<Array>

Returns coordinates of connected pixels grouped together.

Returns:

  • (Array<Array>)

    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

#linesHash

Returns vertical and horizontal lines detected.

Returns:

  • (Hash)

    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