Apr 28, 2026 | 513 words | 5 min read
5.2.2. Task 2#
Learning Objectives#
Define what an image kernel is and explain how it is used in filtering.
Apply a sliding-window kernel operation with stride \(1\).
Use zero-padding to handle image boundaries during filtering.
Build reusable functions for kernel creation and kernel application.
Introduction#
This task is the first of two related image filtering tasks.
Here, you will focus on the core mechanics of kernel filtering: what a kernel is, how it moves across an image, and how each output pixel is computed from a local neighborhood. You will implement these ideas using a mean kernel so you can focus on the filtering process itself. In the next task, you will reuse this same workflow and swap in a Gaussian kernel.
Task Instructions#
Deliverable Reminder
Create a flowchart of your algorithm and save it as
tp1_team_2_teamnumber.pdf. Start your
program from a copy of the
ENGR133_Python_Template.py
template and name it
tp1_team_2_teamnumber.py.
You will write two functions, one to create a mean kernel and one to apply it, then demonstrate both on the \(5\times5\) test image below.
Create Mean Kernel Function#
Create a function named create_mean_kernel that takes one parameter,
kernel_size, and returns a normalized mean kernel as a 2D NumPy array of
floats.
Requirements:
kernel_sizewill be an odd positive integer (for example, \(3\) or \(5\)).The output kernel shape must be
(kernel_size, kernel_size).The output data type must be
float.All entries should have equal value.
The sum of all kernel entries must be \(1.0\).
Apply Kernel Function#
Create a function named apply_kernel that takes two parameters,
image and kernel, and returns a filtered image as a 2D NumPy
array of floats.
Requirements:
imageis a 2D NumPy array (grayscale).kernelis a 2D odd-sized square NumPy array.Zero-pad the image based on the kernel size using the
pad_imagefunction from Section 15.2.3.Move the kernel across the image with stride \(1\).
At each location, multiply element-wise between the kernel and the current image window, sum the products, and assign that value to the corresponding output pixel.
The returned output image must have the same shape as the input image.
Kernel movement
At each position the kernel is centered on the current output pixel. Extract the local window from the padded image, multiply element-wise by the kernel, sum all products, and store the result in the output image. This sliding-window pattern is the core operation you will reuse in the next task.
Main Function#
In your main function, you will need to do the following:
Prompt the user for
kernel_size.Call
create_mean_kernel.Build the \(5\times5\) demo image shown in (5.7).
Call
apply_kernelusing the demo image and the created kernel.Print:
kernel size in the form
Kernel size: NxNkernel sum in the form
Kernel sum: ...the kernel array
the filtered image array
Hint
Try np.array2string for help displaying arrays with format specifiers. See the
numpy.array2string
documentation
for more information.
Sample Output#
Test cases for kernel creation and kernel application workflow. Use the values in Table 5.6 below to test your program.
Case |
Kernel Size |
|---|---|
1 |
3 |
2 |
5 |
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_2_teamnumber.py Enter kernel size: 3 Kernel size: 3x3 Kernel sum: 1.000000 Kernel: [[0.111111 0.111111 0.111111] [0.111111 0.111111 0.111111] [0.111111 0.111111 0.111111]] Filtered image: [[ 2.333333 10.444444 12.777778 10.444444 2.333333] [10.444444 46.888889 57.333333 46.888889 10.444444] [12.777778 57.333333 70.111111 57.333333 12.777778] [10.444444 46.888889 57.333333 46.888889 10.444444] [ 2.333333 10.444444 12.777778 10.444444 2.333333]]
Case 2 Sample Output
$ python3 tp1_team_2_teamnumber.py Enter kernel size: 5 Kernel size: 5x5 Kernel sum: 1.000000 Kernel: [[0.04 0.04 0.04 0.04 0.04] [0.04 0.04 0.04 0.04 0.04] [0.04 0.04 0.04 0.04 0.04] [0.04 0.04 0.04 0.04 0.04] [0.04 0.04 0.04 0.04 0.04]] Filtered image: [[21.24 27.76 27.76 27.76 21.24] [27.76 36.28 36.28 36.28 27.76] [27.76 36.28 36.28 36.28 27.76] [27.76 36.28 36.28 36.28 27.76] [21.24 27.76 27.76 27.76 21.24]]
Deliverables |
Description |
|---|---|
tp1_team_2_teamnumber.pdf |
Flowchart(s) for this task. |
tp1_team_2_teamnumber.py |
Your completed Python code. |