Class: Ocarina::LetterpressCropper

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/ocarina/letterpress_cropper.rb

Overview

creates tiles of character images from letterpress game boards

Instance Method Summary collapse

Methods included from Util

#char_to_binary_string, #filename_for_noise_image, #filename_for_quantized_image, #filename_for_training_image, #int_to_binary_string, #is_lower?, #is_upper?, #noise_image_for_char, #pixel_number_to_col, #pixel_number_to_row, #pixel_to_bit, #quantize_image, #reference_image_for_char, #sigma

Constructor Details

#initialize(config) ⇒ LetterpressCropper

Returns a new instance of LetterpressCropper.



17
18
19
# File 'lib/ocarina/letterpress_cropper.rb', line 17

def initialize(config)
  @config = config
end

Instance Method Details

#crop(image) ⇒ Object

returns an N by N array of image tiles



44
45
46
47
48
49
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
# File 'lib/ocarina/letterpress_cropper.rb', line 44

def crop(image)
  if image.columns != LETTERPRESS_EXPECTED_WIDTH || image.rows != LETTERPRESS_EXPECTED_HEIGHT
    image = image.resize(LETTERPRESS_EXPECTED_WIDTH, LETTERPRESS_EXPECTED_HEIGHT)
  end

  image = quantize_image(image)

  y_offset = LETTERPRESS_HEIGHT_OFFSET

  rows = [ ]

  border = 1

  0.upto(LETTERPRESS_TILES_DOWN - 1) do
    x_offset = 0
    row = [ ]

    0.upto(LETTERPRESS_TILES_ACROSS - 1) do

      tile = image.crop(x_offset - border, y_offset + border, LETTERPRESS_TILE_PIXELS - border, LETTERPRESS_TILE_PIXELS - border, true)
      box = tile.bounding_box
      min_bound_width = 0.75 * @config.char_width
      if box.width > min_bound_width
        tile = tile.crop(box.x - border, box.y - border, box.width + 2 * border, box.height + 2 * border, true)
      end

      tile.resize!(@config.char_width, @config.char_height)
      row << tile
      x_offset += LETTERPRESS_TILE_PIXELS

    end
    rows << row

    y_offset += LETTERPRESS_TILE_PIXELS
  end

  rows
end

#decipher_board(network, board_image) ⇒ Object

crops the board into tiles, runs recognizer on each of the tiles, and returns resulting array of array of chars



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ocarina/letterpress_cropper.rb', line 24

def decipher_board(network, board_image)
  tile_rows = crop board_image

  result = [ ]

  tile_rows.each do |tile_row|
    row = [ ]
    tile_row.each do |tile|
      row << network.recognize(tile).chr
    end

    result << row
  end

  result
end