TÍCH CHẬP
TÍCH CHẬP
REPORT
LINEAR ALGEBRA
__________________________________________________
CONVOLUTION, INNER PRODUCT AND IMAGE
PROCESSING REVISITED
__________________________________________________
Group information:
Group No: 1 Class: CC04 Completion date: 18/12/2023
2
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
Contents
Chapter 1: Introduction…. .......................................................................... 4
Overview .......................................................................................................... 4
Task.................................................................................................................. 4
Goal.................................................................................................................. 4
Chapter 2: Image, Convolution Theorem................................................... 5
2.1 Image ...................................................................................................................5
2.2 Convolution .........................................................................................................5
2.2.1 Convolution of vectors............................................................................................... 5
2.2.2 Convolution of matrices............................................................................................. 7
Chapter 3: Convolution filters ..................................................................... 8
3.1 Smoothing filters .................................................................................................8
3.2 Mean filters .........................................................................................................8
3.3 Gaussian smoothing ............................................................................................8
3.4 Sharpening filters ................................................................................................9
3.5 Sobel edge detector .............................................................................................10
3.6 Laplacian edge detection .............................................................................. 12
Chapter 4: Code and answer questions ...................................................... 14
4.1 Code to solve tasks ..............................................................................................14
4.2 Answering questions ...........................................................................................19
4.3 Command .................................................................................................... 22
Chapter 5: Discussion and result ................................................................. 23
Chapter 6: References .................................................................................. 28
3
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
Chapter 1 Introduction
Overview
In this report, we will look at the applications of matrix multiplication to the filtering of images.
The most prevalent operations in filtering image are blurring/smoothing, sharpening, and edge
detection. All of these so-called convolution filters. This report gives information about a
grayscale image and the applications of these filters on this image by using the colors of nearby
pixel. Following with the definition of convolution and inner product to understand the operation
between the image matrix and matrix filter. Tasks are done with coding on MATLAB with
explanation and figures.
Task
Load the grayscale image and save it as a matrix, then determine its size. Introduce some noise
into the image. Then use convolution to apply some operations on images. The operations consist
of smoothing, Gaussian blur, sharpening, Sobel horizontal and vertical in edge detection,
Laplacian edge detection. Also answers the questions in the tasks with appropriate evidence.
Goal
In this project we will look at the convolution of two matrices and its application to image
processing. During learning proof-based linear algebra undergraduate class, we are geared with
applied matrix theory in doing the project. The functions are built on MATLAB in order to solve
tasks, help students have elementary knowledge and skills in programming equations to solve
liner algebra problems. While equipped with linear algebra theory and programming, we also
have a chance to apply soft skills such as time management, problem-solving, teamwork skills.
4
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
2.2 Convolution
Definition:
If f and g are discrete functions, then 𝑓 ∗ 𝑔 is the convolution of f and g and is defined as:
(𝑓 ∗ 𝑔)(𝑥) = 𝑓(𝑢)𝑔(𝑥 − 𝑢)
Properties of convolution
If f, g, and h are functions and a is a constant, then:
𝑓 ∗ 𝑔 = 𝑔 ∗ 𝑓 (commutativity)
𝑓 ∗ (𝑔 ∗ ℎ) = (𝑓 ∗ 𝑔) ∗ ℎ (associativity)
𝑓 ∗ (𝑔 + ℎ) = (𝑓 ∗ 𝑔) + (𝑓 ∗ ℎ) (distributivity)
𝑎(𝑓 ∗ 𝑔) = (𝑎𝑓) ∗ 𝑔 = 𝑓 ∗ (𝑎𝑔) (associativity with scalar multiplication)
Note that it does not matter if g or f is the kernel, due to commutativity.
2.2.1 Convolution of vectors
If a function f ranges over a finite set of values a = 𝑎 𝑎 , … , 𝑎 ,
then it can be represented as vector [𝑎 𝑎 ⋯ 𝑎 ].
𝐶 = 𝑎 𝑏
5
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
Where u ranges over all legal subscripts for 𝑎 and 𝑏 , specifically 𝑢 = max(1, 𝑥 − 𝑛 +
1) … min (𝑥, 𝑚).
If we assume that the two vectors a and b have the same dimensionality, then the convolution c
is:
𝑐 =𝑎 𝑏
𝑐 =𝑎 𝑏 +𝑎 𝑏
𝑐 = 𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏
…
𝑐 =𝑎 𝑏 +𝑎 𝑏 + ⋯+ 𝑎 𝑏
…
𝑐 =𝑎 𝑏
Note that the sum for each component only includes those products for which the subscripts are
valid.
Example:
1 0
Assume 𝒂 = 0 , 𝒃 = 1 .
−1 2
Then
𝑎 1⋅0 0
⎡ 𝑎 𝑏 +𝑎 𝑏 ⎤ ⎡ 1⋅1+0⋅0 ⎤ ⎡ ⎤
⎢ ⎥ ⎢ ⎥ ⎢1⎥
𝑎 ∗ 𝑏 = ⎢𝑎 𝑏 + 𝑎 𝑏 + 𝑎 𝑏 ⎥ = ⎢1 ⋅ 2 + 0 ⋅ 1 + (−1)0⎥ = ⎢ 2 ⎥
⎢ 𝑎 𝑏 +𝑎 𝑏 ⎥ ⎢ 0 ⋅ 2 + (−1)1 ⎥ ⎢−1⎥
⎣ 𝑎 𝑏 ⎦ ⎣ (−1)2 ⎦ ⎣−2⎦
6
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
𝐶 = 𝑎 𝑏 ,
Example:
𝑏 𝑏 𝑏 𝑏 𝑏
𝑎 𝑎 𝑎
𝑏 𝑏 𝑏 𝑏 𝑏
Let 𝐴 = 𝑎 𝑎 𝑎 and 𝐵 =
𝑎 𝑎 𝑎 𝑏 𝑏 𝑏 𝑏 𝑏
𝑏 𝑏 𝑏 𝑏 𝑏
Then for 𝐶 = 𝐴 ∗ 𝐵, the entry 𝑐 =𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏 +
𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏 .
7
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
1 2 1
2 4 2 × 16
1
1 2 1
8
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
0 -1 0
-1 5 -1
0 -1 0
9
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
10
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
1 0 -1
Kernel = 2 0 -2
1 0 -1
… … … … … … … …
11
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
∂ f ∂ f
∇ f= +
∂x ∂y
The following mask can be used to approximate the Laplacian:
0 1 0
∇ ≈ 1 -4 1
0 1 0
12
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
Example:
13
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
%% Task 2
ImJPG_Noisy = double(ImJPG)+50*(rand(m,n)-0.5); % introduce some
noise into the image;
%% Task 3, 4
% Define the average kernels
Kernel_Average1 = [ 0 1 0 ; 1 1 1 ; 0 1 0 ]/5;
Kernel_Average2 = [ 1 1 1 ; 1 1 1 ; 1 1 1 ]/9;
%% Task 5
% Define the average kernels
FilteredImage1 = conv2(ImJPG, Kernel_Average1);
FilteredImage2 = conv2(ImJPG, Kernel_Average2);
% Convert the results back to the integer format
FilteredImage1_uint8 = uint8(FilteredImage1);
FilteredImage2_uint8 = uint8(FilteredImage2);
ImJPG_Average1 = conv2(Kernel_Average1,ImJPG_Noisy);
ImJPG_Average2 = conv2(Kernel_Average2,ImJPG_Noisy);
SmoothedImage1 = uint8(ImJPG_Average1);
SmoothedImage2 = uint8(ImJPG_Average2);
figure('Name','Original');
imshow(ImJPG, []);
title('Original Image');
% Display the first smoothed image
figure('Name','Kernel Average 1');
imshow(SmoothedImage1, []);
title('Smoothed Image (Kernel Average 1)');
% Display the second smoothed image
figure('Name','Kernel Average 2');
imshow(SmoothedImage2, []);
title('Smoothed Image (Kernel Average 2)');
[m1, n1] = size (ImJPG_Average1);
14
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
%% Task 6
Kernel_Gauss = [ 0 1 0 ; 1 4 1 ; 0 1 0 ]/8;
%% Task 7
% Perform the convolution using the function conv2 and Kernel Gauss
ImJPG_Gauss = conv2(Kernel_Gauss, ImJPG_Noisy);
SmoothedImage3 = uint8(ImJPG_Gauss);
% Display the result in a new window
figure('Name','Kernel Gauss');
imshow(ImJPG_Gauss, []);
title('Gauss Image');
%% Task 8
% Perform another convolution with the Gaussian kernel on the image
ImJPG Gauss
ImJPG_Gauss2 = conv2(Kernel_Gauss,ImJPG_Gauss);
SmoothedImage4 = uint8(ImJPG_Gauss2);
figure('Name','Kernel Gauss twice');
imshow(SmoothedImage4, []);
title('Gauss Image 2');
fprintf('The size of the matrix ImJPG_Gauss2:\n');
[m3, n3] = size (ImJPG_Gauss2);
fprintf('Number of rows:\n');
disp(m3);
fprintf('Number of columns:\n')
disp(n3);
15
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
%% Task 9
Kernel_Large = [ 0 1 2 1 0 ; 1 4 8 4 1 ; 2 8 16 8 2 ; 1 4 8 4 1 ; 0 1
2 1 0 ]/80;
% Apply the kernel Kernel Large to the matrix ImJPG
ImJPG_Large = conv2(Kernel_Large, ImJPG);
% Display the figure in a new figure window.
ImageLarge = uint8(ImJPG_Large);
figure('name','Kernel Large');
imshow(ImageLarge, []);
title('Large Kernel Image')
% Compare with applying Gaussian blur applied twice, performing the
% Gaussian blur twice blurs more than applying the kernel
Kernel_Large.
%% Task 10
Kernel_Sharp1 = [ 0 -1 0 , -1 5 -1 , 0 -1 0 ];
Kernel_Sharp2 = [ -1 -1 -1 , -1 9 -1 , -1 -1 -1 ];
%% Task 11
% Perform the convolution of the original image ImJPG with the kernel
Kernel Sharp1
ImJPG_Sharp1 = conv2(Kernel_Sharp1,ImJPG);
% Perform the convolution of the original image ImJPG with the kernel
Kernel Sharp2
ImJPG_Sharp2 = conv2(Kernel_Sharp2,ImJPG);
% Display the results in new figure windows
Imagesharp1 = uint8(ImJPG_Sharp1);
figure('name','Kernel Sharp 1');
imshow(Imagesharp1, []);
title('Kernel Sharp 1')
16
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
Imagesharp2=uint8(ImJPG_Sharp2);
figure('name','Kernel Sharp 2');
imshow(Imagesharp2, []);
title('Kernel Sharp 2')
%% Task 12
% Sobel 1 (vertical changes)
sobelKernel1 = [ -1, 0, 1 ; -2, 0, 2 ; -1, 0, 1 ];
% Sobel 2 (horizontal changes)
sobelKernel2 = [ -1, -2, -1 ; 0, 0, 0 ; 1, 2, 1 ];
%% Task 13
% Perform convolution with Sobel kernels
ImJPG_Sobel1 = conv2(ImJPG, sobelKernel1);
ImJPG_Sobel2 = conv2(ImJPG, sobelKernel2);
% Display the results in new figure windows
figure('name', 'Sobel vertical')
imshow(uint8(ImJPG_Sobel1));
title('Sobel vertical image')
figure('name', 'Sobel horiztontal')
imshow(uint8(ImJPG_Sobel2));
title('Sobel horizontal image');
%% Task 14
% Sum up the matrices to create a combined image
figure('name', 'combined image');
imshow(uint8(ImJPG_Sobel1+ImJPG_Sobel2));
title('Horizontal and vertical edges')
%% Task 15
% Define the Laplace kernel as a sum of two discrete second-order
partial derivatives
Kernel_Laplace = [ 0 -1 0 ; -1 4 -1 ; 0 -1 0 ];
%% Task 16
ImGray = im2gray(ImJPG);
% Perform the convolution using conv2
ImJPG_Laplace = uint8(conv2(ImGray, Kernel_Laplace));
% Display the Laplacian-filtered image
figure('name', 'Laplacian-filtered image')
imshow(ImJPG_Laplace);
title('Laplacian-Filtered Image');
17
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
18
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
Explanation:
We have the property of convolution known as associativity of convolution in the Theorem
definition above (2.2) which is:
(𝑓 ∗ 𝑔) ∗ ℎ = 𝑓 ∗ (𝑔 ∗ ℎ)
We convolve an image I with Gaussian kernel (𝐾 ), denoted as:
𝐼∗𝐾
The image is convolved with the Gaussian kernel, resulting in a new image that has undergone
Gaussian blurring. Now if we convolve again this resultant image with the same Gaussian kernel,
it can be represented as:
(𝐼 ∗ 𝐾 )∗𝐾 =𝐾 ∗𝐼
Convolving an image with a Gaussian kernel and then convolving the result with the same
Gaussian kernel (𝐾 ) is equivalent to convolving the image directly with squared kernel
(𝐾 ).
# Note: This explanation does not mean it is exactly equal to each other because of arithmetic
and finite precision, rounding errors and precision limitations might cause slight differences in
the final result.
Question 6: What is the size of the matrix for this filter?
The size of the matrix for this filter is [5 5].
Question 7: Compare with applying Gaussian blur applied twice. Which one blurs more?
Before answering question 6, first take a look at what do blurring mean?
Blurring: To blur is to make something less clear or distinct. This could be interpreted quite
broadly in the context of image analysis - anything that reduces or distorts the detail of an image
might apply. When we blur an image, we make the colour transition from one side of an edge in
the image to another smooth rather than sudden. The effect is to average out rapid changes in
pixel intensity.
Answer: The larger blur area is likely to blur more extensively compared to applying Gaussian
blur twice when observing the resulting images side by side.
Explain it by compare the result of the filtered images Gaussian Blur applied twice and Large
Blur using Matlab:
Coding implemented to compare two filters’s result:
clear; clc;
ImJPG = imread('einstein.jpg'); % load the file einstein.jpg as a
matrix with mxn dimensions
ImJPG_Noisy = double(ImJPG)+50*(rand(m,n)-0.5); % introduce some
noise into the image;
% Gaussian blur kernel
Kernel_Gauss = (1 / 8) * [0 1 0;1 4 1;0 1 0];
% Apply Gaussian blur twice
Kernel_Gauss_Double = conv2(Kernel_Gauss, Kernel_Gauss);
ImJPG_Gauss2 = uint8(conv2(Kernel_Gauss_Double, ImJPG_Noisy));
19
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
20
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
4.3 Command
Numbe
Name Command Description Example
r
Specifies the format of
imread(filena ImJPG=imread(einstein.j
1 Imread the file with the
me) pg)
standard file extension
Returns the lengths of
2 Size size(A) the queried dimensions [m, n]=size(ImJPG)
of A separately
Converts the values
3 Double double(X) in X to double double(ImJPG)
precision.
Convert an array to an
8-bit array of type uint8 by
unsigned using
4 uint8(Y) uint8(ImJPG)
integer the uint8 function,
arrays stored as 1-byte (8-bit)
unsigned integers
Returns an sz1-by-...-
by-szN array of
rand(sz1,..,sz random numbers,
5 Random rand(3, 4)
N) where sz1,...,szN indic
ate the size of each
dimension.
Returns the two-
2D dimensional
6 conv2 C=conv2(A, B)
Convolution convolution of
matrices A and B.
Create a figure, and figure('Name','Gaussia
7 Figure figure specify n Filter');
the Name property
Displays the grayscale
image I in a
Display figure. imshow uses
8 imshow(I) the default display Imshow(ImJPG)
image
range for the image
data type and
optimizes figure, axes,
21
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
22
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
23
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
24
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
25
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
Figure 5.12: Sobel horizontal Image Figure 5.13: Horizontal and vertical
26
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
The topic creates opportunities for students to learn about applications of Linear Algebra in
image processing with different filters
With careful preparation and best efforts, group 01 have completed the assignment.
Through this large exercise, the group has:
✓ Know how to use solve convolution and matrix problems on MATLAB.
✓ Increase interest in the subject.
✓ Cultivate study and teamwork skills.
27
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra
Chapter 6 References
[1] https://thelifesciencesmagazine.com/digital-image-analysis/
[2] The University of Edinburgh, School of Informatics, Frank Keller, Lecture 15:
Convolutions and Kernels, February 23, 2012.
[3] https://www.dynamsoft.com/blog/insights/image-processing/image-processing-101-spatial-
filters-convolution/
[4] The University of Edinburgh, Spatial Filters – Mean Filters,
https://homepages.inf.ed.ac.uk/rbf/HIPR2/mean.htm
[5] https://blog.demofox.org/2022/02/26/image-sharpening-convolution-kernels/
[6] University of Auckland, Edge Detection-Sobel 2
https://www.cs.auckland.ac.nz/compsci373s1c/PatricesLectures/Edge%20detection-
Sobel_2up.pdf
[7] University of South Florida, Chapter 5, Edge Detection
https://www.cse.usf.edu/~r1k/MachineVisionBook/MachineVision.files/MachineVision_Chapt
er5.pdf
28