Awesome!
Completion rate improved to 3.45single
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:
dft = np.fft.fft2(image)
dft_shift = np.fft.fftshift(dft)
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
crow, ccol = rows // 2, cols // 2
mask = np.zeros((rows, cols), np.uint8)
mask[crow-30:crow+30, ccol-30:ccol+30] = 1
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)
highpass_mask[crow-5:crow+5, ccol-5:ccol+5] = 0
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
dft_inverse = np.fft.ifftshift(dft_filtered) # Inverse shifting
image_filtered = np.fft.ifft2(dft_inverse) # Inverse transformation
image_filtered = np.abs(image_filtered) # Remove negative values
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
imagematrix and store it inrowsandcolsvariables; - Calculate the center point and store in
crowandccol; - Define the
low_maskas an array of zeros with(rows, cols)shape andnp.uint8type; - Choose the 20x20 center region of
low_maskand fill it with1. - Define the
high_maskas an array of ones with(rows, cols)shape andnp.uint8type; - Choose the 20x20 center region of
high_maskand fill it with0; - Apply
low_maskandhigh_masktodft_shiftand store the filtered frequences inlowpass_dft_filteredandhighpass_dft_filteredaccordingly; - 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