THE UNIVERSITY OF DANANG
UNIVERSITY OF SCIENCE AND TECHNOLOGY
Faculty of Advanced Science and Technology
MIDTERM
IMAGE PROCESSING
Instructor
Class
Student
: Prof. Ho Phuoc Tien
: 21ES
: Tran Dinh Bao
Le Dinh Bao Tin
Da Nang, 25๐กโ March, 2025
Tran Dinh Bao – Le Dinh Bao Tin
Midterm
MIDTERM - 03/25
Image Processing
Students:
1. Tran Dinh Bao
2. Le Dinh Bao Tin
Problem 1. Filtering in the frequency domain
Given a grayscale image, filter this image using an ideal low-pass filter. Vary the cut-off
frequency of the ideal low-pass filter and observe the results. What can we see in the output
images? Illustrate and try to explain the observed phenomena.
We now replace the ideal low-pass filter with the Butterworth and Gaussian low-pass filters
and repeat the above filtering. In these cases, are there any differences in the results
compared with those from the ideal low-pass filter? Show the results appropriately to illustrate
the comparisons.
1. Code
% Read and convert image to grayscale
img = im2double(rgb2gray(imread('images.jpg')));
[M, N] = size(img);
% Compute the FFT of the image
F = fftshift(fft2(img));
% Define cutoff frequencies
cutoff_frequencies = [10, 30, 60];
% Create filters and apply them
figure;
for i = 1:length(cutoff_frequencies)
D0 = cutoff_frequencies(i);
% Ideal Low-Pass Filter (ILPF)
H_ideal = create_ideal_lowpass(M, N, D0);
G_ideal = H_ideal .* F;
img_ideal = real(ifft2(ifftshift(G_ideal)));
% Butterworth Low-Pass Filter (BLPF)
H_butter = create_butterworth_lowpass(M, N, D0, 2);
G_butter = H_butter .* F;
img_butter = real(ifft2(ifftshift(G_butter)));
% Gaussian Low-Pass Filter (GLPF)
H_gaussian = create_gaussian_lowpass(M, N, D0);
G_gaussian = H_gaussian .* F;
img_gaussian = real(ifft2(ifftshift(G_gaussian)));
% Display results
subplot(3, 4, (i - 1) * 4 + 1), imshow(img), title('Original Image');
P a g e 2 | 13
Tran Dinh Bao – Le Dinh Bao Tin
Midterm
subplot(3, 4, (i - 1) * 4 + 2), imshow(img_ideal, []), title(['Ideal LPF (D0='
num2str(D0) ')']);
subplot(3, 4, (i - 1) * 4 + 3), imshow(img_butter, []), title(['Butterworth LPF
(D0=' num2str(D0) ')']);
subplot(3, 4, (i - 1) * 4 + 4), imshow(img_gaussian, []), title(['Gaussian LPF
(D0=' num2str(D0) ')']);
end
% Functions to create filters
function H = create_ideal_lowpass(M, N, D0)
[U, V] = meshgrid(-N/2:N/2-1, -M/2:M/2-1);
D = sqrt(U.^2 + V.^2);
H = double(D <= D0);
end
function H = create_butterworth_lowpass(M, N, D0, n)
[U, V] = meshgrid(-N/2:N/2-1, -M/2:M/2-1);
D = sqrt(U.^2 + V.^2);
H = 1 ./ (1 + (D ./ D0).^(2 * n));
end
function H = create_gaussian_lowpass(M, N, D0)
[U, V] = meshgrid(-N/2:N/2-1, -M/2:M/2-1);
D = sqrt(U.^2 + V.^2);
H = exp(-(D.^2) / (2 * D0^2));
end
2. Figures/Tables
Figure 1. Original image
P a g e 3 | 13
Midterm
Tran Dinh Bao – Le Dinh Bao Tin
Figure 2. Result images
3. Explanation
What can we see in the output images? Try to explain the observed phenomena
• The first row appears the most blurred, indicating the strongest low-pass filtering effect.
• The middle row has less blurring, suggesting a higher cutoff frequency, allowing more
details.
• The bottom row shows images that are nearly as sharp as the original, meaning very
little low-pass filtering was applied.
• This effect occurs because low-pass filtering removes high-frequency components
(sharp details), causing a smooth and blurred appearance.
• The more aggressive the filter (lower cutoff frequency), the more details are lost,
resulting in excessive blurring.
Are there any differences in the results compared with those from the ideal low-pass
filter?
P a g e 4 | 13
Midterm
Tran Dinh Bao – Le Dinh Bao Tin
• The three types of filters (Ideal, Butterworth, and Gaussian) differ in how they attenuate
frequencies.
• Ideal Low-Pass Filter:
− The ideal low-pass filter removes all high frequencies above a certain limit. This
creates a very sharp cut between the kept and removed frequencies. Because of
this, the filtered image may have ringing artifacts, which look like waves or
unwanted patterns around edges. These artifacts happen because the filter does
not smoothly transition between high and low frequencies. As a result, the image
may look unnatural, especially around sharp edges.
• Butterworth Low-Pass Filter:
− The Butterworth filter is different from the ideal filter because it does not cut off
frequencies suddenly. Instead, it reduces high frequencies gradually. This makes
the image blurrier, but it avoids strong ringing effects. The result is a smoother and
more natural-looking image, where edges are less sharp but do not have strange
patterns like in the ideal filter.
• Gaussian Low-Pass Filter:
− The Gaussian filter gives the smoothest blurring effect. It reduces high frequencies
in a very gentle way. Because of this, the image looks soft and natural, with no
ringing or sharp transitions. However, since it removes details smoothly, it may blur
the image more than needed, making it look too soft. This filter is often used in
image processing because it produces the most visually pleasing results.
Problem 2. Simulate a 2D mosaic image for a colour image (as in Alleysson’s paper). Show
the magnitude spectrum of this mosaic image. Compare this spectrum with the magnitude
spectrum of a natural image. Explain the eventual differences.
1. Code
% Load the input image
try
original_img = imread('kodim19.png');
rgb_img = im2double(original_img);
catch
error('Could not load image file.');
end
% Generate Bayer mosaic (RGGB pattern)
[rows, cols, ~] = size(rgb_img);
cfa_img = zeros(rows, cols);
% Sample R, G, B according to Bayer pattern RGGB
for i = 1:rows
for j = 1:cols
if mod(i, 2) == 1 && mod(j, 2) == 1
% Red at (odd, odd)
cfa_img(i, j) = rgb_img(i, j, 1);
elseif mod(i, 2) == 0 && mod(j, 2) == 0 % Blue at (even, even)
P a g e 5 | 13
Tran Dinh Bao – Le Dinh Bao Tin
Midterm
cfa_img(i, j) = rgb_img(i, j, 3);
else
% Green at (odd, even) or (even, odd)
cfa_img(i, j) = rgb_img(i, j, 2);
end
end
end
% Create a color visualization of the Bayer pattern
bayer_colored = zeros(rows, cols, 3);
for i = 1:rows
for j = 1:cols
if mod(i, 2) == 1 && mod(j, 2) == 1
% Red at (odd, odd)
bayer_colored(i, j, 1) = cfa_img(i, j);
elseif mod(i, 2) == 0 && mod(j, 2) == 0 % Blue at (even, even)
bayer_colored(i, j, 3) = cfa_img(i, j);
else
% Green at (odd, even) or (even, odd)
bayer_colored(i, j, 2) = cfa_img(i, j);
end
end
end
% Apply 2D Discrete Fourier Transform to the CFA image
S_cfa = fft2(cfa_img);
% Center the zero frequency component
S_cfa_shift = fftshift(S_cfa);
% Calculate the magnitude of the DFT
S_cfa_magnitude = abs(S_cfa_shift);
% Analyze the original RGB channels separately for comparison
S_r = fftshift(fft2(rgb_img(:,:,1)));
S_g = fftshift(fft2(rgb_img(:,:,2)));
S_b = fftshift(fft2(rgb_img(:,:,3)));
S_r_magnitude = abs(S_r);
S_g_magnitude = abs(S_g);
S_b_magnitude = abs(S_b);
% Display results
figure('Position', [100, 100, 1200, 400]);
subplot(1, 2, 1);
imshow(bayer_colored);
title('Bayer Mosaic Image', 'FontSize', 12);
subplot(1, 2, 2);
imagesc(log(1 + S_cfa_magnitude));
colormap jet;
colorbar;
title('Magnitude Spectrum of Mosaic Image', 'FontSize', 12);
figure('Position', [100, 100, 1200, 400]);
subplot(1, 2, 1);
imshow(rgb_img);
title('Original RGB Image', 'FontSize', 12);
P a g e 6 | 13
Midterm
Tran Dinh Bao – Le Dinh Bao Tin
subplot(1, 2, 2);
imagesc(log(1 + S_gray_magnitude));
colormap jet;
colorbar;
title('Magnitude Spectrum of Natural Image', 'FontSize', 12);
% Add overall title
sgtitle('Fourier Analysis of Bayer Color Filter Array (CFA) Pattern', 'FontSize', 14);
% Print information about the spectra
fprintf('CFA Spectrum - Max magnitude: %f, Mean magnitude: %f, Ratio: %f\n',
max(S_cfa_magnitude(:)), mean(S_cfa_magnitude(:)),
max(S_cfa_magnitude(:))/mean(S_cfa_magnitude(:)));
fprintf('Red Channel - Max magnitude: %f, Mean magnitude: %f, Ratio: %f\n',
max(S_r_magnitude(:)), mean(S_r_magnitude(:)),
max(S_r_magnitude(:))/mean(S_r_magnitude(:)));
% Add the green and blue channel information
fprintf('Green Channel - Max magnitude: %f, Mean magnitude: %f, Ratio: %f\n',
max(S_g_magnitude(:)), mean(S_g_magnitude(:)),
max(S_g_magnitude(:))/mean(S_g_magnitude(:)));
fprintf('Blue Channel - Max magnitude: %f, Mean magnitude: %f, Ratio: %f\n',
max(S_b_magnitude(:)), mean(S_b_magnitude(:)),
max(S_b_magnitude(:))/mean(S_b_magnitude(:)));
2. Figures/Tables
Figure 3. Original image – Kodim19.png
P a g e 7 | 13
Tran Dinh Bao – Le Dinh Bao Tin
Midterm
Figure 4. Mosaic Image and its Magnitude Spectrum Image
Figure 5. Original Image and its Magnitude Spectrum Image
3. Explanation
Compare the magnitude spectrum of mosaic image with the magnitude spectrum of a
natural image
• Magnitude Spectrum of Mosaic Image
− The magnitude spectrum of the mosaic image has many repeating patterns. These
patterns come from the Bayer filter, which captures only one color per pixel.
Because of this, some details are missing, and the image contains extra highfrequency signals. This creates a grid-like effect in the spectrum. The energy is
P a g e 8 | 13
Tran Dinh Bao – Le Dinh Bao Tin
Midterm
spread out in different directions, causing aliasing, which means some details in the
image may look incorrect or distorted.
• Magnitude Spectrum of Natural Image
− The magnitude spectrum of the natural image is smoother and more natural. Most
of the energy is in the low frequencies, which appear at the center of the spectrum.
This means the image has smooth changes in color and brightness. There are no
strong repeating patterns, and there is no aliasing. This makes the natural image
look more realistic and clear compared to the mosaic image.
Explain the eventual differences
• The Bayer mosaic pattern creates a repeating structure in the image. This pattern
causes clear peaks in the frequency spectrum. These peaks appear because the
image does not capture all colors at every pixel, leading to missing information in some
areas.
• A natural image has many different textures and edges. This gives it a smooth and
continuous range of frequencies. The frequency spectrum of a natural image does not
have strong repeating patterns like the mosaic image. Instead, it looks more evenly
spread out.
• The mosaic image has an aliasing effect because it does not capture all color details
at every point. This reduces the spatial resolution of each color channel and adds
unwanted high-frequency noise to the spectrum. Because of this, the mosaic image
may look less clear than the natural image.
Problem 3. Filtering in the spatial domain.
We consider the following filters.
๐น = [1
2
4
2
1]/10
1
1 2
๐บ=
4
10 2
[1]
1 2 4 2
1 2 4 8 4
๐ป=
4 8 16 8
100 2 4 8 4
[1 2 4 2
1
2
4
2
1]
Given a grayscale image I, we filter I according to the two following methods. “*” denotes the
convolution operation.
P a g e 9 | 13
Midterm
Tran Dinh Bao – Le Dinh Bao Tin
Method 1: ๐1 = ๐ผ ∗ ๐น; ๐2 = ๐1 ∗ ๐บ
Method 2: ๐3 = ๐ผ ∗ ๐ป
Compare ๐2 and ๐3 . Explain this comparison.
Which method should be used? Explain.
1. Code
% Read the grayscale image
I = im2double(imread('kodim19.png'));
if size(I, 3) == 3
I = rgb2gray(I); % Convert to grayscale if needed
end
% Define the filters
F = [1 2 4 2 1] / 10;
G = [1; 2; 4; 2; 1] / 10;
H = (1/100) * [1 2 4 2 1; 2 4 8 4 2; 4 8 16 8 4; 2 4 8 4 2; 1 2 4 2 1];
% Apply filtering using Method 1
Y1 = conv2(I, F, 'same'); % Convolve with F (horizontal)
Y2 = conv2(Y1, G, 'same'); % Convolve with G (vertical)
% Apply filtering using Method 2
Y3 = conv2(I, H, 'same'); % Convolve directly with H
% Display results
figure;
subplot(1,3,1), imshow(I, []), title('Original Image');
subplot(1,3,2), imshow(Y2, []), title('Filtered Image (Method 1)');
subplot(1,3,3), imshow(Y3, []), title('Filtered Image (Method 2)');
2. Figures/Tables
P a g e 10 | 13
Midterm
Tran Dinh Bao – Le Dinh Bao Tin
Figure 6. Grayscale Image
Figure 7. Images for Method 1 (left) and Method 2 (right)
P a g e 11 | 13
Midterm
Tran Dinh Bao – Le Dinh Bao Tin
3. Explanation
Method 1 (Y1 = I * F and Y2 = Y1 * G):
• Process: In this method, the image undergoes two convolutions: first, with the filter F,
and then with the filter G.
• Effect:
− Filter F: This filter is a 5x1 smoothing filter. It gives a weighted average of pixels
in the neighbourhood, thus reducing noise and smoothing the image. It smooths
both the edges and finer details.
− Filter G: This filter is a 1x5 filter. It is a weighted kernel that emphasizes the
central pixel while slightly smoothing the neighbouring pixels. When applied after
F, it further smooths the image but also reduces the sharpness of edges.
− Result: The result of this method is a more blurred image. The details and edges
are smoothed out more significantly because of the two layers of convolution
with smoothing filters.
Method 2 (Y3 = I * H):
• Process: In this method, the image undergoes a single convolution with the filter H,
which is a 5x5 smoothing filter.
• Effect:
− Filter H: This is a larger smoothing kernel compared to F or G, and it’s
normalized by a factor of 100. It has a higher weight for pixels in the centre,
which results in a greater blurring effect. Given its large size, it smooths out a
significant portion of the image, including both high and low frequencies. This
might make it useful for removing fine noise, but it can blur the image more
aggressively than the combination of F and G.
− Result: The result is an image where fine details and edges are heavily
smoothed, with a noticeable reduction in sharpness. The image looks softer and
less detailed due to the large filter size and the strong smoothing effect.
Comparison and Explanation:
• Blurring Effect:
− Y2 (Method 1) has a milder blurring effect because the two smaller filters F and
G are applied successively. While the smoothing effect is noticeable, the image
retains a bit more detail and texture compared to Y3 (Method 2).
− Y3 (Method 2) produces a much more blurred image due to the large filter H,
which has a stronger smoothing effect and can erase finer details more easily.
P a g e 12 | 13
Midterm
Tran Dinh Bao – Le Dinh Bao Tin
• Preservation of Edges:
− Y2 (Method 1) likely preserves edges better compared to Y3 (Method 2),
especially since the filter G is smaller and less aggressive in blurring. This
method retains more of the image structure and sharpness, which might be
useful in cases where edge preservation is important.
− Y3 (Method 2) will result in much softer edges, and fine textures are lost due to
the large kernel size of H. This can be a problem if you need to keep important
features sharp, such as edges in detailed objects.
Which Method Should Be Used:
• Method 1 is more appropriate if:
− You need to maintain edge sharpness and some level of detail.
− The goal is to reduce noise or smooth the image without losing too much
structure. This method provides a balanced blur effect.
• Method 2 is more suitable if:
− You need a stronger blur effect, such as when you want to remove large noise
or create a soft-focus effect.
− The image contains too much detail or fine noise, and you want to smooth it out
heavily, even if it results in the loss of finer textures and edges.
P a g e 13 | 13
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )