Module: MachineLearningWorkbench::Tools::Imaging

Defined in:
lib/machine_learning_workbench/tools/imaging.rb

Constant Summary collapse

Forkable =
MachineLearningWorkbench::Tools::Execution
Norm =
MachineLearningWorkbench::Tools::Normalization

Class Method Summary collapse

Class Method Details

.display(narr, disp_size: nil, shape: nil, in_fork: true) ⇒ Object

Show a numeric matrix as image in a RMagick window

Parameters:

  • narr (NArray)

    numeric matrix to display

  • disp_size (Array) (defaults to: nil)

    the size of the image to display

  • shape (Array) (defaults to: nil)

    the true shape of the image (numeric matrix could be flattened)

  • in_fork (bool) (defaults to: true)

    whether to execute the display in fork (and continue running)



33
34
35
36
37
38
39
40
41
42
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 33

def self.display narr, disp_size: nil, shape: nil, in_fork: true
  require 'rmagick'
  img = narr_to_img narr, shape: shape
  img.resize!(*disp_size, Magick::TriangleFilter,0.51) if disp_size
  if in_fork
    MachineLearningWorkbench::Tools::Execution.in_fork { img.display }
  else
    img.display
  end
end

.narr_from_png(fname, scale: nil, flat: false) ⇒ Object

Create numeric matrix from png by filename.

Parameters:

  • fname

    the file name

  • scale (defaults to: nil)

    optional rescaling of the image

  • flat (bool) (defaults to: false)

    whether to return a flat array

  • dtype

    dtype for the numeric matrix, leave ‘nil` for automatic detection



49
50
51
52
53
54
55
56
57
58
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 49

def self.narr_from_png fname, scale: nil, flat: false
  require 'rmagick'
  img = Magick::ImageList.new(fname).first
  img.scale!(scale) if scale
  shape = [img.columns, img.rows]
  pixels = img.export_pixels(0, 0, *shape, 'I') # 'I' for intensity
  raise "Sanity check" unless shape.reduce(:*)==pixels.size
  return pixels.to_na if flat
  pixels.to_na.to_dimensions shape
end

.narr_to_img(narr, shape: nil) ⇒ Object

Create RMagick::Image from numeric matrix data

Parameters:

  • narr (NArray)

    numeric matrix to display

  • shape (Array<Integer>) (defaults to: nil)

    optional reshaping



11
12
13
14
15
16
17
18
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 11

def self.narr_to_img narr, shape: nil
  require 'rmagick'
  shape ||= narr.shape
  shape = [1, shape] if shape.kind_of?(Integer) || shape.size == 1
  # `Image::constitute` requires Float pixels to be in [0,1]
  pixels = Norm.feature_scaling narr.cast_to(NArray), to: [0,1]
  Magick::Image.constitute *shape, "I", pixels.to_a.flatten
end

.narr_to_png(narr, fname, shape: nil) ⇒ Object

Create PNG file from numeric matrix data

Parameters:

  • narr (NArray)

    numeric matrix to display

  • fname (String)

    path to save PNG

  • shape (Array<Integer>) (defaults to: nil)

    optional reshaping before saving



24
25
26
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 24

def self.narr_to_png narr, fname, shape: nil
  narr_to_img(narr, shape: shape).write fname
end