Lecture03 FT 2D
Lecture03 FT 2D
g=f*h g=fh
implies implies
G=FH G=F*H
and likewise
Convolution Theorem - 1-D Example 1
1 if x 0
Dirac function ( x)
0 otherwise
* =
Convolution Theorem - 1-D Example 2
* =
1
x
-0.5 0.5 -0.5 0.5 -1
f x g x F F f x F g x
~ 1 ~ ~
Convolution Theorem - 1-D Example 3
Constant Function g ( x) c
f x g x cf (x)
F u Gu cF (u)
=
~
F[c] c (u)
Convolution Theorem – 1-D Example 4
f (x)* g (x)
f(x)
* g(x)
=
1 1 1
0.8 0.8 0.8
0.6 0.6 0.6
0.4 0.4 0.4
0.2 0.2 0.2
00 50 100 00 50 100 00 50 100
x y 0
( x, y ) 0
-1
0 otherwise
-1
-0.5
-0.5
0 0
0.5 0.5
1 1 x
y
Point Sources and Delta Functions
Other properties of delta function (x, y):
( x, y ) ( x, y )
( x, y ) dx dy 1
g ( x, y ) ( x, y ) dx dy g (0,0)
g ( x, y ) ( x , y ) dx dy g ( , )
“sifting” property
The point spread function, which is the output image for an input
point source at the origin of the xy-plane, is denoted by h(x, y).
Point Sources and Delta Functions
Now let us consider an arbitrary input picture f (x, y). The picture can
be considered to be a linear sum of point sources:
f ( x, y ) f ( , ) ( x, y ) d d
In other words, the image f (x, y) is a linear sum of point sources located
at (, ) in the xy-plane with and ranging from -∞ to + ∞.
If we apply a linear operation O on the picture f(x,y), then by the linearity
of the operation, it means that the response to a sum of excitations is
equal to the sum of responses to each excitation:
O f ( x, y ) O f ( , ) ( x , y ) d d
f ( , )O ( x , y )d d
g ( x, y ) f ( x, y ) h( x, y )
f ( , )h( x , y ) d d g ( x, y )
Convolution of f and h
Point Sources and Delta Functions
(2-D Convolution Theorem)
A simple change of variables can write the previous convolution into:
g ( x, y ) f ( x , y )h( , ) d d
Since g ( x, y ) f ( x, y ) h( x, y ) g ( x, y ) f ( x, y )
1 x 2 y 2 1 / 16
h ( x, y )
0 elsewhere
Point Sources and Delta Functions
(2-D Convolution Theorem)
ex2_1_pointspreadfunction.m
f ( x, y ) h( x, y ) g ( x, y )
1 1
100
0.8 0.8
80
0.6 0.6
60
0.4 0.4
40
0.2 0.2 20
0 0 0
1 1
40
1 1
0.5 0.5 30 40
0 0 30
0 0 20
20
-0.5 -0.5 10 10
-1 -1 -1 -1
Point Sources and Delta Functions
(2-D Convolution Theorem)
ex2_1_pointspreadfunction.m
f ( x, y ) h( x, y ) g ( x, y )
5
50 50
10
100 100
15
150 20 150
25
200 200
30
250 35 250
40
300 300
45
350 350
100 200 300 400 10 20 30 40 100 200 300 400
F-1[F(u,v) . G(u,v)]
Same!
2-D Continuous Fourier Transform
2-D Fourier Transform: F ( u, v )
f ( x, y )e j 2 ( uxvy )dxdy
j 2 ( ux vy )
Inverse Fourier Transform: f ( x, y ) F ( u , v ) e dudv
1 | x | 1 / 2 and | y | 1 / 2 1/ 2 1/ 2
rect ( x, y )
j 2 ( ux vy )
F ( u, v ) e dxdy
0 elsewhere
1 / 2 1 / 2
sin u
1/ 2
j 2vy
The pattern itself has a spatial period e dy
of (u2 + v2)-1/2 along a direction that u 1 / 2
subtends an angle tan-1 (v/u) with the sin u sin v
x-axis. sin c(u, v )
u v
2-D Fourier Transform
RECT = fftshift(fft2(rect));
rect(x,y) RECT(u,v)
sinc(u, v)
0.9
500
0.8
0.7 400
0.6
0.5 300
0.4
200
0.3
0.2
100
0.1
0 0
-1 1 -10 10
-0.5 0.5 -5 5
0 0 0 0
0.5 -0.5 5 -5
10 -10
1 -1 y v
x u
2-D Fourier Transform
RECT = fftshift(fft2(rect));
rect(x,y) RECT(u,v)
sinc(u, v)
1
60
0.9
0.8 50
0.7
0.6 40
0.5
30
0.4
0.3 20
0.2
10
0.1
0 0
-1 1 -10 10
-0.5 0.5 -5 5
0 0 0 0
0.5 -0.5 5 -5
10 -10
1 -1 y v
x u
2-D Fourier Transform
%%% Example 2_2: 2D FT
ex2_2_2DFT.m
clear
N = 48;
%% rect(x,y): pointspreadfunction
rect = zeros(N,N);
rect(N/4+1:N*3/4,N/4+1:N*3/4) = 1;
ax = linspace(-1, 1, N);
dx = ax(2) - ax(1);
RECT = fftshift(fft2(rect));
U = 1/dx*[-N/2:N/2-1]/N;
V = U;
%%% Display original 2-D image and RECT(u,v) 2-D FFT one
figure(1);
subplot(1,2,1); h1 = mesh(ax,ax,rect); xlabel('x'); ylabel('y');
axis tight; title('rect(x,y)'); view(45, 30);
colormap(gray*0); set(h1,'CDataMapping', 'scaled');
subplot(1,2,2); h2 = mesh(U,V,RECT); xlabel('u'); ylabel('v');
axis tight; title('RECT(u,v)'); view(45, 30);
colormap(gray*0); set(h2,'CDataMapping', 'scaled');
The 2-D Discrete Fourier Transform
The 2-D Discrete Fourier Transform (DFT) is defined as:
MATLAB: F = fft2(f);
The Inverse Discrete Fourier Transform (IDFT) is defined as:
MATLAB: f = ifft2(F);
2-D Parseral’s Theorem:
Placing the Coefficients
-v
The 2-D Discrete Fourier Transform
The Fourier transform represents an image in terms of
exponentials of the form ej2(ux+vy).
u=-2, v=2 u=-1, v=2 u=0, v=2 u=1, v=2 u=2, v=2
u=-2, v=1 u=-1, v=1 u=0, v=1 u=1, v=1 u=2, v=1
U
u=-2, v=0 u=-1, v=0 u=0, v=0 u=1, v=0 u=2, v=0
u=-2, v=-1 u=-1, v=-1 u=0, v=-1 u=1, v=-1 u=2, v=-1
u=-2, v=-2 u=-1, v=-2 u=0, v=-2 u=1, v=-2 u=2, v=-2
Visualizing the Fourier Transform Image
in MATLAB
F(u,v) is a Fourier transform of f(x,y) and it has complex entries.
F = fft2(f);
To clearly display the Fourier Spectrum (amplitude) |F(u,v)|:
– Reduce dynamic range of |F(u,v)| by displaying the log:
D = log(1+abs(F));
– Cyclically rotate the image so that F(0,0) is in the center:
D = fftshift(D);
Example:
|F(u)| = 100 4 2 1 0 0 1 2 4
log(1+|F(u)|) = 4.62 1.61 1.01 0.69 0 0 0.69 1.01 1.61
log(1+|F(u)|)/0.0462 = 10 4 2 1 0 0 1 2 4
fftshift(log(1+|F(u)|) = 0 1 2 4 10 4 2 1 0
Visualizing the Fourier Image
– MATLAB Example
Original f(x, y) |F(u,v)|
100 100
200 200
300 300
400 400
100 200 300 400 500 100 200 300 400 500
Can barely see a spot Shifted Fourier Image Shifted Log Fourier Image log(1+ |F(u,v)|)
100 100
200 200
300 300
400 400
100 200 300 400 500 100 200 300 400 500
Visualizing the Fourier Image in MATLAB
%%% Example 2_3_FFTimagedisplay
Clear
img = 'img_flower.jpg'; ex2_2_2DFT.m
fxy = double(imread(img)); % convert uint8 image data to floating
figure(2);
subplot(2,2,1); h1 = image(fxy); axis tight; title('Original f(x,y)');
colormap(gray); set(h1, 'CDataMapping', 'scaled');
subplot(2,2,2); h2 = image(abs(Fuv)); axis tight; title('|F(u, v)|');
colormap(gray); set(h2, 'CDataMapping', 'scaled');
subplot(2,2,3); h3 = image(Fuv2); axis tight; title('Shifted Fourier Image');
colormap(gray); set(h3, 'CDataMapping', 'scaled');
subplot(2,2,4); h4 = image(Fuv3); axis tight;
title('Shifted Log Fourier Image log(1+ |F(u,v)|)');
colormap(gray); set(h4, 'CDataMapping', 'scaled');
Visualizing the Fourier Image in MATLAB
Fuv_shift = fftshift(Fuv);
Fuv Fuv after fftshift
1 N/2 N N/2 N 1
M/2
M/2 M
M 1
If F f (r, ) F (, )
Scaling:
Example - Rotation
Image f(x, y) Image - Rotated
2D FFT 2D FFT
Magnitude vs Phase
o Images have very samilar magnitude transform
o But, phase is very different from image to image
Demonstration
o Take two pictures, swap the phase transforms,
compute the inverse - what does the result look
like?
Phase and Magnitude
The magnitude of
Original image
Fourier transform
2D FFT
The phase
transform
Original image
The magnitude of
Fourier transform
2D FFT
The phase
transform
Phase and Magnitude - swapping
H ( ) F ( ) G( )
• Here, F is the spectrum of the function, G is the
spectrum of the filter, and H is the filtered
function. Multiplication is point-wise
Qualitative Filters
F G = H
Low-pass
=
= High-pass
Band-pass
=
Filtering in the Spatial Domain
• Filtering the spatial domain is achieved by convolution
h( x) f g f (u) g ( x u)du
• Qualitatively: Slide the filter to each position, x, then sum
up the function multiplied by the filter at that position
Convolution
Example
Images Filtering (LPF)
• Work in the discrete spatial domain
• Convert the filter into a matrix, the filter mask
• Move the matrix over each point in the image, multiply the entries by
the pixels below, then sum
• Filtering can be visualized by drawing a ”convolution kernel”.
• A kernel is a small grid showing how a pixel's filtered value depends
on its neighbors.
• To perform a low-pass filter (LPF) by simply averaging adjacent 3x3
pixels. Each pixel and its eight neighbors are multiplied by 1/9 and
added together. The pixel in the middle is replaced by the sum.
1 1 1 0 1 0
LPF1 1 1 1 LPF 2 1 4 1
1 1
9 8
1 1 1 0 1 0
1 1 1
LPF1 1 1 1
1
9
1 1 1
Images Filtering (HPF)
• A high-pass filter (HPF) can be used to make an image appear
sharper.
• These filters emphasize fine details in the image – exactly the
opposite of the low-pass filter.
• High-pass filtering works in exactly the same way as low-pass
filtering, but just uses a different convolution kernel.
• In the example below, notice the minus signs for the adjacent pixels.
If there is no change in intensity, nothing happens. But if one pixel is
brighter than its immediate neighbors, it gets boosted.
0 1 0
HPF 1 8 1
1
4
0 1 0
• While low-pass filtering smooths out noise, high-pass filtering does
the opposite: it amplifies noise.
• High-pass filtering can also cause small, faint details to be greatly
exaggerated.
Filtering Algorithm
LPF
HPF
Image Filtering – MATLAB Example
ex3_1_imagefilters.m
clear
img = imread('ex-image1.jpg', 'jpg');
[M, N] = size(img);
LPF1 = ones(5)/25;
F = LPF1; MATLAB function:
K = floor(size(F)/2); K = K(1);
k = [-K:K]; imread( )
img_out = zeros(size(img));
for m = 1+K:M-K,
imfinfo( )
for n = 1+K:N-K, imwrite( )
win = double(img(m+k, n+k)).*F; imformats( )
img_out(m, n) = floor(sum(win(:))); image( )
end;
end; uint( )
double( )
figure(1); colormap( )
subplot(1,2,1); imagesc(img); legend('Input');
axis tight; axis square; colormap(gray);
subplot(1,2,2); imagesc(img_out); legend('Output');
axis tight; axis square; colormap(gray);
Image Processing - Sampling
Sampling The Image
Sampling a function f(x) with impulse train of cycle T
produces replicas in the frequency domain with cycle
1/T:
f(x) |F()|
Continuous
^F
x
f(x)
*
|F()|
^F
x
T Fs = 1/T
=
=
f(x) |F()|
Discrete ^F
x -1/2T 1/2T
Cyclic and Symmetry of Discrete Signals
1 cycle
|F()|
f(x)
^
F
x
T’ 1/T’
|F()|
f(x) ^
F
x -1/2T’ 1/2T’
Critical Sampling
• If the maximal frequency of f(x) is max, it is clear from
the above replicas that max should be smaller that
1/2T.
1
• Alternatively: 2max
T
-1/2T 1/2T
Critical Sampling & Aliasing
Input Input
Reconstructed Reconstructed
Aliasing
Aliasing in Color Images
Image demosaicing may produce aliasing in color planes.
Sampling the Transform
Sampling a function F() with impulse train of cycle S
produces replicas in the image domain with cycle 1/S:
f(x) |F()|
^ -1
F
x
*
^F-1
x
S
1/S
^-1
F |F()|
f(x)
x
Sampling Image & Transform
Sampling both f(x) with impulse train of cycle T and F() with
impulse train of cycle S:
f(x) |F()|
^F
x
*
^F
x
T 1/T
^
F
x -1/2T 1/2T
^-1
F
... ... S
1/T
1/S
Number of Samples
• Question: Assuming f(x) was samples with N samples.
What is the minimal number of samples in F() in order
to fully reconstruct f(x) ?
• Answer:
– If we sample f(x) with N samples of cycle T, the support of
f(x) is NT.
– The support of F() is 1/T in the frequency domain.
– If we sample F() with M samples, the sample cycle is 1/MT.
– The replicas in the spatial domain are each MT.
– In order to avoid replicas overlap, MT should be equal or
bigger than NT (the function support).
M N
Optimal Interpolation
If sampling rate is above Nyquist - it is possible to fully
reconstruct f(x) from its samples:
f(x) |F()|
^-1
F
x -1/2T 1/2T
*
f(x) |F()|
^-1
F
1
x -1/2T 1/2T
f(x) |F()|
^-1
F
x -1/2T 1/2T
Image Scaling
How is it possible to scale a digitized image?
f(x) |F()|
^
F
T x
-1/2T 1/2T
f(x) |F()|
^
F
x
T/2 -1/T 1/T
^
F-1
Zero-padding in F(u, v)
Image Scaling: Example 2
Original f(x,y) fftshift(F(u, v))
50 50
100
^
F 100
150 150
200 200
250 250
300 300
350 350
100 200 300 400 100 200 300 400
200 ^
F-1 200
300 300
400 400
500 500
600 600
700 700
200 400 600 800 200 400 600 800
Image Scaling: Example 2
ex2_4_imagescaling.m
%%% Example 2_4_imagescaling
Clear
img = 'img_vaquerosgray.jpg';
fxy = double(imread(img)); %% original f(x,y),convert from uint8 to floating data
[nFx, nFy] = size(fxy);
figure(4);
subplot(2,2,1); h1 = image(fxy); axis tight; title('Original f(x,y)');
colormap(gray); set(h1, 'CDataMapping', 'scaled');
subplot(2,2,2); h2 = image(log10(abs(Fuv)+1)); axis tight; title('F(u, v)');
colormap(gray); set(h2, 'CDataMapping', 'scaled');
subplot(2,2,4); h3 = image(log10(abs(Fuv2)+1)); axis tight; title('Zero-padded F(u,v)');
colormap(gray); set(h3, 'CDataMapping', 'scaled');
subplot(2,2,3); h4 = image(abs(fxy2)); axis tight; title('Scaled f(x,y)');
colormap(gray); set(h4, 'CDataMapping', 'scaled');