You are on page 1of 36

Digital Image Processing – LAB

K L University
Department of Electronics and Communication Engineering
Course Code: 15EC4110

Contents

1 Experiment 1 5
1.1 Histogram processing . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.1 Normalized histograms . . . . . . . . . . . . . . . . . . . . 5
1.1.2 Matlab Function . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 Matlab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2.1 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Application 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3.1 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.4 Experimental Tasks . . . . . . . . . . . . . . . . . . . . . . . . . 10

2 Experiment 2 11
2.1 Histogram Equalization . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.1 Continuous Case . . . . . . . . . . . . . . . . . . . . . . . 11
2.1.2 Discrete case (quantities): . . . . . . . . . . . . . . . . . . 12
2.2 Matlab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2.1 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3 Application 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.1 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.4 Experimental Tasks . . . . . . . . . . . . . . . . . . . . . . . . . 19

3 Experiment 3 20
3.1 Filtering in the spatial domain (Spatial Filtering) . . . . . . . . . 20
3.1.1 Linear Spatial Filtering . . . . . . . . . . . . . . . . . . . 21
3.1.2 Nonlinear Spatial Filtering . . . . . . . . . . . . . . . . . 21
3.2 Matlab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.2.1 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.3 Application 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.3.1 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.4 Experimental Tasks . . . . . . . . . . . . . . . . . . . . . . . . . 27

1
4 Experiment 4 28
4.1 Frequency Domain Filtering . . . . . . . . . . . . . . . . . . . . . 28
4.1.1 Low-pass filtering . . . . . . . . . . . . . . . . . . . . . . . 29
4.1.2 High-pass filtering . . . . . . . . . . . . . . . . . . . . . . 30
4.2 Matlab Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.2.1 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.3 Experimental Tasks . . . . . . . . . . . . . . . . . . . . . . . . . 36

2
This set of experiments is based on MATLAB. These experiments will allow
any instructor to cover experiments in most of the topics treated in a regular
image processing course to be completed successfully.

Introduction
Motivation for a digital image processing course arises mainly from two areas
of applications, namely, a) an increase in the pictorial information available for
human interpretation, and b) image processing for automatic and autonomous
machine control. This is because vision is the most important human sense in
terms of the amount of information it conveys and because of a good visualiza-
tion is very important for the correct information converged in an image.
Many image processing circuits require dedicated software to perform their
tasks. These packages usually are highly priced and are not easily modified
by the final user. The basis for the image processing is MATLAB software
package now available almost anywhere for other uses and that is used as the
engine for the image processing experiments. The software written for the image
processing experiments is available from the users at no cost to interested users
and instructors.
Image processing applications include many topics, among which we can
mention remote sensing, ultrasound images, meteorology, astronomy, inspection,
radar, seismology, radiology, autonomous navigation, recognition, etc.

Image Processing Laboratory


MATLAB is a matrix oriented computing engine. Thus, it is almost perfect for
image processing because images can be thought of as matrices. A definition
for an image can then be the following:
An image is a N × N array of elements. Each element in the array is a
number which represents the sampled intensity. The samples are named pixels
(picture element). After sampling each pixel is quantized. Each intensity is
assigned a number within a finite set of values, usually between 0 and K-1,
where K=2B is the possible number of gray levels, each represented by B bits.
The digitizing of images is now complete and each image is now an array which
can be handled more appropriately as a matrix.
Our Image processing laboratory based on MATLAB has the capability to
perform the following tasks:
Pre-Lab Session: Digital Image Representation, Reading Images, Display,
Writing Images, Image Classes and their conversion.
Exp 1: Point-to-point transformation. This laboratory experiment provides
for thresholding an image and the evaluation of its histogram. The user can
choose a threshold level to see the image showing only the pixels at that thresh-
old.

3
Application 1: Develop an automatic thresholding algorithm such as (Otsus)
for defect identification.
Exp 2: Histogram Processing for Contrast Enhancement. This laboratory
experiment provides for histogram equalization and matching for contrast en-
hancement. The user must express these algorithms on 10 sets of different
contrast images and measure the performance of the algorithms on each image
set. This experiment illustrates the relationship among the intensities (gray
levels) of an image and its histogram. It shows how to improve the image by
equalizing the histogram.
Application 2: Color Image Enhancement Using Histogram Equalization
Method without Changing Hue and Saturation
Exp 3: Spatial Filtering. Linear and Non-linear spatial filtering. Learn the
concepts of designing spatial filters in time domain. Use different kinds of spatial
filters to improve the quality of images. Measure the quality of improved images
against the original images.
Application 3: Fingerprint Image Enhancement using Filtering Techniques
Exp 4: Frequency Domain Filtering: Use low pass and high pass filtering
kernels to sharpen an image with Two-dimensional Fourier transform. The
purpose of this experiment is to provide an understanding of the harmonic
content of an image using the discrete Fourier transform (DFT). This experiment
is designed so the student learns the concept of masking with the DFT.
Exp 5: Morphological operations I. This experiment is intended so students
can appreciate the effect of morphological operations using a small structuring
element on simple binary images. The operations that can be performed are
erosion, dilation, opening, closing, open-close, close-open. how morphological
functions change images by applying consecutive erosion and dilation operations.
Exp 6: Linear filtering using 2D convolution. After completing this exper-
iment every student should understand the concepts of filtering using linear
convolution.
Exp 7: Edge detection. Use at least 5 edge detectors. This experiment
enables students to understand the concept of edge detectors and the operation
in noisy images.
Exp 8: Image Restoration. Adding Noise. Types of Noise. This experiment
enables students understanding spatial noise filters in restoration.
Exp 9: Image Registration. Geometric spatial transformations on images to
handle miss-aligned images. This experiment enables student to apply transfor-
mation on images captured in the wild using unconstrained capture methods.
Exp 10: Video background subtraction with analysis. This experiment en-
ables student to understand the dynamics of video processing. Motion segmen-
tation is a critical task in surveillance video object detection and estimation.

4
1 Experiment 1
Point–to–point transformation. This laboratory experiment provides for
thresholding an image and the evaluation of its histogram. The user can choose
a threshold level to see the image showing only the pixels at that threshold.

1.1 Histogram processing


In Statistics, Histogram is a graphical representation showing a visual im-
frequency
pression of the distribution of data.
distribution of An Image Histogram is a type of histogram that acts as a graphical rep-
data resentation of the lightness/color distribution in a digital image. It plots the
number of pixels for each value.
The histogram of a digital image with gray levels in the range [0, L-1] is a
discrete function

h(rk ) = nk

Where
• rk is the k th gray level
• nk is the number of pixels in the image having gray level rk

1.1.1 Normalized histograms


can be obtained by dividing all elements of h(rk ) by the total number of
pixels in the image:

h(rk ) nk
P (rk ) = = , f or k = 1, 2, ..., L
n n
n total number of pixels
Thus P (rk ) gives an estimate of the probability of occurrence of gray level
rk . Note that the sum of all components of a normalized histogram is equal to
1.
1.1.2 Matlab Function
h= imhist(f,b)

3 f is the input image


3 h Its histogram h(rk )
b=256
3 b number of bins used in forming the histogram (b=255 is the default).

5
1.2 Matlab Code

1 clc ;
2 clear all ;
3 close all ;
4

5 I=imread ( ’ cameraman . t i f ’ ) ;
6 I=i m r e s i z e ( I , [ 2 5 6 2 5 6 ] ) ;
7 f i g u r e , imshow ( I ) ;
8 %% t h e s i m p l e s t way t o p l o t i t s h i s t o g r a m i s t o u s e
i m h i s t with no output s p e c i f i e d
9

10 figure , imhist ( I ) ;
11

12 %% Histograms o f t e n a r e p l o t t e d u s i n g bar g r a p h s
13

14 h=i m h i s t ( I ) ;
15 h1=h ( 1 : 1 0 : 2 5 6 ) ;
16 horz =1:10:256;
17 f i g u r e , bar ( horz , h1 ) ;
18 a x i s ( [ 0 255 0 2 5 0 0 ] )
19 s e t ( gca , ’ x t i c k ’ , 0 : 5 0 : 2 5 5 )
20 s e t ( gca , ’ y t i c k ’ , 0 : 5 0 0 : 2 5 0 0 )
21 %% Stem graph
22

23 h=i m h i s t ( I ) ;
24 h1=h ( 1 : 1 0 : 2 5 6 ) ;
25 horz =1:10:256;
26 f i g u r e , stem ( horz , h1 , ’ f i l l ’ ) ;
27 a x i s ( [ 0 255 0 2 5 0 0 ] )
28 s e t ( gca , ’ x t i c k ’ , 0 : 5 0 : 2 5 5 )
29 s e t ( gca , ’ y t i c k ’ , 0 : 5 0 0 : 2 5 0 0 )
30

31 %% p l o t graph
32

33 h=i m h i s t ( I ) ;
34 figure , plot (h) ;
35 a x i s ( [ 0 255 0 2 5 0 0 ] )
36 s e t ( gca , ’ x t i c k ’ , 0 : 5 0 : 2 5 5 )
37 s e t ( gca , ’ y t i c k ’ , 0 : 5 0 0 : 2 5 0 0 )

6
1.2.1 Results

(a) Input Image (b) imhist

(c) bar (d) stem

(e) plot

Figure 1: Various ways to plot an image histogram

7
1.3 Application 1
Develop an automatic thresholding algorithm for defect identification
1 clc ;
2 clear all ;
3 close all ;
4

5 I 1 = imread ( ’ b l o o d . png ’ ) ;
6 % f a s t e s t h i s t o g r a m computation .
7 I = im2uint8 ( I1 ( : ) ) ;
8

9 % STEP 1 : Compute mean i n t e n s i t y o f image from histogram ,


s e t T=mean ( I )
10 [ counts ,N]= i m h i s t ( I ) ;
11 i =1;
12 mu=cumsum ( c o u n t s ) ;
13 T( i ) =(sum (N. ∗ c o u n t s ) ) /mu( end ) ;
14 T( i )=round (T( i ) ) ;
15

16 % STEP 2 : compute Mean above T (MAT) and Mean below T (


MBT) u s i n g T from
17 % step 1
18 mu2=cumsum ( c o u n t s ( 1 :T( i ) ) ) ;
19 MBT=sum (N( 1 :T( i ) ) . ∗ c o u n t s ( 1 :T( i ) ) ) /mu2( end ) ;
20

21 mu3=cumsum ( c o u n t s (T( i ) : end ) ) ;


22 MAT=sum (N(T( i ) : end ) . ∗ c o u n t s (T( i ) : end ) ) /mu3( end ) ;
23 i=i +1;
24 % new T = (MAT+MBT) /2
25 T( i )=round ( (MAT+MBT) / 2 ) ;
26

27 % STEP 3 t o n : r e p e a t s t e p 2 i f T( i )˜=T( i −1)


28 w h i l e abs (T( i )−T( i −1) )>=1
29 mu2=cumsum ( c o u n t s ( 1 :T( i ) ) ) ;
30 MBT=sum (N( 1 :T( i ) ) . ∗ c o u n t s ( 1 :T( i ) ) ) /mu2( end ) ;
31

32 mu3=cumsum ( c o u n t s (T( i ) : end ) ) ;


33 MAT=sum (N(T( i ) : end ) . ∗ c o u n t s (T( i ) : end ) ) /mu3( end ) ;
34

35 i=i +1;
36 T( i )=round ( (MAT+MBT) / 2 ) ;
37 T h r e s h o l d=T( i ) ;
38 end

8
39

40 % Normalize t h e t h r e s h o l d t o t h e r a n g e [ i , 1 ] .
41 l e v e l = ( T h r e s h o l d − 1 ) / (N( end ) − 1 ) ;
42

43 BW = im2bw ( I1 , l e v e l ) ;
44 imshow (BW) ;

9
1.3.1 Results

(a) Input Image (b) Output Image

Figure 2: Results of the automatic thresholding algorithm

1.4 Experimental Tasks


Task 1: Test the application for different set of ‘10 images.
Task 2: Give a performance report on the tests performed with a perfor-
mance parameter.
Task 3: Write a report.

10
2 Experiment 2
Histogram Processing for Contrast Enhancement. This laboratory experi-
ment provides for histogram equalization and matching for contrast enhance-
ment. The user must express these algorithms on 10 sets of different contrast
images and measure the performance of the algorithms on each image set. This
experiment illustrates the relationship among the intensities (gray levels) of an
image and its histogram. It shows how to improve the image by equalizing the
histogram.

2.1 Histogram Equalization


Histogram Equalization is a method which increases the dynamic range of
the gray-level in a low-contrast image to cover full range of gray-levels.
Histogram equalization is achieved by having a transformation function T (r),
which can be defined to be the Cumulative Distribution Function (CDF) of a
given Probability Density Function (PDF) of a gray-levels in a given image (the
histogram of an image can be considered as the approximation of the PDF of
that image).
2.1.1 Continuous Case
* For intensity levels that are continuous quantities normalised to the range
[0, 1].

Let Pr (r) – probability density function (PDF) of the intensity levels. The
following transformation on the input levels to obtain output levels, S :
Z r
S = T (r) = Pr (w)dw
0

w-dummy variable of integration.


* The PDF of the output levels is uniform:


1 0≤S≤1
PS (S) =
0 Otherwise

* Image, whose intensity levels are equally likely, and it cover the entire
range [0 ,1].

This transformation is called intensity-levels equalization process (and it’s


nothing more than the cumulative distribution function (CDF)).

11
2.1.2 Discrete case (quantities):
The equalization transformation becomes:
Xk Xk nj
Sk = T (rk ) = Pr (rj ) = , f or k = 1, 2, ..., L
j=1 j=1 n
Sk –Intensity value of the output image corresponding to value rk in the input
image.

12
2.2 Matlab Code

1 clc ;
2 close all ;
3 clear all ;
4

5 F=imread ( ’ low−c o n t r a s t −ex −02. png ’ ) ;


6 F=i m r e s i z e (F , [ 3 0 0 3 0 0 ] ) ;
7

8 [ rows c o l s ]= s i z e (F) ;
9 G=F ;
10

11 %% Histogram a r r a y
12 myhist=z e r o s ( 2 5 6 , 1 ) ;
13 f o r k=0 : 255
14 myhist ( k+1)=numel ( f i n d (F==k ) ) ; %number o f e l e m e n t s where
F has gray l e v e l e q u a l t o ’ k ’
15 end
16 %End o f Histogram a r r a y
17

18

19 %% C a l c u l a t e c d f
20 c d f=z e r o s ( 2 5 6 , 1 ) ;
21 c d f ( 1 )=myhist ( 1 ) ;
22 f o r k=2 : 256
23 c d f ( k )=c d f ( k−1)+myhist ( k ) ;
24 end
25 %End o f C a l c u l a t e c d f
26

27 %% Find E q u a l i z e d h i s t o g r a m a r r a y
28 cumprob=c d f / ( rows . ∗ c o l s ) ;
29 e q u a l i z e d h i s t=f l o o r ( ( cumprob ) . ∗ 2 5 5 ) ;
30

31 f o r i =1 : c o l s
32 f o r j =1 : rows
33 f o r m = 0 : 255
34 i f (F( i , j )==m)
35 G( i , j )=e q u a l i z e d h i s t (m+1) ;
36 end
37 end
38 end
39 end
40

13
41 %% E q u a l i z e d Histogram a r r a y
42 myeqhist=z e r o s ( 2 5 6 , 1 ) ;
43 f o r k=0 : 255
44 myeqhist ( k+1)=numel ( f i n d (G==k ) ) ;
45 end
46 %End o f E q u a l i z e d Histogram a r r a y
47

48 %% p l o t s and f i g u r e s
49 figure (1) ;
50 subplot (2 ,1 ,1) ;
51 imshow (F) ;
52 t i t l e ( ’ O r i g i n a l Image ’ ) ;
53 subplot (2 ,1 ,2) ;
54 bar ( myhist ) ;
55 t i t l e ( ’ Histogram o f O r i g i n a l Image ’ ) ;
56

57 figure (2) ;
58 subplot (2 ,1 ,1) ;
59 imshow (G) ;
60 t i t l e ( ’ Histogram E q u a l i z e d Image ’ ) ;
61 subplot (2 ,1 ,2) ;
62 bar ( myeqhist ) ;
63 t i t l e ( ’ E q u a l i z e d Histogram o f Image ’ ) ;
64 %end p l o t s and f i g u r e s

14
2.2.1 Results

(a) Input image (b) its histogram

(c) Histogram equalized


(d) its histogram
image

Figure 3: Histogram Processing for Contrast Enhancement

15
2.3 Application 2
Color Image Enhancement Using Histogram Equalization Method without
Changing Hue and Saturation
1 clc ;
2 clear all ;
3 close all ;
4 %% Improving C o l o r e d Low c o n t r a s t Image u s i n g HE
5 I = imread ( ’LC6 . j p g ’ ) ;
6 I = imresize ( I ,[512 512]) ;
7 imshow ( I )
8 HSv = rgb2hsv ( I ) ;
9 % f i g u r e , imshow (HSv)
10 H = HSv ( : , : , 1 ) ;
11 S = HSv ( : , : , 2 ) ;
12 V = HSv ( : , : , 3 ) ;
13

14 Vc = V∗ 3 . 4 ;
15 HSV = c a t ( 3 ,H, S , Vc ) ;
16 RGB = hsv2rgb (HSV) ;
17 f i g u r e , imshow (RGB) ;
18

19 %% RGB 2 HSI
20 A = I;
21 I=d o u b l e (A) / 2 5 5 ;
22

23 R=I ( : , : , 1 ) ;
24 G=I ( : , : , 2 ) ;
25 B=I ( : , : , 3 ) ;
26

27 %Hue
28 numi =1/2∗((R−G) +(R−B) ) ;
29 denom=((R−G) . ˆ 2 + ( (R−B) . ∗ (G−B) ) ) . ˆ 0 . 5 ;
30

31 %To a v o i d d i v i d e by z e r o e x c e p t i o n add a s m a l l number i n


t h e denominator
32 H=a c o s d ( numi . / ( denom +0.000001) ) ;
33

34 %I f B>G then H= 360−Theta


35 H(B>G)=360−H(B>G) ;
36

37 %Normalize t o t h e r a n g e [ 0 1 ]
38 H=H/ 3 6 0 ;

16
39

40 %S a t u r a t i o n
41 S=1− ( 3 . / ( sum ( I , 3 ) +0.000001) ) . ∗ min ( I , [ ] , 3 ) ;
42

43 %I n t e n s i t y
44 I=sum ( I , 3 ) . / 3 ;
45 I = I ∗4.4;
46

47 %HSI
48 HSI=z e r o s ( s i z e (A) ) ;
49 HSI ( : , : , 1 )=H;
50 HSI ( : , : , 2 )=S ;
51 HSI ( : , : , 3 )=I ;
52

53 %% HSI t o RGB
54 H1=HSI ( : , : , 1 ) ;
55 S1=HSI ( : , : , 2 ) ;
56 I 1=HSI ( : , : , 3 ) ;
57

58 %M u l t i p l y Hue by 360 t o r e p r e s e n t i n t h e r a n g e [ 0 3 6 0 ]
59 H1=H1 ∗ 3 6 0 ;
60

61 %P r e a l l o c a t e t h e R,G and B components


62 R1=z e r o s ( s i z e (H1) ) ;
63 G1=z e r o s ( s i z e (H1) ) ;
64 B1=z e r o s ( s i z e (H1) ) ;
65 RGB1=z e r o s ( [ s i z e (H1) , 3 ] ) ;
66

67 %RG S e c t o r (0<=H<120)
68 %When H i s i n t h e above s e c t o r , t h e RGB components
equations are
69

70 B1 (H1<120)=I 1 (H1<120) .∗(1 − S1 (H1<120) ) ;


71 R1(H1<120)=I 1 (H1<120) . ∗ ( 1 + ( ( S1 (H1<120) . ∗ c o s d (H1(H1<120) )
) . / c o s d (60−H1(H1<120) ) ) ) ;
72 G1(H1<120) =3.∗ I 1 (H1<120)−(R1(H1<120)+B1 (H1<120) ) ;
73

74 %GB S e c t o r (120<=H<240)
75 %When H i s i n t h e above s e c t o r , t h e RGB components
equations are
76 %S u b t r a c t 120 from Hue
77 H2=H1−120;
78

79 R1(H1>=120&H1<240)=I 1 (H1>=120&H1<240) .∗(1 − S1 (H1>=120&H1


<240) ) ;
80 G1(H1>=120&H1<240)=I 1 (H1>=120&H1<240) . ∗ ( 1 + ( ( S1 (H1>=120&

17
H1<240) . ∗ c o s d (H2(H1>=120&H1<240) ) ) . / c o s d (60−H2(H1
>=120&H1<240) ) ) ) ;
81 B1 (H1>=120&H1<240) =3.∗ I 1 (H1>=120&H1<240)−(R1(H1>=120&H1
<240)+G1(H1>=120&H1<240) ) ;
82

83 %BR S e c t o r (240<=H<=360)
84 %When H i s i n t h e above s e c t o r , t h e RGB components
equations are
85 %S u b t r a c t 240 from Hue
86

87 H2=H1−240;
88

89 G1(H1>=240&H1<=360)=I 1 (H1>=240&H1<=360) .∗(1 − S1 (H1>=240&


H1<=360) ) ;
90 B1 (H1>=240&H1<=360)=I 1 (H1>=240&H1<=360) . ∗ ( 1 + ( ( S1 (H1
>=240&H1<=360) . ∗ c o s d (H2(H1>=240&H1<=360) ) ) . / c o s d (60−
H2(H1>=240&H1<=360) ) ) ) ;
91 R1(H1>=240&H1<=360)=3.∗ I 1 (H1>=240&H1<=360)−(G1(H1>=240&
H1<=360)+B1 (H1>=240&H1<=360) ) ;
92

93 %Form RGB Image


94 RGB1 ( : , : , 1 )=R1 ;
95 RGB1 ( : , : , 2 )=G1 ;
96 RGB1 ( : , : , 3 )=B1 ;
97

98 %R e p r e s e n t t h e image i n t h e r a n g e [ 0 2 5 5 ]
99 RGB1=i m 2 u i n t 8 (RGB1) ;
100 f i g u r e , imshow (RGB1) ;

18
2.3.1 Results

(a) Input image

(b) Enhanced image using HSV (c) Enhanced image using HSI
method method

Figure 4: Enhancement of night-time park image

2.4 Experimental Tasks


Task 1: Test the application for different set of ‘10 images.
Task 2: Give a performance report on the tests performed with a perfor-
mance parameter.
Task 3: Write a report.

19
3 Experiment 3
Spatial Filtering. Linear and Non-linear spatial filtering. Learn the concepts
of designing spatial filters in time domain. Use different kinds of spatial filters to
improve the quality of images. Measure the quality of improved images against
the original images.

3.1 Filtering in the spatial domain (Spatial Filtering)


Refers to image operators that change the gray value at any pixel (x, y)
depending on the pixel values in a square neighborhood centered at (x, y) using
a fixed integer matrix of the same size. The integer matrix is called a filter ,
mask, kernel or a window.
The mechanism of spatial filtering, shown below, consists simply of moving
the filter mask from pixel to pixel in an image. At each pixel (x, y). the response
of the filter at that pixel is calculated using a predefined relationship (linear or
nonlinear).

Figure 5: Spatial filtering

Note:The size of mask must be odd (i.e. 3 × 3, 5 × 5, etc.) to ensure it has


a center. The smallest meaningful size is 3 × 3.

20
3.1.1 Linear Spatial Filtering
The process consists of moving the filter mask from pixel to pixel in an
image. At each pixel (x, y), the response is given by a sum of products of the
filter coefficients and the corresponding image pixels in the area spanned by the
filter mask. For the 3 × 3 mask shown in the previous figure, the result (or
response), R, of linear filtering is:

R = w(−1, −1)f (x − 1, y − 1) + w(−1, 0)f (x − 1, y) + ...


+w(0, 0)f (x, y) + ... + w(1, 0)f (x + 1, y) + w(1, 1)f (x + 1, y + 1)

In general, linear filtering of an image f of size M × N with a filter mask of


size m × n is given by the expression:

a
X b
X
g(x, y) = w(s, t)f (x + s, y + t)
s=−a t=−b

Where a = (m−1)/2 and b = (n−1)/2. To generate a complete filtered image


this equation must be applied for x = 0, 1, 2, ..., M − 1 and y = 0, 1, 2, ..., N − 1.

3.1.2 Nonlinear Spatial Filtering


The operation also consists of moving the filter mask from pixel to pixel in
an image. The filtering operation is based conditionally on the values of the
pixels in the neighborhood, and they do not explicitly use coefficients in the
sum-of-products manner.
For example, noise reduction can be achieved effectively with a nonlinear
filter whose basic function is to compute the median gray-level value in the
neighborhood in which the filter is located. Computation of the median is a
nonlinear operation.

21
3.2 Matlab Code

1 clc ;
2 clear all ;
3 close all ;
4

5 %Read an Image
6 Img = imread ( ’LF . j p g ’ ) ;
7 Img=r g b 2 g r a y ( Img ) ;
8 A = i m n o i s e ( Img , ’ Gaussian ’ , 0 . 0 4 , 0 . 0 0 3 ) ;
9 %A =i m n o i s e ( Img , ’ s a l t & pepper ’ , 0 . 0 1 ) ;
10 %
11 %Image with n o i s e
12 f i g u r e , imshow (A) ;
13

14 %% L i n e a r f i l t e r i n g
15

16 %% H = f s p e c i a l ( ’ g a u s s i a n ’ , h s i z e , sigma ) %r e t u r n s a
r o t a t i o n a l l y symmetric Gaussian l o w p a s s f i l t e r o f s i z e
h s i z e with s t a n d a r d d e v i a t i o n sigma ( p o s i t i v e ) .
17 H = f s p e c i a l ( ’ Gaussian ’ , [ 9 9 ] , 1 . 7 6 ) ;
18

19 %% H = f s p e c i a l ( ’ average ’ , h s i z e ) % r e t u r n s an a v e r a g i n g
f i l t e r h of size hsize .
20 H = f s p e c i a l ( ’ average ’ , [ 3 3 ] ) ;
21

22 %% H = f s p e c i a l ( ’ d i s k ’ , r a d i u s ) r e t u r n s a c i r c u l a r
a v e r a g i n g f i l t e r ( p i l l b o x ) w i t h i n t h e s q u a r e matrix o f
s i z e 2∗ r a d i u s +1.
23 H = f s p e c i a l ( ’ disk ’ ,5) ;
24

25 %% H = f s p e c i a l ( ’ motion ’ , l e n , t h e t a ) r e t u r n s a f i l t e r t o
approximate , once c o n v o l v e d with an image , t h e l i n e a r
motion o f a camera by l e n p i x e l s , with an a n g l e o f
t h e t a d e g r e e s i n a c o u n t e r c l o c k w i s e d i r e c t i o n . The
f i l t e r becomes a v e c t o r f o r h o r i z o n t a l and v e r t i c a l
motions .
26 H = f s p e c i a l ( ’ motion ’ , 9 , 0 ) ;
27

28 %f i l t e r s : ’ ave rage ’ , ’ d i s k ’ , ’ g a u s s i a n ’ , ’ l a p l a c i a n ’ , ’ l o g ’ ’
motion ’ , ’ p r e w i t t ’ , ’ s o b e l ’ , ’ unsharp ’
29 SF = i m f i l t e r (A,H) ;
30 f i g u r e , imshow ( SF ) ;

22
31

32 %% N o n l i n e a r f i l t e r i n g
33 % B = medfilt2 ( , padopt ) c o n t r o l s how m e d f i l t 2 pads t h e
matrix b o u n d a r i e s .
34

35 H = m e d f i l t 2 (A, ’ symmetric ’ ) ;
36 f i g u r e , imshow (H) ;

23
3.2.1 Results

(a) Input image

(b) gaussian filtering Output Image

24
3.3 Application 3
Fingerprint Image Enhancement using Filtering Techniques
1 clc ;
2 clear all ;
3 close all ;
4

5 %Read an Image
6 Img = imread ( ’ FPI1 . png ’ ) ;
7 % Img=r g b 2 g r a y ( Img ) ;
8 % A = i m n o i s e ( Img , ’ Gaussian ’ , 0 . 0 4 , 0 . 0 0 3 ) ;
9 %A =i m n o i s e ( Img , ’ s a l t & pepper ’ , 0 . 0 1 ) ;
10 Img = i m r e s i z e ( Img , [ 2 5 6 2 5 6 ] ) ;
11 %Image with n o i s e
12 f i g u r e , imshow ( Img ) ;
13

14 %% H = f s p e c i a l ( ’ g a u s s i a n ’ , h s i z e , sigma ) %r e t u r n s a
r o t a t i o n a l l y symmetric Gaussian l o w p a s s f i l t e r o f s i z e
h s i z e with s t a n d a r d d e v i a t i o n sigma ( p o s i t i v e ) .
15 Hg = f s p e c i a l ( ’ Gaussian ’ , [ 5 5 ] , 5 0 . 5 ) ;
16

17 %f i l t e r s : ’ ave rage ’ , ’ d i s k ’ , ’ g a u s s i a n ’ , ’ l a p l a c i a n ’ , ’ l o g ’ ’
motion ’ , ’ p r e w i t t ’ , ’ s o b e l ’ , ’ unsharp ’
18 PSF = f s p e c i a l ( ’ g a u s s i a n ’ , 5 , 2 ) ;
19 PSF = Hg ;
20 UNDERPSF = o n e s ( s i z e (PSF) −4) ;
21 INITPSF = padarray (UNDERPSF, [ 2 2 ] , ’ r e p l i c a t e ’ , ’ both ’ ) ;
22 SF = i m f i l t e r ( Img , Hg) ;
23 % SF = h i s t e q ( SF ) ;
24 SF1 = edge ( Img , ’ s o b e l ’ , 0 . 2 5 ) ;
25 WEIGHT = SF1 ;
26 se = s t r e l ( ’ l i n e ’ ,3 ,0) ;
27 WEIGHT = 1−d o u b l e ( i m d i l a t e (WEIGHT, s e ) ) ;
28 WEIGHT( [ 1 : 3 end − ( 0 : 2 ) ] , : ) = 0 ;
29 WEIGHT( : , [ 1 : 3 end − ( 0 : 2 ) ] ) = 0 ;
30 f i g u r e ; imshow (WEIGHT) ; t i t l e ( ’ Weight a r r a y ’ ) ;
31 [ J , P ] = d e c o n v b l i n d ( Img , INITPSF , 2 0 0 , [ ] ,WEIGHT) ;
32 f i g u r e ; imshow ( J ) ; t i t l e ( ’ F Inge r P r i n t Enhnaced Image ’ ) ;
33 SF3 = edge ( J , ’ canny ’ , 0 . 0 2 ) ;
34 f i g u r e , imshow ( ˜ SF3 ) ;
35 SF2 = edge ( Img , ’ canny ’ , 0 . 0 2 ) ;
36 f i g u r e , imshow ( ˜ SF2 )

25
3.3.1 Results

(a) Input image

(b) FInger Print Enhnaced Image (c) Output Image

26
3.4 Experimental Tasks
Task 1: Test the application for different set of ‘10 images.
Task 2: Give a performance report on the tests performed with a perfor-
mance parameter.
Task 3: Write a report.

27
4 Experiment 4
Frequency Domain Filtering: Use low pass and high pass filtering kernels
to sharpen an image with Two-dimensional Fourier transform. The purpose of
this experiment is to provide an understanding of the harmonic content of an
image using the discrete Fourier transform (DFT). This experiment is designed
so the student learns the concept of masking with the DFT.

4.1 Frequency Domain Filtering


The principal objective of enhancement is to process a given image so that
the result is more suitable than the original image for a specific application.
Two broad methods are possible:
(1) Spatial domain techniques and
(2) Frequency domain techniques
Here we concentrate only on Frequency domain techniques. The convolu-
tion theorem is the foundation of frequency domain techniques. Consider the
following spatial domain operation:
g(x, y) = h(x, y) ∗ f (x, y)
The convolution theorem states that following frequency domain relationship
holds:
G(u, v) = H(u, v)F (u, v)
Where G, H and F are the Fourier transforms of g, h and f respectively. H
is known as the transfer function of the process. Many image enhancement
problems can be expressed in the form of the above equation. The goal is to
select a transfer function that changes the image in such a way that some feature
of the image is enhanced. Examples include edge detection, noise removal,
emphasis of information is the image.
The frequency filters process an image in the frequency domain. Application
of this type of filtering is easy:
(1) Transform the image into the Fourier domain
(2) Multiply the image by the filter
(3) Take the inverse transform of the image
All frequency filters can also be implemented in the spatial domain and, if
there exists a simple kernel for the desired filter effect, it is computationally
less expensive to perform the filtering in the spatial domain. Frequency filtering
is more appropriate if no straight forward kernel can be found in the spatial
domain, and may also be more efficient.

28
Figure 8: block diagram

4.1.1 Low-pass filtering


Edges and sharp transitions in the gray levels of an image contribute signifi-
cantly to the high-frequency content of its Fourier transform. Blurring (smooth-
ing) is achieved in the frequency domain by attenuating a specified range of
high-frequency components. This task is performed through low-pass filtering.
The three low-pass filters that we will consider are:
(a) Ideal low-pass filter
(b) Butterworth low-pass filter
(c) Gaussian low-pass filter

4.1.1.1 Ideal low-pass filter


The ideal low-pass filter is one which satisfies the relation:
Where D0 is a specified nonnegative quantity, and D(u, v) is the distance
from point (u, v) to the origin of the frequency plane;

1, if D(u, v) ≤ D0
H(u, v) =
0, if D(u, v) > D0

The filter is called ideal because all the frequencies inside the circle of radius
D are passed with no attenuation, whereas all frequencies outside this circle are
completely attenuated.
1
/2
D(u, v) = (u2 + v 2 )

The drawback of this filter function is a ringing effect that occurs along the
edges of the filtered spatial domain image.

29
4.1.1.2 Butterworth low-pass filter
The Butterworth low-pass filter is an approximation to the ideal filter with-
out the step discontinuity. The transfer function of the Butterworth low-pass
filter of order n and with cut-off frequency locus at a distance D from the origin
is defined by the relation:
1
H(u, v) = 2n
1 + [D(u, v)/D0 ]

Where D(u, v) is given by,



0, if D(u, v) ≤ D0
H(u, v) =
1, if D(u, v) > D0

Unlike the ideal low-pass filter, the Butterworth filter does not have a sharp
discontinuity that establishes a clear cut-off between passed and filtered fre-
quencies.
4.1.1.3 Gaussian low-pass filter
Gaussian low pass filter have smooth transition between pass band and stop
band. It does not introduce any ringing in the output image. The transfer
function of GLPF is given by:
2
(u,v)/2D02
H(u, v) = e−D

Here, D0 = cut off frequency, D(u, v) = D0 .

4.1.2 High-pass filtering


Image sharpening can be achieved in the frequency domain by a high-pass
filtering process. High-pass filter will attenuate the low-frequency components
without disturbing high frequency information.

1, if D(u, v) ≤ D0
H(u, v) =
0, if D(u, v) > D0

We only consider zero-phase-shift filters that are radially symmetric and can
be completely specified by cross section extending as a function of distance from
the origin at center. The three high-pass filters that we will consider are:
(a) Ideal high-pass filter

(b) Butterworth high-pass filter


(c) Gaussian high-pass filter

30
4.1.2.1 Ideal high-pass filter
The ideal high-pass filter is one which satisfies the relation:

0, if D(u, v) ≤ D0
H(u, v) =
1, if D(u, v) > D0

This filter completely attenuates all frequencies inside a circle of radius D0 while
passing, without attenuation, all frequencies outside the circle.
4.1.2.2 Butterworth high-pass filter
As in the case of the Butterworth low-pass filter, common practice is to
select the cut-off frequency locus at points for which H(u, v) is down to 1 /√2 of
its maximum value.
4.1.2.3 Gaussian high-pass filter
Gaussian low pass filter have smooth transition between pass band and stop
band. The parameter D0 is a measure of spread of the gaussian curve. Larger
the value D0 , larger is the cut off frequency. The transfer function of GLPF is
given by:
2
(u,v)/2D02
H(u, v) = 1 − e−D

Here, D0 = cut off frequency, D(u, v) is the distance from origin of fourier
transform.

31
4.2 Matlab Code

1 clc ;
2 clear all ;
3 close all ;
4

5 I=imread ( ’ FF3 . j p g ’ ) ;
6 %I=i m r e s i z e ( I , [ 5 1 2 5 1 2 ] ) ;
7 I=r g b 2 g r a y ( I ) ;
8

9 PQ=s i z e ( I ) ;
10 F=f f t 2 ( I ) ; % Ft o f i n p u t
11 % c r e a t e i d e a l LPF & BLPF
12 M=PQ( 1 ) ;
13 N=PQ( 2 ) ;
14 D0=30;
15 n=2;
16 u =0:(M−1) ;
17 v =0:(N−1) ;
18 % compute t h e i n d i c e s f o r u s e i n meshgrid
19 i d x=f i n d ( u>M/ 2 ) ;
20 u ( i d x )=u ( i d x )−M;
21 i d y=f i n d ( v>N/ 2 ) ;
22 v ( i d y )=v ( i d y )−N;
23 % compute t h e meshgrid a r r a y s
24 [ V,U]= meshgrid ( v , u ) ;
25 % compute t h e d i s t a n c e D(U,V)
26 D=s q r t (U.ˆ2+V. ˆ 2 ) ;
27

28 %% I d e a l Low p a s s f i l t e r
29 H=d o u b l e (D<=D0) ; % low p a s s f i l t e r
30 f i g u r e , s u r f l ( f f t s h i f t (H) ) , s h a d i n g i n t e r p , colormap ( gray ) ;
%p l o t f i l t e r
31 f i g u r e , imshow ( f f t s h i f t ( l o g (H) +1) ) ; %p l o t f i l t e r a s image
32 %f i l t e r m u l t i f i c a t i o n
33 f 1=r e a l ( i f f t 2 (H. ∗ F) ) ; %m u l t i p l i c a t i o n , IDFT , Real p a r t
34 %D i s p l a y images
35 imshow ( I ) ;
36 f i g u r e , imshow ( f f t s h i f t ( l o g (F) +1) , [ ] ) ;%d i s p l a y i n p u t and
i t s DFT
37 f i g u r e , imshow ( f 1 /max( f 1 ( : ) ) ) ; %d i s p l a y output
38 %
39 %% Butterworth Low p a s s f i l t e r

32
40 %
41 H=1./(1+D. / D0) . ˆ ( 2 ∗ n ) ;
42 f i g u r e , s u r f l ( f f t s h i f t (H) ) , s h a d i n g i n t e r p , colormap ( gray ) ;
%p l o t f i l t e r
43 f i g u r e , imshow ( f f t s h i f t ( l o g (H) +1) ) ; %p l o t f i l t e r a s image
44 % f i l t e r i m p l e m e n t a t i o n i n f r e q u e n c y domain
45 f 1=r e a l ( i f f t 2 (H. ∗ F) ) ; % f i l t e r m u l t i p l i c a t i o n , IDFT , Real
part
46 f i g u r e , imshow ( f1 , [ ] ) ; %d i s p l a y output
47 %
48 %% Gaussian Low p a s s f i l t e r
49 %
50 H=exp (−(D. ˆ 2 ) . / ( 2 ∗ ( D0ˆ 2 ) ) ) ; % Gaussian LPF
51 f i g u r e , s u r f l ( f f t s h i f t (H) ) , s h a d i n g i n t e r p , colormap ( gray ) ;
%p l o t f i l t e r
52 f i g u r e , imshow ( f f t s h i f t ( l o g (H) +1) ) ; %p l o t f i l t e r a s image
53 % f i l t e r i m p l e m e n t a t i o n i n f r e q u e n c y domain
54 f 1=r e a l ( i f f t 2 (H. ∗ F) ) ; % f i l t e r m u l t i p l i c a t i o n , IDFT , Real
part
55 f i g u r e , imshow ( f1 , [ ] ) ; %d i s p l a y output
56

57 %% I d e a l High p a s s f i l t e r
58 H=d o u b l e (D<=D0) ; % low p a s s f i l t e r
59 H1=1−H;
60 f i g u r e , s u r f l ( f f t s h i f t (H1) ) , s h a d i n g i n t e r p , colormap ( gray )
; %p l o t f i l t e r
61 f i g u r e , imshow ( f f t s h i f t ( l o g (H1) +1) ) ; %p l o t f i l t e r a s
image
62 %f i l t e r m u l t i f i c a t i o n
63 f 1=r e a l ( i f f t 2 (H1 . ∗ F) ) ; %m u l t i p l i c a t i o n , IDFT , Real p a r t
64 %D i s p l a y images
65 imshow ( I ) ;
66 f i g u r e , imshow ( f f t s h i f t ( l o g (F) +1) , [ ] ) ;%d i s p l a y i n p u t and
i t s DFT
67 f i g u r e , imshow ( f 1 /max( f 1 ( : ) ) ) ; %d i s p l a y output
68

69 %% Butterworth High p a s s f i l t e r
70

71 H=1./(1+D. / D0) . ˆ ( 2 ∗ n ) ;
72 H1=1−H;
73 f i g u r e , s u r f l ( f f t s h i f t (H1) ) , s h a d i n g i n t e r p , colormap ( gray )
; %p l o t f i l t e r
74 f i g u r e , imshow ( f f t s h i f t ( l o g (H1) +1) ) ; %p l o t f i l t e r a s
image
75 % f i l t e r i m p l e m e n t a t i o n i n f r e q u e n c y domain
76 f 1=r e a l ( i f f t 2 (H1 . ∗ F) ) ; % f i l t e r m u l t i p l i c a t i o n , IDFT , Real

33
part
77 f i g u r e , imshow ( f1 , [ ] ) ; %d i s p l a y output
78 %
79 %% Gaussian High p a s s f i l t e r
80 %
81 H=exp (−(D. ˆ 2 ) . / ( 2 ∗ ( D0ˆ 2 ) ) ) ; % Gaussian LPF
82 H1=1−H;
83 f i g u r e , s u r f l ( f f t s h i f t (H1) ) , s h a d i n g i n t e r p , colormap ( gray )
; %p l o t f i l t e r
84 f i g u r e , imshow ( f f t s h i f t ( l o g (H1) +1) ) ; %p l o t f i l t e r a s
image
85 % f i l t e r i m p l e m e n t a t i o n i n f r e q u e n c y domain
86 f 1=r e a l ( i f f t 2 (H1 . ∗ F) ) ; % f i l t e r m u l t i p l i c a t i o n , IDFT , Real
part
87 f i g u r e , imshow ( f1 , [ ] ) ; %d i s p l a y output

34
4.2.1 Results

Figure 9: Input Figure

(a) Ideal low-pass filter (b) Butterworth low-pass filter

(c) Gaussian low-pass filter

Figure 10: Low-pass filter outputs

35
(a) Ideal high-pass filter (b) Butterworth high-pass filter

(c) Gaussian high-pass filter

Figure 11: High-pass filter outputs

4.3 Experimental Tasks


Task 1: Test the application for different set of ‘10 images.
Task 2: Give a performance report on the tests performed with a perfor-
mance parameter.
Task 3: Write a report.

36

You might also like