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.
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 inrows
andcols
variables; - Calculate the center point and store in
crow
andccol
; - Define the
low_mask
as an array of zeros with(rows, cols)
shape andnp.uint8
type; - Choose the 20x20 center region of
low_mask
and fill it with1
. - Define the
high_mask
as an array of ones with(rows, cols)
shape andnp.uint8
type; - Choose the 20x20 center region of
high_mask
and fill it with0
; - Apply
low_mask
andhigh_mask
todft_shift
and store the filtered frequences inlowpass_dft_filtered
andhighpass_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
.
- Do inverse shifting and store in
- 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
.
- Do inverse shifting and store in
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.45
Low-pass and High-pass Filters
Swipe to show menu
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.
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 inrows
andcols
variables; - Calculate the center point and store in
crow
andccol
; - Define the
low_mask
as an array of zeros with(rows, cols)
shape andnp.uint8
type; - Choose the 20x20 center region of
low_mask
and fill it with1
. - Define the
high_mask
as an array of ones with(rows, cols)
shape andnp.uint8
type; - Choose the 20x20 center region of
high_mask
and fill it with0
; - Apply
low_mask
andhigh_mask
todft_shift
and store the filtered frequences inlowpass_dft_filtered
andhighpass_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
.
- Do inverse shifting and store in
- 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
.
- Do inverse shifting and store in
Solution
Thanks for your feedback!
Awesome!
Completion rate improved to 3.45single