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
-
.display(narr, disp_size: nil, shape: nil, in_fork: true) ⇒ Object
Show a numeric matrix as image in a RMagick window.
-
.narr_from_png(fname, scale: nil, flat: false) ⇒ Object
Create numeric matrix from png by filename.
-
.narr_to_img(narr, shape: nil) ⇒ Object
Create RMagick::Image from numeric matrix data.
-
.narr_to_png(narr, fname, shape: nil) ⇒ Object
Create PNG file from numeric matrix data.
Class Method Details
.display(narr, disp_size: nil, shape: nil, in_fork: true) ⇒ Object
Show a numeric matrix as image in a RMagick window
30 31 32 33 34 35 36 37 38 |
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 30 def self.display narr, disp_size: nil, shape: nil, in_fork: true 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.
45 46 47 48 49 50 51 52 53 |
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 45 def self.narr_from_png fname, scale: nil, flat: false 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
9 10 11 12 13 14 15 |
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 9 def self.narr_to_img narr, shape: nil 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, 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
21 22 23 |
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 21 def self.narr_to_png narr, fname, shape: nil narr_to_img(narr, shape: shape).write fname end |