lib/dnn/image.rb in ruby-dnn-1.2.0 vs lib/dnn/image.rb in ruby-dnn-1.2.1

- old
+ new

@@ -112,9 +112,44 @@ x = x.mean(axis: 2, keepdims: true).concatenate(img[true, true, 3..3], axis: 2) end Numo::UInt8.cast(x) end + # Image convert image channel to RGB. + # @param [Numo::UInt8] img Image to RGB. + def self.to_rgb(img) + img_check(img) + case img.shape[2] + when 1 + return img.concatenate(img, axis: 2).concatenate(img, axis: 2) + when 2 + img = img[true, true, 0...1] + return img.concatenate(img, axis: 2).concatenate(img, axis: 2) + when 4 + return img[true, true, 0...3].clone + end + img + end + + # Image convert image channel to RGBA. + # @param [Numo::UInt8] img Image to RGBA. + def self.to_rgba(img) + img_check(img) + case img.shape[2] + when 1 + alpha = Numo::UInt8.new(*img.shape[0..1], 1).fill(255) + return img.concatenate(img, axis: 2).concatenate(img, axis: 2).concatenate(alpha, axis: 2) + when 2 + alpha = img[true, true, 1...2] + img = img[true, true, 0...1] + return img.concatenate(img, axis: 2).concatenate(img, axis: 2).concatenate(alpha, axis: 2) + when 3 + alpha = Numo::UInt8.new(*img.shape[0..1], 1).fill(255) + return img.concatenate(alpha, axis: 2) + end + img + end + private_class_method def self.img_check(img) raise TypeError, "img: #{img.class} is not an instance of the Numo::UInt8 class." unless img.is_a?(Numo::UInt8) if img.shape.length != 3 raise ImageShapeError, "img shape is #{img.shape}. But img shape must be 3 dimensional." elsif !img.shape[2].between?(1, 4)