0% found this document useful (0 votes)
15 views41 pages

CH 5 Fourier Transform II

The document discusses the Discrete Fourier Transform (DFT) and its applications in digital image processing, particularly focusing on filtering in the frequency domain. It covers various types of filters, including lowpass, highpass, and notch filters, and explains the relationship between convolution and multiplication in frequency filtering. Additionally, it provides MATLAB code examples for implementing these filters and visualizing the results.

Uploaded by

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

CH 5 Fourier Transform II

The document discusses the Discrete Fourier Transform (DFT) and its applications in digital image processing, particularly focusing on filtering in the frequency domain. It covers various types of filters, including lowpass, highpass, and notch filters, and explains the relationship between convolution and multiplication in frequency filtering. Additionally, it provides MATLAB code examples for implementing these filters and visualizing the results.

Uploaded by

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

Introduction to Digital Image

Processing

Ho Dinh Duan
HCMC – 2020/22
Ch 5

Fourier Transform II
Contents

• Filtering in the Frequency Domain


• The Ideal, Butterwood and
Gaussian kernels
Recall: DTF in two dimensions
• The Discrete Fourier Transform (DFT) in two dimension takes
a matrix f(x,y) of size M x N as input (as in the case of a digital
image), and outputs a matrix F(u,v) of complex numbers, of
the same size

𝑀 −1 𝑁 −1
𝑥𝑢 +𝑦𝑣
𝐹 ( 𝑢,𝑣 )= ∑ ∑ 𝑓 ( 𝑥, 𝑦 ) exp ⁡{−2𝜋 𝑖( ¿ )}¿
0 0 𝑀 𝑁

and the inverse DFT


𝑀 − 1 𝑁 −1
1 𝑥𝑢 + 𝑦𝑣
𝑓 (𝑥 , 𝑦 )=
𝑀𝑁
∑∑ 𝐹 ( 𝑢, 𝑣 ) exp ⁡{2 𝜋 𝑖( ¿
𝑀 𝑁
)}¿
0 0

The coefficient (1/MN) is a scaling factor


Discrete Fourier transform: A summary
Convolution: a crucial tool
accompanying Fourier

Convolution

with where the kernel

and the Convolution theorem


Convolution vs Correlation

Another process that relates the spatial and frequency domains, that is
correlation.
By definition, the correlation of two function f(x,y) and g(x,y) is defined
as the product of the conjugate of Fourier transform of f(x,y) and the
Fourier transform of g(x,y):
F*(u,v) G(u,v),
where F(u,v) and G(u,v) are the Fourier transforms of f and g, respectively.
An important application of the correlation is for template matching,
where the problem is to find the the closest match between an unknown
image and a set of known images. One way to obtain this is by finding the
correlation between the unknown and each of the known image, where the
image that gives largest correlation value is the solution. (this is also
related to the problem of pattern recognition).
Filtering in the Frequency Domain - the Ideas
Filtering in the frequency domain involves the
relationship between convolution (in the spatial
domain) and multiplication (in the frequency domain)
• The following Convolution Theorem shows an
interesting relationship between the spatial domain
and frequency domain:

where

Conversely
Solving spatial problems in the
Frequency Domain

Note: the Fourier transform is sometimes called “Integral transform”


An example of image enhancement based
on frequency domain

(a) Original image

(b) Filtered Fourier


transform of (a): the outer
part has been truncated
(filtered out certain
frequencies)

(a) Enhanced image: certain


noise was removed
Some properties of the DFT
• Similarity: the forward and inverse transforms
are very similar, except for the scale factor 1/MN
and the negative sign in the exponent of the
forward transform
• Separability:
[
𝑒𝑥𝑝 2 𝜋 𝑖 (
𝑥𝑢 𝑦𝑣
+
𝑀 𝑁 ]
) =𝑒𝑥𝑝
[ ]
𝑥𝑢
𝑀
.𝑒𝑥𝑝
[ ]
𝑦𝑣
𝑁

which means that the operation can be performed


independently with the first only depends on yv and
the second only on xu, and this saves computing
time significantly
Some properties of the DFT
• Linearity:

This property is very useful in dealing with image degradation due to


noise, which can be modeled as a sum of the original (non-degraded)
image and the noise image, and usually the DFT of the noise is easy to
remove (in sense of frequencies)
Some properties of the DFT
• The Convolution Theorem: we have seen in a
previous slide
• The DC coefficients: the value F(0,0) of the
DFT is called the DC coefficient (“Direct
Current”, in contrast of the “AC”, alternative
current), which is equal to the sum of all terms in
the input matrix (Prove this?), and does not
depend on the frequencies of other components,
whose frequencies vary from low to high (and
very high, towards the higher terms of the
Fourier series)
Some properties of the DFT
• Shifting: for purpose of display, it is convenient
to have the DC component at the center of the
image (Matlab command: fftshift())
• Conjugate symmetry: That is,
ℱ ( 𝑢, 𝑣 )=ℱ ( −𝑢+𝑝𝑀 ,− 𝑣+𝑞𝑁 ) , 𝑓𝑜𝑟 𝑎𝑛𝑦 𝑖𝑛𝑡𝑒𝑔𝑒𝑟𝑠 𝑝𝑎𝑛𝑑 𝑞

This means that half of the transform is a mirror


image of the conjugate of the other half
Displaying the transformed
image
Displaying the Fourier transform
• One trouble is that the magnitude of the DC
component is in general much larger than other
values, implying an effect that the picture of the
transform resembles a single white dot
surrounded by black or darker pixels (the true
spectrum)
• One way to overcome this is to use the logarithm
of the |F(u,v)|:
Log(1+|F(u,v)|)
Displaying the Fourier transform

The “Cameraman” and its DFT


(source: Alasdair McAndrew, 2004)
Displaying the Fourier Spectrum
– Matlab example
%Create a black 30x30 image
f=zeros(30,30);
%With a white rectangle in it.
f(5:24,13:17)=1;
imshow(f,'InitialMagnification', 'fit')
%Calculate the DFT.
F=fft2(f);
%There are real and imaginary parts to F.
%Use the abs function to compute the magnitude
%of the combined components.
F2=abs(F);
figure, imshow(F2,[], 'InitialMagnification','fit')
Displaying the Fourier Spectrum
(cont’d)
%To create a finer sampling of the Fourier
transform,
%you can add zero padding to f when
computing its DFT
%Also note that we use a power of 2, 2^256
%This is because the FFT -Fast Fourier
Transform
%is fastest when the image size is a power of 2
F=fft2(f, 256, 256);
F2=abs(F);
figure, imshow(F2, [])
Displaying the Fourier Spectrum
(cont’d)

%The zero-frequency coefficient


is displayed in the
%upper left hand corner. To
display it in the center,
%you can use the function fftshift.
F2=fftshift(F);
F2=abs(F2);
figure,imshow(F2,[])
Displaying the Fourier Spectrum
(cont’d)

%In Fourier transforms, high


peaks are so high that they
hide details;
%Reduce contrast with the
log function.
F2=log(1+F2);
figure,imshow(F2,[])
Displaying the Fourier Spectrum
(cont’d)

To get the results shown in the last image of the table, you can
also combine MATLAB calls as in:
f=zeros(30,30);
f(5:24,13:17)=1;
F=fft2(f, 256,256);
F2=fftshift(F);
figure,imshow(log(1+abs(F2)),[])
Notice in these calls to imshow(), the second argument is empty
square brackets. This maps the minimum value in the image to
black and the maximum value in the image to white.
Filtering with Frequencies
Frequency domain filters
• From the previous materials, it means we can
perform linear spatial filters as a simple
component-wise multiply in the frequency
domain.
• This suggests that we could use Fourier
transforms to speed up spatial filters. This only
works for large images that are correctly padded,
where multiple transformations are applied in the
frequency domain before moving back to the
spatial domain.
Padding an image
• When applying Fourier transforms padding is
very important. Note that, because images are
infinitely tiled in the frequency domain, filtering
produces wraparound artefacts if you don't zero
pad the image to a larger size.
• The paddedsize() function calculates a correct
padding size to avoid this problem. The
paddedsize function can also help optimize the
performance of the DFT by providing power of 2
padding sizes.
Basic steps in Frequency
filtering using Matlab
1. Obtain the padding parameters using function paddedsize:
PQ=paddedsize(size(f));
2. Obtain the Fourier transform of the image with padding:
F=fft2(f, PQ(1), PQ(2));
3. Generate a filter function, H, the same size as the image(*)
4. Multiply the transformed image by the filter:
G=H.*F;
5. Obtain the real part of the inverse FFT of G:
g=real(ifft2(G));
6. Crop the top, left rectangle to the original size:
g=g(1:size(f, 1), 1:size(f, 2));

This step is the topic of Filter Design; you can either use the Matlab built-in
(*)

functions for filter design or your own design


Example: Applying the Sobel filter in the Frequency Domain with Matlab

• • Frequency Domain filtering


Spatial Domain filtering
%Create the Frequency Filtered Image
%Create the Spatial Filtered Image f = imread('entry2.png');
f = imread('entry2.png'); h = fspecial('sobel'); PQ =
paddedsize(size(f)); F = fft2(double(f),
h = fspecial('sobel');
PQ(1), PQ(2)); H = fft2(double(h), PQ(1),
sfi = imfilter(double(f),h, 0, 'conv'); PQ(2)); F_fH = H.*F; ffi = ifft2(F_fH); ffi =
%Display results (show all values) ffi(2:size(f,1)+1, 2:size(f,2)+1);
figure,imshow(sfi, []); %Display results (show all values)
%The abs function gets correct figure, imshow(ffi,[])
magnitude %The abs function gets correct magnitude
%when used on complex numbers %when used on complex numbers
sfim = abs(sfi); ffim = abs(ffi);
figure, imshow(sfim, []); figure, imshow(ffim, []);
%threshold into a binary image %threshold into a binary image
figure, imshow(sfim > 0.2*max(sfim(:))); figure, imshow(ffim > 0.2*max(ffim(:)));

You will notice that both approaches result in a similar looking, but usually the
frequency filtering got a finer/slimmer result (see next slide)
Original image Spatial Sobel filtered frequency filtered (complex)

50 50 50
100 100 100
150 150 150
200 200 200
250 250 250
300 300 300
350 350 350
400 400 400
450 450 450

100 200 300 400 500 600 100 200 300 400 500 600 100 200 300 400 500 600

freq filtered (magnitude) freq filtered (binarized)

50 50
100 100
150 150
200 200
250 250
300 300
350 350
400 400
450 450

100 200 300 400 500 600 100 200 300 400 500 600
Common frequency
domain filters
Frequency Domain specific
filters
You can also create filters directly in the
frequency domain. There are three
commonly discussed filters in the frequency
domain:
– Lowpass filters, sometimes known as
smoothing filters
– Highpass filters, sometimes known as
sharpening filters
– Notch filters, sometimes known as band-stop
filters
Lowpass filters
• Lowpass filters:
– create a blurred (or smoothed) image
– attenuate the high frequencies and leave the low frequencies
of the Fourier transform relatively unchanged

• Three main lowpass filters are discussed


1. Ideal lowpass filter (ILPF)
2. Butterworth lowpass filter (BLPF)
3. Gaussian lowpass filter (GLPF)
Ideal filter

In the formulae, D0 is a specified


nonnegative number. D(u,v) is the
distance from point (u,v) to the
center of the filter
Matlab codes for creating an Ideal filter
Ideal filter in the frequency domain (disk)

50

100

150

R=64; % disk radius 200

L=512; % size of image 250

300

350

a=double(zeros(L,L)); 400

for k=1:L 450

500

for l=1:L 50 100 150 200 250 300 350 400 450 500

if (k-L/2)^2+(l-L/2)^2<R^2
a(k,l)=1;
end
end
end
imshow(a); title('Ideal filter in
the frequency domain (disk)’);
mesh(a)
Butterworth filter
Matlab codes for the Butterworth filter
% create a Butterworth filter, zero's Butterworth filter in the frequency domain (n=1)

everywhere, except for a disk 50

% at center of image with brightness 100

gradually decreasing outwards 150

% (dimming out!) 200

250

% define some constants; you can modify 300

350
them as you wish
400
D=64; % disk radius 450

L=512; % size of image 500

N=1; % the power of Butterworth filter, can 50 100 150 200 250 300 350 400 450 500

be any integer>0

H=double(zeros(L,L));
for k=1:L
for l=1:L

H(k,l)=1/(1+(sqrt((k-L/2)^2+(l-L/2)^2)/D))^
(2*N);
end
end
imshow(H); title(‘Butterworth filter in the
frequency domain (n=1)');
figure; mesh(H)
Gaussian filter
Highpass filters
• Highpass filters:
– sharpen (or shows the edges of) an image
– attenuate the low frequencies and leave the high
frequencies of the Fourier transform relatively
unchanged
• So far the filters mentioned here take values in the [0,1]
• The highpass filter (Hhp) is often represented by its
relationship to the lowpass filter (Hlp):

Note: an alternative is to take the inverse of the lowpass, but then there will be no
cut-off frequencies
An example of making highpass filter from
lowpass in the previous slide

Highpass filter in the frequency domain (inverse Butterworth)

50

100

150

200

250

300

350

400

450

500

50 100 150 200 250 300 350 400 450 500

Low values towards the center


Notch filters
• are used to remove repetitive "Spectral"
noise from an image
• are like a narrow highpass filter, but they
"notch" out frequencies other than the DC
component
• attenuate a selected frequency (and some
of its neighbors) and leave other
frequencies of the Fourier transform
relatively unchanged
Homework 5.1
5.1.1. Convert the Matlab code examples in this lecture
note to Python (and test them)
5.1.2. Take an image in your library, insert two kinds of
noise into it, design a Notch filter to denoise, and
implement with Python.
End of Chapter

You might also like