You are on page 1of 31

University of Technology and Education

Faculty of Electrical & Electronic Engineering

Lecture:
IMAGE PROCESSING
Chương 3:
Biến đổi Fourier

Ngo Ba Viet 1
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Fourier Transform
Giới thiệu
- Fourier Transform (FT): an image in the spatial is transformed to
consider in the frequency domain. One can use the FT in filtering
image or others
- FT of two-dimensional (2D)
- Properties of Discrete Fourier Transform
- Applications
• Extracting feature of image based on frequency
• Filtering noise
• Determining frequencies in an image
2
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Fourier Transform
Biến đổi Fourier rời rạc 1D

Hàm f thay đổi theo thời gian biểu diễn sóng âm

Biến đổi Fourier rời rạc của tín hiệu 1D:


1 𝑀−1
𝐹(𝑢) = ෌𝑥=0 𝑓(𝑥)𝑒 −𝑗2𝜋𝑢𝑥/𝑀 ; với u=0,1,2,…M-1
𝑀
Biến đổi Fourier ngược:
𝑀−1
𝑓(𝑥) = ෌𝑢=0 𝐹(𝑢)𝑒 𝑗2𝜋𝑢𝑥/𝑀 ; với x=0,1,2,…M-1 3
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Fourier Transform

4
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

Biến đổi Fourier rời rạc 2D


- Có thể xem hình ảnh là một hàm biến đổi theo không gian 2 chiều.
- Cường độ của điểm ảnh là một hàm số theo toạ độ trục tung và
trục hoành tương ứng với vị trí của điểm ảnh đó.
- Có thể xem ảnh như một cảnh có những gợn sóng nhấp nhô, với
chiều cao của cảnh tương ứng với giá trị của điểm ảnh.

5
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

Biến đổi Fourier rời rạc 2D


- Có thể biểu diễn ảnh thành tổng các sóng sine theo hai chiều.
z = a.sin(ux+vy)
+ với x và y là toạ độ của điểm trên mặt phẳng ảnh,
+ z là độ cao (hay cường độ) của sóng tại điểm đó,
+ a là biên độ (độ cao lớn nhất) của sóng,
+ u và v là số lần sóng lặp lại theo chiều x và y riêng biệt (tức tần số).

6
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

Biến đổi Fourier rời rạc 2D


Biến đổi Fourier rời rạc 2D của ảnh g(x, y) được thực hiện theo biểu
thức sau:

M 1 N 1 2 ux  2  vy 

Gu, v    g x, y e
j j
M
e N

x 0 y 0

với g(x,y) là ảnh số có kích thước M×N, u=0,1,…,M-1 và


v=0,1,…,N-1. G(u,v) được xem như là ảnh Fourier.

7
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

Biến đổi Fourier rời rạc 2D


Từ dạng Fourier G(u,v), ta thu được ảnh g(x,y) bằng hàm biến đổi
ngược Fourier rời rạc:
2  ux  2  vy 
1 M 1 N 1
g  x, y     
j j
G u , v e M
e N

MN u 0 v 0
Với x=0,1,…,M-1 và y=0,1,…,N-1. g(x,y)

8
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

Giả sử R(u,v) và I(u,v) biểu diễn các thành phần thực và phần ảo của G(u,v),
phổ biên độ Fourier được định nghĩa::
𝐺(𝑢, 𝑣) = 𝑅2 𝑢, 𝑣 + 𝐼 2 𝑢, 𝑣
Góc pha của của biến đổi được xác định như sau:
𝐼(𝑢, 𝑣)
𝜃(𝑢, 𝑣) = tan−1
𝑅(𝑢, 𝑣)
Phổ công suất được định nghĩa như là bình phương của biên độ:
𝑃 𝑢, 𝑣 = |𝐺(𝑢, 𝑣)|2 = 𝑅2 𝑢, 𝑣 + 𝐼 2 𝑢, 𝑣
Giá trị biến đổi tại gốc của miền tần số (nghĩa là hệ số G(0,0) ) được gọi là
thành phần một chiều DC của biến đổi Fourier. Thuật ngữ này có thể gọi là
thành phần có tần số bằng không.

9
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Ví dụ: Biến đổi Fourier của ảnh kích thước 4x4 sau:

0 0 0 0
0 1 1 0
g ( x, y )   
0 1 1 0
 
0 0 0 0

Sử dụng công thức Euler: e  j  cos  j sin

10
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

0 0 0 0
g(𝑥, 𝑦) = 0 1 1 0
0 1 1 0
0 0 0 0
Biến đổi Fourier của 𝑔 với việc tính toán từng giá trị cụ thể tại từng vị
trí, chẳng hạn như với 𝑢 = 1 và 𝑣 = 0.
1 1 2 2
−𝑗2𝜋 4 −𝑗2𝜋 4 −𝑗2𝜋 4 −𝑗2𝜋 4
G 1,0 = 𝑒 +𝑒 +𝑒 +𝑒
= −𝑗 − 𝑗 − 1 − 1
= −2 − 2𝑗

4 + 𝑗0 −2 − 𝑗2 0 + 𝑗0 −2 + 𝑗2
−2 − 𝑗2 0 + 𝑗2 0 + 𝑗0 2 + 𝑗0
G(u, v) =
0 + 𝑗0 0 + 𝑗0 0 + 𝑗0 0 + 𝑗0
−2 + 𝑗2 2 + 𝑗0 0 + 𝑗0 0 − 𝑗2
11
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Ví dụ 3.1: Biến đổi Fourier không dùng hàm có sẵn trong MATLAB
clear all;clc
a=zeros(4,4);
a(2:3,2:3)=1;
%a =[0 0 0 0; 0 1 1 0; 0 1 1 0; 0 0 0 0]
[M,N]=size(a)
for u=1:M % size(a,1)
for v=1:N% size(a,2)
for x=1:size(a,1)
for y=1:size(a,2)
phi=(2*pi*(u-1)*(x-1)+2*pi*(v-1)*(y-1))/4;
p(x,y)=complex(cos(phi),-sin(phi));
end
end
F(u,v)=sum(sum(p.*a));
end 12
end
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Central shift
𝑀
𝜀 𝑓(𝑥, 𝑦)(−1)(𝑥+𝑦) = 𝐹(𝑢 − , 𝑣 − 𝑁/2)
2

Trong kết quả của biến đổi DFT, gốc tọa độ ở góc trên bên trái của ma
trận tần số.
Trong các ứng dụng, gốc tọa độ này cần được dịch về trung tâm hình
chữ nhật tần số, bằng cách chuyển đổi vị trí các vùng trong kết quả.

13
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

14
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Central shift
Trong trường hợp kích thước ảnh không là số chẵn, ta có thể dịch vòng
điểm ảnh theo hàng, sau đó dịch vòng theo cột để dời gốc tọa độ về
trung tâm.

VD: Thực hiện dời gốc tọa độ về trung tâm cho ảnh DFT A

1 4 7 
A  3 9 2
6 5 8 

7 1 4 
8 6 5 
Ah  2 3 9  Ac  7 1 4
8 6 5 2 3 9

Dịch vòng theo hàng 1 đơn vị Dịch vòng theo cột 1 đơn vị 15
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

16
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
F=fft2(f)
This function will return image of FT with the 𝑀 × 𝑁 size, having data
arranged as in Fig. and the axis origin will be the upper left side of image and
the end period is at the frequency rectangular center.

(a) Fourier spectrum with 𝑀 × 𝑁 size and four quad angles of the period;
(b) Fourier spectrum with 𝑀 × 𝑁 size with the center origin of frequency.
This stectrum is obtain by multiplying the function 𝑓(𝑥, 𝑦) and −1 𝑥+𝑦
before using the FT.
17
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Some functions for Fourier transform:
F=fft2(f)
Amplitude spectrum of Fourier transform:
S=abs(F)
The center shift, Fc=fftshift(F)

Fig. (a). Original image; (b) Fourier spectrum with the center at the foue
quad angles; (c) Fourier spectrum shifted with the center at the coordinate
origin; (d) Fourier spectrum represented using log transform

CMR: −𝟏 𝒙+𝒚 = 𝒆𝒋𝝅(𝒙+𝒚) e  j  cos  j sin 


18
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
0 0 0 0
0 1 1 0
Ví dụ:
g ( x, y )   
0 1 1 0
 
0 0 0 0
Biến đổi Fourier của ảnh g(x,y) với center shift

4 −2 − 𝑗2 0 −2 + 𝑗2
−2 − 𝑗2 𝑗2 0 2
G(u, v) =
0 0 0 0
−2 + 𝑗2 2 0 𝑗2
0 0 0 0
0 2j -2 + 2j 2
Gc(u, v) = 0 -2 + 2j 4 2 – 2j
0 2 -2 – 2j 2j
19
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Functions: “fftshift” and “ifftshift” will
Ví dụ 3.2: Perform the FT of image
transfer the angle position into the
and calculate its Fourier spectrum.
original one.
F=ifftshift(Fc)
clear all;
The inverse FT
close all;
f=ifft2(F)
f=imread('TestDFT.tif');
F is the FT, f is image in the spatial
F=fft2(f);
domain. If the input of F is real, f is real.
S=abs(F);
However, in practice, function ifft2 often
Fc=fftshift(F);
has a small complex value, its result
Sc=abs(Fc);
will be complex, so we can use function
S2=log(1+abs(Fc));
to produce real values:
figure;
f=real(ifft2(F))
subplot(2,2,1);imshow(f);
subplot(2,2,2);imshow(S);
subplot(2,2,3);imshow(Sc);
subplot(2,2,4);imshow(S2);

20
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Bài tập:
Tính toán DFT của ảnh sau:
0 0 0 0
0 1 1 0
g ( x, y )   
0 1 1 0
 
0 0 0 0
Hướng dẫn:
Biến đổi Fourier rời rạc của một ảnh hai chiều (2-DFT) g(x, y) có thể
được viết lại theo dạng như sau

G  U * g *V T

j 2xu j 2yv
 
U ( x, u )  e M
; x, u  0 : M  1 V ( y, v)  e N
; y, v  0 : N  1
21
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Bài tập:
Đầu tiên, tìm ma trận U:

e 4 
 j2 00  j2 01  j2 02  j2 03
e 4
e 4
e 4

  j2 10  j2 11  j2 12  j2 13 

 e 4
U   j2 20
e 4 e 4 e 4 
e 4  j2 21
e 4
 j2 22
e 4 e 4 
 j2 23

  j2 30  j2 31  j2 32  j2 33 


 e 4 e 4 e 4 e 4 
Rút gọn U để biểu diễn ở dạng đại số các phần tử trong ma trận U, ta được
ma trận U như sau:
1 1 1 1
1  j  1 j 
U  
1  1 1  1 
 
1 j  1  j  22
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Bài tập:
Tương tự như tính U, ta tìm được ma trận V:

1 1 1 1
1  j  1 j 
V  
1  1 1  1 
 
1 j  1  j 
Suy ra:
1 1 1 1  0 0 0 0 1 1 1 1
1  j  1 j  0 1 1 0 1  j  1 j 
G  U * g *V   * *
1  1 1  1  0 1 1 0 1  1 1  1 
     
1 j  1  j  0 0 0 0 1 j  1  j 

23
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Bài tập:
Kết quả biến đổi Fourier của g(x,y):

 4  2  j2 0  2  j 2
 2  j 2 j2 0 2 
G 
 0 0 0 0 
 
 2  2 j 2 0  j2 

Ảnh biên độ của G:

 4 2 2 0 2 2
 
G 
2 2 2 0 2 
 0 0 0 0 
 
2 2 2 0 2 
24
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Bài tập:
Tính toán DFT của ảnh sau:

25
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Exercise

Verify the relationship of the DFTs of the following images

A B

26
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

Tạo chương trình con fft2_new.m để tìm biến đổi Fourier của ảnh.

function G=fft2_new(g) %create matrix U


% G is fourier tranform of g %U(x,u)=exp(-j*2*pi*x*u/M)
%=================== % x, u run from 0 to M-1
% Get size of matrix f U=[ ];
for x=0:M-1
M=size(g,1); % get number of rows for u=0:M-1
N=size(g,2) ; % get number of U(x+1,u+1)=exp(-
columns j*2*pi*m*p/M);
%=================== end
end

27
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

Tạo chương trình con fft2_new.m để tìm biến đổi Fourier của ảnh.

%=================== %===================
% create matrix V %Fourier transform
%V(y,v)=exp(-j*2*pi*y*v/N) G=U*g*V;
% y, v run from 0 to N-1
V=[ ];
for y=0:N-1
for v=0:N-1
V(y+1,v+1)=exp(-
j*2*pi*y*v/N);
end
end

28
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms
Tạo chương trình con ifft2_new.m để biến đổi Fourier ngược của ảnh

function g=ifft2_new(G) %===================


% g is invert fourier tranform %create matrix U
of G %U(m,p)=exp(-j*2*pi*x*u/M)
%=================== % x, u run from 0 to M-1
%Get size of matrix G U=[ ];
M=size(G,1); % get number of for x=0:M-1
rows for u=0:M-1
N=size(G,2) ; % get number of U(x+1,u+1)=exp(-
columns j*2*pi*x*u/M);
%=================== end
end

29
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

Kiểm tra kết quả trên Matlab sử dụng fft2 và ifft2

% Test and compare together G1 and G2, g1 và g2


g=[0 0 0 0; 0 1 1 0; 0 1 1 0; 0 0 0 0]
G1=fft2(g)
G2=fft2_new(g)
g1=ifft2(G1)
g2=ifft2_new(G1)

• MAGNITUDE(G) = SQRT( REAL(G)^2+IMAGINARY(G)^2 )

•PHASE(F) = ATAN( IMAGINARY(G)/REAL(G) )

30
University of Technology and Education
Faculty of Electrical & Electronic Engineering
Image Transforms

The End

31

You might also like