We use image filtering to smooth images and reduce noise. The gaussian_filter helps blur an image gently by averaging nearby pixels with a bell-shaped curve.
0
0
Image filtering (gaussian_filter) in SciPy
Introduction
To remove small specks or noise from a photo taken in low light.
To prepare an image before detecting edges or shapes.
To create a soft blur effect for artistic purposes.
To smooth data in scientific images like medical scans.
To reduce detail before compressing an image.
Syntax
SciPy
scipy.ndimage.gaussian_filter(input, sigma, order=0, output=None, mode='reflect', cval=0.0, truncate=4.0)
input: The image or array to filter.
sigma: Controls how much to blur. Bigger sigma means more blur.
Examples
Apply a mild blur with sigma 1 to the image.
SciPy
from scipy.ndimage import gaussian_filter blurred = gaussian_filter(image, sigma=1)
Apply a stronger blur with sigma 3 for more smoothing.
SciPy
blurred = gaussian_filter(image, sigma=3)Apply different blur amounts for each axis in a 2D image.
SciPy
blurred = gaussian_filter(image, sigma=[1, 2])
Sample Program
This code creates a small noisy image with a bright square in the center. Then it smooths the image using gaussian_filter with sigma=1. Finally, it prints both the original and blurred images as arrays rounded to 2 decimals.
SciPy
import numpy as np from scipy.ndimage import gaussian_filter import matplotlib.pyplot as plt # Create a simple 2D image with noise np.random.seed(0) image = np.zeros((10, 10)) image[4:6, 4:6] = 10 # bright square in the middle image += np.random.normal(0, 1, image.shape) # add noise # Apply gaussian filter with sigma=1 blurred_image = gaussian_filter(image, sigma=1) # Print original and blurred images print('Original image:\n', np.round(image, 2)) print('\nBlurred image:\n', np.round(blurred_image, 2))
OutputSuccess
Important Notes
The sigma value controls the blur strength. Try small values first.
Gaussian filter works well to reduce noise but keeps edges smoother than simple averaging.
Use mode parameter to control how edges are handled (default is 'reflect').
Summary
Gaussian filter smooths images by averaging pixels with a bell curve.
It helps reduce noise and prepare images for further analysis.
Adjust sigma to control how much blur you want.