# Butterworth Low Pass Filter in Image Pre-processing

Ringing effect in output after smoothing process is common remember that Ideal loss pass filters, once applied over image it smooths image and shows rings were also appeared around the object. This post is about exploring another important loss pass filter ‘Butterworth loss pass filter’ which is also applied for smoothing process but it generates lesser ringing effect than Ideal loss pass filter (ILPF) and even with better smoothing effects than ILPF. So, let’s start with definition of Butterworth loss pass filter.

## What is Butterworth loss pass filter ?

Butterworth is type of low pass filter that allows low frequency components, and dropping the high frequncy components of the images.It is used to smoothing the images, and reduce the noise with enchancing the details of the images.

**Elements of the Butterworth loss pass filter**

**Cutoff Frequency**: The cutoff frequency is the theshold frequcny at which the filter starts to attenuate the image frequencies.

**Frequency Response**: The Butterworth filter’s frequency response is flat in the band pass region and reaches smoothly towards zero in the band stop.

**Order of the Filter**: The filter’s order determines the level of details captured whenever there is transition between the passband and the stopband.

**Mathematical representation of Butterworth loss pass filter**

Butterworth loss pass filter is defined as:

Where D0 (knot) represents the cutoff frequency , n represents the order of filter, And D(u,v) is given by :

**Example Numerical**

*Let **input** image be :*

[[ 0, 50, 100],

[150, 200, 250],

[100, 150, 200]]

*Apply Discrete Fourier Transform (DFT) to convert the image to the frequency domain:*

[[ 1350 + 0j, -450 + 0j, -450 + 0j],

[-225 + 0j, 0 + 0j, 0 + 0j],

[-225 + 0j, 0 + 0j, 0 + 0j]]

*Create butterworth low pass filter mask ,for a 3x3 image, the distances from the center (1,1) in the frequency domain are* :

[[sqrt(2), 1, sqrt(2)],

[1, 0, 1],

[sqrt(2), 1, sqrt(2)]]

*With a cutoff frequency D0=1D_0 = 1D0=1 and filter order n=2n = 2n=2, the Butterworth filter mask is computed:*

[[1 / (1 + (sqrt(2)/1)⁴), 1 / (1 + (1/1)⁴), 1 / (1 + (sqrt(2)/1)⁴)],

[1 / (1 + (1/1)⁴), 1 / (1 + (0/1)⁴), 1 / (1 + (1/1)⁴)],

[1 / (1 + (sqrt(2)/1)⁴), 1 / (1 + (1/1)⁴), 1 / (1 + (sqrt(2)/1)⁴)]]

*simpler into :*

[[0.2, 0.5, 0.2],

[0.5, 1.0, 0.5],

[0.2, 0.5, 0.2]]

*Apply filter:*

[[1350*0.2, -450*0.5, -450*0.2],

[-225*0.5, 0*1.0, 0*0.5],

[-225*0.2, 0*0.5, 0*0.2]]

*simpling into :*

[[ 270 + 0j, -225 + 0j, -90 + 0j],

[-112.5 + 0j, 0 + 0j, 0 + 0j],

[ -45 + 0j, 0 + 0j, 0 + 0j]]

*Perform Inverse DFT (IDFT) to convert the filtered image back to the spatial domain which is **Output **image:*

[[27, 30, 27],

[33, 36, 33],

[27, 30, 27]]

*Note:** Aim of the example is to illustrate the high level steps during applying Butterworth low pass filter over image only.*

## Python example implementation of Butterworth loss pass filter

import numpy as np

import cv2

import matplotlib.pyplot as pltdef butterworth_lowpass_filter(shape, cutoff, order):

rows, cols = shape

crow, ccol = rows // 2 , cols // 2

mask = np.zeros((rows, cols), dtype=np.float32)

for u in range(rows):

for v in range(cols):

distance = np.sqrt((u — crow)**2 + (v — ccol)**2)

mask[u, v] = 1 / (1 + (distance / cutoff)**(2 * order))

return maskdef apply_butterworth_lowpass_filter(image, cutoff, order):

dft = cv2.dft(np.float32(image), flags=cv2.DFT_COMPLEX_OUTPUT)

dft_shift = np.fft.fftshift(dft)

mask = butterworth_lowpass_filter(image.shape, cutoff, order)

fshift = dft_shift * mask[:, :, np.newaxis]

f_ishift = np.fft.ifftshift(fshift)

img_back = cv2.idft(f_ishift)

img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])

return img_back# Load the image

image = cv2.imread(‘example.jpg’, 0)# Apply Butterworth Lowpass Filter

cutoff = 30 # Adjust the cutoff frequency as needed

order = 2 # Adjust the order of the filter as needed

filtered_image = apply_butterworth_lowpass_filter(image, cutoff, order)# Display the original and filtered images

plt.subplot(121), plt.imshow(image, cmap=’gray’), plt.title(‘Original Image’)

plt.subplot(122), plt.imshow(filtered_image, cmap=’gray’), plt.title(‘Filtered Image’)

plt.show()

## Common python libraries those use Butterworth low pass filters

OpenCv

SciPy

Scikit-image

## Applications of Butterworth low pass filters

Noise Reduction :medical imaging

Image Smoothing:Photography and Videography

Preprocessing for Image Segmentation:Automated Inspection Systems

Feature Extraction:Object Recognition

## Limitations

· *Limited Control over Passband and Stopband Ripples*

· *Smooth Transition but Limited Sharpness*

· *Fixed Filter Characteristics*

· *Not Optimal for All Applications*

With exploring the limitations associated with Butterworth low pass filters, post reaches to the end. I hope post act as useful resource over exploring the concepts of Butterworth low pass filter in image preprocessing.

Thank you readers !!