Module: DNN::Image
- Defined in:
- lib/dnn/image.rb
Defined Under Namespace
Classes: ImageError, ImageReadError, ImageShapeError, ImageWriteError
Constant Summary collapse
- RGB =
3- RGBA =
4
Class Method Summary collapse
-
.read(file_name, channel_type = RGB) ⇒ Object
Read image from file.
-
.resize(img, out_height, out_width) ⇒ Object
Resize the image.
-
.to_gray_scale(img) ⇒ Object
Image convert to gray scale.
-
.trim(img, y, x, height, width) ⇒ Object
Trimming the image.
-
.write(file_name, img, quality: 100) ⇒ Object
Write image to file.
Class Method Details
.read(file_name, channel_type = RGB) ⇒ Object
Read image from file.
20 21 22 23 24 25 26 |
# File 'lib/dnn/image.rb', line 20 def self.read(file_name, channel_type = RGB) raise ImageReadError, "#{file_name} is not found." unless File.exist?(file_name) bin, w, h, n = Stb.stbi_load(file_name, channel_type) raise ImageReadError, "#{file_name} load failed." if bin == "" img = Numo::UInt8.from_binary(bin) img.reshape(h, w, channel_type) end |
.resize(img, out_height, out_width) ⇒ Object
Resize the image.
67 68 69 70 71 72 73 |
# File 'lib/dnn/image.rb', line 67 def self.resize(img, out_height, out_width) img_check(img) in_height, in_width, ch = *img.shape out_bin, res = Stb.stbir_resize_uint8(img.to_binary, in_width, in_height, 0, out_width, out_height, 0, ch) img2 = Numo::UInt8.from_binary(out_bin).reshape(out_height, out_width, ch) img2 end |
.to_gray_scale(img) ⇒ Object
Image convert to gray scale.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/dnn/image.rb', line 88 def self.to_gray_scale(img) img_check(img) if img.shape[2] == RGB x = Numo::SFloat.cast(img) x = x.mean(axis: 2, keepdims: true) elsif img.shape[2] == RGBA x = Numo::SFloat.cast(img[true, true, 0..2]) x = x.mean(axis: 2, keepdims: true).concatenate(img[true, true, 3..3], axis: 2) end Numo::UInt8.cast(x) end |
.trim(img, y, x, height, width) ⇒ Object
Trimming the image.
81 82 83 84 |
# File 'lib/dnn/image.rb', line 81 def self.trim(img, y, x, height, width) img_check(img) img[y...(y + height), x...(x + width), true].clone end |
.write(file_name, img, quality: 100) ⇒ Object
Write image to file.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dnn/image.rb', line 32 def self.write(file_name, img, quality: 100) img_check(img) match_data = file_name.match(%r`(.*)/.+$`) if match_data dir_name = match_data[1] Dir.mkdir(dir_name) unless Dir.exist?(dir_name) end h, w, ch = img.shape bin = img.to_binary match_data = file_name.match(/\.(\w+)$/i) if match_data ext = match_data[1] else raise ImageWriteError, "File name has not extension." end case ext when "png" stride_in_bytes = w * ch res = Stb.stbi_write_png(file_name, w, h, ch, bin, stride_in_bytes) when "bmp" res = Stb.stbi_write_bmp(file_name, w, h, ch, bin) when "jpg", "jpeg" raise TypeError, "quality:#{quality.class} is not an instance of Integer class." unless quality.is_a?(Integer) raise ArgumentError, "quality should be between 1 and 100." unless quality.between?(1, 100) res = Stb.stbi_write_jpg(file_name, w, h, ch, bin, quality) else raise ImageWriteError, "Extension '#{ext}' is not support." end raise ImageWriteError, "Image write failed." if res == 0 end |