Apr 28, 2026 | 719 words | 7 min read
5.2.3. Task 3#
Learning Objectives#
Reuse kernel-filtering fundamentals from the previous task in a Gaussian-filter setting.
Construct a normalized Gaussian kernel using a chosen \(\sigma\).
Apply Gaussian filtering to grayscale images using padding and sliding windows.
Explain why Gaussian blur is typically preferred over mean blur for this workflow.
Introduction#
In the previous task, you implemented the core kernel workflow: create a kernel, pad the image, slide the kernel, and compute weighted sums. In this task, you will keep that same pipeline and replace the mean kernel with a Gaussian kernel.
Why Blur First?#
Blurring helps suppress small pixel-level variation (noise-like detail) so larger shapes and boundaries become easier to analyze in later steps.
Why Gaussian Instead of Mean Blur?#
The mean filter gives equal weight to all values in the local window. This smooths the image, but can produce blocky behavior and remove too much structure.
A Gaussian filter still smooths the image, but it gives larger weights to values near the center of the kernel and smaller weights to values farther away. You can think of this as a weighted average where nearby pixels influence the result more than distant pixels.
Note
For easier viewing, we recommend using light mode for the following figures.
Start from the following small reference image:
Fig. 5.5 Original Black-White Grid#
Mean blur output:
Fig. 5.6 Mean Blur Output#
Gaussian blur output:
Fig. 5.7 Gaussian Blur Output#
Larger comparison:
Fig. 5.8 Large Black-White Grid#
Fig. 5.9 Large Mean Blur Output#
Fig. 5.10 Large Gaussian Blur Output#
Both methods blur the original image, so both reduce fine detail. The important difference is how strongly they blur: mean blur is usually more extreme because every pixel in the local window is weighted equally. Gaussian blur is typically less extreme, because center-near pixels get higher weight and farther pixels get lower weight, so more of the original structure is preserved.
Gaussian blur is therefore commonly preferred before feature extraction steps like edge detection, because it smooths noise while preserving larger structures.
Task Instructions#
Deliverable Reminder
Create a flowchart of your algorithm and save it as
tp1_team_3_teamnumber.pdf. Start your
program from a copy of the
ENGR133_Python_Template.py
template and name it
tp1_team_3_teamnumber.py.
You will write two functions, one to build a Gaussian kernel and one to apply it,
then use them to blur a user-selected grayscale image. Before you begin, create a
folder named ref_images in the same directory as your program and download the
reference images listed in Table 5.8 into it.
Reference Image |
Download Link |
|---|---|
Color Reference A |
|
Color Reference B |
Create Gaussian Kernel Function#
Fig. 5.11 Normal Curve#
Create a function named create_gaussian_kernel that takes one parameter,
\(\sigma\), and returns a normalized 2D Gaussian kernel as a NumPy array.
Use the 2D Gaussian formula:
Where:
\(x\) and \(y\) are relative offsets from the kernel center.
\(\sigma\) controls spread:
smaller \(\sigma\) gives a tighter blur
larger \(\sigma\) gives a wider blur
Determine kernel size with:
This keeps the kernel odd-sized and includes values out to about \(\pm 3\sigma\) from the center.
Requirements:
\(\sigma\) is a positive float.
The kernel must be square and odd-sized.
Normalize the kernel so the sum of all elements is \(1.0\).
Return a 2D NumPy array of floats.
Note
For this assignment, use \(\sigma = 1\) in main.
Gaussian Filter Function#
Create a function named gaussian_filter that takes two parameters,
image and \(\sigma\), and returns the Gaussian-blurred image.
Requirements:
imageis a 2D grayscale NumPy array.Build the kernel by calling
create_gaussian_kernel(sigma).Zero-pad the image based on kernel size.
Slide the kernel across the image with stride \(1\).
At each output location, compute the weighted sum of the local window.
Return a NumPy array with the same shape as the input image.
To determine padding width:
Note
Do not modify the original image in place. Write filtered results into a separate output array.
Main Function#
Note
You will need the load_img and rgb_to_grayscale functions developed
in Section 15.2.2. Copy them into your program.
In your main function, you will need to do the following:
Ask the user for an image filename (for example,
ref_col_a.png).Load the image from the
ref_imagesfolder usingload_img.Convert the image to grayscale using
rgb_to_grayscale.Apply
gaussian_filterwith \(\sigma = 1\).Display and save the blurred image.
Sample Output#
Test cases for Gaussian kernel creation and filtering workflow. Use the values in Table 5.9 below to test your program.
Case |
image_path |
|---|---|
1 |
ref_col_a.png |
2 |
ref_col_b.png |
Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.
Case 1 Sample Output
$ python3 tp1_team_3_teamnumber.py Enter the path to the image file: ref_col_a.png
Fig. 5.12 Case_1_ref_col_a_smooth.png#
Case 2 Sample Output
$ python3 tp1_team_3_teamnumber.py Enter the path to the image file: ref_col_b.png
Fig. 5.13 Case_2_ref_col_b_smooth.png#
Deliverables |
Description |
|---|---|
tp1_team_3_teamnumber.pdf |
Flowchart(s) for this task. |
tp1_team_3_teamnumber.py |
Your completed Python code. |