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(nmat, disp_size: [300, 300], shape: nil, in_fork: true) ⇒ Object
Show a NMatrix as image in a RMagick window.
-
.nmat_from_png(fname, scale: nil, flat: false, dtype: nil) ⇒ Object
Create NMatrix from png by filename.
-
.nmat_to_img(nmat, shape: nil) ⇒ Object
Create RMagick::Image from NMatrix data.
-
.nmat_to_png(nmat, fname, shape: nil) ⇒ Object
Create PNG file from NMatrix data.
Class Method Details
.display(nmat, disp_size: [300, 300], shape: nil, in_fork: true) ⇒ Object
Show a NMatrix as image in a RMagick window
24 25 26 27 28 29 30 31 |
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 24 def self.display nmat, disp_size: [300, 300], shape: nil, in_fork: true img = nmat_to_img(nmat, shape: shape).resize(*disp_size) if in_fork MachineLearningWorkbench::Tools::Execution.in_fork { img.display } else img.display end end |
.nmat_from_png(fname, scale: nil, flat: false, dtype: nil) ⇒ Object
Create NMatrix from png by filename.
38 39 40 41 42 43 44 45 46 |
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 38 def self.nmat_from_png fname, scale: nil, flat: false, dtype: nil 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_nm(nil, dtype) if flat NMatrix.new shape, pixels, dtype: dtype end |
.nmat_to_img(nmat, shape: nil) ⇒ Object
Create RMagick::Image from NMatrix data
7 8 9 10 11 12 13 |
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 7 def self.nmat_to_img nmat, shape: nil shape ||= nmat.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 nmat.round(4), to: [0,1] Magick::Image.constitute *shape, "I", pixels.to_flat_a end |
.nmat_to_png(nmat, fname, shape: nil) ⇒ Object
Create PNG file from NMatrix data
16 17 18 |
# File 'lib/machine_learning_workbench/tools/imaging.rb', line 16 def self.nmat_to_png nmat, fname, shape: nil nmat_to_img(nmat, shape: shape).write fname end |