In this prac you will research, implement and test some image filtering opera
tions. Image filtering by convolution is a fundamental step in many computer
vision tasks and you will find it useful to have a firm grasp of how it works. For
example, later in the course we will come across Convolutional Neural Networks
(CNNs) which are built from convolutional image filters.
The main aims of the prac are:
• to understand the basics of how images are stored and processed in mem
• to gain exposure to several common image filters, and understand how
• to get practical experience implementing convolutional image filters;
• to test your intuition about image filtering by running some experiments;
• to report your results in a clear and concise manner.
This assignment relates to the following ACS CBOK areas: abstraction, design,
hardware and software, data and information, HCI and programming.
Follow the instructions in this Python notebook and the accompanying file
a1code.py to answer each question. It’s your responsibility to make sure your
answer to each question is clearly labelled and easy to understand. Note that
most questions require some combination of Python code, graphical output, and
text analysing or describing your results. Although we will check your code as
needed, marks will be assigned based on the quality of your write up rather than
for code correctness! This is not a programming test – we are more interested
in your understanding of the topic.
Only a small amount of code is required to answer each question. We will make
extensive use of the Python libraries
• numpy for mathematical functions
• skimage for image loading and processing
• matplotlib for displaying graphical results
• jupyter for Jupyter Notebooks
You should get familiar with the documentation for these libraries so that you
can use them effectively.
To get started, below is some setup code to import the libraries we need. You
should not need to edit it.
# Numpy is the main package for scientific computing with Python.
import numpy as np
#from skimage import io
# Imports all the methods we define in the file a1code.py
from a1code import *
# Matplotlib is a useful plotting library for python
import matplotlib.pyplot as plt
# This code is to make matplotlib figures appear inline in the
# notebook rather than in a new window.
plt.rcParams[‘figure.figsize’] = (10.0, 8.0) # set default size of plots
plt.rcParams[‘image.interpolation’] = ‘nearest’
plt.rcParams[‘image.cmap’] = ‘gray’
# Some more magic so that the notebook will reload external python modules;
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
Question 0: Numpy warm up! (5%)
Before starting the assignment, make sure you have a working Python 3 instal
lation, with up to date versions of the libraries mentioned above. If this is all
new to you, I’d suggest downloading an all in one Python installation such as
Anaconda. Alternatively you can use a Python package manager such as pip or
conda, to get the libraries you need. If you’re struggling with this please ask a
question on the MyUni discussion forum.
For this assignment, you need some familiarity with numpy syntax. The numpy
QuickStart should be enough to get you started:
Here are a few warm up exercises to make sure you understand the basics.
Answer them in the space below. Be sure to print the output of each question
so we can see it!
1. Create a 1D numpy array Z with 12 elements. Fill with values 1 to 12.
2. Reshape Z into a 2D numpy array A with 3 rows and 4 columns.
3. Reshape Z into a 2D numpy array B with 4 rows and 3 columns.
4. Calculate the matrix product of A and B.
5. Calculate the element wise product of 𝐴 and 𝐵𝑇 (B transpose).
You need to be comfortable with numpy arrays because that is how we store
images. Let’s do that next!
Question 1: Loading and displaying an image (10%)
Below is a function to display an image using the pyplot module in matplotlib.
Implement the load() and print_stats() functions in a1code.py so that the
following code loads the mandrill image, displays it and prints its height, width
def display(img, caption=”):
# Show image using pyplot
image1 = load(‘images/cat.jpg’)
Return to this question after reading through the rest of the assignment. Find 2
more images to use as test cases in this assignment for all the following questions
and display them below. Use your print_stats() function to display their height,
width and number of channels. Explain why you have chosen each image.
Your code to load and display your images here
Your explanation of images here