EECE\CS 253 Image Processing Lecture Notes: Frequency Lecture Notes Filtering Richard Alan Peters II Department of Electrical Engineering and Computer Science Fall Semester 2011 This work is licensed under.

Download Report

Transcript EECE\CS 253 Image Processing Lecture Notes: Frequency Lecture Notes Filtering Richard Alan Peters II Department of Electrical Engineering and Computer Science Fall Semester 2011 This work is licensed under.

EECE\CS 253 Image Processing
Lecture Notes:
Frequency
Lecture
Notes Filtering
Richard Alan Peters II
Department of Electrical Engineering and
Computer Science
Fall Semester 2011
This work is licensed under the Creative Commons Attribution-Noncommercial 2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or
send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.
Convolution Property of the Fourier Transform
Let functions f (r , c) and g(r , c) have
Fourier Transforms F(u , v) and G(u , v).
Then,
F {f * g } = F ×G.
Moreover,
F {f ×g } = F * G.
11/6/2015
* = convolution
· = multiplication
The Fourier Transform of a
convolution equals the
product of the Fourier
Transforms. Similarly, the
Fourier Transform of a
convolution is the product of
the Fourier Transforms
1999-2011 by Richard Alan Peters II
2
Convolution via
Fourier Transform
Image & Mask
Transforms
Pixel-wise
Product
11/6/2015
1999-2011 by Richard Alan Peters II
Inverse
Transform
3
How to Convolve via FT in Matlab
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
Read the image from a file into a variable, say I.
Read in or create the convolution mask, h. The mask is usually 1-band
Compute the sum of the mask: s = sum(h(:));
If s == 0, set s = 1;
For color images you may
Replace h with h = h/s;
need to do each step for
Create: H = zeros(size(I));
each band separately.
Copy h into the middle of H.
Shift H into position: H = ifftshift(H);
Take the 2D FT of I and H: FI=fft2(I); FH=fft2(H);
Pointwise multiply the FTs: FJ=FI.*FH;
Compute the inverse FT: J = real(ifft2(FJ));
11/6/2015
1999-2011 by Richard Alan Peters II
4
Coordinate Origin of the FFT
Center =
(floor(R/2)+1, floor(C/2)+1)
Even
Odd
Even
Odd
Image Origin
Image Origin
Weight Matrix Origin
Weight Matrix Origin
After FFT shift
After FFT shift
After IFFT shift
After IFFT shift
11/6/2015
1999-2011 by Richard Alan Peters II
5
Matlab’s fftshift and ifftshift
J = fftshift(I):
5
6
4
8
9
7
I (1,1)  J ( R/2 +1, C/2 +1)
2
I = ifftshift(J):
J ( R/2 +1, C/2 +1)  I (1,1)
3
1
2
3
4
5
6
7
8
9
1
1
2
3
4
5
6
7
8
9
5
6
4
8
9
7
2
3
1
where x = floor(x) = the largest integer smaller than x.
11/6/2015
1999-2011 by Richard Alan Peters II
6
Blurring: Averaging / Lowpass Filtering
Blurring results from:

Pixel averaging in the spatial domain:
–
–

Each pixel in the output is a weighted average of its neighbors.
Is a convolution whose weight matrix sums to 1.
Lowpass filtering in the frequency domain:
–
–
High frequencies are diminished or eliminated
Individual frequency components are multiplied by a nonincreasing
function of  such as 1/ = 1/(u2+v2).
The values of the output image are all non-negative.
11/6/2015
1999-2011 by Richard Alan Peters II
7
Sharpening: Differencing / Highpass Filtering
Sharpening results from adding to the image, a copy of
itself that has been:

Pixel-differenced in the spatial domain:
–
–

Each pixel in the output is a difference between itself and a weighted
average of its neighbors.
Is a convolution whose weight matrix sums to 0.
Highpass filtered in the frequency domain:
–
–
High frequencies are enhanced or amplified.
Individual frequency components are multiplied by an increasing
function of  such as  = (u2+v2), where  is a constant.
The values of the output image positive & negative.
11/6/2015
1999-2011 by Richard Alan Peters II
8
Recall:
Convolution Property of the Fourier Transform
Let functions f (r , c) and g( r , c) have
Fourier Transforms F (u , v) and G (u, v).
* = convolution
· = multiplication
Then,
F {f *g } = F×G.
Moreover,
F {f×g } = F * G.
Thus we can compute f *g by
f *g =
11/6/2015
F
-1
{F×G}.
The Fourier Transform of a
convolution equals the
product of the Fourier
Transforms. Similarly, the
Fourier Transform of a
convolution is the product of
the Fourier Transforms
1999-2011 by Richard Alan Peters II
9
Ideal Lowpass Filter
11/6/2015
1999-2011 by Richard Alan Peters II
10
Ideal Lowpass Filter
Multiply by
this, or …
Fourier Domain Rep.
11/6/2015
Image size: 512x512
FD filter radius: 16
… convolve
by this
Spatial Representation
1999-2011 by Richard Alan Peters II
Central Profile
11
Ideal Lowpass Filter
Multiply by
this, or …
Fourier Domain Rep.
11/6/2015
Image size: 512x512
FD filter radius: 8
… convolve
by this
Spatial Representation
1999-2011 by Richard Alan Peters II
Central Profile
12
Consider the
image below:
Power Spectrum and Phase of an Image
Original Image
11/6/2015
Power Spectrum
1999-2011 by Richard Alan Peters II
Phase
13
Ideal Lowpass Filter
Original Image
11/6/2015
Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD filter radius: 16
Ideal LPF in FD
14
Ideal Lowpass Filter
Filtered Image
11/6/2015
Filtered Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD filter radius: 16
Original Image
15
Ideal Lowpass Filter
Original Image
11/6/2015
Filtered Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD filter radius: 16
Filtered Image
16
Ideal Highpass Filter
11/6/2015
1999-2011 by Richard Alan Peters II
17
Ideal Highpass Filter
Multiply by
this, or …
Fourier Domain Rep.
11/6/2015
Image size: 512x512
FD notch radius: 16
… convolve
by this
Spatial Representation
1999-2011 by Richard Alan Peters II
Central Profile
18
Ideal Highpass Filter
Original Image
11/6/2015
Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD notch radius: 16
Ideal HPF in FD
19
*signed image:
0 mapped to 128
Ideal Highpass Filter
Filtered Image*
11/6/2015
Filtered Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD notch radius: 16
Original Image
20
*signed image:
0 mapped to 128
Ideal Highpass Filter
Original Image
11/6/2015
Filtered Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD notch radius: 16
Filtered Image*
21
*signed image:
0 mapped to 128
Ideal Highpass Filter
Positive Pixels
11/6/2015
Filtered Image*
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD notch radius: 16
Negative Pixels
22
Ideal Bandpass Filter
11/6/2015
1999-2011 by Richard Alan Peters II
23
Ideal Bandpass Filter
A bandpass filter is created by
(1) subtracting a FD radius 2 lowpass filtered image from a FD radius 1 lowpass
filtered image, where 2 < 1, or
(2) convolving the image with a mask that is the difference of the two lowpass masks.
FD LP mask with radius 1
11/6/2015
=
FD LP mask with radius 2
1999-2011 by Richard Alan Peters II
FD BP mask 1 - 2
24
*signed image:
0 mapped to 128
Ideal Bandpass Filter
image LPF radius 1
11/6/2015
image LPF radius 2
1999-2011 by Richard Alan Peters II
image BPF radii 1, 2*
25
*signed image:
0 mapped to 128
Ideal Bandpass Filter
filtered image*
11/6/2015
filter power spectrum
1999-2011 by Richard Alan Peters II
original image
26
*signed image: 0
mapped to 128
Ideal Bandpass Filter
original image*
11/6/2015
filter power spectrum
1999-2011 by Richard Alan Peters II
filtered image
27
*signed image:
0 mapped to 128
A Different Ideal Bandpass Filter
original image
11/6/2015
filter power spectrum
1999-2011 by Richard Alan Peters II
filtered image*
28
The Optimal Filter
11/6/2015
1999-2011 by Richard Alan Peters II
29
The Uncertainty Relation
space
frequency
FT
space
frequency
FT
11/6/2015
If  x  y is the extent of
the object in spaceand
if  u  v is its extent in
frequencythen,
1
 x  y  u  v 
16 2
A small object in space
has a large frequency
extent and vice-versa.
1999-2011 by Richard Alan Peters II
30
The Uncertainty Relation
 small extent 
A symmetric pair of
lines in the frequency
domain becomes a
sinusoidal line in the
spatial domain.
 large extent 
frequency
 large extent 
 small extent 
 small extent 
space
IFT
11/6/2015
Recall: a symmetric
pair of impulses in the
frequency domain
becomes a sinusoid in
the spatial domain.
space
IFT
 small extent 
 large extent 
frequency
 large extent 
1999-2011 by Richard Alan Peters II
31
Ideal Filters Do Not Produce Ideal Results
IFT
A sharp cutoff in the
frequency domain…
11/6/2015
…causes ringing in the
spatial domain.
1999-2011 by Richard Alan Peters II
32
Ideal Filters Do Not Produce Ideal Results
Ideal LPF
Blurring the image above
w/ an ideal lowpass filter…
11/6/2015
…distorts the results with
ringing or ghosting.
1999-2011 by Richard Alan Peters II
33
Optimal Filter: The Gaussian
IFT
The Gaussian filter optimizes the uncertainty relation.
It provides the sharpest cutoff with the least ringing.
11/6/2015
1999-2011 by Richard Alan Peters II
34
One-Dimensional Gaussian
g ( x) 
11/6/2015
1
 2
e
 ( x   ) 2 2 2
1999-2011 by Richard Alan Peters II
35
Two-Dimensional Gaussian
R = 512, C = 512
g ( r, c)  g ( r ) g (c)
c


1
 r  c 2
1
 r  c 2
e
e


If  and  are
different for r & c…
( r   r )2 ( y   c )

2 r2
2 c2
2
 c2 ( x   r ) 2   r2 ( y   c ) 2
2 r2 c2
…or if  and  are
the same for r & c.
r
 = 257,  = 64
11/6/2015
g ( r, c)   212 e

( r   )2  ( c   )2
2 2
1999-2011 by Richard Alan Peters II
36
Optimal Filter: The Gaussian
Gaussian LPF
With a gaussian lowpass
filter, the image above …
11/6/2015
… is blurred without ringing
or ghosting.
1999-2011 by Richard Alan Peters II
37
Compare with an “Ideal” LPF
Ideal LPF
Blurring the image above w/
an ideal lowpass filter…
11/6/2015
…distorts the results with
ringing or ghosting.
1999-2011 by Richard Alan Peters II
38
Gaussian Lowpass Filter
11/6/2015
1999-2011 by Richard Alan Peters II
39
Gaussian Lowpass Filter
Multiply by
this, or …
Fourier Domain Rep.
11/6/2015
Image size: 512x512
SD filter sigma = 8
… convolve
by this
Spatial Representation
1999-2011 by Richard Alan Peters II
Central Profile
40
Gaussian Lowpass Filter
Multiply by
this, or …
Fourier Domain Rep.
11/6/2015
Image size: 512x512
SD filter sigma = 2
… convolve
by this
Spatial Representation
1999-2011 by Richard Alan Peters II
Central Profile
41
Gaussian Lowpass Filter
Original Image
11/6/2015
Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
SD filter sigma = 8
Gaussian LPF in FD
42
Gaussian Lowpass Filter
Filtered Image
11/6/2015
Filtered Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
SD filter sigma = 8
Original Image
43
Gaussian Lowpass Filter
Filtered Image
11/6/2015
Filtered Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
SD filter sigma = 8
Original Image
44
Gaussian Highpass Filter
11/6/2015
1999-2011 by Richard Alan Peters II
45
Gaussian Highpass Filter
Multiply by
this, or …
Fourier Domain Rep.
11/6/2015
Image size: 512x512
FD notch sigma = 8
… convolve
by this
Spatial Representation
1999-2011 by Richard Alan Peters II
Central Profile
46
Gaussian Highpass Filter
Original Image
11/6/2015
Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD notch sigma = 8
Gaussian HPF in FD
47
*signed image:
0 mapped to 128
Gaussian Highpass Filter
Filtered Image*
11/6/2015
Filtered Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD notch sigma = 8
Original Image
48
*signed image:
0 mapped to 128
Gaussian Highpass Filter
Original Image
11/6/2015
Filtered Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD notch sigma = 8
Filtered Image*
49
*signed image:
0 mapped to 128
Gaussian Highpass Filter
Positive Pixels
11/6/2015
Filtered Image*
1999-2011 by Richard Alan Peters II
Image size: 512x512
FD notch sigma = 8
Negative Pixels
50
*signed image:
0 mapped to 128
Another Gaussian Highpass Filter
original image
11/6/2015
filter power spectrum
1999-2011 by Richard Alan Peters II
filtered image*
51
Gaussian Bandpass Filter
11/6/2015
1999-2011 by Richard Alan Peters II
52
Gaussian Bandpass Filter
A bandpass filter is created by
(1) subtracting a FD radius 2 lowpass filtered image from a FD radius 1 lowpass
filtered image, where 2 < 1, or
(2) convolving the image with a mask that is the difference of the two lowpass masks.
FD LP mask with radius 1
11/6/2015
=
FD LP mask with radius 2
1999-2011 by Richard Alan Peters II
FD BP mask 1 - 2
53
*signed image:
0 mapped to 128
Gaussian Bandpass Filter
image LPF radius 1
11/6/2015
image LPF radius 2
1999-2011 by Richard Alan Peters II
image BPF radii 1, 2*
54
*signed image:
0 mapped to 128
Gaussian Bandpass Filter
filtered image*
11/6/2015
filter power spectrum
1999-2011 by Richard Alan Peters II
original image
55
*signed image:
0 mapped to 128
Ideal Bandpass Filter
original image
11/6/2015
filter power spectrum
1999-2011 by Richard Alan Peters II
filtered image*
56
Gaussian Bandpass Filter
Fourier Domain Rep.
11/6/2015
Spatial Representation
1999-2011 by Richard Alan Peters II
Image size: 512x512
sigma = 2 - sigma = 8
Central Profile
57
Gaussian Bandpass Filter
Original Image
11/6/2015
Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
sigma = 2 - sigma = 8
Gaussian BPF in FD
58
*signed image:
0 mapped to 128
Gaussian Bandpass Filter
Filtered Image*
11/6/2015
Filtered Power Spectrum
1999-2011 by Richard Alan Peters II
Image size: 512x512
sigma = 2 - sigma = 8
Original Image
59
*signed image:
0 mapped to 128
Gaussian Bandpass Filter
Positive Image
Pixels
Filtered
11/6/2015
Filtered Image*
1999-2011 by Richard Alan Peters II
Image size: 512x512
sigma = 2 - sigma = 8
Negative Pixels
60
Ideal vs. Gaussian Filters
11/6/2015
1999-2011 by Richard Alan Peters II
61
*signed image:
0 mapped to 128
Ideal Lowpass and Highpass Filters
Ideal LPF
11/6/2015
Original Image
1999-2011 by Richard Alan Peters II
Ideal HPF*
62
*signed image:
0 mapped to 128
Gaussian Lowpass and Highpass Filters
Gaussian LPF
11/6/2015
Original Image
1999-2011 by Richard Alan Peters II
Gaussian HPF*
63
*signed image:
0 mapped to 128
Ideal and Gaussian Bandpass Filters
Ideal BPF*
11/6/2015
Original Image
1999-2011 by Richard Alan Peters II
Gaussian BPF*
64
*signed image:
0 mapped to 128
Gaussian and Ideal Bandpass Filters
Gaussian BPF*
11/6/2015
Original Image
1999-2011 by Richard Alan Peters II
Ideal BPF*
65
Effects on Power Spectrum
11/6/2015
1999-2011 by Richard Alan Peters II
66
Power Spectrum and Phase of an Image
original image
11/6/2015
power spectrum
1999-2011 by Richard Alan Peters II
phase
67
Power Spectrum and Phase of a Blurred Image
blurred image
11/6/2015
power spectrum
1999-2011 by Richard Alan Peters II
phase
68
Power Spectrum and Phase of an Image
original image
11/6/2015
power spectrum
1999-2011 by Richard Alan Peters II
phase
69
Power Spectrum and Phase of a Sharpened Image
sharpened image
11/6/2015
power spectrum
1999-2011 by Richard Alan Peters II
phase
70