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

Swipe to show menu

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
We're sorry to hear that something went wrong. What happened?

Ask AI

expand
ChatGPT

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

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

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt