\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{{\kern-4mu}\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{\kern-4mu}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} \newcommand\pound{\unit{lbs}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{\kern-4mu}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{\kern-4mu}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Capacitance \newcommand\farad{\unit{F}} \newcommand\F{\farad} \newcommand\microfarad{\micro\farad} \newcommand\muF{\micro\farad} % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

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.

(5.7)#\[\begin{split}\begin{bmatrix} 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 255 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \end{bmatrix}\end{split}\]

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_size will 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:

  • image is a 2D NumPy array (grayscale).

  • kernel is a 2D odd-sized square NumPy array.

  • Zero-pad the image based on the kernel size using the pad_image function 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:

  1. Prompt the user for kernel_size.

  2. Call create_mean_kernel.

  3. Build the \(5\times5\) demo image shown in (5.7).

  4. Call apply_kernel using the demo image and the created kernel.

  5. Print:

    • kernel size in the form Kernel size: NxN

    • kernel 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.

Table 5.6 Test Cases#

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]]

Table 5.7 Deliverables#

Deliverables

Description

tp1_team_2_teamnumber.pdf

Flowchart(s) for this task.

tp1_team_2_teamnumber.py

Your completed Python code.