0% found this document useful (0 votes)
2K views112 pages

Digital Image Processing S Jayaraman S Esakkirajan and T Veerakumar

easy

Uploaded by

Hariharan Hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views112 pages

Digital Image Processing S Jayaraman S Esakkirajan and T Veerakumar

easy

Uploaded by

Hariharan Hari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 112

Scilab Textbook Companion for

Digital Image Processing


by S. Jayaraman, S. Esakkirajan And T.
Veerakumar
1
Created by
R.Senthilkumar
M.E., Assistant Professor
Electronics Engineering
Inst. of Road and Transport Tech., Erode
College Teacher
Dr. R.K.Gnanamurhty, Principal,V. C. E. W.
Cross-Checked by
Anuradha Amrutkar, IIT Bombay
August 10, 2013
1
Funded by a grant from the National Mission on Education through ICT,
http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab
codes written in it can be downloaded from the Textbook Companion Project
section at the website http://scilab.in
Book Description
Title: Digital Image Processing
Author: S. Jayaraman, S. Esakkirajan And T. Veerakumar
Publisher: Tata McGraw - Hill Education Pvt. Ltd, New Delhi
Edition: 3
Year: 2010
ISBN: 978-0-07-014479-8
1
Scilab numbering policy used in this document and the relation to the
above book.
Exa Example (Solved example)
Eqn Equation (Particular equation of the above book)
AP Appendix to Example(Scilab Code that is an Appednix to a particular
Example of the above book)
For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means
a scilab code whose theory is explained in Section 2.3 of the book.
2
Contents
List of Scilab Codes 4
1 Introduction to Image Processing System 9
2 2D Signals and Systems 12
3 Convolution and Correlation 15
4 Image Transforms 24
5 Image Enhancement 34
6 Image Restoration and Denoising 44
7 Image Segmentation 64
8 Object Recognition 73
9 Image Compression 77
10 Binary Image Processing 81
11 Colur Image Processing 85
12 Wavelet based Image Processing 99
3
List of Scilab Codes
Exa 1.3 Program to calculate number of samples required for an
image . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Exa 1.13 False contouring Scilab code . . . . . . . . . . . . . . . 10
Exa 2.12 Frequency Response . . . . . . . . . . . . . . . . . . . 12
Exa 2.16 Frequency Response . . . . . . . . . . . . . . . . . . . 12
Exa 3.1 2D Linear Convlolution . . . . . . . . . . . . . . . . . 15
Exa 3.2 2D Linear Convolution . . . . . . . . . . . . . . . . . . 15
Exa 3.3 2D Linear Convolution . . . . . . . . . . . . . . . . . . 16
Exa 3.7 2D Linear Convolution . . . . . . . . . . . . . . . . . . 17
Exa 3.8 2D Linear Convolution . . . . . . . . . . . . . . . . . . 17
Exa 3.11 Linear Convolution of any signal with an impule signal
given rise to the same signal . . . . . . . . . . . . . . . 18
Exa 3.12 Circular Convolution between two 2D matrices . . . . 18
Exa 3.13 Circular Convolution exspressed as linear convolution
plus alias . . . . . . . . . . . . . . . . . . . . . . . . . 19
Exa 3.14 Linear Cross correlation of a 2D matrix . . . . . . . . 20
Exa 3.15 Circular correlation between two signals . . . . . . . . 20
Exa 3.16 Circular correlation between two signals . . . . . . . . 21
Exa 3.17 Linear auto correlation of a 2D matrix . . . . . . . . . 22
Exa 3.18 Linear Cross correlation of a 2D matrix . . . . . . . . 22
Exa 4.4 DFT of 4x4 grayscale image . . . . . . . . . . . . . . . 24
Exa 4.5 2D DFT of 4X4 grayscale image . . . . . . . . . . . . 24
Exa 4.6 Scilab code to intergchange phase information between
two images . . . . . . . . . . . . . . . . . . . . . . . . 25
Exa 4.10 Program to compute discrete cosine transform . . . . . 26
Exa 4.12 Program to perform KL tranform for the given 2D matrix 28
Exa 4.13 Program to nd the singular value decomposition of
given matrix . . . . . . . . . . . . . . . . . . . . . . . 32
4
Exa 5.5 Scilab code for brightness enhancement . . . . . . . . 34
Exa 5.7 Scilab code for brightness suppression . . . . . . . . . 36
Exa 5.9 Scilab code for Contrast Manipulation . . . . . . . . . 36
Exa 5.13 Scilab code to determine image negative . . . . . . . . 38
Exa 5.16 Scilab code that performs threshold operation . . . . . 38
Exa 5.20 Program performs gray level slicing without background 40
Exa 6.1 Scilab code to create motion blur . . . . . . . . . . . . 44
Exa 6.5 Scilab code performs inverse ltering . . . . . . . . . . 45
Exa 6.7 Scilab code performs inverse ltering . . . . . . . . . . 48
Exa 6.9 Scilab code performs Pseudo inverse ltering . . . . . 50
Exa 6.13 Scilab code to perform wiener ltering of the corrupted
image . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Exa 6.18 Scilab code to Perform Average Filtering operation . . 55
Exa 6.21 Scilab code to Perform median ltering . . . . . . . . 57
Exa 6.23 Scilab code to Perform median ltering of colour image 59
Exa 6.24 Scilab code to Perform Trimmed Average Filter . . . . 61
Exa 7.23 Scilab code for Dierentiation of Gaussian function . . 64
Exa 7.25 Scilab code for Dierentiation of Gaussian Filter function 67
Exa 7.27 Scilab code for Edge Detection using Dierent Edge de-
tectors . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Exa 7.30 Scilab code to perform watershed transform . . . . . . 71
Exa 8.4 To verify the given matrix is a covaraince matrix . . . 73
Exa 8.5 To compute the covariance of the given 2D data . . . 73
Exa 8.9 Develop a perceptron AND function with bipolar inputs
and targets . . . . . . . . . . . . . . . . . . . . . . . . 75
Exa 9.9 Program performs Block Truncation Coding BTC . . . 77
Exa 9.59 Program performs Block Truncation Coding . . . . . . 79
Exa 10.17 Scilab Code for dilation and erosion process . . . . . . 81
Exa 10.19 Scilab Code to perform an opening and closing opera-
tion on the image . . . . . . . . . . . . . . . . . . . . 82
Exa 11.4 Read an RGB image and extract the three colour com-
ponents red green blue . . . . . . . . . . . . . . . . . . 85
Exa 11.12 Read a Colour image and separate the colour image into
red green and blue planes . . . . . . . . . . . . . . . . 86
Exa 11.16 Compute the histogram of the colour image . . . . . . 88
Exa 11.18 Perform histogram equalisation of the given RGB image 90
Exa 11.21 This program performs median ltering of the colour
image . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
5
Exa 11.24 Fitlering only the luminance component . . . . . . . . 92
Exa 11.28 Perform gamma correction for the given colour image . 93
Exa 11.30 Perform Pseudo Colouring Operation . . . . . . . . . . 95
Exa 11.32 Read an RGB image and segment it using the threshold
method . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Exa 12.9 Scilab code to perform wavelet decomposition . . . . . 99
Exa 12.42 Scilab code to generate dierent levels of a Gaussian
pyramid . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Exa 12.57 Scilab code to implement watermarking in spatial domain 101
Exa 12.63 Scilab code to implement wavelet based watermarking 104
AP 1 2D Fast Fourier Transform . . . . . . . . . . . . . . . 107
AP 2 2D Inverse FFT . . . . . . . . . . . . . . . . . . . . . 108
AP 3 Median Filtering function . . . . . . . . . . . . . . . . 108
AP 4 To caculate gray level . . . . . . . . . . . . . . . . . . 109
AP 5 To change the gray level of gray image . . . . . . . . . 109
6
List of Figures
1.1 False contouring Scilab code . . . . . . . . . . . . . . . . . . 11
2.1 Frequency Response . . . . . . . . . . . . . . . . . . . . . . 13
2.2 Frequency Response . . . . . . . . . . . . . . . . . . . . . . 14
4.1 Scilab code to intergchange phase information between two
images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.2 Program to compute discrete cosine transform . . . . . . . . 29
4.3 Program to compute discrete cosine transform . . . . . . . . 30
5.1 Scilab code for brightness enhancement . . . . . . . . . . . . 35
5.2 Scilab code for brightness suppression . . . . . . . . . . . . . 37
5.3 Scilab code for Contrast Manipulation . . . . . . . . . . . . 39
5.4 Scilab code to determine image negative . . . . . . . . . . . 39
5.5 Scilab code that performs threshold operation . . . . . . . . 41
5.6 Program performs gray level slicing without background . . 43
6.1 Scilab code to create motion blur . . . . . . . . . . . . . . . 45
6.2 Scilab code performs inverse ltering . . . . . . . . . . . . . 47
6.3 Scilab code performs inverse ltering . . . . . . . . . . . . . 49
6.4 Scilab code performs inverse ltering . . . . . . . . . . . . . 51
6.5 Scilab code performs Pseudo inverse ltering . . . . . . . . . 53
6.6 Scilab code to perform wiener ltering of the corrupted image 56
6.7 Scilab code to Perform Average Filtering operation . . . . . 58
6.8 Scilab code to Perform median ltering . . . . . . . . . . . . 60
6.9 Scilab code to Perform median ltering of colour image . . . 61
6.10 Scilab code to Perform Trimmed Average Filter . . . . . . . 63
7.1 Scilab code for Dierentiation of Gaussian function . . . . . 65
7.2 Scilab code for Dierentiation of Gaussian function . . . . . 66
7
7.3 Scilab code for Dierentiation of Gaussian Filter function . . 67
7.4 Scilab code for Dierentiation of Gaussian Filter function . . 68
7.5 Scilab code for Edge Detection using Dierent Edge detectors 70
7.6 Scilab code to perform watershed transform . . . . . . . . . 72
9.1 Program performs Block Truncation Coding . . . . . . . . . 80
10.1 Scilab Code for dilation and erosion process . . . . . . . . . 82
10.2 Scilab Code to perform an opening and closing operation on
the image . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11.1 Read an RGB image and extract the three colour components
red green blue . . . . . . . . . . . . . . . . . . . . . . . . . . 87
11.2 Read a Colour image and separate the colour image into red
green and blue planes . . . . . . . . . . . . . . . . . . . . . . 89
11.3 This program performs median ltering of the colour image . 92
11.4 Fitlering only the luminance component . . . . . . . . . . . 94
11.5 Perform gamma correction for the given colour image . . . . 95
11.6 Perform Pseudo Colouring Operation . . . . . . . . . . . . . 97
11.7 Read an RGB image and segment it using the threshold method 98
12.1 Scilab code to generate dierent levels of a Gaussian pyramid 102
12.2 Scilab code to implement watermarking in spatial domain . . 104
8
Chapter 1
Introduction to Image
Processing System
Scilab code Exa 1.3 Program to calculate number of samples required for
an image
1 // Capti on : Program t o c a l c u l a t e number o f s ampl es
r e q ui r e d f o r an i mage
2 // Example1 . 3
3 // page 12
4 clc;
5 close;
6 // di mens i on o f t he i mage i n i nc he s
7 m = 4;
8 n = 6;
9 N = 400; // number o f dot s per i nc h i n each d i r e c t i o n
10 N2 = 2*N; // number o f dot s per i nc h i n both
h o r i z o n t a l & v e r t i c a l
11 Fs = m*N2*n*N2;
12 disp(Fs, Number o f s ampl es r e uq i r e d t o pr e s e r v e t he
i nf o r ma t i o n i n t he i mage= )
13 // Re s ul t
14 //Number o f s ampl es r e uq i r e d t o pr e s e r v e t he
i nf o r ma t i o n i n t he i mage=
9
15 // 15360000.
check Appendix AP 4 for dependency:
gray.sci
check Appendix AP 5 for dependency:
grayslice.sci
Scilab code Exa 1.13 False contouring Scilab code
1 // Capti on : Fal s e c ont our i ng S c i l a b code
2 // Fi g1 . 1 3
3 // page 13
4 clc;
5 close;
6 a =ReadImage( E: \DIP JAYARAMAN\ Chapter1 \ t i g e r pub . j pg
);
7 a = uint8(a);
8 figure
9 imshow(a)
10 title( Or i g i na l i mage );
11 // us i ng 128 gr ay l e v e l s
12 figure
13 a_128 = grayslice(a,128);
14 gray_128 = gray (128);
15 ShowImage(a_128 , Image wi th 128 gr ay l e v e l s ,
gray_128);
16 // us i ng 64 gr ay l e v e l s
17 figure
18 a_64 = grayslice(a,64);
19 gray_64 = gray (64);
20 ShowImage(a_64 , Image wi th 64 gr ay l e v e l s ,gray_64);
21 // us i ng 32 gr ay l e v e l s
22 figure
23 a_32 = grayslice(a,32);
10
Figure 1.1: False contouring Scilab code
24 gray_32 = gray (32);
25 ShowImage(a_32 , Image wi th 32 gr ay l e v e l s ,gray_32);
26 // us i ng 16 gr ay l e v e l s
27 figure
28 a_16 = grayslice(a,16);
29 gray_16 = gray (16);
30 ShowImage(a_16 , Image wi th 16 gr ay l e v e l s ,gray_16);
31 // us i ng 8 gr ay l e v e l s
32 a_8 = grayslice(a,8);
33 gray_8 = gray (8);
34 ShowImage(a_8 , Image wi th 8 gr ay l e v e l s ,gray_8);
11
Chapter 2
2D Signals and Systems
Scilab code Exa 2.12 Frequency Response
1 // Capti on : Frequency Response
2 // Fi g2 . 1 2
3 // page 60
4 clc;
5 close;
6 [X, Y] = meshgrid(-%pi :.09: %pi);
7 Z = 2*cos(X)+2* cos(Y);
8 surf(X,Y,Z);
9 xgrid (1)
Scilab code Exa 2.16 Frequency Response
1 // Capti on : Frequency Response
2 // Fi g2 . 1 6
3 // page 64
12
Figure 2.1: Frequency Response
4 clc;
5 close;
6 [X, Y] = meshgrid(-%pi :.05: %pi);
7 Z = 2-cos(X)-cos(Y);
8 surf(X,Y,Z);
9 xgrid (1)
13
Figure 2.2: Frequency Response
14
Chapter 3
Convolution and Correlation
Scilab code Exa 3.1 2D Linear Convlolution
1 // Capti on : 2D Li ne ar Convol ut i on
2 // Example3 . 1 & Example3 . 4
3 // page 85 & page 107
4 clc;
5 x =[4,5,6;7,8 ,9];
6 h = [1;1;1];
7 disp(x, x= )
8 disp(h, h= )
9 [y,X,H] = conv2d2(x,h);
10 disp(y, Li ne ar 2D c o nvo l ut i o n r e s u l t y = )
11 // Re s ul t
12 // Li ne ar 2D c o nvo l ut i o n r e s u l t y =
13 //
14 // 4 . 5 . 6 .
15 // 1 1 . 1 3 . 1 5 .
16 // 1 1 . 1 3 . 1 5 .
17 // 7 . 8 . 9 .
Scilab code Exa 3.2 2D Linear Convolution
15
1 // Capti on : 2D Li ne ar Convol ut i on
2 // Example3 . 2 & Example3 . 5 & Example3 . 9
3 // page 91 & page 108 & page 116
4 clc;
5 x =[1,2,3;4,5,6;7 ,8,9];
6 h = [1 ,1;1 ,1;1 ,1];
7 y = conv2d2(x,h);
8 disp(y, Li ne ar 2D c o nvo l ut i o n r e s u l t y = )
9 // Re s ul t
10 // Li ne ar 2D c o nvo l ut i o n r e s u l t y =
11 //
12 // 1 . 3 . 5 . 3 .
13 // 5 . 1 2 . 1 6 . 9 .
14 // 1 2 . 2 7 . 3 3 . 1 8 .
15 // 1 1 . 2 4 . 2 8 . 1 5 .
16 // 7 . 1 5 . 1 7 . 9 .
17 //
Scilab code Exa 3.3 2D Linear Convolution
1 // Capti on : 2D Li ne ar Convol ut i on
2 // Example3 . 3 & Example3 . 6 & Example3 . 1 0
3 // page 100 & page 109 & page 119
4 clc;
5 x =[1,2,3;4,5 ,6;7 ,8,9];
6 h = [3,4,5];
7 y = conv2d2(x,h);
8 disp(y, Li ne ar 2D c o nvo l ut i o n r e s u l t y = )
9 // Re s ul t
10 // Li ne ar 2D c o nvo l ut i o n r e s u l t y =
11 //
12 // 3 . 1 0 . 2 2 . 2 2 . 1 5 .
13 // 1 2 . 3 1 . 5 8 . 4 9 . 3 0 .
14 // 2 1 . 5 2 . 9 4 . 7 6 . 4 5 .
16
Scilab code Exa 3.7 2D Linear Convolution
1 // Capti on : 2D Li ne ar Convol ut i on
2 // Example3 . 7
3 // page 111
4 clc;
5 x =[1 ,2;3 ,4];
6 h = [5 ,6;7 ,8];
7 y = conv2d2(x,h);
8 disp(y, Li ne ar 2D c o nvo l ut i o n r e s u l t y = )
9 // Re s ul t
10 // Li ne ar 2D c o nvo l ut i o n r e s u l t y =
11 // Li ne ar 2D c o nvo l ut i o n r e s u l t y =
12 //
13 // 5 . 1 6 . 1 2 .
14 // 2 2 . 6 0 . 4 0 .
15 // 2 1 . 5 2 . 32
Scilab code Exa 3.8 2D Linear Convolution
1 // Capti on : 2D Li ne ar Convol ut i on
2 // Example3 . 8
3 // page 113
4 clc;
5 x =[1,2,3;4,5 ,6;7 ,8,9];
6 h = [1;1;1];
7 y = conv2d2(x,h);
8 disp(y, Li ne ar 2D c o nvo l ut i o n r e s u l t y = )
9 // Re s ul t
10 // Li ne ar 2D c o nvo l ut i o n r e s u l t y =
11 // // 1 . 2 . 3 .
12 // 5 . 7 . 9 .
17
13 // 1 2 . 1 5 . 1 8 .
14 // 1 1 . 1 3 . 1 5 .
15 // 7 . 8 . 9 .
Scilab code Exa 3.11 Linear Convolution of any signal with an impule
signal given rise to the same signal
1 // Capti on : Li ne ar COnvol uti on o f any s i g n a l wi th an
i mpul s e s i g n a l g i v e s
2 // r i s e t o t he same s i g n a l
3 // Example3 . 1 1
4 // page 121
5 clc;
6 x =[1 ,2;3 ,4];
7 h = 1;
8 y = conv2d2(x,h);
9 disp(y, Li ne ar 2D c o nvo l ut i o n r e s u l t y = )
10 // Re s ul t
11 // Li ne ar 2D c o nvo l ut i o n r e s u l t y =
12 // // Li ne ar 2D c o nvo l ut i o n r e s u l t y =
13 //
14 // 1 . 2 .
15 // 3 . 4 .
Scilab code Exa 3.12 Circular Convolution between two 2D matrices
1 // Capti on : Ci r c ul a r Convol ut i on between two 2D
ma t r i c e s
2 // Example3 . 1 2
3 // page 122
4 clc;
5 x = [1 ,2;3 ,4];
6 h = [5 ,6;7 ,8];
18
7 X = fft2d(x); // 2D FFT o f x mat r i x
8 H = fft2d(h); // 2D FFT o f h mat r i x
9 Y = X.*H; // El ement by El ement mu l t i p l i c a t i o n
10 y = ifft2d(Y);
11 disp(y, Ci r c ul a r Convol ut i on Re s ul t y = )
12 // Re s ul t
13 // Ci r c ul a r Convol ut i on Re s ul t y =
14 //
15 // 7 0 . 6 8 .
16 // 6 2 . 6 0 .
Scilab code Exa 3.13 Circular Convolution exspressed as linear convolu-
tion plus alias
1 // Capti on : Ci r c ul a r Convol ut i on e xpr e s s e d as l i n e a r
c o nvo l ut i o n pl us a l i a s
2 // Example3 . 1 3
3 // page 123
4 clc;
5 x = [1 ,2;3 ,4];
6 h = [5 ,6;7 ,8];
7 y = conv2d(x,h);
8 y1 = [y(:,1)+y(:,$),y(:,2)];
9 y2 = [y1(1,:)+y1($,:);y1(2,:)]
10 disp(y, Li ne ar Convol ut i on r e s u l t y= )
11 disp(y2, c i r c u l a r c o nvo l ut i o n e xpe s s e d as l i n e a r
c o nvo l ut i o n pl us a l i a s = )
12 // Re s ul t
13 // Li ne ar Convol ut i on r e s u l t y=
14 //
15 // 5 . 1 6 . 1 2 .
16 // 2 2 . 6 0 . 4 0 .
17 // 2 1 . 5 2 . 3 2 .
18 //
19 // c i r c u l a r c o nvo l ut i o n e xpe s s e d as l i n e a r
19
c o nvo l ut i o n pl us a l i a s =
20 //
21 // 7 0 . 6 8 .
22 // 6 2 . 6 0 .
23 //
Scilab code Exa 3.14 Linear Cross correlation of a 2D matrix
1 // Capti on : l i n e a r c r o s s c o r r e l a t i o n o f a 2D mat r i x
2 // Example3 . 1 4
3 // page 129
4 clc;
5 x = [3 ,1;2 ,4];
6 h1 = [1 ,5;2 ,3];
7 h2 = h1(:,$:-1:1);
8 h = h2($:-1:1,:);
9 y = conv2d(x,h)
10 disp(y, Li ne ar c r o s s Co r r e l a t i o n r e s u l t y= )
11 // Re s ul t
12 // Li ne ar c r o s s Co r r e l a t i o n r e s u l t y=
13 //
14 // 9 . 9 . 2 .
15 // 2 1 . 2 4 . 9 .
16 // 1 0 . 2 2 . 4 .
Scilab code Exa 3.15 Circular correlation between two signals
1 // Capti on : Ci r c ul a r c o r r e l a t i o n between two s i g n a l s
2 // Example3 . 1 5
3 // page 131
4 clc;
5 x = [1 ,5;2 ,4];
6 h = [3 ,2;4 ,1];
20
7 h = h(:,$:-1:1);
8 h = h($:-1:1,:);
9 X = fft2d(x);
10 H = fft2d(h);
11 Y = X.*H;
12 y = ifft2d(Y);
13 disp(y, Ci r c ul a r Co r r e l a t i o n r e s u l t y= )
14 // Re s ul t
15 // Ci r c ul a r Co r r e l a t i o n r e s u l t y=
16 //
17 // 3 7 . 2 3 .
18 // 3 5 . 2 5 .
Scilab code Exa 3.16 Circular correlation between two signals
1 // Capti on : Ci r c ul a r c o r r e l a t i o n between two s i g n a l s
2 // Example3 . 1 6
3 // page 134
4 clc;
5 x = [5 ,10;15 ,20];
6 h = [3 ,6;9 ,12];
7 h = h(:,$:-1:1);
8 h = h($:-1:1,:);
9 X = fft2d(x);
10 H = fft2d(h);
11 Y = X.*H;
12 y = ifft2d(Y);
13 disp(y, Ci r c ul a r Co r r e l a t i o n r e s u l t y= )
14 // Re s ul t
15 // Ci r c ul a r Co r r e l a t i o n r e s u l t y=
16 //
17 // 300. 330.
18 // 420. 450.
21
Scilab code Exa 3.17 Linear auto correlation of a 2D matrix
1 // Capti on : l i n e a r auto c o r r e l a t i o n o f a 2D mat r i x
2 // Example3 . 1 7
3 // page 136
4 clc;
5 x1 = [1 ,1;1 ,1];
6 x2 = x1(:,$:-1:1);
7 x2 = x2($:-1:1,:);
8 x = conv2d(x1,x2)
9 disp(x, Li ne ar auto Co r r e l a t i o n r e s u l t x= )
10 // Re s ul t
11 // Li ne ar auto Co r r e l a t i o n r e s u l t x=
12 //
13 // 1 . 2 . 1 .
14 // 2 . 4 . 2 .
15 // 1 . 2 . 1 .
Scilab code Exa 3.18 Linear Cross correlation of a 2D matrix
1 // Capti on : l i n e a r c r o s s c o r r e l a t i o n o f a 2D mat r i x
2 // Example3 . 1 8
3 // page 141
4 clc;
5 x = [1 ,1;1 ,1];
6 h1 = [1 ,2;3 ,4];
7 h2 = h1(:,$:-1:1);
8 h = h2($:-1:1,:);
9 y = conv2d(x,h)
10 disp(y, Li ne ar c r o s s Co r r e l a t i o n r e s u l t y= )
11 // Re s ul t
12 // Li ne ar c r o s s Co r r e l a t i o n r e s u l t y=
22
13 //
14 // 4 . 7 . 3 .
15 // 6 . 1 0 . 4 .
16 // 2 . 3 . 1 .
23
Chapter 4
Image Transforms
Scilab code Exa 4.4 DFT of 4x4 grayscale image
1 // Capti on : 2D DFT o f 4x4 g r a y s c a l e i mage
2 // Example4 . 4
3 // page 170
4 clc;
5 f = [1,1,1,1;1,1,1,1;1,1,1,1;1,1,1,1];
6 N =4; //4poi nt DFT
7 kernel = dft_mtx(N);
8 F = kernel *(f*kernel );
9 disp(F, 2D DFT o f gi ve n 2D i mage = )
10 // Re s ul t
11 // 2D DFT o f gi ve n 2D i mage =
12 //
13 // 1 6 . 0 0 0
14 // 0 0 0 0
15 // 0 0 0 0
16 // 0 0 0 0
Scilab code Exa 4.5 2D DFT of 4X4 grayscale image
24
1 // Capti on : 2D DFT o f 4x4 g r a y s c a l e i mage
2 // Example4 . 5
3 // page 171
4 clc;
5 F = [16,0,0,0;0,0,0,0;0,0,0,0;0,0,0,0];
6 N =4; //4poi nt DFT
7 kernel = dft_mtx(N);
8 f = (kernel *(F*kernel ))/(N^2);
9 f = real(f);
10 disp(f, I nv e r s e 2D DFT o f t he t r ans f or me d i mage f =
)
11 // Re s ul t
12 // I nv e r s e 2D DFT o f t he t r ans f or me d i mage f =
13 //
14 // 1 . 1 . 1 . 1 .
15 // 1 . 1 . 1 . 1 .
16 // 1 . 1 . 1 . 1 .
17 // 1 . 1 . 1 . 1 .
check Appendix AP 1 for dependency:
fft2d.sce
check Appendix AP 2 for dependency:
ifft2d.sce
Scilab code Exa 4.6 Scilab code to intergchange phase information be-
tween two images
1 // Capti on : S c i l a b code t o i nt e r g c ha ng e phase
i nf o r ma t i o n between two i mages
2 // Example4 . 6
3 // page 174175
4 clc;
5 close;
25
6 a = imread( E: \DIP JAYARAMAN\ Chapter4 \ l e na . png );
//SIVP t ool box
7 b = imread( E: \DIP JAYARAMAN\ Chapter4 \baboon . png );
8 a = rgb2gray(a);
9 b = rgb2gray(b);
10 a = imresize(a,0.5);
11 b = imresize(b,0.5);
12 figure (1)
13 ShowImage(a, Or i g i na l l e na Image ); //IPD t ool box
14 title( Or i g i na l l e na Image );
15 figure (2)
16 ShowImage(b, Or i g i na l baboon Image );
17 title( Or i g i na l baboon Image )
18 ffta = fft2d(double(a));
19 fftb = fft2d(double(b));
20 mag_a = abs(ffta);
21 mag_b = abs(fftb);
22 ph_a = atan(imag(ffta),real(ffta));
23 ph_b = atan(imag(fftb),real(fftb));
24 newfft_a = mag_a .*(exp(%i*ph_b));
25 newfft_b = mag_b .*(exp(%i*ph_a));
26 rec_a = ifft2d(newfft_a);
27 rec_b = ifft2d(newfft_b);
28 figure (3)
29 ShowImage(uint8(rec_a), l e na Image a f t e r phase
r e v e r s a l );
30 title( l e na Image a f t e r phase r e v e r s a l )
31 figure (4)
32 ShowImage(uint8(rec_b), baboon Image a f t e r phase
r e v e r s a l );
33 title( baboon Image a f t e r phase r e v e r s a l )
Scilab code Exa 4.10 Program to compute discrete cosine transform
26
Figure 4.1: Scilab code to intergchange phase information between two im-
ages
27
1 // Capti on : Program t o compute d i s c r e t e c o s i ne
t r anf or m
2 // Example4 . 1 0
3 // page 198
4 clc;
5 N =4; //DCT mat r i x o f or de r f o ur
6 X = dct_mtx(N);
7 disp(X, DCT mat r i x o f or de r f o ur )
8 // Re s ul t
9 //DCT mat r i x o f or de r f o ur
10 //
11 // 0 . 5 0 . 5 0 . 5 0 . 5
12 // 0. 6532815 0. 2705981 0. 2705981
0. 6532815
13 // 0 . 5 0 . 5 0 . 5 0 . 5
14 // 0. 2705981 0. 6532815 0. 6532815
0. 2705981
Scilab code Exa 4.12 Program to perform KL tranform for the given 2D
matrix
1 // Capti on : Program t o per f or m KL t r ans f or m f o r t he
gi ve n 2D mat r i x
2 // Example4 . 1 2
3 // page 208
4 clear;
5 clc;
6 X = [4,3,5,6;4,2,7,7;5,5,6,7];
7 [m,n]= size(X);
8 A = [];
9 E = [];
28
Figure 4.2: Program to compute discrete cosine transform
29
Figure 4.3: Program to compute discrete cosine transform
30
10 for i =1:n
11 A = A+X(:,i);
12 E = E+X(:,i)*X(:,i) ;
13 end
14 mx = A/n; //mean mat r i x
15 E = E/n;
16 C = E - mx*mx ;// c o va r i a nc e mat r i x C = E[ xx ] mxmx
17 [V,D] = spec(C); // e i g e n va l ue s and e i g e n v e c t o r s
18 d = diag(D); // di a g o na l e l e me nt s od e i g e n va l ue s
19 [d,i] = gsort(d); // s o r t i n g t he e l e me nt s o f D i n
de s c e ndi ng or de r
20 for j = 1: length(d)
21 T(:,j)= V(:,i(j));
22 end
23 T =T
24 disp(d, Ei gen Val ues ar e U = )
25 disp(T, The e i g e n ve c t o r mat r i x T = )
26 disp(T, The KL t r anf or m b a s i s i s = )
27 //KL t r ans f or m
28 for i = 1:n
29 Y(:,i)= T*X(:,i);
30 end
31 disp(Y, KL t r a ns f o r ma t i o n o f t he i nput mat r i x Y = )
32 // Re c ons t r uc t i on
33 for i = 1:n
34 x(:,i)= T*Y(:,i);
35 end
36 disp(x, Re c ons t r uc t mat r i x o f t he gi ve n sampl e
mat r i x X = )
37 // Re s ul t
38 // Ei gen Val ues ar e U =
39 // 6. 1963372
40 // 0. 2147417
41 // 0. 0264211
42 // The e i g e n ve c t o r mat r i x T =
43 // 0. 4384533 0. 8471005 0. 3002988
44 // 0. 4460381 0. 4951684 0. 7455591
45 // 0. 7802620 0. 1929481 0. 5949473
31
46 // The KL t r anf or m b a s i s i s =
47 // 0. 4384533 0. 8471005 0. 3002988
48 // 0. 4460381 0. 4951684 0. 7455591
49 // 0. 7802620 0. 1929481 0. 5949473
50 // KL t r a ns f o r ma t i o n o f t he i nput mat r i x Y =
51 // 6. 6437095 4. 5110551 9. 9237632
10. 662515
52 // 3. 5312743 4. 0755729 3. 2373664
4. 4289635
53 // 0. 6254808 1. 0198466 1. 0190104
0. 8336957
54 // Re c ons t r uc t mat r i x o f t he gi ve n sampl e mat r i x x =
55 // 4 . 3 . 5 . 6 .
56 // 4 . 2 . 7 . 7 .
57 // 5 . 5 . 6 . 7 .
Scilab code Exa 4.13 Program to nd the singular value decomposition
of given matrix
1 // Capti on : Program t o f i nd t he s i n g u l a r val ue
de c ompos i t i on o f gi ve n mat r i x
2 // Example4 . 1 3
3 // page 210
4 clear;
5 clc;
6 A = [1,-2,3;3,2,-1];
7 [U,S,V]= svd(A);
8 A_recon = U*S*V;
9 disp(U, U = )
10 disp(S, S = )
11 disp(V, V = )
12 disp(A_recon , A mat r i x f rom svd = )
13 // Re s ul t
14 // U =
15 //
32
16 // 0. 7071068 0. 7071068
17 // 0. 7071068 0. 7071068
18 //
19 // S =
20 //
21 // 4. 2426407 0 . 0 .
22 // 0 . 3. 1622777 0 .
23 //
24 // V =
25 //
26 // 0. 3333333 0. 8944272 0. 2981424
27 // 0. 6666667 2. 776D16 0. 7453560
28 // 0. 6666667 0. 4472136 0. 5962848
29 //
30 // A mat r i x f rom svd =
31 //
32 // 1 . 2 . 3 .
33 // 3 . 2 . 1 .
33
Chapter 5
Image Enhancement
Scilab code Exa 5.5 Scilab code for brightness enhancement
1 // Capti on : S c i l a b code f o r b r i g h t n e s s enhancement
2 // Fi g5 . 5
3 // page 246
4 clc;
5 close;
6 // a = i mread ( E: \DIP JAYARAMAN\ Chapter5 \ pl a t e . GIF ) ;
//SIVP t ool box
7 a = imread( E: \DIP JAYARAMAN\ Chapter4 \baboon . png );
8 a = rgb2gray(a);
9 b = double(a)+50;
10 b = uint8(b);
11 figure (1)
12 ShowImage(a, Or i g i na l Image );
13 title( Or i g i na l Image )
14 figure (2)
15 ShowImage(b, Enhanced Image );
16 title( Enhanced Image )
34
Figure 5.1: Scilab code for brightness enhancement
35
Scilab code Exa 5.7 Scilab code for brightness suppression
1 // Capti on : S c i l a b code f o r b r i g h t n e s s s uppr e s s i o n
2 // Fi g5 . 7
3 // page 247
4 clc;
5 close;
6 a = imread( E: \DIP JAYARAMAN\ Chapter4 \baboon . png );
7 a = rgb2gray(a);
8 b = double(a) -50;
9 b = uint8(b);
10 figure (1)
11 ShowImage(a, Or i g i na l Image );
12 title( Or i g i na l Image )
13 figure (2)
14 ShowImage(b, Br i g ht ne s s Supr e s s e d Image );
15 title( Br i g ht ne s s Supr e s s e d Image )
Scilab code Exa 5.9 Scilab code for Contrast Manipulation
1 // Capti on : S c i l a b code f o r Cont r as t Mani pul at i on
2 // Fi g5 . 9
3 // page 248
4 clc;
5 close;
6 a = imread( E: \DIP JAYARAMAN\ Chapter4 \ l e na . png );
7 a = rgb2gray(a);
8 b = double(a)*0.5;
9 b = uint8(b)
10 c = double(b)*2;
36
Figure 5.2: Scilab code for brightness suppression
37
11 c = uint8(c)
12 figure (1)
13 ShowImage(a, Or i g i na l Image );
14 title( Or i g i na l Image )
15 figure (2)
16 ShowImage(b, De c r e as e i n Cont r as t );
17 title( De c r e as e i n Cont r as t )
18 figure (3)
19 ShowImage(c, I n c r e a s e i n Cont r as t );
20 title( I n c r e a s e i n Cont r as t )
Scilab code Exa 5.13 Scilab code to determine image negative
1 // Capti on : S c i l a b code t o de t e r mi ne i mage ne g a t i ve
2 // Fi g . 5 . 1 3
3 // page 252
4 clc;
5 close;
6 a = imread( E: \DIP JAYARAMAN\ Chapter5 \ l a b e l . j pg );
7 k = 255- double(a);
8 k = uint8(k);
9 imshow(a);
10 title( Or i g i na l onca Image )
11 imshow(k);
12 title( Ne gat i ve o f Or i g i na l Image )
Scilab code Exa 5.16 Scilab code that performs threshold operation
38
Figure 5.3: Scilab code for Contrast Manipulation
Figure 5.4: Scilab code to determine image negative
39
1 // Capti on : S c i l a b code t hat pe r f or ms t hr e s ho l d
o pe r a t i o n
2 // Fi g5 . 1 6
3 // page 254
4 clc;
5 close;
6 a = imread( E: \ Di g i t a l I ma g e Pr o c e s s i ng Ja ya r a ma n \
Chapter5 \ l e na . png );
7 a = rgb2gray(a);
8 [m n] = size(a);
9 t = input( Enter t he t hr e s ho l d par amet er );
10 for i = 1:m
11 for j = 1:n
12 if(a(i,j)<t)
13 b(i,j)=0;
14 else
15 b(i,j)=255;
16 end
17 end
18 end
19 figure (1)
20 ShowImage(a, Or i g i na l Image );
21 title( Or i g i na l Image )
22 figure (2)
23 ShowImage(b, Thr es hol ded Image );
24 title( Thr es hol ded Image )
25 xlabel(sprintf( Thr es hol d val ue i s %g ,t))
26 // Re s ul t
27 // Enter t he t hr e s ho l d par amet er 140
Scilab code Exa 5.20 Program performs gray level slicing without back-
ground
40
Figure 5.5: Scilab code that performs threshold operation
41
1 // Capti on : Program pe r f or ms gr ay l e v e l s l i c i n g
wi t hout background
2 // Fi g . 5 . 2 0
3 // page256
4 clc;
5 x = imread( E: \ Di g i t a l I ma g e Pr o c e s s i ng Ja ya r a ma n \
Chapter5 \ l e na . png );
6 x = rgb2gray(x);
7 y = double(x);
8 [m,n]= size(y);
9 L = max(max(x));
10 a = round(L/2);
11 b = L;
12 for i =1:m
13 for j =1:n
14 if(y(i,j)>=a & y(i,j)<=b)
15 z(i,j) = L;
16 else
17 z(i,j)=0;
18 end
19 end
20 end
21 z = uint8(z);
22 figure (1)
23 ShowImage(x, Or i g i na l Image );
24 title( Or gi nal Image )
25 figure (2)
26 ShowImage(z, Gray Le ve l S l i c i n g );
27 title( Gray Le ve l S l i c i n g wi t hout pr e s e r v i ng
background )
42
Figure 5.6: Program performs gray level slicing without background
43
Chapter 6
Image Restoration and
Denoising
Scilab code Exa 6.1 Scilab code to create motion blur
1 // Capti on : S c i l a b code t o c r e a t e moti on bl ur
2 // Fi g6 . 1
3 // page 326
4 clc;
5 close;
6 a = imread( E: \DIP JAYARAMAN\ Chapter6 \humm. j pg );//
SIVP t ool box
7 // f i l t e r c o e f f i c i e n t s o f f s p e c i a l ( moti on , 1 0 , 2 5 )
8 H =[0 ,0 ,0 ,0 ,0 ,0 ,0 ,0.0032 ,0.0449 ,0.0865 ,0.0072;...
9 0 ,0 ,0 ,0 ,0 ,0.0092 ,0.0509 ,0.0925 ,0.0629 ,0.0213 ,0;...
10 0 ,0 ,0 ,0.0152 ,0.0569 ,0.0985 ,0.0569 ,0.0152 ,0 ,0 ,0;...
11 0 ,0.0213 ,0.0629 ,0.0925 ,0.0509 ,0.0092 ,0 ,0 ,0 ,0 ,0;...
12 0.0072 ,0.0865 ,0.0449 ,0.0032 ,0 ,0 ,0 ,0 ,0 ,0 ,0];
13 Motion_Blur = imfilter(a,H);
14 Motion_Blur =uint8(Motion_Blur);
15 ShowImage(a, o r i g i n a l Image )
16 title( o r i g i n a l Image )
17 figure
18 ShowImage(Motion_Blur , Moti on Bl ur r e d Image )
44
Figure 6.1: Scilab code to create motion blur
19 title( 10 x25 Moti on Bl ur r e d Image )
check Appendix AP 1 for dependency:
fft2d.sce
check Appendix AP 2 for dependency:
ifft2d.sce
Scilab code Exa 6.5 Scilab code performs inverse ltering
1 // Capti on : S c i l a b code pe r f or ms i n v e r s e f i l t e r i n g
2 // Degrade t he i mage by means o f a known bl ur
3 // Apply i n v e r s e f i l t e r t o t he bl ur r e d i mage and s e e
t he r e s t o r e d i mage
4 // Fi g6 . 5
5 // page 330
6 clc;
7 close;
45
8 x =imread( E: \DIP JAYARAMAN\ Chapter6 \ f l o we r 2 . j pg );
9 x=double(rgb2gray(x));
10 [M N]=size(x);
11 h = zeros(M,N);
12 for i = 1:11
13 for j = 1:11
14 h(i,j) = 1/121;
15 end
16 end
17 sigma = sqrt (4*10^( -7));
18 freqx = fft2d(x); // Four i e r t r ans f or m o f i nput i mage
19 freqh = fft2d(h);// Four i e r t r ans f or m o f de gr adat i on
20 y = real(ifft2d(freqh.*freqx));
21 freqy = fft2d(y);
22 powfreqx = freqx .^2/(M*N);
23 alpha = 0.5; // I n d i c a t e s i n v e r s e f i l t e r
24 freqg = ((freqh.) ).*abs(powfreqx)./(abs(freqh .^2)
.*abs(powfreqx)+alpha*sigma ^2);
25 Resfreqx = freqg .* freqy;
26 Resa = real(ifft2d(Resfreqx));
27 x = uint8(x);
28 y = uint8(y);
29 Resa = uint8(Resa)
30 ShowImage(x, Or i g i na l Image )
31 title( Or i g i na l Image )
32 figure
33 ShowImage(y, Degraded Image )
34 title( Degraded Image )
35 figure
36 ShowImage(Resa , Res t or ed Image )
37 title( Res t or ed Image )
check Appendix AP 1 for dependency:
fft2d.sce
check Appendix AP 2 for dependency:
46
Figure 6.2: Scilab code performs inverse ltering
47
ifft2d.sce
Scilab code Exa 6.7 Scilab code performs inverse ltering
1 // Capti on : S c i l a b code pe r f or ms i n v e r s e f i l t e r i n g
2 // Degrade t he i mage by means o f a known bl ur and
whi t e no i s e
3 //The i mage i s degr aded as we l l as c or r upt e d by
no i s e
4 // Apply i n v e r s e f i l t e r t o r e s t o r e t he i mage
5 // Fi g6 . 7
6 // page 332
7 clc;
8 close;
9 x =imread( E: \DIP JAYARAMAN\ Chapter6 \ f l o we r 2 . j pg );
10 x=double(rgb2gray(x));
11 [M N]=size(x);
12 h = zeros(M,N);
13 for i = 1:11
14 for j = 1:11
15 h(i,j) = 1/121;
16 end
17 end
18 sigma = sqrt (4*10^( -7));
19 freqx = fft2d(x); // Four i e r t r ans f or m o f i nput i mage
20 freqh = fft2d(h);// Fo ur i e r t r ans f or m o f de gr adat i on
21 y = real(ifft2d(freqh.*freqx))+10* rand(M,N, normal )
;
22 freqy = fft2d(y);
23 powfreqx = freqx .^2/(M*N);
24 alpha = 0.5; // I n d i c a t e s i n v e r s e f i l t e r
25 freqg = ((freqh.) ).*abs(powfreqx)./(abs(freqh .^2)
.*abs(powfreqx)+alpha*sigma ^2);
26 Resfreqx = freqg .* freqy;
48
Figure 6.3: Scilab code performs inverse ltering
49
27 Resa = real(ifft2d(Resfreqx));
28 x = uint8(x);
29 y = uint8(y);
30 Resa = uint8(Resa)
31 ShowImage(x, Or i g i na l Image )
32 title( Or i g i na l Image )
33 figure
34 ShowImage(y, Degraded+no i s e Image )
35 title( Degraded+no i s e Image )
36 figure
37 ShowImage(Resa , Res t or ed Image )
38 title( Res t or ed Image )
check Appendix AP 1 for dependency:
fft2d.sce
check Appendix AP 2 for dependency:
ifft2d.sce
Scilab code Exa 6.9 Scilab code performs Pseudo inverse ltering
1 // Capti on : S c i l a b code pe r f or ms Pseudo i n v e r s e
f i l t e r i n g
2 // Degrade t he i mage by means o f a known bl ur and
whi t e no i s e
3 //The i mage i s degr aded as we l l as c or r upt e d by
no i s e
4 // Apply Pseudo i n v e r s e f i l t e r t o r e s t o r e t he i mage
5 // Fi g6 . 9
6 // page 333
7 clc;
8 close;
9 x =imread( E: \DIP JAYARAMAN\ Chapter6 \ f l o we r 2 . j pg );
50
Figure 6.4: Scilab code performs inverse ltering
51
10 x=double(rgb2gray(x));
11 [M N]=size(x);
12 h = zeros(M,N);
13 for i = 1:11
14 for j = 1:11
15 h(i,j) = 1/121;
16 end
17 end
18 mask_b = ones (11 ,11) /121;
19 [m1 ,n1] = size(mask_b);
20 Thr_Freq = 0.2;
21 freqx = fft2d(x); // Four i e r t r ans f or m o f i nput i mage
22 freqh = fft2d(h);// Fo ur i e r t r ans f or m o f de gr adat i on
23 y = real(ifft2d(freqh.*freqx))+25* rand(M,N, normal )
;
24 freqy = fft2d(y);
25 psf=zeros(M,N);
26 psf(M/2+1-(m1 -1) /2:M/2+1+(m1 -1)/2,N/2+1 -(n1 -1) /2:N
/2+1+(n1 -1)/2) = mask_b;
27 psf = fftshift(psf);
28 freq_res = fft2d(psf);
29 Inv_filt = freq_res ./(( abs(freq_res)).^2+ Thr_Freq);
30 z = real(ifft2d(freqy.* Inv_filt));
31 x = uint8(x);
32 y = uint8(y);
33 z = uint8(z)
34 ShowImage(x, Or i g i na l Image )
35 title( Or i g i na l Image )
36 figure
37 ShowImage(y, Degraded+no i s e Image )
38 title( Degraded+no i s e Image )
39 figure
40 ShowImage(z, Res t or ed Image )
41 title( Res t or ed Image )
check Appendix AP 1 for dependency:
52
Figure 6.5: Scilab code performs Pseudo inverse ltering
53
fft2d.sce
check Appendix AP 2 for dependency:
ifft2d.sce
Scilab code Exa 6.13 Scilab code to perform wiener ltering of the cor-
rupted image
1 // Capti on : S c i l a b code t o per f or m wi e ne r f i l t e r i n g
o f t he c or r upt e d i mage
2 // Fi g6 . 1 3
3 // Page 339
4 close;
5 clc;
6 x = imread( E: \DIP JAYARAMAN\ Chapter6 \ f l o we r 2 . j pg );
//SIVP t ool box
7 x=double(rgb2gray(x));
8 sigma = 50;
9 Gamma = 1;
10 alpha = 1; // I t i n d i c a t e s Wi ener f i l t e r
11 [M N]=size(x);
12 h = zeros(M,N);
13 for i = 1:5
14 for j = 1:5
15 h(i,j) = 1/25;
16 end
17 end
18 Freqa = fft2d(x);
19 Freqh = fft2d(h);
20 y = real(ifft2d(Freqh.*Freqa)) // i mage de gr adat i on
21 y = y+25* rand(M,N, normal ); // Addi ng random no i s e
wi th normal d i s t r i b u t i o n
22 Freqy = fft2d(y);
23 Powy = abs(Freqy).^2/(M*N);
24 sFreqh = Freqh .*( abs(Freqh) >0)+1/ Gamma*(abs(Freqh)
==0);
54
25 iFreqh = 1/ sFreqh;
26 iFreqh = iFreqh .*( abs(Freqh)*Gamma >1)+Gamma*abs(
sFreqh)*iFreqh *(abs(sFreqh)*Gamma <=1);
27 iFreqh = iFreqh /(max(max(abs(iFreqh))));
28 Powy = Powy .*(Powy >sigma ^2)+sigma ^2*(Powy <= sigma ^2);
29 Freqg = iFreqh .*(Powy -sigma ^2) ./(Powy -(1-alpha)*
sigma ^2);
30 ResFreqa = Freqg .* Freqy;
31 Resa = real(ifft2d(ResFreqa));
32 x = uint8(x);
33 y = uint8(y);
34 Resa = uint8(Resa);
35 ShowImage(x, Or i g i na l Image )
36 title( Or i g i na l Image )
37 figure
38 ShowImage(y, Degraded Image )
39 title( Degraded Image )
40 figure
41 ShowImage(Resa , Res t or ed Image )
42 title( Res t or ed Image )
Scilab code Exa 6.18 Scilab code to Perform Average Filtering operation
1 // Capti on : S c i l a b code t o Perf orm Average Fi l t e r i n g
o pe r a t i o n
2 // Fi g6 . 1 8
3 // page 349
4 clc;
5 close;
6 a= imread( E: \DIP JAYARAMAN\ Chapter6 \ l e nna . j pg );//
SIVP t ool box
7 a=imnoise(a, s a l t & pepper , 0.2); //Add s a l t &pepper
no i s e t ot he i mage
55
Figure 6.6: Scilab code to perform wiener ltering of the corrupted image
56
8 a=double(a);
9 [m n]=size(a);
10 N=input( e nt e r t he window s i z e= ); //The window s i z e
can be 3x3 , 5 x5e t c
11 Start=(N+1)/2;
12 Out_Imag=a;
13 for i=Start:(m-Start +1)
14 for j=Start:(n-Start +1)
15 limit=(N-1)/2;
16 Sum =0;
17 for k=-limit:limit ,
18 for l=-limit:limit ,
19 Sum=Sum+a(i+k,j+l);
20 end
21 end
22 Out_Imag(i,j)=Sum/(N*N);
23 end
24 end
25 a = uint8(a);
26 Out_Imag = uint8(Out_Imag);
27 ShowImage(a, o r i g i n a l Image )
28 title( Noi sy Image )
29 figure
30 ShowImage(Out_Imag , ave r age f i l t e r e d Image )
31 title( 5x5 ave r age f i l t e r e d Image );
Scilab code Exa 6.21 Scilab code to Perform median ltering
1 // Capti on : S c i l a b code t o Perf orm medi an f i l t e r i n g
2 // Fi g6 . 2 1
3 // page 352
4 clc;
5 close;
57
Figure 6.7: Scilab code to Perform Average Filtering operation
6 c = imread( E: \DIP JAYARAMAN\ Chapter6 \cameraman . j pg
);//SIVP t ool box
7 N = input( Enter t he window s i z e );
8 a = double(imnoise(c, s a l t & pepper ,0.2));
9 [m,n] = size(a);
10 b = a;
11 if(modulo(N,2) ==1)
12 Start = (N+1)/2;
13 End = Start;
14 limit1 = (N-1) /2;
15 limit2 = limit1;
16 else
17 Start = N/2;
18 End = Start +1;
19 limit1 = (N/2) -1;
20 limit2 = limit1 +1;
21 end
22 for i = Start:(m-End+1)
23 for j = Start:(n-End+1)
24 I =1;
25 for k = -limit1:limit2
26 for l = -limit1:limit2
27 mat(I)= a(i+k,j+1)
28 I = I+1;
29 end
58
30 end
31 mat = gsort(mat);
32 if(modulo(N,2) ==1)
33 b(i,j) = (mat (((N^2)+1) /2));
34 else
35 b(i,j) = (mat((N^2)/2)+mat(((N^2) /2)+1))/2;
36 end
37 end
38 end
39 a = uint8(a);
40 b = uint8(b);
41 figure
42 ShowImage(c, Or i g i na l Image )
43 title( Or i g i na l Image )
44 figure
45 ShowImage(a, noi s y i mage )
46 title( noi s y i mage )
47 figure
48 ShowImage(b, Median Fi l t e r e d Image )
49 title( 5x5 Median Fi l t e r e d Image )
check Appendix AP 3 for dependency:
Func_medianall.sci
Scilab code Exa 6.23 Scilab code to Perform median ltering of colour
image
1 // Capti on : S c i l a b code t o Perf orm medi an f i l t e r i n g o f
c o l o ur i mage
2 // Fi g6 . 2 3 ( a )
3 // page 353
4 clc;
5 close;
59
Figure 6.8: Scilab code to Perform median ltering
6 a=imread( E: \DIP JAYARAMAN\ Chapter6 \ pe ppe r s . png );
//SIVP t ool box
7 N=input( e nt e r t he window s i z e );
8 b=imresize(a ,[256 ,256]);
9 b=imnoise(b, s a l t & pepper ,.1);
10 [m n]=size(b);
11 R=b(:,:,1);
12 G=b(:,:,2);
13 B=b(:,:,3);
14 Out_R=Func_medianall(R,N);// Appl yi ng Median f i l t e r
t o R pl ane
15 Out_G=Func_medianall(G,N);// Appl yi ng Median f i l t e r
t o G pl ane
16 Out_B=Func_medianall(B,N);// Appl yi ng Median f i l t e r
t o B pl ane
17 Out_Image (:,:,1)=Out_R;
18 Out_Image (:,:,2)=Out_G;
19 Out_Image (:,:,3)=Out_B;
60
Figure 6.9: Scilab code to Perform median ltering of colour image
20 b = uint8(b);
21 Out_Image = uint8(Out_Image);
22 // ShowCol orImage ( b , no i s e added )
23 // t i t l e ( no i s e added )
24 figure
25 ShowColorImage(Out_Image , 3x3 medi an f i l t e r e d )
26 title( 3x3 medi an f i l t e r e d )
Scilab code Exa 6.24 Scilab code to Perform Trimmed Average Filter
1 // Capti on : S c i l a b code t o Perf orm Trimmed Average
Fi l t e r
2 // Al pha tri mmed ave r age f i l t e r
3 // Fi g6 . 2 4
4 // page 355
5 clc;
6 close;
61
7 c = imread( E: \DIP JAYARAMAN\ Chapter6 \ l e nna . j pg );//
SIVP t ool box
8 s = 1; // s de not e s t he number o f va l ue s t o be l e f t
i n t he end
9 r = 1;
10 N = 9; // 3x3 window
11 a = double(imnoise(c, g a us s i a n ));
12 [m,n] = size(a);
13 b = zeros(m,n);
14 for i= 2:m-1
15 for j = 2:n-1
16 mat = [a(i,j),a(i,j-1),a(i,j+1),a(i-1,j),a(i
+1,j),a(i-1,j-1) ,...
17 a(i-1,j+1),a(i-1,j+1),a(i+1,j+1)];
18 sorted_mat = gsort(mat);
19 Sum =0;
20 for k=r+s:(N-s)
21 Sum = Sum+mat(k);
22 end
23 b(i,j)= Sum/(N-r-s);
24 end
25 end
26 a = uint8(a);
27 b = uint8(b);
28 // f i g u r e
29 // imshow( c )
30 // t i t l e ( Or i g i na l Image )
31 figure
32 ShowImage(a, noi s y i mage )
33 title( noi s y i mage )
34 figure
35 ShowImage(b, Trimmed Average Fi l t e r e d Image )
36 title( Trimmed Average Fi l t e r e d Image )
62
Figure 6.10: Scilab code to Perform Trimmed Average Filter
63
Chapter 7
Image Segmentation
Scilab code Exa 7.23 Scilab code for Dierentiation of Gaussian function
1 // Capti on : S c i l a b code f o r Di f f e r e n t i a t i o n o f
Gaus s i an f unc t i o n
2 // Fi g7 . 2 3
3 // page388
4 clc;
5 close;
6 sigma=input( Enter t he val ue o f si gma : )
7 i= -10:.1:10;
8 j= -10:.1:10;
9 r=sqrt(i.*i+j.*j);
10 y=(1/( sigma ^2))*(((r.*r)/sigma ^2) -1).*exp(-r.*r/2*
sigma ^2);
11 plot(i,y)
12 legend(sprintf( The si gma val ue i s %g ,sigma))
13 xtitle( Di f f e r e n t i a t i o n o f Gaus s i an f unc t i o n )
64
Figure 7.1: Scilab code for Dierentiation of Gaussian function
65
Figure 7.2: Scilab code for Dierentiation of Gaussian function
66
Figure 7.3: Scilab code for Dierentiation of Gaussian Filter function
Scilab code Exa 7.25 Scilab code for Dierentiation of Gaussian Filter
function
1 // Capti on : S c i l a b code f o r Di f f e r e n t i a t i o n o f
Gaus s i an Fi l t e r f unc t i o n
2 // Fi g7 . 2 5
3 // page389
4 clc;
5 close;
6 sigma1 = input( Enter t he val ue o f si gma1 : )
7 sigma2 = input( Enter t he val ue o f si gma2 : )
8 i= -10:.1:10;
9 j= -10:.1:10;
67
Figure 7.4: Scilab code for Dierentiation of Gaussian Filter function
10 r=sqrt(i.*i+j.*j);
11 y1 = (1/( sigma1 ^2))*(((r.*r)/sigma1 ^2) -1).*exp(-r.*r
/2* sigma1 ^2);
12 y2 = (1/( sigma2 ^2))*(((r.*r)/sigma2 ^2) -1).*exp(-r.*r
/2* sigma2 ^2);
13 y = y1 -y2;
14 plot(i,y)
15 xtitle( Shape o f DOG Fi l t e r )
16 // Re s ul t
17 // Enter t he val ue o f si gma1 : 4
18 // Enter t he val ue o f si gma2 : 1
19 //
68
Scilab code Exa 7.27 Scilab code for Edge Detection using Dierent Edge
detectors
1 // Capti on : S c i l a b code f o r Edge De t e c t i on us i ng
Di f f e r e n t Edge d e t e c t o r s
2 // [ 1 ] . Sobe l [ 2 ] . Pr e wi t t [ 3 ] . Log [ 4 ] . Canny
3 // Fi g7 . 2 7
4 // page389
5 close;
6 clc;
7 a = imread( E: \DIP JAYARAMAN\ Chapter7 \ s a i l i n g . j pg );
8 a = rgb2gray(a);
9 c = edge(a, s o be l );
10 d = edge(a, pr e wi t t );
11 e = edge(a, l o g );
12 f = edge(a, canny );
13 ShowImage(a, Or i g i na l Image )
14 title( Or i g i na l Image )
15 figure
16 ShowImage(c, Sobe l )
17 title( Sobe l )
18 figure
19 ShowImage(d, Pr e wi t t )
20 title( Pr e wi t t )
21 figure
22 ShowImage(e, Log )
23 title( Log )
24 figure
25 ShowImage(f, Canny )
26 title( Canny )
69
Figure 7.5: Scilab code for Edge Detection using Dierent Edge detectors
70
Scilab code Exa 7.30 Scilab code to perform watershed transform
1 // Capti on : S c i l a b code t o per f or m wat er s hed
t r ans f or m
2 // Fi g7 . 3 0
3 // Page396
4 clc;
5 close;
6 b = imread( E: \DIP JAYARAMAN\ Chapter7 \ t e a s e t . png );
7 a = rgb2gray(b);
8 global EDGE_SOBEL;
9 Gradient = EdgeFilter(a, EDGE_SOBEL);
10 Threshold1 = CalculateOtsuThreshold(Gradient); //
de t e r mi ne a t hr e s ho l d
11 EdgeImage = ~SegmentByThreshold(Gradient ,Threshold1)
;
12 DistanceImage = DistanceTransform(EdgeImage);
13 Threshold2 = CalculateOtsuThreshold(DistanceImage)
// de t e r mi ne a t hr e s ho l d
14 ThresholdImage = SegmentByThreshold(DistanceImage ,
Threshold2);
15 MarkerImage = SearchBlobs(ThresholdImage);
16 SegmentedImage = Watershed(Gradient ,MarkerImage);
17 figure
18 ShowColorImage(b, t e a s e t )
19 title( t e a s e t . png )
20 figure
21 ColorMapLength = length(unique(SegmentedImage));
22 ShowImage(SegmentedImage , Re s ul t o f Watershed
Transf orm ,jetcolormap(ColorMapLength));
71
Figure 7.6: Scilab code to perform watershed transform
72
Chapter 8
Object Recognition
Scilab code Exa 8.4 To verify the given matrix is a covaraince matrix
1 // Capti on : To v e r i f y t he gi ve n mat r i x i s a
c o va r a i nc e mat r i x
2 // Probl em 4
3 // page438
4 close;
5 clear all;
6 clc;
7 K = [37 , -15; -15 ,37];
8 evals = spec(K);
9 evals = gsort(evals);
10 disp(evals , Ei gen Val ues ar e = )
11 if (evals ==abs(evals)) then
12 disp( Both t he e i g e n va l ue s ar e nonne g a t i ve and
t he gi ve n mat r i x i s a c o va r i a nc e mat r i x );
13 else
14 disp( nonc o va r i a nc e mat r i x )
15 end
Scilab code Exa 8.5 To compute the covariance of the given 2D data
73
1 // Capti on : To compute t he c o va r i a nc e o f t he gi ve n 2D
data
2 // Probl em 5
3 // page439
4 close;
5 clear all;
6 clc;
7 X1 = [2,1];
8 X2 = [3,2];
9 X3 = [2,3];
10 X4 = [1,2];
11 X = [X1,X2,X3,X4];
12 disp(X, X= );
13 [M,N] = size(X); //M=rows , N = col umns
14 for i =1:N
15 m(i) = mean(X(:,i));
16 A(:,i) = X(:,i)-m(i);
17 end
18 m = m;
19 disp(m, mean = );
20 K = A*A;
21 K = K/(M-1);
22 disp(K, The Covar ai nce mati x i s K = )
23 // Re s ul t
24 //X=
25 // 2 . 3 . 2 . 1 .
26 // 1 . 2 . 3 . 2 .
27 //mean =
28 // 1 . 5 2 . 5 2 . 5 1 . 5
29 //
30 //The Covar ai nce mati x i s K =
31 // 0 . 5 0 . 5 0 . 5 0 . 5
32 // 0 . 5 0 . 5 0 . 5 0 . 5
33 // 0 . 5 0 . 5 0 . 5 0 . 5
34 // 0 . 5 0 . 5 0 . 5 0 . 5
74
Scilab code Exa 8.9 Develop a perceptron AND function with bipolar in-
puts and targets
1 // Capti on : Devel op a pe r c e pt r on AND f unc t i o n wi th
b i p o l a r i nput s and t a r g e t s
2 // Probl em 9
3 // page441
4 close;
5 clear all;
6 clc;
7 X1 = [1,-1,1,-1]; //X1 and X2 ar e i nput v e c t o r s t o
AND f unc t i o n
8 X2 = [1,1,-1,-1];
9 //b = [ 1 , 1 , 1 , 1 ] ; // Bi as i ng ve c t o r
10 T = [1,-1,-1,-1]; // Tar get ve c t o r f o r AND f unc t i o n
11 W1 = 0; // Wei ghts ar e i n i t i a l i z e d
12 W2 = 0;
13 b = 0; // bi a s i n i t i a l i z e d
14 alpha = 1; // l e a r n i n g r a t e
15 for i = 1: length(X1)
16 Yin(i) = b+X1(i)*W1+X2(i)*W2;
17 if (Yin(i) >=1)
18 Y(i)=1;
19 elseif ((Yin(i) <1)&(Yin(i) >=-1))
20 Y(i)=0;
21 elseif(Yin(i) <-1)
22 Y(i)=-1;
23 end
24 disp(Yin(i), Yin= )
25 disp(Y(i), Y= )
26 if(Y(i)~=T(i))
27 b = b+alpha*T(i);
28 W1 = W1+alpha*T(i)*X1(i);
29 W2 = W2+alpha*T(i)*X2(i);
75
30 disp(b, b= )
31 disp(W1, W1= )
32 disp(W2, W2= )
33 end
34 end
35 disp( Fi nal Wei ghts a f t e r one i t e r a t i o n ar e )
36 disp(b, Bi as Wei gth b= )
37 disp(W1, W1= )
38 disp(W2, W2= )
76
Chapter 9
Image Compression
Scilab code Exa 9.9 Program performs Block Truncation Coding BTC
1 // Capti on : Program pe r f or ms Bl ock Tr uncat i on Codi ng (
BTC)
2 // Example 9 . 9
3 // page512
4 close;
5 clear all;
6 clc;
7 x =
[65 ,75 ,80 ,70;72 ,75 ,82 ,68;84 ,72 ,62 ,65;66 ,68 ,72 ,80];
8 disp(x, Or i g i na l Bl ock i s x = )
9 [m1 n1]=size(x);
10 blk=input( Enter t he bl oc k s i z e : );
11 for i = 1 : blk : m1
12 for j = 1 : blk : n1
13 y = x(i:i+(blk -1),j:j+(blk -1)) ;
14 m = mean(mean(y));
15 disp(m, mean val ue i s m = )
16 sig=std2(y);
17 disp(sig , Standard de v i a t i o n o f t he bl oc k i s
= )
77
18 b = y > m ; // t he bi nar y bl oc k
19 disp(b, Bi nar y a l l o c a t i o n mat r i x i s B= )
20 K = sum(sum(b));
21 disp(K, number o f ones = )
22 if (K ~= blk^2 ) & ( K ~= 0)
23 ml = m-sig*sqrt(K/(( blk ^2)-K));
24 disp(ml, The val ue o f a = )
25 mu = m+sig*sqrt (((blk ^2)-K)/K);
26 disp(mu, The val ue o f b = )
27 x(i:i+(blk -1), j:j+(blk -1)) = b*mu
+(1- b)*ml;
28 end
29 end
30 end
31 disp(round(x), Re c ons t r uc t e d Bl ock i s x = )
32 // Re s ul t
33 // Or i g i na l Bl ock i s x =
34 //
35 // 6 5 . 7 5 . 8 0 . 7 0 .
36 // 7 2 . 7 5 . 8 2 . 6 8 .
37 // 8 4 . 7 2 . 6 2 . 6 5 .
38 // 6 6 . 6 8 . 7 2 . 8 0 .
39 //
40 // Enter t he bl oc k s i z e : 4
41 //mean val ue i s m = 72. 25
42 // Standard de v i a t i o n o f t he bl oc k i s = 6. 6282225
43 // Bi nar y a l l o c a t i o n mat r i x i s B=
44 //
45 // F T T F
46 // F T T F
47 // T F F F
48 // F F F T
49 //
50 // number o f ones = 6
51 //The val ue o f a = 67. 115801
52 //The val ue o f b = 80. 806998
53 // Re c ons t r uc t e d Bl ock i s x =
54 //
78
55 // 6 7 . 8 1 . 8 1 . 6 7 .
56 // 6 7 . 8 1 . 8 1 . 6 7 .
57 // 8 1 . 6 7 . 6 7 . 6 7 .
58 // 6 7 . 6 7 . 6 7 . 8 1 .
Scilab code Exa 9.59 Program performs Block Truncation Coding
1 // Capti on : Program pe r f or ms Bl ock Tr uncat i on Codi ng (
BTC) by c hoos i ng d i f f e r e n t
2 // bl oc k s i z e s
3 // Fi g . 9 . 5 9 : MATLAB Example1
4 // page514
5 close;
6 clc;
7 x =imread( E: \ Di g i t a l I ma g e Pr o c e s s i ng Ja ya r a ma n \
Chapter9 \ l e nna . j pg ); //SIVP t ool box
8 //x=i mr e s i z e ( x , [ 2 5 6 2 5 6 ] ) ;
9 x1=x;
10 x=double(x);
11 [m1 n1]=size(x);
12 blk=input( Enter t he bl oc k s i z e : );
13 for i = 1 : blk : m1
14 for j = 1 : blk : n1
15 y = x(i:i+(blk -1),j:j+(blk -1)) ;
16 m = mean(mean(y));
17 sig=std2(y);
18 b = y > m ; // t he bi nar y bl oc k
19 K = sum(sum(b));
20 if (K ~= blk^2 ) & ( K ~= 0)
21 ml = m-sig*sqrt(K/(( blk ^2)-K));
22 mu = m+sig*sqrt (((blk ^2)-K)/K);
23 x(i:i+(blk -1), j:j+(blk -1)) = b*mu
+(1- b)*ml;
24 end
25 end
79
Figure 9.1: Program performs Block Truncation Coding
26 end
27 // imshow( ui nt 8 ( x ) )
28 // t i t l e ( Re c ons t r uc t e d Image )
29 x = uint8(x);
30 figure (1)
31 imshow(x1)
32 title( Or i g i na l Image ); //IPD t ool box
33 figure (2)
34 ShowImage(x, Re c ons t r uc t e d Image ); //IPD t ool box
35 title( Bl ock Si z e = 8 )
80
Chapter 10
Binary Image Processing
Scilab code Exa 10.17 Scilab Code for dilation and erosion process
1 // Capti on : S c i l a b Code f o r d i l a t i o n and e r o s i o n
pr o c e s s
2 // Fi g . 1 0 . 1 7
3 // Page553
4 close;
5 clear all;
6 clc;
7 a = imread( E: \DIP JAYARAMAN\ Chapter10 \morph1 . bmp );
//SIVP t ool box
8 //b =[ 1 , 1 , 1 ; 1 , 1 , 1 ; 1 , 1 , 1 ] ;
9 StructureElement = CreateStructureElement( s quar e ,
3) ;
10 a1 = DilateImage(a,StructureElement);
11 a2 = ErodeImage(a,StructureElement);
12 // Di s pl a yi ng o r i g i n a l Image
13 // imshow( a )
14 figure (1)
15 ShowImage(a, Or i g i na l Image );
16 // Di s pl a yi ng Di l at e d Image
17 // imshow( a1 )
18 figure (2)
81
Figure 10.1: Scilab Code for dilation and erosion process
19 ShowImage(a1 , Di l at e d Image );
20 xtitle( Di l at e d Image )
21 // Di s pl a yi ng Eroded Image
22 // imshow( a2 )
23 figure (3)
24 ShowImage(a2 , Eroded Image );
25 xtitle( Eroded Image )
Scilab code Exa 10.19 Scilab Code to perform an opening and closing op-
eration on the image
1 // Capti on : S c i l a b Code t o per f or m an openi ng and
c l o s i n g o pe r a t i o n on t he i mage
2 // Fi g . 1 0 . 1 9
3 // Page555
4 close;
5 clear all;
6 clc;
7 a = imread( E: \DIP JAYARAMAN\ Chapter10 \morph2 . bmp );
82
//SIVP t ool box
8 //b =[ 1 , 1 , 1 ; 1 , 1 , 1 ; 1 , 1 , 1 ] ;
9 StructureElement = CreateStructureElement( s quar e ,
3) ;
10 // Openi ng i s done by f i r s t appl yi ng e r o s i o n and then
d i l a t i o n o pe r a t i o ns on i mage
11 b1 = ErodeImage(a,StructureElement);
12 b2 = DilateImage(b1,StructureElement);
13 // Cl os i ng i s done by f i r s t appl yi ng d i l a t i o n and
then e r o s i o n o pe r a t i o n on i mage
14 a1 = DilateImage(a,StructureElement);
15 a2 = ErodeImage(a1,StructureElement);
16 // Di s pl a yi ng o r i g i n a l Image
17 figure (1)
18 ShowImage(a, Or i g i na l Image );
19 // Di s pl a yi ng Opened Image
20 figure (2)
21 ShowImage(b2 , Opened Image );
22 xtitle( Opened Image )
23 // Di s pl a yi ng Cl os ed Image
24 figure (3)
25 ShowImage(a2 , Cl os ed Image );
26 xtitle( Cl os ed Image )
83
Figure 10.2: Scilab Code to perform an opening and closing operation on the
image
84
Chapter 11
Colur Image Processing
Scilab code Exa 11.4 Read an RGB image and extract the three colour
components red green blue
1 // Capti on : Read an RGB i mage and e x t r a c t t he t hr e e
c o l o ur components : red , gr e e n
2 // and bl ue
3 // Fi g . 1 1 . 4 : MATLAB Example1
4 // page588
5 clc;
6 close;
7 RGB = imread( E: \DIP JAYARAMAN\ Chapter11 \ pe ppe r s . png
); //SIVP t ool box
8 R = RGB;
9 G = RGB;
10 B = RGB;
11 R(:,:,2)=0;
12 R(:,:,3)=0;
13 G(:,:,1)=0;
14 G(:,:,3)=0;
15 B(:,:,1)=0;
16 B(:,:,2)=0;
17 figure (1)
18 ShowColorImage(RGB , Or i g i na l Col or Image ); //IPD
85
t ool box
19 title( Or i g i na l Col or Image );
20 figure (2)
21 ShowColorImage(R, Red Component );
22 figure (3)
23 ShowColorImage(G, Green Component );
24 figure (4)
25 ShowColorImage(B, Bl ue Component );
Scilab code Exa 11.12 Read a Colour image and separate the colour im-
age into red green and blue planes
1 // Capti on : Read a Col our i mage and s e pa r a t e t he
c o l o ur i mage i nt o : red , gr e e n
2 // and bl ue pl a ne s
3 // Fi g . 1 1 . 1 2 : MATLAB Example2
4 // page592
5 clc;
6 close;
7 RGB = imread( E: \DIP JAYARAMAN\ Chapter11 \ pe ppe r s . png
); //SIVP t ool box
8 a1 = RGB;
9 b1 = RGB;
10 c1 = RGB;
11 a1(:,:,1)=0;
12 b1(:,:,2)=0;
13 c1(:,:,3)=0;
14 figure (1)
15 ShowColorImage(RGB , Or i g i na l Col or Image ); //IPD
t ool box
16 figure (2)
17 ShowColorImage(a1 , Red Mi s s i ng );
18 figure (3)
86
Figure 11.1: Read an RGB image and extract the three colour components
red green blue
87
19 ShowColorImage(b1 , Green Mi s s i ng );
20 figure (4)
21 ShowColorImage(c1 , Bl ue Mi s s i ng );
Scilab code Exa 11.16 Compute the histogram of the colour image
1 // Capti on : Compute t he hi s t ogr am o f t he c o l o ur i mage
2 // Fi g . 1 1 . 1 6 : MATLAB Example3
3 // page595
4 clc;
5 close;
6 I = imread( E: \DIP JAYARAMAN\ Chapter11 \ l ave nde r . j pg
); //SIVP t ool box
7 figure (1)
8 ShowColorImage(I, Or i g i na l Col or Image ); //IPD
t ool box
9 J = im2double(I);
10 [index ,map] = RGB2Ind(I); //IPD t ool box
11 pixels = prod(size(index));
12 hsv = rgb2hsv(J);
13 h = hsv(:,1);
14 s = hsv(:,2);
15 v = hsv(:,3);
16 // Fi nds l o c a t i o n o f bl ac k and whi t e p i x e l s
17 darks = find(v <0.2);
18 lights = find(s<0.05 & v >0.85);
19 h([darks lights ])=-1;
20 // Gets t he number o f a l l p i x e l s f o r each c o l o ur bi n
21 black_pixels = length(darks)/pixels;
22 white_pixels = length(lights)/pixels;
23 red = length(find((h > .9167 | h <= .083) & h ~= -1)
)/pixels;
24 yellow = length(find(h > .083 & h <= .25))/pixels;
88
Figure 11.2: Read a Colour image and separate the colour image into red
green and blue planes
89
25 green = length(find(h > .25 & h <= .4167))/pixels;
26 cyan = length(find(h > .4167 & h <= .5833))/pixels;
27 blue = length(find(h > .5833 & h <= .75))/pixels;
28 magenta = length(find(h > .75 & h <= .9167))/pixels;
29 // Pl o t s hi s t ogr am
30 figure (2)
31 a=gca();
32 a.data_bounds =[0 ,0;8 ,1]
33 n = 0:0.1:1;
34 plot2d2(n,red*ones(1,length(n)) ,5)
35 n1 = 1:0.1:2;
36 plot2d2(n1,yellow*ones(1,length(n)) ,7)
37 n2 = 2:0.1:3;
38 plot2d2(n2,green*ones(1,length(n)) ,8)
39 n3 = 3:0.1:4;
40 plot2d2(n3,cyan*ones(1,length(n)) ,9)
41 n4 = 4:0.1:5;
42 plot2d2(n4,blue*ones(1,length(n)) ,2)
43 n5 = 5:0.1:6;
44 plot2d2(n5,magenta*ones(1,length(n)) ,3)
45 n6 = 6:0.1:7;
46 plot2d2(n6,white_pixels*ones(1,length(n)) ,0)
47 n7 = 7:0.1:8
48 plot2d2(n7,black_pixels*ones(1,length(n)) ,5)
Scilab code Exa 11.18 Perform histogram equalisation of the given RGB
image
1 // Capti on : Perf orm hi s t ogr am e q u a l i s a t i o n o f t he
gi ve n RGB i mage
2 // Fi g . 1 1 . 1 8 : MATLAB Example4
3 // page596
4 clc;
5 close;
6 a = imread( E: \DIP JAYARAMAN\ Chapter11 \ pe ppe r s . png )
90
; //SIVP t ool box
7 // c o nve r s i o n o f RGB t o YIQ f or mat
8 b = rgb2ntsc(a);
9 // Hi stogram e q u a l i s a t i o n o f Y component al one
10 b(:,:,1) =
11 // c o nve r s i o n o f YIQ t o RGB f or mat
12 c = ntsc2rgb(b);
13 figure (1)
14 ShowColorImage(a, Or i g i na l Image ); //IPD t ool box
15 figure (2)
16 ShowColorImage(c, Hi s t ogt r am e q ua l i z e d Image );
//IPD t ool box
Scilab code Exa 11.21 This program performs median ltering of the colour
image
1 // Capti on : Thi s program pe r f or ms medi an f i l t e r i n g o f
t he c o l o ur i mage
2 // Fi g . 1 1 . 2 1 : MATLAB Example5
3 // page598
4 clc;
5 close;
6 a = imread( E: \DIP JAYARAMAN\ Chapter11 \ pe ppe r s . png )
; //SIVP t ool box
7 b = imnoise(a, s a l t & pepper , 0.2);
8 c(:,:,1)= MedianFilter(b(:,:,1), [3 3]);
9 c(:,:,2)= MedianFilter(b(:,:,2), [3 3]);
10 c(:,:,3)= MedianFilter(b(:,:,3), [3 3]);
11 figure (1)
12 ShowColorImage(a, Or i g i na l Image ); //IPD t ool box
13 figure (2)
14 ShowColorImage(b, c or r upt e d Image ); //IPD
t ool box
15 figure (3)
16 ShowColorImage(c, Median Fi l t e r e d Image ); //IPD
91
Figure 11.3: This program performs median ltering of the colour image
t ool box
Scilab code Exa 11.24 Fitlering only the luminance component
1 // Capti on : Fi t l e r i n g onl y t he l umi nance component
92
2 // Fi g . 1 1 . 2 4 : MATLAB Example6
3 // page599
4 clc;
5 close;
6 a = imread( E: \DIP JAYARAMAN\ Chapter11 \ pe ppe r s . png )
; //SIVP t ool box
7 // c o nve r s i o n o f RGB t o YIQ f or mat
8 yiq = rgb2ntsc(a);
9 // Ext r ac t t he Y component al one
10 b = yiq(:,:,1);
11 h = [-1,-1,-1;-1,8,-1;-1,-1,-1];
12 // Perf orm hi gh pas s f i l t e r i n g onl y on Y component
13 c1 = conv2d2(b,h);
14 [m,n]= size(b);
15 for i =1:m
16 for j=1:n
17 D(i,j)= c1(i,j);
18 end
19 end
20 yiq(:,:,1)=D;
21 // c onve r t YIQ t o RGB f or mat
22 a1 = ntsc2rgb(yiq);
23 figure (1)
24 ShowColorImage(a, Or i g i na l Image ); //IPD t ool box
25 figure (2)
26 ShowColorImage(a1 , Hi gh Pass f i l t e r e d Image ); //
IPD t ool box
Scilab code Exa 11.28 Perform gamma correction for the given colour im-
age
1 // Capti on : Perf orm gamma c o r r e c t i o n f o r t he gi ve n
c o l o ur i mage
93
Figure 11.4: Fitlering only the luminance component
2 // Fi g . 1 1 . 2 8 : MATLAB Example7
3 // page603
4 close;
5 clear all;
6 clc;
7 I = imread( E: \DIP JAYARAMAN\ Chapter11 \ ar ar auna . png
); //SIVP t ool box
8 gamma_Value = 0.5;
9 max_intensity = 255; // f o r ui nt 8 i mage
10 // Look up t a bl e c r e a t i o n
11 LUT = max_intensity .*(([0: max_intensity ]./
max_intensity).^ gamma_Value);
12 LUT = floor(LUT);
13 // Mapping o f i nput p i x e l s i nt o l ookup t a bl e va l ue s
14 K = double(I)+1;
15 J = zeros(I);
16 [m,n,p]= size(K);
17 for i = 1:m
18 for j =1:n
19 for k = 1:p
20 J(i,j,k)= LUT(K(i,j,k));
21 end
94
Figure 11.5: Perform gamma correction for the given colour image
22 end
23 end
24 figure (1)
25 ShowColorImage(I, Or i g i na l Image ); //IPD t ool box
26 figure (2)
27 ShowColorImage(uint8(J), Gamma Cor r e c t e d Image );
//IPD t ool box
Scilab code Exa 11.30 Perform Pseudo Colouring Operation
1 // Capti on : Perf orm PseudoCol our i ng Oper at i on
2 // Fi g . 1 1 . 3 0
3 // page604
4 close;
5 clear all;
6 clc;
7 K = imread( E: \DIP JAYARAMAN\ Chapter11 \ l e nna . j pg );
//SIVP t ool box
8 [m,n]= size(K);
9 I = uint8(K);
95
10 for i = 1:m
11 for j =1:n
12 if (I(i,j) >=0 & I(i,j) <50)
13 J(i,j,1)=I(i,j)+50;
14 J(i,j,2)=I(i,j)+100;
15 J(i,j,3)=I(i,j)+10;
16 elseif (I(i,j) >=50 & I(i,j) <100)
17 J(i,j,1)=I(i,j)+35;
18 J(i,j,2)=I(i,j)+128;
19 J(i,j,3)=I(i,j)+10;
20 elseif(I(i,j) >=100 & I(i,j) <150)
21 J(i,j,1)=I(i,j)+152;
22 J(i,j,2)=I(i,j)+130;
23 J(i,j,3)=I(i,j)+15;
24 elseif(I(i,j) >=150 & I(i,j) <200)
25 J(i,j,1)=I(i,j)+50;
26 J(i,j,2)=I(i,j)+140;
27 J(i,j,3)=I(i,j)+25;
28 elseif(I(i,j) >=200 & I(i,j) <=256)
29 J(i,j,1)=I(i,j)+120;
30 J(i,j,2)=I(i,j)+160;
31 J(i,j,3)=I(i,j)+45;
32 end
33 end
34 end
35 figure (1)
36 ShowImage(K, Or i g i na l Image ); //IPD t ool box
37 figure (2)
38 ShowColorImage(J, Pseudo Col our ed Image ); //IPD
t ool box
Scilab code Exa 11.32 Read an RGB image and segment it using the
threshold method
96
Figure 11.6: Perform Pseudo Colouring Operation
1 // Capti on : Read an RGB i mage and segment i t us i ng t he
t hr e s ho l d method
2 // Fi g11 . 3 2
3 // Page605
4 close;
5 clc;
6 I = imread( E: \DIP JAYARAMAN\ Chapter11 \ ar ar auna . png
); //SIVP t ool box
7 // Conver s i on o f RGB t o YCbCr
8 b = rgb2ycbcr_1(I); //SIVP t ool box
9 [m,n,p]=size(b);
10 b = uint8(b);
11 // Thr es hol d i s a ppl i e d onl y t o Cb component
12 mask = b(:,:,2) >120;
13 figure (1)
14 ShowColorImage(I, Or i g i na l Image ); //IPD t ool box
15 figure (2)
16 ShowImage(mask , Segmented Image ); //IPD t ool box
97
Figure 11.7: Read an RGB image and segment it using the threshold method
98
Chapter 12
Wavelet based Image
Processing
Scilab code Exa 12.9 Scilab code to perform wavelet decomposition
1 // Capti on : S c i l a b code t o per f or m wave l e t
de c ompos i t i on
2 // Fi g12 . 1 0
3 // Page624
4 clc;
5 close;
6 x = ReadImage( E: \DIP JAYARAMAN\ Chapter12 \ l e nna . j pg
);
7 //The i mage i n uns i gne d i n t e g e r or doubl e has t o be
c onve r t e d i nt o nor mal i z e d
8 // doubl e f or mat
9 x = im2double(x);
10 // Fi r s t Le ve l de c ompos i t i on
11 [CA ,CH,CV,CD]=dwt2(x, db1 );
12 // Second l e v e l de c ompos i t i on
13 [CA1 ,CH1 ,CV1 ,CD1]=dwt2(CA , db1 );
14 CA = im2int8(CA);
15 CH = im2int8(CH);
16 CV = im2int8(CV);
99
17 CD = im2int8(CD);
18 CA1 = im2int8(CA1);
19 CH1 = im2int8(CH1);
20 CV1 = im2int8(CV1);
21 CD1 = im2int8(CD1);
22 A = [CA,CH;CV,CD];
23 B = [CA1 ,CH1;CV1 ,CD1];
24 imshow(B)
25 title( Re s ul t o f Second Le ve l Decompos i t i on )
Scilab code Exa 12.42 Scilab code to generate dierent levels of a Gaus-
sian pyramid
1 // Capti on : S c i l a b code t o g e ne r a t e d i f f e r e n t l e v e l s
o f a Gaus s i an pyrami d
2 // Fi g12 . 4 2
3 // Page651
4 clc;
5 close;
6 a = imread( E: \DIP JAYARAMAN\ Chapter12 \ appl e 3 . bmp );
7 a = rgb2gray(a);
8 b = a;
9 kernelsize = input( Enter t he s i z e o f t he k e r ne l : );
10 sd = input( Enter t he s t andar d de v i a t i o n o f ht e
Gaus s i an window : );
11 rf = input( Enter t he Reduct i on Fact or : );
12 // Rout i ne t o g e ne r a t e Gaus s i an k e r ne l
13 k = zeros(kernelsize , kernelsize);
14 [m n] = size(b);
15 t = 0;
16 for i = 1: kernelsize
17 for j=1: kernelsize
18 k(i,j) = exp(-((i-kernelsize /2) .^2+(j-
kernelsize /2) .^2) /(2*sd.^2))/(2* %pi*sd
.^2);
100
19 t = t+k(i,j);
20 end
21 end
22 for i = 1: kernelsize
23 for j = 1: kernelsize
24 k(i,j) = k(i,j)/t;
25 end
26 end
27 for t = 1:1:rf
28 // c onvol ve i t wi th t he p i c t u r e
29 FilteredImg = b;
30 if t==1
31 FilteredImg = filter2(k,b)/255;
32 else
33 FilteredImg = filter2(k,b);
34 end;
35 // compute t he s i z e o f t he r educed i mage
36 m = m/2;
37 n = n/2;
38 // c r e a t e t he r educed i mage t hr ough s ampl i ng
39 b = zeros(m,n);
40 for i = 1:m
41 for j = 1:n
42 b(i,j) = FilteredImg(i*2,j*2);
43 end;
44 end;
45 end;
46 figure
47 ShowImage(a, Or i g i na l Image )
48 figure
49 ShowImage(b, Di f f e r e n t Le ve l s o f Gausai n Pyramid )
50 title( Di f f e r e n t Le ve l s o f Gausai n Pyramid Le ve l 2 )
101
Figure 12.1: Scilab code to generate dierent levels of a Gaussian pyramid
102
Scilab code Exa 12.57 Scilab code to implement watermarking in spatial
domain
1 // Capti on : S c i l a b code t o i mpl ement wat er mar ki ng i n
s p a t i a l domain
2 // Fi g12 . 5 7
3 // Page662
4 clc
5 close
6 a = imread( E: \DIP JAYARAMAN\ Chapter12 \cameraman . j pg
);
7 figure
8 imshow(a)
9 title( Base Image );
10 b = imread( E: \DIP JAYARAMAN\ Chapter12 \ keyi mage . j pg
);
11 b = rgb2gray(b);
12 b = imresize(b,[32 32], bi c ubi c );
13 [m1 n1]=size(b);
14 figure
15 imshow(b)
16 title( Mark Image );
17 [m n]=size(a);
18 i1 = 1;
19 j1 = 1;
20 p = 1;
21 c = a;
22 iii = 1;
23 jjj = 1;
24 a = uint8(a);
25 b = uint8(b);
26 for ff = 1:8
27 for i = 1:32
28 jjj = 1;
29 for j = j1:j1+n1 -1
30 a(i,j) = bitand(a(i,j),uint8 (254)); //
LSB o f bas e i mage i s s e t t o z e r o .
31 temp = bitand(b(i,jjj),uint8 ((2^ff) -1));
103
Figure 12.2: Scilab code to implement watermarking in spatial domain
//MSB o f t he mark i s e x t r a c t e d .
32 temp = temp /((2^ff) -1);
33 c(i,j) = bitor(a(i,j),uint8(temp));//MSB
o f mark i s i n e r t e d i nt o t he %LSB o f
t he bas e
34 jjj = jjj+1;
35 end
36 end
37 j1 = j1+32;
38 end
39 imshow(c)
40 title( Marked Image );
41 imwrite(c, E: \DIP JAYARAMAN\ Chapter12 \markimg . j pg );
Scilab code Exa 12.63 Scilab code to implement wavelet based watermark-
ing
1 // Capti on : S c i l a b code t o i mpl ement wavel et based
wat er mar ki ng
104
2 // Fi g12 . 6 3
3 // Page666
4 clc;
5 close;
6 // Or i g i na l Image
7 img = imread( E: \DIP JAYARAMAN\ Chapter12 \cameraman .
j pg );
8 figure
9 imshow(img)
10 title( Or i g i na l Image );
11 [p q] = size(img);
12 // Gener at e t he key
13 // key = i mread ( E: \DIP JAYARAMAN\ Chapter12 \ keyi mg1 .
png ) ;
14 // key = i mr e s i z e ( key , [ p q ] ) ;
15 key = imread( E: \DIP JAYARAMAN\ Chapter12 \ keyi mage .
j pg );
16 key = rgb2gray(key);
17 c = 0.001; // I n i t i a l i s e t he wei ght o f Watermarki ng
18 figure
19 imshow(key)
20 title( Key );
21 // Wavel et t r ans f or m o f o r i g i n a l i mage ( bas e i mage )
22 img = double(img);
23 key = double(key);
24 [ca ,ch,cv,cd] = dwt2(img , db1 );//Compute 2D wave l e t
t r ans f or m
25 // Perf orm t he wat er mar ki ng
26 y = [ca ch;cv cd];
27 Y = y + c*key;
28 p=p/2;
29 q=q/2;
30 for i=1:p
31 for j=1:q
32 nca(i,j) = Y(i,j);
33 ncv(i,j) = Y(i+p,j);
34 nch(i,j) = Y(i,j+q);
35 ncd(i,j) = Y (i+p,j+q);
105
36 end
37 end
38 // Di s pl ay t he Watermarked i mage
39 wimg = idwt2(nca ,nch ,ncv ,ncd , db1 );
40 wimg1 = uint8(wimg);
41 figure
42 imshow(wimg1)
43 title( Watermarked Image )
44 // Ext r ac t i o n o f key f rom Watermarked i mage
45 [rca ,rch ,rcv ,rcd] = dwt2(wimg , db1 ); //Compute 2D
wave l e t t r ans f or m
46 n1=[rca ,rch;rcv ,rcd];
47 N1=n1-y;
48 N1 = N1*4;
49 N1 = im2int8(N1);
50 figure
51 imshow(N1)
52 title( Ext r ac t t he key f rom watermarked i mage )
106
Appendix
Scilab code AP 1 2D Fast Fourier Transform
1 function [a2] = fft2d(a)
2 // a = any r e a l or compl ex 2D mat r i x
3 // a2 = 2DDFT o f 2D mat r i x a
4 m=size(a,1)
5 n=size(a,2)
6 // f o u r i e r t r ans f or m al ong t he rows
7 for i=1:n
8 a1(:,i)=exp(-2*%i*%pi *(0:m-1) .*.(0:m-1)/m)*a(:,i)
9 end
10 // f o u r i e r t r ans f or m al ong t he col umns
11 for j=1:m
12 a2temp=exp(-2*%i*%pi *(0:n-1) .*.(0:n-1)/n)*(a1(j,:))
.
13 a2(j,:)=a2temp.
14 end
15 for i = 1:m
16 for j = 1:n
17 if((abs(real(a2(i,j))) <0.0001)&(abs(imag(a2(
i,j))) <0.0001))
18 a2(i,j)=0;
19 elseif(abs(real(a2(i,j))) <0.0001)
20 a2(i,j)= 0+%i*imag(a2(i,j));
21 elseif(abs(imag(a2(i,j))) <0.0001)
22 a2(i,j)= real(a2(i,j))+0;
23 end
24 end
107
25 end
Scilab code AP 2 2D Inverse FFT
1 function [a] =ifft2d(a2)
2 // a2 = 2DDFT o f any r e a l or compl ex 2D mat r i x
3 // a = 2DIDFT o f a2
4 m=size(a2 ,1)
5 n=size(a2 ,2)
6 // I nv e r s e Fo ur i e r t r ans f or m al ong t he rows
7 for i=1:n
8 a1(:,i)=exp(2*%i*%pi *(0:m-1) .*.(0:m-1)/m)*a2(:,i)
9 end
10 // I nv e r s e f o u r i e r t r ans f or m al ong t he col umns
11 for j=1:m
12 atemp=exp (2*%i*%pi *(0:n-1) .*.(0:n-1)/n)*(a1(j,:)).
13 a(j,:)=atemp.
14 end
15 a = a/(m*n)
16 a = real(a)
17 endfunction
Scilab code AP 3 Median Filtering function
1 //The i nput t o t he f unc t i o n ar e t he c or r upt e d i mage
a and t he di mens i on
2 function [Out_Imag] = Func_medianall(a,N)
3 a=double(a);
4 [m n]=size(a);
5 Out_Imag=a;
6 if(modulo(N,2) ==1)
7 Start=(N+1)/2;
8 End=Start;
9 else
10 Start=N/2;
11 End=Start +1;
12 end
13 if(modulo(N,2) ==1)
108
14 limit1 =(N-1)/2;
15 limit2=limit1;
16 else
17 limit1 =(N/2) -1;
18 limit2=limit1 +1;
19 end
20 for i=Start:(m-End+1),
21 for j=Start:(n-End +1),
22 I=1;
23 for k=-limit1:limit2 ,
24 for l=-limit1:limit2 ,
25 mat(I)=a(i+k,j+l);
26 I=I+1;
27 end
28 end
29 mat=gsort(mat); // Sor t t he e l e me nt s t o
f i nd t he medi an
30 if(modulo(N,2) ==1)
31 Out_Imag(i,j)=(mat(((N^2)+1)/2));
32 else
33 Out_Imag(i,j)=(mat((N^2)/2)+mat (((N^2)/2)
+1))/2;
34 end
35 end
36 end
Scilab code AP 4 To caculate gray level
1 function [g] = gray(m)
2 g = (0:m-1) /max(m-1,1)
3 g = [g g g]
4 endfunction
Scilab code AP 5 To change the gray level of gray image
1 function [bout] = grayslice(I,z)
2
109
3 // Output v a r i a b l e s i n i t i a l i s a t i o n ( not f ound i n
i nput v a r i a b l e s )
4 bout =[];
5
6 // Number o f arguments i n f unc t i o n c a l l
7 [%nargout ,%nargin] = argn (0)
8
9 if %nargin ==1 then
10 z = 10;
11 elseif ~type(z)==1 then
12 z = double(z);
13 end;
14 n = z;
15 if typeof(I)== ui nt 8 then
16 z = (255*(0:n-1))/n;
17 elseif isa(I, ui nt 16 ) | isa(I, i nt 1 6 ) then
18 z = 65535*(0:(n-1))/n;
19 else // I i s doubl e or s i n g l e
20 z = (0:(n-1))/n
21 end;
22 [m,n] = size(I);
23 b = zeros(m,n);
24 // Loop over a l l i n t e r v a l s , e xc e pt t he l a s t
25 for i = 1: length(z) -1
26 // j i s t he i ndex val ue we wi l l output , s o i t
depend upon s t o r a g e c l a s s
27 if typeof(b)== ui nt 8
28 j = i-1;
29 else
30 j = i;
31 end
32 d = find(I>=z(i) & I<z(i+1));
33 if ~isempty(d),
34 b(d) = j;
35 end
36 end
37
38 // Take c ar e o f t hat l a s t i n t e r v a l
110
39 d = find(I >= z($));
40 if ~isempty(d) then
41 // j i s t he i ndex val ue we wi l l output , s o i t
depend upon s t o r a g e c l a s s
42 if typeof(b)== ui nt 8 then
43 j = length(z) -1;
44 else
45 j = length(z);
46 end;
47 b(d) = j;
48 end;
49 bout = b;
50 bout = double(bout);
51 endfunction
111

You might also like