Digital Image Processing Using Matlab: Filters (Detail)
Digital Image Processing Using Matlab: Filters (Detail)
HOMEWORK 2
Contents
PROBLEM 2
SOLUTION 3
1. Fourier transform 3
1.1. The 2 – D DFT and its inverse 3
2. Frequency domain smoothing filters 5
2.1. Ideal low-pass filters (ILPF) 5
REFERENCES 15
2
PROBLEM
影像處理與軟體實現[HW2]
課程碼:P953300 授課教授:陳響亮 教授 助教:陳怡瑄 日期:2011/04/07
題目:請以C# 撰寫一程式,可讀入一影像檔,並可執行以下之影像
頻率域之影像增強功能。
a. 每一程式需設計一適當之人機操作介面。
b. 每一功能請以不同方法分開撰寫,各項參數需讓使用者自行輸入。
c. 以C# 撰寫時,可直接呼叫Matlab 現有函式,但呼叫多寡,將列為評分考
量。
(呼叫越少,分數越高)
d. 本次作業視同三次平時作業成績。
一、 傅立葉轉換
1. 二維的DFT及其反轉換
二、 頻率域的平滑濾波器
1. 理想低通濾波器
2. 巴特沃斯低通濾波器
3. 高斯低通濾波器
三、 頻率域濾波器的銳化
1. 理想高通濾波器
2. 巴特沃斯高通濾波器
3. 高斯高通濾波器
四、 同態濾波
◎繳交日期:請於2011/04/21 am9:00以前準時繳交。
◎Word檔內容:需包含程式片段與執行結果之說明。
◎檔案繳交格式:Word檔與程式檔進行壓縮一併繳交。
◎檔案名稱格式:[HW-X]學號。例:[HW-1]P96981035。
◎檔案請上傳至:140.116.86.200;port:21;帳號密碼皆為:image。
3
SOLUTION
1. Fourier transform
In Matlab, there is a function that can compute the 1 – D DFT, the function is fft. The syntax of the
function is,
F = fft (f)
For f is a matrix, so fft function returns the Fourier transform of each column of the matrix. Refer to the
reference [1], so we can use the function fft for computing the 2 – D DFT by first computing a 1 – D DFT
along each column of the input image f, and then computing a 1 – D DFT along each row of this
intermediate result. So the function dft below, that computes the 2 – D DFT by following the reason
above,
Similarly, the inverse Fourier transform is computed by using function idft below,
function g = idft(F)
% Note that: we ignore imaginary part of input F, F is the Fourier
% transform and f is the resulting image.
g1 = ifft(F);
g2 = g1';
g = im2uint8(ifft(g2));
imshow(g)
We also use the function fft2 in Matlab to obtain directly the 2 – D DFT; fft2 function is the Fast Fourier
Transform. In fact, the DFT and its inverse are obtained by using a FFT algorithm. And, the inverse of FFT
is obtained by using the function ifft2. Hereafter, we would like to use the FFT.
5
Filter Inverse
Fourier
function Fourier
transform
H(u,v) transform
Pre- Pre-
F(u,v) H(u,v)F(u,v)
processing processing
f(x,y) g(x,y)
Input image Enhanced image
1 if D (u, v) ≤ D0
H (u, v) =
0 if D (u, v) ≥ D0
Where D (u, v) is the distance from any point (u, v) to the center (origin) of the Fourier transform and it is
given by,
function g = ilpf(f,D0)
[M,N]=size(f);
F=fft2(double(f));
u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
u(idx)=u(idx)-M;
6
idy=find(v>N/2);
v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);
D=sqrt(U.^2+V.^2);
H=double(D<=D0);
G=H.*F;
g=real(ifft2(double(G)));
subplot(1,2,1); imshow(f); title('Input image');
subplot(1,2,2); imshow(g,[ ]); title('Enhanced image');
end
( )
( )
7
function g = blpf(f,n,D0)
[M,N]=size(f);
F=fft2(double(f));
u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);
D=sqrt(U.^2+V.^2);
H = 1./(1 + (D./D0).^(2*n));
G=H.*F;
g=real(ifft2(double(G)));
subplot(1,2,1); imshow(f); title('Input image');
subplot(1,2,2); imshow(g,[ ]); title('Enhanced image');
end
We type the command,
( ) ( ) ( )
function g = glpf(f,D0)
[M,N]=size(f);
F=fft2(double(f));
u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);
D=sqrt(U.^2+V.^2);
H = exp(-(D.^2)./(2*(D0^2)));
G=H.*F;
g=real(ifft2(double(G)));
subplot(1,2,1); imshow(f); title('Input image');
subplot(1,2,2); imshow(g,[ ]); title('Enhanced image');
end
We type the command,
Image sharpening can be achieved in the frequency domain by a high-pass filtering process. The high-
pass filter function, Hhp (u, v) is defined as,
function g = ihpf(f,D0)
[M,N]=size(f);
F=fft2(double(f));
u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);
D=sqrt(U.^2+V.^2);
H=double(D<=D0);
H=1-H;
G=H.*F;
10
g=real(ifft2(double(G)));
subplot(1,2,1); imshow(f); title('Input image');
subplot(1,2,2); imshow(g,[ ]); title('Enhanced image');
end
We type the command,
( )
( )
We change the filter function H (u,v) in the BLPF for obtaining the BHPF. Here is the BHPF function,
function g = bhpf(f,n,D0)
[M,N]=size(f);
F=fft2(double(f));
u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
11
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);
D=sqrt(U.^2+V.^2);
H = 1./(1 + (D0./D).^(2*n));
G=H.*F;
g=real(ifft2(double(G)));
subplot(1,2,1); imshow(f); title('Input image');
subplot(1,2,2); imshow(g,[ ]); title('Enhanced image');
end
We type the command,
( ) ( )
We change the filter function H (u,v) in the GLPF for obtaining the BHPF. Here is the GHPF function,
function g = ghpf(f,D0)
[M,N]=size(f);
12
F=fft2(double(f));
u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);
D=sqrt(U.^2+V.^2);
H = 1 - exp(-(D.^2)./(2*(D0^2)));
G=H.*F;
g=real(ifft2(double(G)));
subplot(1,2,1); imshow(f); title('Input image');
subplot(1,2,2); imshow(g,[ ]); title('Enhanced image');
end
We type the command,
4. Homomorphic filtering
f(x, y) g(x, y)
Input Enhanced
immage image
In the Homomorphic filtering, the filter function H (u, v) is varied according to the special purpose. In the
function below, we choose one, that is,
( ) ( ) ( )
Where, H and L are two parameters. Here is the Homomorphic filter function,
function g = homo_filter(f,D0,gamaH,gamaL)
[M,N]=size(f);
u=0:(M-1);
v=0:(N-1);
idx=find(u>M/2);
u(idx)=u(idx)-M;
idy=find(v>N/2);
v(idy)=v(idy)-N;
[V,U]=meshgrid(v,u);
D=sqrt(U.^2+V.^2);
H = 1 - exp(-(D.^2)./(2*(D0^2))); % Gaussian high-pass filter
H = (gamaH - gamaL)*H + gamaL;
14
REFERENCES
1. Rafael C. Gonzalez,.; Woods, R. E., “Digital Image Processing“, Prentice
Hall, 2002.
3. http://www.mathworks.com/
4. http://www.imageprocessingplace.com/index.htm