Module: Magro::Filter

Defined in:
lib/magro/filter.rb

Overview

Filter module provides functions for image filtering.

Class Method Summary collapse

Class Method Details

.convolve2d(arr1, arr2) ⇒ Numo::NArray

Convolve two 2-dimensional arrays.

Parameters:

  • arr1 (Numo::NArray)

    (shape: [row1, col1]) First input array.

  • arr2 (Numo::NArray)

    (shape: [row2, col2]) Second input array.

Returns:

  • (Numo::NArray)

    (shape: [row1 - row2 + 1, col1 - col2 + 1]) Convolution of arr1 with arr2.

Raises:

  • (ArgumentError)

    This error is raised when class of input array is not Numo::NArray.



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.

Examples:

image = Magro::IO.imread('foo.png')
kernel = Numo::DFloat[
  [1, 1, 1],
  [1, 1, 1],
  [1, 1, 1]
]
blured_image = Magro::Filter.filter2d(image, kernel)
Magro::IO.imsave('bar.png', blured_image)

Parameters:

  • image (Numo::UInt8)

    (shape: [height, width, n_channels]) Input image to be filtered.

  • kernel (Numo::DFloat)

    (shape: [kernel_height, kernel_width]) Box filter.

  • scale (Float/Nil) (defaults to: nil)

    Scale parameter for box filter. If nil is given, the box filter is normalized with sum of filter values.

  • offset (Integer) (defaults to: 0)

    Offset value of filtered image.

Returns:

  • (Numo::UInt8)

    (shape: [height, width, n_channels]) Filtered image.

Raises:

  • (ArgumentError)

    This error is raised when class of input image is not Numo::NArray.



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