You are on page 1of 16

NATIONAL CHENG KUNG UNIVERSITY

Inst. of Manufacturing Information & Systems

DIGITAL IMAGE PROCESSING AND SOFTWARE IMPLEMENTATION

HOMEWORK 2

Professor name: Chen, Shang-Liang


Student name: Nguyen Van Thanh
Student ID: P96007019
Class: P9-009 Image Processing and Software Implementation
Time: [4] 2  4
1

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

2.2. Butterworth low-pass filter (BLPF) 6

2.3. Gaussian low-pass filter (GLPF) 8

3. Sharpening frequency domain filters 9

3.1. Ideal high-pass filters (IHPF) 9

3.2. Butterworth high-pass filters (BHPF) 10

3.3. Gaussian high-pass filters (GHPF) 11


4. Homomorphic filtering 13

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

1.1. The 2 – D DFT and its inverse

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,

function [s, sc, slog] = dft(f)


F1 = fft(f); % 1-D column transforms
F2 = F1';
F = fft(F2) % 1-D row transforms
s = abs(F); % obtaining the Fourier spectrum
Fc = fftshift(F); % fftshift is function that can be used to move the
% origin of the transform to the center of frequency
% rectangle
sc = abs(Fc); % the Fourier spectrum of Fc
slog = log(1 + sc); % log transform
subplot(2,3,1); imshow(f);
subplot(2,3,2); imshow(s, [ ]);
subplot(2,3,4); imshow(sc, [ ]);
subplot(2,3,5); imshow(slog, [ ]);

We type the command,

>> f = imread (‘Fig4.03(a).jpg’);


>> [s, sc, slog] = dft (f)
4

It yields the images below,

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

2. Frequency domain smoothing filters


We would like to show the basic steps for filtering in the frequency domain as the diagram below,

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

2.1. Ideal low-pass filters (ILPF)

With ILPF case, the filter function H (u, v) is defined as,

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,

D (u, v) = sqrt (u2 + v2)

And D0 is a specified distance from the origin of the (centered) transform.

We write a function to compute the ILPF, this function name is ilpf,

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

We type the command,

>> f = imread (‘Fig4.11(a).jpg’);


>> g = ilpf (f, 30);
It yields the images below,

2.2. Butterworth low-pass filter (BLPF)

With the BLPF, the filter function H (u, v) is,

( )
( )
7

Where n is the order.

We write a function to compute the BLPF, this function name is blpf,

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,

>> f = imread (‘Fig4.11(a).jpg’);


>> g = blpf (f, 2, 30);

It yields the images below,


8

2.3. Gaussian low-pass filter (GLPF)

With the GLPF, the filter function H (u, v) is,

( ) ( ) ( )

We write a function to compute the BLPF, this function name is blpf,

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,

>> f = imread (‘Fig4.11(a).jpg’);


>> g = glpf (f, 30);

It yields the images below,


9

3. Sharpening frequency domain filters

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,

Hhp (u, v) = 1 – Hlp (u, v)

3.1. Ideal high-pass filters (IHPF)


With the reason above, we can obtain the IHPF from the ILPF by changing the filter function H (u, v). The
function to compute the IHPF is,

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,

>> f = imread (‘Fig4.11(a).jpg’);


>> g = ihpf (f, 15);

It yields the images below,

3.2. Butterworth high-pass filters (BHPF)


The filter function of the BHPF of order n and with cutoff frequency locus at distance D 0 from the origin is
given by,

( )
( )

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,

>> f = imread (‘Fig4.11(a).jpg’);


>> g = bhpf (f, 2, 15);

It yields the images below,

3.3. Gaussian high-pass filters (GHPF)


The filter function of GHPF with cutoff frequency locus at a distance D0 from the origin is given by,

( ) ( )

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,

>> f = imread (‘Fig4.11(a).jpg’);


>> g = bhpf (f, 15);

It yields the images below,


13

4. Homomorphic filtering

The Homomorphic filtering approach is summaried in the Figure below.

Homomorphic filtering approach for


image enhancement

Ln DFT H (u, v) Inverse DFT exp

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

z = log2(1+double(f)); % Log transfrom


Z = fft2(z);
S=H.*Z;
s=real(ifft2(double(S)));
g = 2.^s;
subplot(1,2,1); imshow(f); title('Input image');
subplot(1,2,2); imshow(g,[ ]); title('Enhanced image');
end
We type the command,

>> f = imread (‘Fig.ex.jpg’);


>> g = homo_filter(f,10,0.5,0.2);

It yields the images below,


15

REFERENCES
1. Rafael C. Gonzalez,.; Woods, R. E., “Digital Image Processing“, Prentice
Hall, 2002.

2. Rafael C. Gonzalez, Richard E. Woods, Steven L; Woods, R. E., “Digital


Image Processing Using MATLAB“, Prentice Hall, 2005.

3. http://www.mathworks.com/

4. http://www.imageprocessingplace.com/index.htm

You might also like