Spatial filtering helps us change images by looking at each pixel and its neighbors. It is useful to highlight edges, blur, or sharpen pictures.
Spatial filtering (convolution kernels) in Signal Processing
output_image = convolve2d(input_image, kernel, mode='same')input_image is the image you want to filter.
kernel is a small matrix that defines the filter effect.
kernel = [[1/9, 1/9, 1/9], [1/9, 1/9, 1/9], [1/9, 1/9, 1/9]]
kernel = [[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]
kernel = [[0, -1, 0], [-1, 5, -1], [0, -1, 0]]
This program creates a small image with a bright center. It applies a sharpening filter using convolution. The printed output shows the original and filtered image arrays. The plot visually compares the two images.
import numpy as np from scipy.signal import convolve2d import matplotlib.pyplot as plt # Create a simple 5x5 image with a bright center image = np.array([[10, 10, 10, 10, 10], [10, 50, 50, 50, 10], [10, 50, 100, 50, 10], [10, 50, 50, 50, 10], [10, 10, 10, 10, 10]]) # Define a sharpening kernel kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) # Apply convolution filtered_image = convolve2d(image, kernel, mode='same', boundary='fill', fillvalue=0) print("Original Image:\n", image) print("\nFiltered Image:\n", filtered_image) # Plot images side by side plt.subplot(1, 2, 1) plt.title('Original Image') plt.imshow(image, cmap='gray', vmin=0, vmax=100) plt.colorbar() plt.subplot(1, 2, 2) plt.title('Filtered Image (Sharpened)') plt.imshow(filtered_image, cmap='gray', vmin=0, vmax=300) plt.colorbar() plt.tight_layout() plt.show()
Edges of the image are handled by padding with zeros here, which can create dark borders.
Kernels must be smaller than the image and usually have odd dimensions (3x3, 5x5).
Changing kernel values changes the filter effect (blur, sharpen, edge detect).
Spatial filtering changes images by combining each pixel with neighbors using a kernel.
Kernels are small matrices that define how to mix pixel values.
Convolution applies the kernel across the whole image to create effects like blur or sharpen.
Practice
Solution
Step 1: Understand the role of convolution kernel
A convolution kernel is a small matrix used to combine pixel values with their neighbors.Step 2: Identify the effect on the image
This combination changes the image appearance, such as blurring or sharpening.Final Answer:
To combine each pixel with its neighbors to modify the image -> Option AQuick Check:
Kernel mixes pixels = Modify image [OK]
- Thinking kernels change file format
- Confusing kernel with resolution change
- Assuming kernel converts color space
Solution
Step 1: Identify averaging kernel pattern
A simple averaging kernel has equal weights to all neighbors, usually all ones before normalization.Step 2: Match given options
[[1, 1, 1], [1, 1, 1], [1, 1, 1]] is all ones in a 3x3 matrix, matching the averaging kernel before dividing by 9.Final Answer:
[[1, 1, 1], [1, 1, 1], [1, 1, 1]] -> Option AQuick Check:
All ones kernel = averaging filter [OK]
- Choosing kernels with negative values for blur
- Confusing sharpening kernels with averaging
- Ignoring kernel size and values
[[0, -1, 0], [-1, 5, -1], [0, -1, 0]] applied to an image, what effect does this kernel produce?Solution
Step 1: Recognize kernel pattern
This kernel has a strong positive center (5) and negative neighbors (-1), typical for sharpening.Step 2: Understand effect on image
It enhances differences between center pixel and neighbors, making edges sharper.Final Answer:
Sharpening the image -> Option BQuick Check:
Positive center + negative neighbors = Sharpen [OK]
- Confusing sharpening with blurring
- Assuming kernel converts color
- Mixing edge detection with sharpening
[[1, 1, 1], [1, 1, 1], [1, 1, 1]] directly to an image without dividing by 9. What is the likely problem?Solution
Step 1: Understand kernel normalization
Averaging kernels require dividing sum by total weights (9) to keep brightness consistent.Step 2: Effect of missing normalization
Without dividing by 9, pixel values sum up and increase brightness, causing overexposure.Final Answer:
The image will become too bright (overexposed) -> Option DQuick Check:
Missing division = brightness increase [OK]
- Expecting kernel to cause syntax error
- Thinking image gets darker without division
- Assuming kernel changes color space
Solution
Step 1: Identify edge detection kernel characteristics
Edge detection kernels emphasize differences by subtracting center pixel multiplied by 4 and adding neighbors.Step 2: Match kernel to edge detection pattern
[[0, 1, 0], [1, -4, 1], [0, 1, 0]] matches the Laplacian kernel, a common edge detector with center -4 and neighbors 1.Final Answer:
[[0, 1, 0], [1, -4, 1], [0, 1, 0]] -> Option CQuick Check:
Laplacian kernel = edge detection [OK]
- Choosing averaging or sharpening kernels for edge detection
- Ignoring negative center value in kernel
- Confusing kernel purpose
Ready to prove it?
Pass the test and this pattern moves unknown
