Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Low-pass and High-pass Filters | Image Processing with OpenCV
Computer Vision Essentials

book
Low-pass and High-pass Filters

One of the key benefits of the Fourier Transformation is it enables us to do high-pass and low-pass filtering.

After we apply the Fourier Transformation:

we need to create a filtering mask

Low-Pass Filtering (Blurring)

A low-pass filter removes high-frequency components, which results in a blurred image. Low-pass mask:

  • rows, cols = image.shape: retrieves the number of rows and columns from the grayscale image;

  • crow, ccol = rows // 2, cols // 2: computes the center coordinates of the image;

  • mask = np.zeros((rows, cols), np.uint8): creates a mask of zeros with the same dimensions as the image;

  • mask[crow-30:crow+30, ccol-30:ccol+30] = 1: sets a 60×60 square region at the center of the mask to 1, allowing only the low-frequency components (near the center of the frequency domain) to pass through while filtering out high-frequency details.

High-Pass Filtering (Edge Detection)

A high-pass filter removes low-frequency components and enhances edges. High-pass mask:

  • highpass_mask = np.ones((rows, cols), np.uint8): initializes a mask of ones with the same dimensions as the image, meaning all frequencies are initially allowed to pass;

  • highpass_mask[crow-5:crow+5, ccol-5:ccol+5] = 0: sets a small 10×10 square at the center (low-frequency region) to zero, effectively blocking those frequencies.

Applying the filter

After creating the mask, we must apply a filter and transform our photo back to the spatial domain:

  • dft_filtered = dft_shift * mask: applies the frequency domain mask (e.g., low-pass or high-pass) by element-wise multiplication with the shifted DFT of the image;

  • dft_inverse = np.fft.ifftshift(dft_filtered): reverses the shift applied earlier to bring the frequency components back to their original positions;

  • image_filtered = np.fft.ifft2(dft_inverse): computes the Inverse Fourier Transform to convert the filtered frequency data back into the spatial domain;

  • image_filtered = np.abs(image_filtered): takes the absolute value to remove any residual imaginary components, resulting in a real-valued filtered image.

Task

Swipe to start coding

You are given an image of the sheep in the image variable and an image in the frequency domain in the dft_shift variable:

  • Get the shape of the image matrix and store it in rows and cols variables;
  • Calculate the center point and store in crow and ccol;
  • Define the low_mask as an array of zeros with (rows, cols) shape and np.uint8 type;
  • Choose the 20x20 center region of low_mask and fill it with 1.
  • Define the high_mask as an array of ones with (rows, cols) shape and np.uint8 type;
  • Choose the 20x20 center region of high_mask and fill it with 0;
  • Apply low_mask and high_mask to dft_shift and store the filtered frequences in lowpass_dft_filtered and highpass_dft_filtered accordingly;
  • Perform inverse transformation for lowpass_dft_filtered:
    • Do inverse shifting and store in lowpass_dft_inverse;
    • Do inverse transformation and store image in image_lowpass;
    • Remove negative values for image_lowpass.
  • Perform inverse transformation for highpass_dft_inverse:
    • Do inverse shifting and store in lowpass_dft_inverse;
    • Do inverse transformation and store image in image_highpass;
    • Remove negative values for image_highpass.

Solution

import io
import cv2
import numpy as np
import urllib.request
from PIL import Image
import matplotlib.pyplot as plt

url = "https://content-media-cdn.codefinity.com/courses/ef049f7b-ce21-45be-a9f2-5103360b0655/task_pictures/sheep.jpg"
with urllib.request.urlopen(url) as response:
image_data = response.read()
image = np.array(Image.open(io.BytesIO(image_data)))
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

dft = np.fft.fft2(image)
dft_shift = np.fft.fftshift(dft)

# Write your code below
rows, cols = image.shape
crow, ccol = rows // 2, cols // 2

low_mask = np.zeros((rows, cols), np.uint8)
low_mask[crow-10:crow+10, ccol-10:ccol+10] = 1

high_mask = np.ones((rows, cols), np.uint8)
high_mask[crow-10:crow+10, ccol-10:ccol+10] = 0

lowpass_dft_filtered = dft_shift * low_mask
highpass_dft_filtered = dft_shift * high_mask

lowpass_dft_inverse = np.fft.ifftshift(lowpass_dft_filtered) # Inverse Shifting
image_lowpass = np.fft.ifft2(lowpass_dft_inverse) # Inverse Transformation
image_lowpass = np.abs(image_lowpass) # Remove negative values

highpass_dft_inverse = np.fft.ifftshift(highpass_dft_filtered) # Inverse Shifting
image_highpass = np.fft.ifft2(highpass_dft_inverse) # Inverse Transformation
image_highpass = np.abs(image_highpass) # Remove negative values
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 3
single

single

import io
import cv2
import numpy as np
import urllib.request
from PIL import Image
import matplotlib.pyplot as plt

url = "https://content-media-cdn.codefinity.com/courses/ef049f7b-ce21-45be-a9f2-5103360b0655/task_pictures/sheep.jpg"
with urllib.request.urlopen(url) as response:
image_data = response.read()
image = np.array(Image.open(io.BytesIO(image_data)))
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

dft = np.fft.fft2(image)
dft_shift = np.fft.fftshift(dft)

# Write your code below
rows, cols = ___
crow, ccol = ___

low_mask = ___
low_mask[___] = ___

high_mask = ___
high_mask[___] = ___

lowpass_dft_filtered = ___
highpass_dft_filtered = ___

lowpass_dft_inverse = ___ # Inverse Shifting
image_lowpass = ___ # Inverse Transformation
image_lowpass = ___ # Remove negative values

highpass_dft_inverse = ___ # Inverse Shifting
image_highpass = ___ # Inverse Transformation
image_highpass = ___ # Remove negative values

# Display the results
plt.figure(figsize=(10,10))

plt.subplot(3,1,1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')

plt.subplot(3,1,2)
plt.imshow(image_lowpass, cmap='gray')
plt.title('Lowpass')

plt.subplot(3,1,3)
plt.imshow(image_highpass, cmap='gray')
plt.title('Highpass')

plt.tight_layout()
plt.show()

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

We use cookies to make your experience better!
some-alt