Module: Retouch::Kernels::Convolve

Defined in:
lib/retouch/kernels/convolve.rb

Class Method Summary collapse

Class Method Details

.gaussian(image, sigma) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
# File 'lib/retouch/kernels/convolve.rb', line 8

def gaussian(image, sigma)
  sigma = Float(sigma)
  raise ArgumentError, "sigma must be positive and at most 100" unless sigma.finite? && sigma.positive? && sigma <= 100

  radius = (sigma * 3).ceil
  weights = (-radius..radius).map { |offset| Math.exp(-(offset * offset) / (2 * sigma * sigma)) }
  total = weights.sum
  weights.map! { |weight| weight / total }
  convolve_axis(convolve_axis(image, weights, horizontal: true), weights, horizontal: false)
end