0% found this document useful (0 votes)
51 views42 pages

TP1 Final Report IP

The document summarizes the results of exercises performed to analyze properties of the Fourier transform on various images. Key findings include: 1) The Fourier transform of a sine wave places a Dirac function at frequencies corresponding to the sine wave parameters. 2) The Fourier transform of a gate function is a sinc function centered at the origin. 3) The Fourier transform of a Gaussian filter is another Gaussian filter with reduced bandwidth inversely proportional to the standard deviation. 4) The Fourier transform of a uniform average filter is a Dirac delta function at the origin, while the Fourier transform of a Laplacian filter shows high frequencies are emphasized.

Uploaded by

pablo orellana
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)
51 views42 pages

TP1 Final Report IP

The document summarizes the results of exercises performed to analyze properties of the Fourier transform on various images. Key findings include: 1) The Fourier transform of a sine wave places a Dirac function at frequencies corresponding to the sine wave parameters. 2) The Fourier transform of a gate function is a sinc function centered at the origin. 3) The Fourier transform of a Gaussian filter is another Gaussian filter with reduced bandwidth inversely proportional to the standard deviation. 4) The Fourier transform of a uniform average filter is a Dirac delta function at the origin, while the Fourier transform of a Laplacian filter shows high frequencies are emphasized.

Uploaded by

pablo orellana
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/ 42

Image Processing

Group:

Pablo Orellana (20225753)

Calderon Wilder (20225780)

M1 E3A Lab 1
Exercise 1: Interpretation of frequency content
1. Calculate the centered Fourier transform of an image of your choice in grayscale.

clear all
% Load the image
image = imread('image4.jpg');
img_col = image;

% Convert the image to grayscale


if size(image, 3) == 3
image = rgb2gray(image);
end
imshowpair(img_col,image,"montage")
title('Image in color vs. Image in grayscale')

1
% Convert the image to double precision for Fourier transform
image = im2double(image);

% Calculate the Fourier transform of the image


f_image = fftshift(fft2(image)); %we use fftshift to center the FT

% Calculate the module of the Fourier transform


module = log(1 + abs(f_image));

% Display the module using imagesc


figure;
imagesc(module);
colormap('jet');
colorbar;
title('Fourier Transform of image');

2
2. Where are the low frequencies, the high frequencies, the coefficients having the greatest amplitude?
Comment on the spectral content. Test on several images. Comment according to the frequency content of the
images.

The low frequencies are located in the centre of the image, the high frequencies in the surroundings of
the image and for this image, the coefficients that carry the greatest amplitude are the ones located in low
frequencies having in (0,0) the highest value which is the sum of the image elements.

3. Find the average of the gray levels of the image using the Fourier transform.

We will use:

%average grey levels using Fourier Transform


fourierImage = fft2(image);
avg = abs(fourierImage(1,1)) / (size(image,1)*size(image,2));
fprintf('Average of Gray Levels of Image: %4f\n' , avg);

Average of Gray Levels of Image: 0.640823

3
Exercise 2: Properties of the Fourier Transform
Check the following properties of the discrete 2D Fourier transform on the image of your choice. When
necessary, be sure to create the appropriate images in terms of content but also in terms of size.

1. Linearity

% Load the image


image1 = imread('image3.jpg');
image2 = imread('image4.jpg');

% Convert the image to grayscale if it's in RGB format


if size(image1, 3) == 3
image1 = rgb2gray(image1);
end

if size(image2, 3) == 3
image2 = rgb2gray(image2);
end
% Convert the image to double precision for Fourier transform
image1 = im2double(image1);
image2 = im2double(image2);

image2 = imresize(image2, size(image1));

%Defining constants
A = 2;
B = 3;

% Apply the Fourier transform on the original images


f_image1 = fftshift(fft2(image1));
f_image2 = fftshift(fft2(image2));

f_images1 = A*f_image1 + B*f_image2;

% Apply linearity property of Fourier transform


images = A*image1 + B*image2;
f_images2 = fftshift(fft2(images));

dif = f_images1 - f_images2;


module_dif = log(1 + abs(dif));

figure;
imagesc(module_dif);
colormap jet;
colorbar;
title('Lineatiry: Proof Fourier Transform of difference');

4
We can say that the linearity property is demonstrated due to the fact that from the Fourier transform graph the
difference between the values of f_image1 and f_images2 is almost zero.

2. Rotation

% Define a rotation angle in degrees


angle = 60;

% Apply rotation property of Fourier transform using imrotate function


rotated_image = imrotate(image, angle) ;
f_rotated_image = fftshift(fft2(rotated_image));

% Display of the original and the rotated image in spacial domain


figure;
subplot(2, 2, 1);
imshow(image);
title('Original Image');

subplot(2, 2, 2);
imshow(rotated_image);
title('Rotated Image');

%Display of the original and the rotated image in frequency domain

5
subplot(2, 2, 3);
imagesc(log(1 + abs(f_image)));
colorbar;
title('Fourier Transform of Original Image');

subplot(2, 2, 4);
imagesc(log(1 + abs(f_rotated_image)));
colorbar;
title('Fourier Transform of Rotated Image');

The rotation property says that if an image is rotated, then its Fourier Tranform will be rotated too. This property
is shown on the graphics above.

3. Frequency Shift (Translation)

% Display the image translated and its Fourier transform


shifted_image = image.*(-1).^((1:size(image,1))'+(1:size(image,2)));
f_shifted_image = fftshift(fft2(shifted_image));

figure;
imagesc(log(1 + abs(f_shifted_image)));
colormap jet;
colorbar;
title('Fourier Transform of Shifted Image');

6
According to the Fourier Transform of Shifted Image, it is seen that the frequency values are translated from the
center to the edges.

The property that is revealed is translation.

The translation property comes from:

If and

And:

This is translation to the center.

Exercise 3: Fourier Transform of basic images

7
1. A sinusoid function

% Create a grid of x and y coordinates


[X, Y] = meshgrid(1:256, 1:256);

% Define different sinusoid frequencies (A and B)


frequencies = [0, 1, 128/256, 64/256, 32/256, 16/256];

% Loop through the frequencies and calculate the sinusoid images


for i = 1:length(frequencies)
A = frequencies(i);
for j = 1:length(frequencies)
B = frequencies(j);

% Generate sinusoid image using the given formula


image = 255 * (sin(2 * pi * (A * X + B * Y)) + 1) / 2;

% Calculate the Fourier transform of the image


f_image = fftshift(fft2(image));

% Display the original image and its Fourier transform


figure;
subplot(1, 2, 1);
imshow(image, []);
title(['Sinusoid Image: A = ' num2str(A) ', B = ' num2str(B)]);

subplot(1, 2, 2);
imagesc(log(1 + abs(f_image)));
colorbar;
title('Fourier Transform of Sinusoid Image');
end
end

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
From the analysis of the fourier transform of sine, it is seen that according to the values of A and B the dirac
function is placed in a certain frequency.

26
Fourier transform of the sine function given:

2. A gate function

% Create a gate function image (image of a rectangle)


gate_image = zeros(256, 256);
gate_image(96:160, 96:160) = 1;
% Calculate the Fourier transform of the gate function image
f_gate_image = fftshift(fft2(gate_image));

% Display the original gate function image and its Fourier transform
figure;
subplot(1, 2, 1);
imshow(gate_image);
title('Gate Function Image');

subplot(1, 2, 2);
imagesc(log(1 + abs(f_gate_image)));
colorbar;
title('Fourier Transform of Gate Function Image');

figure;
plot(abs(f_gate_image(floor(size(f_gate_image,1)/2),:)))
title('Side view of the fourier transform of sine')

27
It is seen that the fourier transform of the gate function is the sinc function with maximum value in the origin.

3. A Gaussian (fspecial)

% Create a Gaussian filter


gaussian_filter = fspecial('gaussian', [256 256], 20);

% Calculate the Fourier transform of the Gaussian filter


f_gaussian_filter = fftshift(fft2(gaussian_filter));

% Display the original Gaussian filter and its Fourier transform


figure;
subplot(1, 2, 1);
imshow(gaussian_filter, []);
title('Gaussian Filter');

subplot(1, 2, 2);
imagesc(log(1 + abs(f_gaussian_filter)));
colorbar;
title('Fourier Transform of Gaussian Filter');

28
figure;
plot(abs(f_gaussian_filter(floor(size(f_gaussian_filter,1)/2),:)))
title('Side view of the fourier transform of gauss function')

29
It is seen that the fourier transform of the gauss function is another gauss function, but with a bandwith reduced
and which is inversely proportional to the standard deviation of the gauss function in the spacial domain.

4. A Uniform average filter. Compare with Gaussian in terms of frequency content.

% Create a uniform average filter


uniform_average_filter = fspecial('average', [256 256]);

% Calculate the Fourier transform of the uniform average filter


f_uniform_average_filter = fftshift(fft2(uniform_average_filter));

% Display the original uniform average filter and its Fourier transform
figure;
subplot(1, 2, 1);
imshow(uniform_average_filter, []);
title('Uniform Average Filter');

subplot(1, 2, 2);
imagesc(log(1 + abs(f_uniform_average_filter)));
colorbar;
title('Fourier Transform of Uniform Average Filter');

30
figure;
[~,ind_max]=max(max(f_uniform_average_filter));

plot(abs(f_uniform_average_filter(ind_max,:)))

31
It is seen that the fourier transform of the uniform average filter is the delta dirac function centered in the origin.

5. A Laplacian filter (use fspecial).

% Create a Laplacian filter


laplacian_filter = fspecial('laplacian');

% Calculate the Fourier transform of the Laplacian filter


f_laplacian_filter = fftshift(fft2(laplacian_filter));

% Display the original Laplacian filter and its Fourier transform


figure;
subplot(1, 2, 1);
imshow(laplacian_filter)

subplot(1, 2, 2);
imagesc(log(1 + abs(f_laplacian_filter)));
colorbar;
title('Fourier Transform of Laplacian Filter');

32
figure;
plot(abs(f_laplacian_filter(floor(size(f_laplacian_filter,1)/2),:)))

33
It is seen that the fourier transform of the laplacian filter is a triangular function which has the minimum in the
origin.

Exercise 4: Fourier Transform and Convolution


Show that a convolution in the spatial domain is equivalent to a multiplication in the frequency domain.
Demonstrate this property with a Laplacian (as it was generated in the previous exercise). Comment on the
obtained result.

%Calculate the convolution


C = conv2(image1,laplacian_filter);
% Calculate the Fourier transform of the Laplacian filter
f_convolution = fftshift(fft2(C));

f_laplacian_filter_p = fftshift(fft2(laplacian_filter, size(image1,1), size(image1,2)));

%Calculate the multiplication of FTs in the frequency domain


M = f_image1.*f_laplacian_filter_p;

% Display the convolution and the multiplication in frequency domain

figure;
subplot(1, 2, 1);
imagesc(log(1 + abs(f_convolution)));
colorbar;

34
title('Fourier Transform of Conv in Spacial domain');

subplot(1, 2, 2);
imagesc(log(1 + abs(M)));
colorbar;
title('Multiplication in frequency domain');

According to the corresponding graphs plotted, it can be seen that the two graphs look alike. This is a
demonstration of how a convolution in the spatial domain is equivalent to a multiplication in the frequency
domain.

Exercise 5: Sampling

1. Calculate the Fourier transform of a pulse train of the period of your choice. To generate an image 256 × 256
with a pulse train of period 4 pixels:

%Generate the pulse train an calculate its fourier tranform


F = zeros (4,4);
F (1,1) = 1;
len_row_img1 = size(image1,1);
len_col_img1 = size(image1,2);
pulse_train = repmat (F, floor(len_row_img1/4), floor(len_col_img1/4));
pulse_train = [pulse_train, zeros(len_row_img1-rem(len_row_img1,4),rem(len_col_img1,4)); zeros(

35
f_pulse_train = fftshift(fft2(pulse_train));
figure;
imagesc(log(1 + abs(f_pulse_train)));
colorbar;
title('Fourier Transform of pulse train');

2. Sample the image of your choice then the UCSB image using the generated pulse train. Note: multiply the
image with the pulse train.

%Sampling
sampled_image = image1.*pulse_train;

3. Calculate the 2D DFT of the sampled image. Make the link with the theoretical results seen in class.

%Calculate the 2D DFT of the sampled image


f_sampled_image = fftshift(fft2(sampled_image));
% Display the fourier tranform
figure;
subplot(1, 2, 1);
imagesc(log(1 + abs(f_image1)));
colorbar;
title('Fourier Transform of image');

36
subplot(1, 2, 2);
imagesc(log(1 + abs(f_sampled_image)));
colorbar;
title('Fourier Transform of sampled image');

It is seen from the graph that the fourier tranform of a pulse train is another pulse train but in frequency domain
and between each pulse there is a distance of fs which is the sampling frequency or the inverse of the pulse
train period.

Sampling the image will give a result in frequency domain of the fourier tranform of the image repeated multiple
times through the frequency domain each sampling frequency fs.

4. Suggest a way to find the original image from the sampled image knowing that the sampling highlights the
phenomena of Aliasing. You are not asked to implement the proposed solution.

When an image is sampled, aliasing occurs when the sampling rate is insufficient to capture all the details in the
original image. Aliasing can result in distorted or "jagged" images that are difficult to interpret.

There are various anti-aliasing techniques that can be used to improve the quality of a sampled image,
including:

37
• Oversampling: This involves increasing the sampling rate of the image to reduce the aliasing effects. The
image is then down-sampled to its original resolution.

• Filtering: Applying a low-pass filter to the sampled image can remove high-frequency components that
cause aliasing. The filtered image is then down-sampled to the original resolution.

Exercise 6: Filtering in the frequency domain


1. Implement a low-pass filtering then a high-pass filtering on the image of your choice. What do these
two operations achieve? We will compare ideal filters with Butterworth filters, taking care to vary the cutoff

frequencies (0.25, 0.5, etc.). Reminder: , where n is the order of the filter, D0 the

frequency cutoff and

len_row = size(f_image1,1);
len_col = size(f_image1,2);
[u, v] = meshgrid(-floor(size(image1,2)/2):floor(size(image1,2)/2)+rem(size(image1,2),2)-1, -fl
D = sqrt(u.^2 + v.^2);
D0 = [10 30 50]; %Defining the cutoff frequencies
len_D0 = size(D0,2);

LPF = ones(len_row,len_col,len_D0);
H = zeros(len_row,len_col,len_D0);
n = 1;

for i=1:len_D0
%Ideal Low pass filter
LPF1 = LPF(:,:,i);
LPF1(D > D0(i)) = 0;
LPF(:,:,i) = LPF1;

%Butterworth Low pass filter


H(:,:,i) = 1./(1+(D./D0(i)).^(2*n));
end

%Filtering
for i=1:len_D0
f_image1_LPF = LPF(:,:,i) .* f_image1;
filtered_image1_LPF = real(ifft2(ifftshift(f_image1_LPF)));
figure;
filtered_image1_butter = real(ifft2(ifftshift(H(:,:,i).*f_image1)));
imshowpair(filtered_image1_LPF, filtered_image1_butter,'montage')
title('Filtered image Ideal LPF Vs. Filtered Image Butterworth LPF cutoff freq= '+string(D0

38
end

figure;
imshowpair(log(1 + abs(LPF(:,:,len_D0))), log(1+abs(H(:,:,len_D0))),'montage')
title('Fourier Transform Ideal LPF(Left) Vs. Fourier Transform Butterworth LPF(Right)')

39
The operation of low pass filtering attenuates components that have a high frequency in the image, leaving the
resting low-frequency components on the image, giving as a result a blurry image. The distintion between the
low frequencies and high frequencies is established by the cutoff frequency.

The difference one can arrive between the Butterworth and ideal low pass filter is that the Butterworth filter does
not supress the high frequencies at all. So, it makes the images less blurry than using the ideal low pass filter
but it is still able to supress the noise involved.

%High pass filter


HPF = ones(len_row,len_col,len_D0);

%Butterworth HPF filter


H = 1 - H;

for i=1:len_D0
%Ideal High pass filter
HPF1 = HPF(:,:,i);
HPF1(D <= D0(i)) = 0;
HPF(:,:,i) = HPF1;
end

%Filtering
for i=1:len_D0
filtered_image1_HPF = real(ifft2(ifftshift(HPF(:,:,i) .* f_image1)));
filtered_image1_butter = real(ifft2(ifftshift(H(:,:,i).*f_image1)));
figure;
imshowpair(filtered_image1_HPF, filtered_image1_butter,'montage')
title('Filtered image Ideal HPF Vs. Filtered Image Butterworth HPF cutoff freq= '+string(D0
end

40
figure;

imshowpair(log(1 + abs(HPF(:,:,len_D0))), log(1+abs(H(:,:,len_D0))),'montage')


title('Fourier Transform Ideal HPF(Left) Vs. Fourier Transform Butterworth HPF(Right)')

41
The operation of high pass filtering attenuates components that have a low frequency in the image, leaving
the resting high-frequency components on the image, giving as a result an image where one can notice the
edges enhanced. The distintion between the low frequencies and high frequencies is established by the cutoff
frequency.

The difference one can arrive between the Butterworth and ideal high pass filter is that the Butterworth filter
does not supress the low frequencies at all. So, it makes the images less blurry than using the ideal high pass
filter and it makes it difficult to detect the edges as we can se in the plotted results.

42

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy