Bird
Raised Fist0
Signal Processingdata~5 mins

Spatial filtering (convolution kernels) in Signal Processing

Choose your learning style10 modes available
Introduction

Spatial filtering helps us change images by looking at each pixel and its neighbors. It is useful to highlight edges, blur, or sharpen pictures.

To remove noise from a photo by smoothing it.
To find edges in an image for object detection.
To sharpen a blurry picture to see details better.
To detect patterns or textures in satellite images.
To prepare images before using them in machine learning.
Syntax
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.

Examples
This kernel blurs the image by averaging each pixel with its neighbors.
Signal Processing
kernel = [[1/9, 1/9, 1/9],
          [1/9, 1/9, 1/9],
          [1/9, 1/9, 1/9]]
This kernel highlights edges by emphasizing differences between a pixel and its neighbors.
Signal Processing
kernel = [[-1, -1, -1],
          [-1, 8, -1],
          [-1, -1, -1]]
This kernel sharpens the image by enhancing the center pixel compared to neighbors.
Signal Processing
kernel = [[0, -1, 0],
          [-1, 5, -1],
          [0, -1, 0]]
Sample Program

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.

Signal Processing
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()
OutputSuccess
Important Notes

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).

Summary

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

(1/5)
1. What is the main purpose of a convolution kernel in spatial filtering?
easy
A. To combine each pixel with its neighbors to modify the image
B. To change the image file format
C. To increase the image resolution
D. To convert the image to grayscale

Solution

  1. Step 1: Understand the role of convolution kernel

    A convolution kernel is a small matrix used to combine pixel values with their neighbors.
  2. Step 2: Identify the effect on the image

    This combination changes the image appearance, such as blurring or sharpening.
  3. Final Answer:

    To combine each pixel with its neighbors to modify the image -> Option A
  4. Quick Check:

    Kernel mixes pixels = Modify image [OK]
Hint: Kernels mix pixels with neighbors to change images [OK]
Common Mistakes:
  • Thinking kernels change file format
  • Confusing kernel with resolution change
  • Assuming kernel converts color space
2. Which of the following is the correct 3x3 kernel for a simple averaging (blur) filter?
easy
A. [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
B. [[0, -1, 0], [-1, 5, -1], [0, -1, 0]]
C. [[1, 2, 1], [2, 4, 2], [1, 2, 1]]
D. [[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]

Solution

  1. Step 1: Identify averaging kernel pattern

    A simple averaging kernel has equal weights to all neighbors, usually all ones before normalization.
  2. 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.
  3. Final Answer:

    [[1, 1, 1], [1, 1, 1], [1, 1, 1]] -> Option A
  4. Quick Check:

    All ones kernel = averaging filter [OK]
Hint: Averaging kernel has all ones before normalization [OK]
Common Mistakes:
  • Choosing kernels with negative values for blur
  • Confusing sharpening kernels with averaging
  • Ignoring kernel size and values
3. Given the 3x3 kernel [[0, -1, 0], [-1, 5, -1], [0, -1, 0]] applied to an image, what effect does this kernel produce?
medium
A. Blurring the image
B. Sharpening the image
C. Edge detection
D. Converting to grayscale

Solution

  1. Step 1: Recognize kernel pattern

    This kernel has a strong positive center (5) and negative neighbors (-1), typical for sharpening.
  2. Step 2: Understand effect on image

    It enhances differences between center pixel and neighbors, making edges sharper.
  3. Final Answer:

    Sharpening the image -> Option B
  4. Quick Check:

    Positive center + negative neighbors = Sharpen [OK]
Hint: Positive center with negative neighbors sharpens image [OK]
Common Mistakes:
  • Confusing sharpening with blurring
  • Assuming kernel converts color
  • Mixing edge detection with sharpening
4. You apply the kernel [[1, 1, 1], [1, 1, 1], [1, 1, 1]] directly to an image without dividing by 9. What is the likely problem?
medium
A. The image will convert to grayscale
B. The image will become darker
C. The kernel will cause a syntax error
D. The image will become too bright (overexposed)

Solution

  1. Step 1: Understand kernel normalization

    Averaging kernels require dividing sum by total weights (9) to keep brightness consistent.
  2. Step 2: Effect of missing normalization

    Without dividing by 9, pixel values sum up and increase brightness, causing overexposure.
  3. Final Answer:

    The image will become too bright (overexposed) -> Option D
  4. Quick Check:

    Missing division = brightness increase [OK]
Hint: Always normalize averaging kernels by sum of weights [OK]
Common Mistakes:
  • Expecting kernel to cause syntax error
  • Thinking image gets darker without division
  • Assuming kernel changes color space
5. You want to create an edge detection filter using a 3x3 kernel. Which kernel below will best highlight edges by detecting differences in pixel intensity?
hard
A. [[0, -1, 0], [-1, 5, -1], [0, -1, 0]]
B. [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
C. [[0, 1, 0], [1, -4, 1], [0, 1, 0]]
D. [[1, 2, 1], [2, 4, 2], [1, 2, 1]]

Solution

  1. Step 1: Identify edge detection kernel characteristics

    Edge detection kernels emphasize differences by subtracting center pixel multiplied by 4 and adding neighbors.
  2. 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.
  3. Final Answer:

    [[0, 1, 0], [1, -4, 1], [0, 1, 0]] -> Option C
  4. Quick Check:

    Laplacian kernel = edge detection [OK]
Hint: Laplacian kernel has center negative, neighbors positive [OK]
Common Mistakes:
  • Choosing averaging or sharpening kernels for edge detection
  • Ignoring negative center value in kernel
  • Confusing kernel purpose
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