Bird
Raised Fist0
Signal Processingdata~10 mins

Frequency domain image filtering in Signal Processing

Choose your learning style10 modes available
Introduction

Frequency domain image filtering helps us change images by focusing on patterns like edges or smooth areas. It works by changing the image into waves, filtering those waves, and then changing back to the image.

To remove noise from a photo by filtering out unwanted patterns.
To sharpen an image by enhancing edges and details.
To blur an image smoothly by reducing high-frequency details.
To detect repeating patterns or textures in an image.
Syntax
Signal Processing
1. Convert image to frequency domain using Fourier Transform.
2. Create a filter mask to keep or remove certain frequencies.
3. Multiply the frequency image by the filter mask.
4. Convert the filtered image back to spatial domain using Inverse Fourier Transform.

The Fourier Transform changes the image from pixels to waves.

The filter mask controls which wave patterns to keep or remove.

Examples
This changes the image into frequency waves.
Signal Processing
# Convert image to frequency domain
F = fft2(image)
This mask keeps low frequencies and removes high frequencies.
Signal Processing
# Create a low-pass filter mask
mask = create_low_pass_mask(shape=image.shape, cutoff=30)
Multiplying keeps only the frequencies allowed by the mask.
Signal Processing
# Apply filter in frequency domain
F_filtered = F * mask
This changes the filtered waves back to an image.
Signal Processing
# Convert back to image
image_filtered = ifft2(F_filtered).real
Sample Program

This code creates a noisy image, applies a low-pass frequency filter to remove noise, and shows the before and after images.

Signal Processing
import numpy as np
import matplotlib.pyplot as plt
from numpy.fft import fft2, ifft2, fftshift, ifftshift

# Create a simple image with noise
np.random.seed(0)
image = np.zeros((100, 100))
image[40:60, 40:60] = 1  # square in center
image += 0.3 * np.random.randn(100, 100)  # add noise

# Step 1: Fourier transform
F = fft2(image)
F_shifted = fftshift(F)  # center frequencies

# Step 2: Create low-pass filter mask
rows, cols = image.shape
crow, ccol = rows // 2, cols // 2
mask = np.zeros_like(image)
radius = 20
for i in range(rows):
    for j in range(cols):
        if (i - crow)**2 + (j - ccol)**2 < radius**2:
            mask[i, j] = 1

# Step 3: Apply mask
F_filtered = F_shifted * mask

# Step 4: Inverse Fourier transform
F_ishift = ifftshift(F_filtered)
image_filtered = ifft2(F_ishift).real

# Plot original and filtered images
plt.figure(figsize=(8,4))
plt.subplot(1,2,1)
plt.title('Original Noisy Image')
plt.imshow(image, cmap='gray')
plt.axis('off')

plt.subplot(1,2,2)
plt.title('Filtered Image (Low-pass)')
plt.imshow(image_filtered, cmap='gray')
plt.axis('off')
plt.show()
OutputSuccess
Important Notes

Frequency domain filtering works best on images with clear patterns or noise.

Choosing the right filter size (cutoff) controls how much detail is kept or removed.

Remember to shift frequencies to center before filtering and shift back before inverse transform.

Summary

Frequency domain filtering changes images by working with wave patterns instead of pixels.

It uses Fourier Transform to move between image and frequency views.

Filters in frequency domain can remove noise or enhance details effectively.

Practice

(1/5)
1. What is the main purpose of applying frequency domain filtering to an image?
easy
A. To remove or enhance specific patterns by modifying frequency components
B. To change the image colors directly
C. To crop the image edges
D. To increase the image file size

Solution

  1. Step 1: Understand frequency domain filtering

    Frequency domain filtering works by changing the image's frequency components, which represent patterns like edges or textures.
  2. Step 2: Identify the effect on image

    By modifying these frequency components, we can remove noise or enhance details, not change colors or crop.
  3. Final Answer:

    To remove or enhance specific patterns by modifying frequency components -> Option A
  4. Quick Check:

    Frequency domain filtering = modify patterns [OK]
Hint: Frequency filtering changes patterns, not colors or size [OK]
Common Mistakes:
  • Confusing frequency filtering with color editing
  • Thinking it crops or resizes images
  • Assuming it increases file size
2. Which Python function is commonly used to convert an image to the frequency domain for filtering?
easy
A. np.mean()
B. np.fft.fft2()
C. np.sort()
D. np.resize()

Solution

  1. Step 1: Identify Fourier transform function

    To convert an image to frequency domain, we use the 2D Fourier transform, which is np.fft.fft2() in Python.
  2. Step 2: Check other options

    np.mean() calculates average, np.sort() sorts values, np.resize() changes shape, none convert to frequency domain.
  3. Final Answer:

    np.fft.fft2() -> Option B
  4. Quick Check:

    Frequency domain = fft2() [OK]
Hint: Use fft2() for 2D Fourier transform [OK]
Common Mistakes:
  • Using mean or sort instead of fft2
  • Confusing resize with frequency transform
  • Not knowing fft2 is 2D transform
3. Given this code snippet, what is the shape of filtered_img after inverse FFT?
import numpy as np
img = np.random.rand(64, 64)
freq = np.fft.fft2(img)
mask = np.zeros((64, 64))
mask[30:34, 30:34] = 1
filtered_freq = freq * mask
filtered_img = np.fft.ifft2(filtered_freq)
medium
A. (128, 128)
B. (4, 4)
C. (30, 34)
D. (64, 64)

Solution

  1. Step 1: Check input image shape

    The original image has shape (64, 64), so the FFT and inverse FFT keep this shape.
  2. Step 2: Understand mask effect

    The mask zeros out frequencies except a small block, but does not change overall shape.
  3. Final Answer:

    (64, 64) -> Option D
  4. Quick Check:

    Inverse FFT output shape = input shape [OK]
Hint: Inverse FFT output shape matches input image shape [OK]
Common Mistakes:
  • Assuming mask changes output size
  • Confusing mask slice size with output shape
  • Expecting doubled or cropped shape
4. What is the error in this code for applying a low-pass filter in frequency domain?
import numpy as np
img = np.random.rand(32, 32)
freq = np.fft.fft2(img)
mask = np.ones((16, 16))
filtered_freq = freq * mask
filtered_img = np.fft.ifft2(filtered_freq)
medium
A. Mask size does not match frequency array size
B. Using fft2 instead of fft
C. Missing conversion to grayscale
D. Inverse FFT should be fft instead of ifft2

Solution

  1. Step 1: Check shapes of freq and mask

    freq has shape (32, 32), but mask is (16, 16). Multiplying arrays of different shapes causes error.
  2. Step 2: Confirm other code parts

    fft2 and ifft2 are correct for 2D images; grayscale conversion is not mandatory here.
  3. Final Answer:

    Mask size does not match frequency array size -> Option A
  4. Quick Check:

    Mask shape must match freq shape [OK]
Hint: Mask and frequency array must have same shape [OK]
Common Mistakes:
  • Using smaller mask without resizing
  • Confusing fft2 with fft
  • Thinking grayscale needed for filtering
5. You want to remove high-frequency noise from a 128x128 image using frequency domain filtering. Which mask design is best?
hard
A. A mask that passes only the highest frequencies at edges
B. A mask with random zeros and ones scattered
C. A circular mask centered at frequency origin with radius 20, passing low frequencies
D. A mask that zeros out the center frequencies and passes edges

Solution

  1. Step 1: Understand noise frequency location

    High-frequency noise appears at edges of frequency domain; low frequencies are near center.
  2. Step 2: Choose mask to keep low frequencies

    A circular mask centered at origin with radius 20 keeps low frequencies and blocks high frequencies, removing noise.
  3. Step 3: Evaluate other options

    Random mask or passing only high frequencies will not remove noise effectively.
  4. Final Answer:

    A circular mask centered at frequency origin with radius 20, passing low frequencies -> Option C
  5. Quick Check:

    Low-pass mask = circular center pass [OK]
Hint: Use circular low-pass mask to keep smooth parts [OK]
Common Mistakes:
  • Passing high frequencies instead of blocking
  • Using random masks without pattern
  • Zeroing low frequencies by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard