C4 - Image Filtering-NTHai-9-2015
C4 - Image Filtering-NTHai-9-2015
Lecture:
IMAGE PROCESSING
Chapter 4:
Image Filtering
1
Nguyen Thanh Hai, PhD
INTRODUCTION
-Image Filtering allows to apply various effects on images in order to
filter noise or to smooth image.
-The 2D filter is a 2D filter matrix
2
Nguyen Thanh Hai, PhD
1
11/11/2015
Fig 4.1 describes a 3x3 spatial filter. Assume that one needs
to calculate a image pixel , in an image, the
corresponding pixel of output image , is calculated by
the sum of products of coefficients (values) between the filter
and image.
3
Nguyen Thanh Hai, PhD
4
Nguyen Thanh Hai, PhD
2
11/11/2015
(, ) = , + , +
5
Nguyen Thanh Hai, PhD
6
Nguyen Thanh Hai, PhD
3
11/11/2015
7
Nguyen Thanh Hai, PhD
8
Nguyen Thanh Hai, PhD
4
11/11/2015
, (, ) = , + , +
, (, ) = , − , −
The symbol minus means that the image is rotated 180 degrees.
9
Nguyen Thanh Hai, PhD
(a) (b)
(b)
(d)
5
11/11/2015
(e) (f)
(g)
(h)
1 1 1 2 2
t: -1 to -1
1 1 1 2 2
x: 0 to 4 1 1 1 2 2 1 1 1 x 12
2 2 2 2 2 s: -1 to 1 1 1 1
2 2 2 2 2 1 1 1
6
11/11/2015
- Ones: matrix 1.
- imfilter(f,h,'Boundary Options','OutputSize','Correlation and Convolution
Options')
- 'Boundary Options' : allow to select a boundary of the output image
- 'Output Size‘: select the output size with 'Correlation and
Convolution Options'.
- More functions in Table. 4.1 of Image Processing book
13
Nguyen Thanh Hai, PhD
* Gaussian (
− n12 + n22 )
hg (n1 , n2 ) = e 2σ 2
hg (n1 , n2 )
or h(n1 , n2 ) =
∑n ∑n h
1 2 g
* Laplacian
∂2 ∂2
∇2 = +
∂x 2 ∂y 2
α 1−α α
1 + α 1+α 1+α
or 1 − α −4 1−α
1 + α 1+α 1+α
α 1−α α
1 + α 1+α 1 + α 14
Nguyen Thanh Hai, PhD
7
11/11/2015
h(n1 , n2 ) =
(n
2
1 )
+ n22 − 2σ 2 hg (n1 , n2 )
2πσ 6
∑n ∑n h
1 2 g
* Prewitt
1 1 1
h = 0 0 0
− 1 − 1 − 1
* Sobel
1 2 1
h = 0 0 0
− 1 − 2 − 1
15
Nguyen Thanh Hai, PhD
clear all;
f=imread('cameraman.bmp');
ha=fspecial('average');
ga=imfilter(f,ha,'replicate');
hd=fspecial('disk'); hm=fspecial('motion');
gd=imfilter(f,hd,'replicate'); gm=imfilter(f,hm,'replicate');
hg=fspecial('gaussian'); hp=fspecial('prewitt');
gg=imfilter(f,hg,'replicate'); gp=imfilter(f,hp,'replicate');
hlap=fspecial('laplacian'); hs=fspecial('sobel');
glap=imfilter(f,hlap,'replicate'); gs=imfilter(f,hs,'replicate');
hlog=fspecial('log');
glog=imfilter(f,hlog,'replicate');
16
Nguyen Thanh Hai, PhD
8
11/11/2015
17
Nguyen Thanh Hai, PhD
18
Nguyen Thanh Hai, PhD
9
11/11/2015
19
Nguyen Thanh Hai, PhD
function g=dftfilt(f,H)
F=fft2(f,size(H,1),size(H,2));
G=H.*F;
g=ifft2(G);
f=real(g);
g=g(1:size(f,1),1:size(f,2))
end
20
Nguyen Thanh Hai, PhD
10
11/11/2015
21
Nguyen Thanh Hai, PhD
clear all;
f=imread('cameraman.tif');
h=fspecial('sobel');
sizeH=size(f)*2;
H=freqz2(h,sizeH);
H1=ifftshift(H);
gd=dftfilt(f,H1);
gs=imfilter(double(f),h);
d=abs(gd-gs);
gd=uint8(gd);imshow(gd);
gd=uint8(gd);imshow(gs);
22
Nguyen Thanh Hai, PhD
11
11/11/2015
Ex of an image with 8 × 5 :
function [U,V]=dftuv(m,n)
u=0:(m-1); [U,V]=dftuv(8,5);
v=0:(n-1); D=U.^2+V.^2
idx=find(u>m/2); D=
u(idx)=u(idx)-m; 0 1 4 4 1
idy=find(v>n/2); 1 2 5 5 2
v(idy)=v(idy)-n; 4 5 8 8 5
[V,U]=meshgrid(v,u); 9 10 13 13 10
end 16 17 20 20 17
9 10 13 13 10
4 5 8 8 5
1 2 5 5 2
24
Nguyen Thanh Hai, PhD
12
11/11/2015
ans =
20 17 16 17 20
13 10 9 10 13
8 5 4 5 8
5 2 1 2 5
4 1 0 1 4
5 2 1 2 5
8 5 4 5 8
10 9 10 13
25
Nguyen Thanh Hai, PhD
1
- ., / =
1 + 1 ., / /1# 56
26
Nguyen Thanh Hai, PhD
13
11/11/2015
- ., / = 7 8
9 :,; ⁄58?9
27
Nguyen Thanh Hai, PhD
Fig. 4.7. left column: 3-D shape of the window filter; middle
columns: images of the filters; right one: the frequency response 1#
of the ideal filters. First row: the ideal lowpass filter; middle one: the
Butterworth lowpass filter; last one: the Gaussian lowpass filter 28
Nguyen Thanh Hai, PhD
14
11/11/2015
15
11/11/2015
BLPF3=fftshift(lpfilter('btw',500,500,50,3));
figure;mesh(BLPF3(1:10:500,1:10:500));
axis([0 50 0 50 0 1]);
saveas(gcf, 'Hinh3.7c.tif', 'tif');
BLPF4=fftshift(lpfilter('btw',500,500,50,4));
figure;mesh(BLPF4(1:10:500,1:10:500));
axis([0 50 0 50 0 1]);
saveas(gcf, 'Hinh3.7d.tif', 'tif');
31
Nguyen Thanh Hai, PhD
Fig 4.8. Plots of the Butterworth lowpass filters with different orders.
32
Nguyen Thanh Hai, PhD
16
11/11/2015
C 5C
(a) Original image (b) 1# = (c) 1# =
D D
+C
(d) 1# = (e) 1# =
EC
(f) 1# =
D D
Fig 4.9. cameraman A × B image after the ideal lowpass filter with
different cut-off frequencies
34
Nguyen Thanh Hai, PhD
17
11/11/2015
function H=hpfilter(type,M,N,D0,n)
if nargin==4
n=1;
End
Hlp=lpfilter(type,M,N,D0,n);
H=1-Hlp;
end
35
Nguyen Thanh Hai, PhD
BLPF=fftshift(hpfilter('btw',500,500,50));
mesh(BLPF(1:10:500,1:10:500));
axis([0 50 0 50 0 1]);
GLPF=fftshift(hpfilter('gaussian',500,500,50));
mesh(GLPF(1:10:500,1:10:500));
axis([0 50 0 50 0 1]);
36
Nguyen Thanh Hai, PhD
18
11/11/2015
(b) (c)
(a)
Fig. 4.10. Express the highpass filter. Top row: mesh with the 3-D
shape; bottom row: 2D images of the mesh; left column: the ideal
highpass filter, middle columns: the Butterworth highpass filter, right
one: the Gaussian highpass filter
37
Nguyen Thanh Hai, PhD
-HIJ ., / = + -HG ., /
In which is the offset, denotes the multiplication constant and
-HG ., / describes the transfer of the highpass filter.
38
Nguyen Thanh Hai, PhD
19
11/11/2015
clear all;
f=imread('satellite.tiff');
[M N]=size(f);
D0=0.1*size(f,1);
HBW=hpfilter('btw',2*M,2*N,D0);
H=0.5+2*HBW;
gbw=dftfilt(f,HBW);
gbw=gscale(gbw);
ghf=dftfilt(f,H);
ghf=gscale(ghf);
ghe=histeq(ghf,256);
39
Nguyen Thanh Hai, PhD
function g=gscale(f)
fmin=min(f(:));
fmax=max(f(:));
for i=1:size(f,1)
for j=1:size(f,2)
g(i,j)=255*(f(i,j)-fmin)/(fmax-fmin);
end
end
g=uint8(g);
end
40
Nguyen Thanh Hai, PhD
20
11/11/2015
Image Filtering
Problem: Calculate the convolution of the image A and the
mask h.
• Suppose the input image is
42
Nguyen Thanh Hai, PhD
21
11/11/2015
Image Filtering
The End
43
Nguyen Thanh Hai, PhD
22