0% found this document useful (0 votes)
37 views28 pages

TÍCH CHẬP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views28 pages

TÍCH CHẬP

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

VIETNAM NATIONAL UNIVERSITY

HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY


FACULTY OF APPLIED SCIENCE
🙞···☼···🙜

REPORT
LINEAR ALGEBRA
__________________________________________________
CONVOLUTION, INNER PRODUCT AND IMAGE
PROCESSING REVISITED
__________________________________________________

Instructor: Phd. Dau The Phiet


Class: CC04
Group: 01

Ho Chi Minh City 18th December, 2023


Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

Group information:
Group No: 1 Class: CC04 Completion date: 18/12/2023

Name Student ID Task


Nguyen Gia Bao 2352096 Code for task 12, 13, 14
Le Gia Bao 2352090 Code and answer questions for task 9, 10, 11
Le Minh Tri Bao 2352091 Code for task 15, 16
Pham Chi Dang 2352253 Research theoretical information
Nguyen Quang Hung 2352438 Code and answer questions for task 4, 5
Dang Phan Trong Thuc 2353166 Write project report
Nguyen Thanh Vinh 2353332 Code and answer questions for task 6, 7, 8

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

Chapter 2 Image, Convolution Theorem


2.1 Image
A digital image consists of a set of elements called pixels. They are known as a picture elements
or image elements. An image is represented in the form of a matrix where each element contains
a single pixel. Each pixel is the smallest point on the image that takes on a specific opacity, color
or shade.
Gray scale images: In a grayscale image, the pixel is an integer value between 0 to 255. Zero is
entirely black, and 255 is white. Gray falls around 127 leading to the white color. These images
are also known as 8-bit color format images. Grayscale images are 2-dimensional arrays, so we
have one value to represent the pixel at each location.

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 [𝑎 𝑎 ⋯ 𝑎 ].

Definition: Convolution of vectors


If the functions f and g are represented as vectors
𝒂 = [𝑎 𝑎 ⋯ 𝑎 ] and 𝒃 = [𝑏 𝑏 ⋯ 𝑏 ], then 𝑓 ∗ 𝑔 is a vector
𝒄 = [𝑐 𝑐 ⋯ 𝑐 ] as follows:

𝐶 = 𝑎 𝑏

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

2.2.2 Convolution of matrices


Definition: Convolution for functions of two variables
If f and g are discrete functions of two variables, then 𝑓 ∗ 𝑔 is the convolution of f and g and is
defined as:

(𝑓 ∗ 𝑔)(𝑥, 𝑦) = 𝑓(𝑢, 𝑣)𝑔(𝑥 − 𝑢, 𝑦 − 𝑣)

We can regard functions of two variables as matrices with


𝐴 = 𝑓(𝑥, 𝑦), and obtain a matrix definition of convolution.

Definition: Convolution of matrices


If the functions f and g are represented as the 𝑛 × 𝑚 matrix A and the 𝑘 × 𝑙 matrix B, then 𝑓 ∗ 𝑔
is an (𝑛 + 𝑘 − 1) × (𝑚 + 𝑙 − 1) matrix C:

𝐶 = 𝑎 𝑏 ,

Where u and v range over all legal subscripts for 𝑎 and 𝑏 , .

Example:
𝑏 𝑏 𝑏 𝑏 𝑏
𝑎 𝑎 𝑎
𝑏 𝑏 𝑏 𝑏 𝑏
Let 𝐴 = 𝑎 𝑎 𝑎 and 𝐵 =
𝑎 𝑎 𝑎 𝑏 𝑏 𝑏 𝑏 𝑏
𝑏 𝑏 𝑏 𝑏 𝑏
Then for 𝐶 = 𝐴 ∗ 𝐵, the entry 𝑐 =𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏 +
𝑎 𝑏 +𝑎 𝑏 +𝑎 𝑏 .

7
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

Chapter 3 Convolution filters


3.1 Smoothing Filters
Image smoothing is a digital image processing technique that reduces and suppresses image
noises. In the spatial domain, neighborhood averaging can generally be used to achieve the
purpose of smoothing. Commonly seen smoothing filters include average smoothing, Gaussian
smoothing, and adaptive smoothing.

3.2 Mean filters (or average filters)


1 1 1
1 1 1 1
×
9× 1
1 1 1 9

Figure 1: 3x3 averaging kernel often used in


The idea of mean filtering is simply to replace each pixel value in an image with the mean
(‘average’) value of its neighbors, including itself. This has the effect of eliminating pixel values
which are unrepresentative of their surroundings. Mean filtering is usually thought of as a
convolution filter. Like other convolutions it is based around a kernel, which represents the shape
and size of the neighborhood to be sampled when calculating the mean. Often a 3×3 square
kernel is used, as shown in Figure 1.

3.3 Gaussian Smoothing


The Gaussian smoothing is a 2-D convolution operator that is used to blur image and remove
detail and noise. This filter is similar to the mean filter (or average filter) but it uses different
kernel that represents the shape of a Gaussian (bell-shaped) hump.
The average smoothing treats the same to all the pixels in the neighborhood. In order to reduce
the blur in the smoothing process and obtain a more natural smoothing effect, it is natural to
think to increase the weight of the template center point and reduce the weight of distant points.
So that the new center point intensity is closer to its nearest neighbors. The Gaussian template is
based on such consideration.

1 2 1
2 4 2 × 16
1

1 2 1

Figure 2: A 3x3 Gaussian smoothing

8
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

3.4 Sharpening filters


This is a convolution operation, sometimes called 'unsharp masking'. It amounts to subtracting a
blurred (low pass or mean-filtered) image from the original, and then enhancing the contrast
back to full range. It does do a high pass filter and then adds those high-frequency details to the
original image, thus boosting the high-frequency content. It’s doing an “Unsharp mask”. The
algorithm for sharpening an image is then:
1. Blur an image using whatever blur you wish (e.g., Box, Tent, Gaussian)
2. Subtract the blurred result from the original image to get high-frequency details.
3. Add the high-frequency details to the original image.
Algebraically, this can be expressed as:
image + (image – blurred)
or:
2 * image – blurred
Blurring is most commonly done by convolving an image with a low frequency kernel that sums
to 1. If we are assuming that path to blurring, we can actually build a sharpening kernel which
encodes the equation we just derived. For “image”, we’ll just use the identity matrix for
convolution which is all zeros except a 1 in the center. That gives us this:
2 * identity – blur
If we wanted to make a 3×3 box blur into a sharpening filter we would calculate it this way:

Here is an example when apply sharpen kernel

0 -1 0
-1 5 -1
0 -1 0

9
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

Figure 3: The sharpening effect


(original image on the left, image result on the right)

3.5 Sobel edge detector


The Sobel filter is used for edge detection. It works by calculating the gradient of image intensity
at each pixel within the image. It finds the direction of the largest increase from light to dark and
the rate of change in that direction. The result shows how abruptly or smoothly the image changes
at each pixel, and therefore how likely it is that that pixel represents an edge. It also shows how
that edge is likely to be oriented. The result of applying the filter to a pixel in a region of constant
intensity is a zero vector. The result of applying it to a pixel on an edge is a vector that points
across the edge from darker to brighter values.
The sobel filter uses two 3 x 3 kernels. One for changes in the horizontal direction, and one for
changes in the vertical direction. The two kernels are convolved with the original image to
calculate the approximations of the derivatives. If we define Gx and Gy as two images that
contain the horizontal and vertical derivative approximations respectively, the computations are:

Where A is the original source image.


The x coordinate is defined as increasing in the right-direction and the y coordinate is defined as
increasing in the down-direction.
To compute Gx and Gy we move the appropriate kernel (window) over the input image,
computing the value for one pixel and then shifting one pixel to the right. Once the end of the
row is reached, we move down to the beginning of the next row.

10
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

The example below shows the calculation of a value of Gx:

1 0 -1
Kernel = 2 0 -2
1 0 -1

a11 a12 a13 … b11 b12 b13 …

a21 a22 a23 … b21 b22 b23 …

a31 a32 a33 … b31 b32 b33 …

… … … … … … … …

Input image Output image


(b22 = a13 - a11 + 2a23 - 2a21 + a33 - a31)
Example:

Figure 4a. Apply Gx

(original image on the left, the result image on the right)

11
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

Figure 4b. Apply Gy

(original image on the left, the result image on the right)

Figure 4: The sobel edge detection

3.6 Laplacian edge detection


The edge detectors discussed earlier (sobel edge detection) computed the first derivative and, if
it was above a threshold, the presence of an edge point was assumed. This results in detection of
too many edge points. A better approach would be to find only the points that have local maxima
in gradient values and consider them edge points. This means that at edge points, there will be a
peak in the first derivative and, equivalently, there will be a zero crossing in the second derivative.
Thus, edge points may be detected by finding the zero crossings of the second derivative of the
image intensity. The Laplacian is the operator in two dimensions that correspond to the second
derivative.
The formula for the Laplacian of a function f (x, y) is:

∂ 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:

Figure 5: The Laplacian edge detection effect

13
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

Chapter 4 Code and answer questions


4.1 Code to solve tasks
%% Task 1
ImJPG = imread('einstein.jpg'); % load the file einstein.jpg as a
matrix with mxn dimensions
fprintf('The size of the original matrix ImJPG:\n');
[m, n] = size (ImJPG);
fprintf('Number of rows:\n');
disp(m);
fprintf('Number of columns:\n')
disp(n);

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

fprintf('The size of the matrices ImJPG Average1, ImJPG


Average2:\n');
fprintf('ImJPG Average1:\n');
fprintf('Number of rows:\n');
disp(m1);
fprintf('Number of columns:\n')
disp(n1);
fprintf('ImJPG Average2:\n');
[m2, n2] = size (ImJPG_Average2);
fprintf('Number of rows:\n');
disp(m2);
fprintf('Number of columns:\n')
disp(n2);

%% 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);

% A filter which is equivalent to applying Gaussian convolution twice


is convolving Gaussian kernel with itself.
G = conv2(Kernel_Gauss,Kernel_Gauss);

15
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

fprintf('Matrix for a filter equivalent to applying Gaussian


convolution twice:\n')
Image5 = conv2(G,ImJPG_Noisy);
ImageG = uint8(Image5);
figure('name','Equivalent matrix as when apply kernel gauss twice');
imshow(ImageG, []);
title('New matrix image')
% The size of the matrix for this filter
fprintf('Size of the new matrix that is equivalent to applying
Gaussian convolution twice:\n')
[mG, nG] = size(G);
fprintf('Number of rows:\n');
disp(mG);
fprintf('Number of columns:\n')
disp(nG);

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

4.2 Answering the questions


Question 1: Why are the fractions 1/5 and 1/9 necessary?
The fractions 1/5 and 1/9 in the average kernels Kernel_Average1 and Kernel_Average2 serve
to normalize the weights of the kernels. Normalization ensures that the sum of all the weights
in the kernel is equal to 1. This is important because the weight of a kernel element represents
the relative contribution of that element to the filtered output. If the weights are not normalized,
then the filtered output may not accurately reflect the original image.
In the case of Kernel_Average1, the sum of the weights before normalization is 5. Dividing each
weight by 5 normalizes the weights so that their sum is equal to 1. Similarly, in the case of
Kernel_Average2, the sum of the weights before normalization is 9. Dividing each weight by 9
normalizes the weights so that their sum is equal to 1.
Question 2: What is the size of the matrices ImJPG_Average1, ImJPG_Average2?
The size of the matrices ImJPG_Average 1 is [1026 770].
The size of the matrices ImJPG_Average 2 is [1026 770].
Question 3: What is the size of the original matrix ImJPG? Use the size function.
The size of the original image is [768 1024], an vertical image.
Question 4: Which filter blurs more? Why?
Kernel Size: The size of the kernel is a crucial factor in determining the amount of blurring
applied to an image. A larger kernel size captures a wider range of neighboring pixels, effectively
averaging out fine details and producing a smoother image. In this case, Kernel_Average2 has a
larger kernel size (3x3) compared to Kernel_Average1 (2x2), resulting in a more pronounced
blurring effect.
Averaging: The averaging process itself contributes to blurring by reducing the intensity
differences between pixels. When averaging a group of pixels, the overall intensity tends to
become more uniform, flattening the local contrast and reducing the appearance of fine details.
Since Kernel_Average2 involves more pixels in the averaging process, it has a stronger tendency
to suppress fine details and create a smoother image.
Edge Detection: Edges are crucial elements in defining the structure and outline of an image. By
averaging over a wider area, Kernel_Average2 effectively averages out the differences between
pixels on either side of an edge, making the edge appear less pronounced. This blurring effect is
particularly noticeable in the smoothed image using Kernel_Average2, where edges appear less
defined and less sharp.
In summary, the combination of a larger kernel size and a more extensive averaging process
makes Kernel_Average2 more effective at blurring the image, resulting in a smoother and less
detailed representation.

Question 5: Devise a matrix for a filter which is equivalent to applying Gaussian


convolution twice
The matrix is equivalent to applying Gaussian convolution twice is convolve the kernel
Gaussian_Kernel with itself.

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

% Larger blur area kernel


Kernel_Large = (1 / 80) * [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];
% Apply larger blur area
ImJPG_Large = uint8(conv2(Kernel_Large, ImJPG));
% Display images
figure;
% Display Gaussian blur applied twice
subplot(1, 2, 1);
imshow(ImJPG_Gauss2);
title('Gaussian Blur Applied Twice');
% Display larger blur area
subplot(1, 2, 2);
imshow(ImJPG_Large);
title('Larger Blur Area');

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

and image object


properties
Adds the specified title
Add title to the current axes or title('My Title')
9 title
standalone
visualization
Command displays
Displays fprintf('Hello');
10 fprintf(text) formatted text centered
variable text
on the icon
Displays the value of [m, n] = [3, 4]
Display
variable X without
11 value of disp(X) disp(m)
printing the variable
variable
name disp(n)

22
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

Chapter 5 Discussion and result


Results when running the code on Matlab to solve the above problem:

Figure 5.1: Screenshot of command window from MATLAB program

23
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

Figure 5.2: Orginal Image Figure 5.3: Kernel Average 1

Figure 5.4: Kernel Average 2 Figure 5.5: Gauss Image

24
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

Figure 5.6: Gauss Image 2 Figure 5.7: New matrix Image

Figure 5.8: Large Kernel Image Figure 5.9: Kernel Sharp 1

25
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

Figure 5.10: Kernel Sharp 2 Figure 5.11: Sobel vertical Image

Figure 5.12: Sobel horizontal Image Figure 5.13: Horizontal and vertical

26
Applied Mathematics Department, FAS, HCMUT Matlab Projects – Linear Algebra

Figure 5.14: Laplacian Filter

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

You might also like