Module: Magro::Filter
- Defined in:
- lib/magro/filter.rb
Overview
Filter module provides functions for image filtering.
Class Method Summary collapse
-
.convolve2d(arr1, arr2) ⇒ Numo::NArray
Convolve two 2-dimensional arrays.
-
.filter2d(image, kernel, scale: nil, offset: 0) ⇒ Numo::UInt8
Applies box filter to image.
Class Method Details
.convolve2d(arr1, arr2) ⇒ Numo::NArray
Convolve two 2-dimensional arrays.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/magro/filter.rb', line 50 def convolve2d(arr1, arr2) # rubocop:disable Metrics/AbcSize raise ArgumentError, 'Expect class of first input array to be Numo::NArray.' unless arr1.is_a?(Numo::NArray) raise ArgumentError, 'Expect class of second input array to be Numo::NArray.' unless arr2.is_a?(Numo::NArray) raise ArgumentError, 'Expect first input array to be 2-dimensional array.' unless arr1.ndim == 2 raise ArgumentError, 'Expect second input array to be 2-dimensional array.' unless arr2.ndim == 2 row1, col1 = arr1.shape row2, col2 = arr2.shape # FIXME: lib/numo/narray/extra.rb:1098: warning: Using the last argument as keyword parameters is deprecated # convolved = im2col(arr1, row2, col2).dot(arr2.flatten) convolved = arr2.flatten.dot(im2col(arr1, row2, col2).transpose) convolved.reshape(row1 - row2 + 1, col1 - col2 + 1) end |
.filter2d(image, kernel, scale: nil, offset: 0) ⇒ Numo::UInt8
Applies box filter to image. This method performs zero padding as a preprocessing.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/magro/filter.rb', line 27 def filter2d(image, kernel, scale: nil, offset: 0) raise ArgumentError, 'Expect class of image to be Numo::NArray.' unless image.is_a?(Numo::NArray) filter_h, filter_w = kernel.shape padded = zero_padding(image, filter_h, filter_w) n_channels = image.shape[2] if n_channels.nil? filter1ch(padded, kernel, scale, offset) else image.class.zeros(image.shape).tap do |filtered| n_channels.times do |c| filtered[true, true, c] = filter1ch(padded[true, true, c], kernel, scale, offset) end end end end |