Adaptive Gaussian filtering with NumPy

1.4k Views Asked by At

Running a Gaussian filter over image with static sigma value is easy:

scipy.ndimage.gaussian_filter(input, sigma)

But how to do this with a sigma value that is different for each pixel? For example, I might have another NumPy array of the same size that indicates what sigma to use for each pixel.

1

There are 1 best solutions below

0
Cris Luengo On

I'm not aware of an adaptive Gaussian filter implementation in OpenCV, scipy.ndimage or scikit-image. DIPlib does have such a filter (disclosure: I'm an author). You can install it with pip install diplib.

This is how you would use the adaptive Gaussian filter where only the size of the kernel changes (this function also can rotate an elongated Gaussian, it's pretty neat, I recommend you play around with that!).

import diplib as dip

img = dip.ImageRead('examples/trui.ics')

# We need an image that indicates the kernel orientation for each pixel,
# we just set this to 0
orientation = img.Similar('SFLOAT')
orientation.Fill(0)
# We need an image that indicates the kernel size for each pixel,
# this is your NumPy array
scale = dip.CreateRadiusCoordinate(img.Sizes()) / 200
# This is the function. Kernel sizes in the `scale` image are multiplied
# by the sigmas given here
out = dip.AdaptiveGauss(img, [orientation, scale], sigmas=[5,5])

dip.ImageWrite(img,'so_in.jpg')
dip.ImageWrite(out,'so_out.jpg')

Do note that image objects in the Python bindings to DIPlib are compatible with NumPy arrays.