Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Created by
R.Senthilkumar
M.E., Assistant Professor
Electronics Engineering
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
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.
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
4 Image Transforms 21
5 Image Enhancement 31
7 Image Segmentation 61
8 Object Recognition 70
9 Image Compression 74
3
List of Scilab Codes
Exa 1.3 Program to calculate number of samples required for an
image . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Exa 1.13 False contouring Scilab code . . . . . . . . . . . . . . . 7
Exa 2.12 Frequency Response . . . . . . . . . . . . . . . . . . . 9
Exa 2.16 Frequency Response . . . . . . . . . . . . . . . . . . . 9
Exa 3.1 2D Linear Convlolution . . . . . . . . . . . . . . . . . 12
Exa 3.2 2D Linear Convolution . . . . . . . . . . . . . . . . . . 12
Exa 3.3 2D Linear Convolution . . . . . . . . . . . . . . . . . . 13
Exa 3.7 2D Linear Convolution . . . . . . . . . . . . . . . . . . 14
Exa 3.8 2D Linear Convolution . . . . . . . . . . . . . . . . . . 14
Exa 3.11 Linear Convolution of an y signal with an impu le signal
given rise to the same signal . . . . . . . . . . . . . . . 15
Exa 3.12 Circular Convolution between two 2D matrices . . . . 15
Exa 3.13 Circular Convolution exspressed as linear convolution
plus alias . . . . . . . . . . . . . . . . . . . . . . . . . 16
Exa 3.14 Linear Cross correlation of a 2D matrix . . . . . . . . 17
Exa 3.15 Circular correlation between two signals . . . . . . . . 17
Exa 3.16 Circular correlation between two signals . . . . . . . . 18
Exa 3.17 Linear auto correlation of a 2D matrix . . . . . . . . . 19
Exa 3.18 Linear Cross correlation of a 2D matrix . . . . . . . . 19
Exa 4.4 DFT of 4x4 grayscale image . . . . . . . . . . . . . . . 21
Exa 4.5 2D DFT of 4X4 grayscale image . . . . . . . . . . . . 21
Exa 4.6 Scilab code to intergchange phase information between
two images . . . . . . . . . . . . . . . . . . . . . . . . 22
Exa 4.10 Program to compute discrete cosine transform . . . . . 23
Exa 4.12 Program to perform KL tranform for the given 2D matrix 25
Exa 4.13 Program to find the singular value decomposition of
given matrix . . . . . . . . . . . . . . . . . . . . . . . 29
4
Exa 5.5 Scilab code for brightness enhancement . . . . . . . . 31
Exa 5.7 Scilab code for brightness suppression . . . . . . . . . 33
Exa 5.9 Scilab code for Contrast Manipulation . . . . . . . . . 33
Exa 5.13 Scilab code to determine image negative . . . . . . . . 35
Exa 5.16 Scilab code that pe rforms threshold operation . . . . . 35
Exa 5.20 Program performs gray level slicing without background 37
Exa 6.1 Scilab code to create motion blur . . . . . . . . . . . . 41
Exa 6.5 Scilab code performs inverse filtering . . . . . . . . . . 42
Exa 6.7 Scilab code performs inverse filtering . . . . . . . . . . 45
Exa 6.9 Scilab code performs Pseudo inverse filtering . . . . . 47
Exa 6.13 Scilab code to per form wiener filtering of the cor rupted
image . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Exa 6.18 Scilab code to Perform Average Filtering operation . . 52
Exa 6.21 Scilab code to Perform median filtering . . . . . . . . 54
Exa 6.23 Scilab code to P erform median filtering of colou r image 56
Exa 6.24 Scilab code to Perform Trimmed Average Filter . . . . 58
Exa 7.23 Scilab code for Differentiation of Gaussian function . . 61
Exa 7.25 Scilab code for Differentiation of Gaussian Filter function 64
Exa 7.27 Scilab code for Edge Detection using Different Edge de-
tectors . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Exa 7.30 Scilab code to perform watershed transform . . . . . . 68
Exa 8.5
Exa 8.4 To verify
To computethethe
given matrix isofathe
covariance covaraince
given 2Dmatrixdata .. .. .. 70
70
Exa 8.9 Develop a perceptron AND function with bipolar inputs
and targets . . . . . . . . . . . . . . . . . . . . . . . . 72
Exa 9.9 Program performs Block Truncation Coding BTC . . . 74
Exa 9.59 Program performs Block Truncation Coding . . . . . . 76
Exa 10.17 Scilab Code for dilation and erosion process . . . . . . 78
Exa 10.19 Scilab Code to perform an opening and closi ng opera-
tion on the image . . . . . . . . . . . . . . . . . . . . 79
Exa 11.4 Read an RGB im age and extract the three colour com-
ponents red green blue . . . . . . . . . . . . . . . . . . 82
Exa 11.12 Read a Colour image and sepa rate the colour image into
red green and blue planes . . . . . . . . . . . . . . . . 83
Exa 11.16 Compute the histogram of the colour image . . . . . . 85
Exa 11.18 Perform histogram equalisation of the given RGB image 87
Exa 11.21 This program performs median filter ing of the colour
image . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
5
Exa 11.24 Fitlering only the lumi nance component . . . . . . . . 89
Exa 11.28 Perform gamma correction for the given colour image . 90
Exa 11.30 Perform Pseudo Colouring Operation . . . . . . . . . . 92
Exa 11.32 Read an RGB image and segment it using the th reshold
method . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Exa 12.9 Scilab code to perform wavelet decomposition . . . . . 96
Exa 12.42 Scilab code to generate different levels of a Gaussian
pyramid . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Exa 12.57 Scilab code to implement watermarking in spatial domain 98
Exa 12.63 Scilab code to implement wavelet based watermarking 101
AP 1 2D Fast Fourier Transform . . . . . . . . . . . . . . . 104
AP 2 2D Inverse FFT . . . . . . . . . . . . . . . . . . . . . 105
AP 3 Median Filtering function . . . . . . . . . . . . . . . . 105
AP 4 To caculate gray level . . . . . . . . . . . . . . . . . . 106
AP 5 To change the gray level of gray image . . . . . . . . . 106
6
List of Figures
1.1 False contouring Scilab code . . . . . . . . . . . . . . . . . . 8
7
7.3 Scilab code for Differentiation of Gaussian Filter function . . 64
7.4 Scilab code for Differentiation of Gaussian Filter function . . 65
7.5 Scilab code for Edge Detection using Different Edge detectors 67
7.6 Scilab code to perform watershed transform . . . . . . . . . 69
11.1 Read an RGB image and extract the three colour compon ents
red green blue . . . . . . . . . . . . . . . . . . . . . . . . . . 84
11.2 Read a Colour image and separat e the colour image into red
green and blue planes . . . . . . . . . . . . . . . . . . . . . . 86
11.3 This program performs median filtering of the colour image . 89
11.4 Fitlering only the luminance compone nt . . . . . . . . . . . 91
11.5 Perform gamma correc tion for the given colour image . . . . 92
11.6 Perform Pseudo Colou ring Operation . . . . . . . . . . . . . 94
11.7 Read an RGB image and segment it using the threshold method 95
8
Chapter 1
Introduction to Image
Processing System
Scilab code Exa 1.3 Program to calculate number of samples required for
an image
9
15 // 15360 000.
89 figure
imshow(a)
10 title( ’ Ori gin al im ag e ’ ) ;
11 // usi ng 1 28 gr ay lev els
12 figure
13 a_12 8 = gray slic e(a,128);
14 gra y_ 128 = gr ay(12 8);
15 ShowImage(a_128, ’Im age wi th 12 8 gr ay le ve ls ’ ,
gray_128);
16 // usi ng 64 gr ay lev els
17 figure
18 a_ 64 = gra ysl ice( a,64 );
19 gra y_6 4 = gr ay(6 4);
20 ShowImage(a_64, ’Im age wi th 64 gr ay lev els ’ ,gray_64);
21 // usi ng 32 gr ay lev els
22 figure
23 a_ 32 = gra ysl ice( a,32 );
10
Figure 1.1: False contouring Scilab code
11
Chapter 2
2D Signals and Systems
12
Figure 2.1: Frequency Response
4 clc ;
5 close ;
6 [X, Y] = mes hgr id(-%p i:.0 5:%p i);
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
15
1 //Caption : 2 −D Linear Con volu tio n
2 //Examp le3 .2 & Exa mpl e3 .5 & Exa mpl e3 .9
3 //p ag e 91 & pag e 10 8 & pag e 11 6
4 clc ;
5 x =[ 1,2,3;4,5,6; 7,8,9];
6 h = [1,1 ;1,1 ;1,1 ];
7 y = con v2 d2(x,h );
8 disp (y , ’ Li ne ar 2D con volu tion resul t y =’ )
9 // Res ult
10 // Li ne ar 2D con vol ut ion result y =
11 //
12// 1. 3. 5. 3.
13// 5. 12. 16. 9.
14// 12. 27. 33. 18.
15// 11. 24. 28. 15.
16// 7. 15. 17. 9.
17 //
16
Scilab code Exa 3.7 2D Linear Convolution
17
13// 12. 15. 18.
14// 11. 13. 15.
15// 7. 8. 9.
Scilab code Exa 3.11 Linear Convolution of any signal with an impule
signal given rise to the same signal
18
7 X = ff t2 d(x) ; //2 D FFT of x matrix
8 H = ff t2 d(h) ; //2 D FFT of h matrix
9 Y = X. *H ; //E le me nt b y El em ent mul tip lic ati on
10 y = if ft 2d( Y);
11 disp (y , ’ Circular Co nv olu ti on Res ult y =’ )
12 // Res ult
13 // Circular Co nv olu tio n Res ult y =
14 //
15// 70. 68.
16// 62. 60.
19
co nv olu ti on pl us alia s =
20 //
21// 70. 68.
22// 62. 60.
23 //
20
7 h = h( :,$:- 1: 1);
8 h = h( $:-1 :1, :);
9 X = ff t2 d(x) ;
10 H = ff t2 d(h) ;
11 Y = X. *H ;
12 y = if ft 2d( Y);
13 disp (y , ’ Circu lar Corre lation resul t y=’ )
14 // Res ult
15 // Circu lar Correl ation resul t y=
16 //
17// 37. 23.
18// 35. 25.
21
Scilab code Exa 3.17 Linear auto correlation of a 2D matrix
22
13 //
14// 4. 7. 3.
15// 6. 10. 4.
16// 2. 3. 1.
23
Chapter 4
Image Transforms
24
1 //Cap tio n : 2D DFT of 4x 4 gra ysc ale im ag e
2 // Examp le4 . 5
3 //pag e 17 1
4 clc ;
5 F = [1 6,0,0,0;0,0,0,0 ;0,0,0,0; 0,0,0,0];
6 N =4; //4− po in t DFT
7 kernel = df t_m tx(N);
8 f = ( kernel *(F* kernel ’))/(N^2);
9 f = real (f);
10 disp (f , ’ Inverse 2D DFT of the trans forme d im ag e f =’
)
11 // Res ult
12 // Inverse 2D DFT of the transf ormed im ag e f =
13 //
14// 1. 1. 1. 1.
15// 1. 1. 1. 1.
16// 1. 1. 1. 1.
17// 1. 1. 1. 1.
fft2d.sce
ifft2d.sce
Scilab code Exa 4.6 Scilab code to intergchange phase information be-
tween two images
1 //Ca pt io n : Scilab
co de to intergc hange ph as e
information be tw ee n two ima ge s
2 // Examp le4 . 6
3 //page 17 4 −175
4 clc ;
5 close ;
25
6 a = im re ad( ’ E : \ DIP JAYARAMAN
\ Chapter4 \ le na . pn g ’ ) ;
//S IVP tool box
7 b = im re ad( ’ E : \ DIP JAYARAMAN
\ Chapter4 \ babo on . png ’ ) ;
8 a = rgb 2gr ay(a);
9 b = rgb 2gr ay(b);
10 a = imr esi ze(a,0 .5 );
11 b = imr esi ze(b,0 .5 );
12 figure (1)
13 ShowImage(a, ’ Origi nal lena Ima ge ’ ) ; //I PD tool box
14 title( ’ Origi nal lena Im ag e ’ ) ;
15 figure (2)
16 ShowImage(b, ’ Ori gin al ba bo on Im ag e ’ ) ;
17 title( ’ Ori gin al ba bo on Im ag e ’ )
18 ff ta = ff t2 d( double (a));
19 ff tb = ff t2 d( 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 new ff t_a = ma g_ a.*( exp (%i*ph_b));
25 new ff t_b = ma g_ b.*( exp (%i*ph_a));
26 re
27 re c_
c_ b
a =
= iff
iff t2
t2 d(newf
d(newf ft_
ft_ b);
a);
28 figure (3)
29 ShowImage( uint8 (rec_a), ’ lena Image afte r ph as e
reversal ’ );
30 title( ’ len a Image after ph as e reversal ’ )
31 figure (4)
32 ShowImage( uint8 (rec_b), ’ba boo n Im ag e aft er ph as e
reversal ’ );
33 title( ’ba boon Image afte r ph as e reve rsa l ’ )
26
Figure 4.1: Scilab code to intergchange phase informat ion b etween two im-
ages
27
1 //Cap ti on : Program to compu te di sc ret e cosin e
tranform
2 // Examp le4 .1 0
3 //pag e 19 8
4 clc ;
5 N =4; //DCT matr ix of order four
6 X = dc t_ mt x(N) ;
7 disp (X , ’DCT matr ix of order four ’ )
8 // Res ult
9 //DCT mat rix of order four
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 . 65 3 2 8 15 0. 6 5 3 2 81 5 −
0.2705981
Scilab code Exa 4.12 Program to perform KL tranform for the given 2D
matrix
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; //m ean matrix
15 E = E / n;
16 C = E - mx* mx’ ; // cov ari an ce matrix C = E [ xx ’] −mx∗mx’
17 [V,D ] = spec (C); / / eige n valu es and eige n vecto rs
18 d = diag (D); / / diagona l ele men ts od eigen values
19 [d,i ] = gsort (d); / / sorting th e el em en ts of D in
des cen ding orde r
20 for j = 1: length ( d )
21 T(:,j)= V(:,i(j)) ;
22 end
23 T =T’
24 disp (d , ’ Ei ge n Va lu es are U = ’ )
25 disp (T , ’T he eigen vector ma tr ix T =’ )
26 disp (T , ’T he K L tr an for m basis is =’ )
27 // KL transf orm
28 for i = 1:n
29 Y(:,i)= T*X(:,i);
30
31 end (Y , ’KL transform
disp ation of th e inp ut ma tr ix Y =’ )
32 // Reconstru ction
33 for i = 1:n
34 x(:,i)= T’*Y(:,i);
35 end
36 disp (x , ’ Rec ons tru ct ma tr ix of th e giv en sa mp le
matrix X =’ )
37 // Res ult
38 // Ei ge n Va lu es are U =
39 // 6.1963372
40 // 0.2147417
41 // 0.0264211
42 // The eigen vector ma tr ix T =
43// 0.4384533 0.8471005 0.3002988
44 // 0.4460381 −0 . 49 5 1 6 84 0. 7 4 5 5 59 1
45 // −0 . 7 8 0 2 6 2 0 0.1929481 0.5949473
31
46 // T he KL tra nf or m basis is =
47// 0.4384533 0.8471005 0.3002988
48 // 0.4460381 −0 . 49 5 1 6 84 0. 7 4 5 5 59 1
49 // −0 . 7 8 0 2 6 2 0 0.1929481 0.5949473
50 // K L transformation of th e inp ut ma tr ix 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 // Rec ons tru ct ma tr ix of th e giv en sa mp le ma tr ix x =
55// 4. 3. 5. 6.
56// 4. 2. 7. 7.
57// 5. 5. 6. 7.
Scilab code Exa 4.13 Program to find the singular value decomposition
of given matrix
32
16 // −0 . 7 07 1 0 6 8 0 . 70 7 1 0 68
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.776D −16 0.7453560
28 // −0 . 6 6 6 6 6 6 7 0.4472136 0.5962848
29 //
30 // A mat rix fr om sv d =
31 //
32 // 1. −2 . 3.
33// 3. 2. − 1.
33
Chapter 5
Image Enhancement
34
35
Scilab code Exa 5.7 Scilab code for brightness suppression
36
37
11 c = uint8 ( c )
12 figure (1)
13 ShowImage(a, ’ Ori gin al Im ag e ’ ) ;
14 title( ’ Ori gin al Im ag e ’ )
15 figure (2)
16 ShowImage(b, ’ Decr ease in Cont rast ’ ) ;
17 title( ’ Decr ease in Cont rast ’ )
18 figure (3)
19 ShowImage(c, ’ Increase in Con tr ast ’ );
20 title( ’ Increase in Co ntr ast ’ )
Scilab code Exa 5.16 Scilab code that performs threshold operation
38
Figure 5.3: Scilab code for Contrast Manipulation
39
1 //Ca pt io n : Scilab co de tha t per for ms threshold
operation
2 //Fig5 .16
3 //pag e 25 4
4 clc ;
5 close ;
6 a = im re ad( ’ E : \ Digita l Image Proce ssing Jayar aman \
Chapter5 \ le na . pn g ’ ) ;
7 a = rgb 2gr ay(a);
8 [m n] = size (a);
9 t = input ( ’ En te r the threshol d par ame te r ’ );
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
21 ShowImage(a,
title( ’ Ori gin ’ al
Ori Im ) ag e ’ ) ;
ginagale ’ Im
22 figure (2)
23 ShowImage(b, ’ Thresho lded Ima ge ’ ) ;
24 title( ’ Thresho lded Ima ge ’ )
25 xlabel( sprintf ( ’ Thr esh old value is %g ’ ,t))
26 // Res ult
27 //En te r th e threshold pa ra me te r 140
Scilab code Exa 5.20 Program performs gray level slicing without back-
ground
40
41
1 //Cap tio n : Prog ram performs gray le ve l s l i c i n g
without back grou nd
2 //Fig .5 .2 0
3 //page256
4 clc ;
5 x = im re ad( ’ E : \ Digita l Image Proce ssing Jayar aman \
Chapter5 \ le na . pn g ’ ) ;
6 x = rgb 2gr ay(x);
7 y = double (x);
8 [m,n]= size (y);
9 L = max ( m ax (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
21 end
z = uint8 (z);
22 figure (1)
23 ShowImage(x, ’ Ori gin al Im ag e ’ ) ;
24 title( ’ Orgin al Im ag e ’ )
25 figure (2)
26 ShowImage(z, ’G ray Lev el Sli cin g ’ ) ;
27 title( ’G ray Le ve l Slici ng wi th ou t preser ving
background ’ )
42
Figure 5.6: Program performs gray level slicing without background
43
Chapter 6
Image Restoration and
Denoising
44
Figure 6.1: Scilab code to create motion blur
45
8 x =imr ea d( ’ E : \ DIP JAYARAMAN
\ Chapter6 \ flow er2 . jpg ’ );
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/ 12 1;
15 end
16 end
17 sig ma = sqrt (4*10^(-7));
18 freq x = ff t2 d(x); // Fouri er tra nsf orm of inp ut ima ge
19 freq h = ff t2 d(h); // Fouri er tra nsfo rm of degr adati on
20 y = real (ifft2d(freqh.*freqx));
21 freq y = ff t2 d(y);
22 powfr eqx = fre qx.^2/(M*N);
23 al ph a = 0. 5; // Ind ica tes inv erse f i l t e r
24 fr eq g = ((fr eq h.’)’). * abs (powfreqx)./( abs (freqh.^2)
.* abs (powfreqx)+alpha*sigma^2);
25 Resfr eqx = fre qg.*freq y;
26 Res a = real (ifft2d(Resfreqx));
27 x = uint8 (x);
28
29 y =a uint8
Res (y);(Resa)
= uint8
30 ShowImage(x, ’ Ori gin al Im ag e ’ )
31 title( ’ Ori gin al Im ag e ’ )
32 figure
33 ShowImage(y, ’ Degraded Imag e ’ )
34 title( ’ Degraded Imag e ’ )
35 figure
36 ShowImage(Resa, ’ Resto red Ima ge ’ )
37 title( ’ Restor ed Ima ge ’ )
46
47
ifft2d.sce
12
13 h
for= izeros (M,N);
= 1:11
14 for j = 1:11
15 h(i, j) = 1/ 12 1;
16 end
17 end
18 sig ma = sqrt (4*10^(-7));
19 freq x = ff t2 d(x); // Fouri er tra nsf orm of inp ut ima ge
20 freq h = ff t2 d(h); // Fouri er tra nsfo rm of degr adati on
21 y = real (ifft2d(freqh.*freqx))+10* rand (M,N, ’ nor mal ’ )
;
22 freq y = ff t2 d(y);
23 powfr eqx = fre qx.^2/(M*N);
24 al ph a = 0. 5; // Ind ica tes inv erse f i l t e r
25 fr eq g = ((fr eq h.’)’). * abs (powfreqx)./( abs (freqh.^2)
.* abs (powfreqx)+alpha*sigma^2);
26 Resfr eqx = fre qg.*freq y;
48
49
27 Res a = real (ifft2d(Resfreqx));
28 x = uint8 (x);
29 y = uint8 (y);
30 Res a = uint8 (Resa)
31 ShowImage(x, ’ Ori gin al Im ag e ’ )
32 title( ’ Ori gin al Im ag e ’ )
33 figure
34 ShowImage(y, ’ Degraded +no is e Imag e ’ )
35 title( ’ Degraded +no is e Imag e ’ )
36 figure
37 ShowImage(Resa, ’ Resto red Ima ge ’ )
38 title( ’ Restor ed Ima ge ’ )
Scilab code Exa 6.9 Scilab code performs Pseudo inverse filtering
50
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/ 12 1;
16 end
17 end
18 mask _b = ones (11,11)/121;
19 [m 1,n1] = size (mask_b);
20 Thr_ Fre q = 0. 2;
21 freq x = ff t2 d(x); // Fouri er tra nsf orm of inp ut ima ge
22 freq h = ff t2 d(h); // Fouri er tra nsfo rm of degr adati on
23 y = real (ifft2d(freqh.*freqx))+25* rand (M,N, ’ nor mal ’ )
;
24 freq y = ff t2 d(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+(n 1 -1)/2 ) = mas k_b;
27 ps f = fftshift (psf);
28 fre q_ res = ff t2 d(ps f);
29
30 Inv_f ilt (ifft2d(freqy.*Inv_filt));
z = real = freq _res./( ( abs (freq_res)).^2+Thr_Freq);
31 x = uint8 (x);
32 y = uint8 (y);
33 z = uint8 ( z )
34 ShowImage(x, ’ Ori gin al Im ag e ’ )
35 title( ’ Ori gin al Im ag e ’ )
36 figure
37 ShowImage(y, ’ Degraded +no is e Imag e ’ )
38 title( ’ Degraded +no is e Imag e ’ )
39 figure
40 ShowImage(z, ’ Resto red Im age ’ )
41 title( ’ Restor ed Ima ge ’ )
52
53
fft2d.sce
check Appendix AP 2 for dependency:
ifft2d.sce
Scilab code Exa 6.13 Scilab code to perform wiener filtering of the cor-
rupted image
10
11 al
[M phN]a ==size
1; (x);
// It in dic at es Wi ene r f i l t e r
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 Freq a = ff t2 d(x);
19 Freq h = ff t2 d(h);
20 y = real (ifft2d(Freqh.*Freqa)) //im ag e degradation
21 y = y+25* rand (M,N, ”normal” ) ; //Ad di ng ran do m no is e
w it h n o r m a l distr ibutio n
22 Freq y = ff t2 d(y);
23 Pow y = abs (Freqy).^2/(M*N);
24 sF req h = Fr eq h.*( abs (Freqh)>0)+1/Gamma*( abs (Freqh)
==0);
54
25 iF req h = 1/sF re qh;
26 iFreqh = iFr eqh ’.*( abs (Freqh)*Gamma>1)+Gamma* abs (
sFreqh)*iFreqh*( abs (sFreqh)*Gamma <=1);
27 iF req h = iF re qh/( max ( max ( abs (iFreqh))));
28 Pow y = Pow y.*(Pow y >sigm a^2)+sigma^2* (Pow y <=sig ma^2);
29 Fre qg = iFre qh.*(Po wy- sig ma^2)./(Po wy -( 1- alp ha)*
sigma^2);
30 ResFr eqa = Fre qg.*Freq y;
31 Res a = real (ifft2d(ResFreqa));
32 x = uint8 (x);
33 y = uint8 (y);
34 Res a = uint8 (Resa);
35 ShowImage(x, ’ Ori gin al Im ag e ’ )
36 title( ’ Ori gin al Im ag e ’ )
37 figure
38 ShowImage(y, ’ Degraded Imag e ’ )
39 title( ’ Degraded Imag e ’ )
40 figure
41 ShowImage(Resa, ’ Resto red Ima ge ’ )
42 title( ’ Restor ed Ima ge ’ )
Scilab code Exa 6.18 Scilab code to Perform Average Filtering operation
1 //Ca pt io n : Scila b co de to Pe rf or m Av er ag e Fi lt er in g
operation
2 //Fig6 .18
3 //pag e 34 9
4 clc ;
5 close ;
6 a= imr ea d( ’ E : \ DIP JAYARAMAN
\ Chapter6 \ len na . jpg ’ ) ; //
SIVP tool box
7 a=imnoise(a, ’ sa lt & pe pp er ’ , 0. 2); //Add s a l t& pepper
noise toth e im ag e
55
56
8 a = double (a);
9 [m n] = size (a);
10 N = input ( ’ enter the window si ze=’ ) ; //T he win do w s i ze
ca n be 3x 3 ,5 x5etc
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_I mag = uint8 (Out_Imag);
27 ShowImage(a, ’ or ig in al Imag e ’ )
28 title(
29 figure ’ Noisy Imag e ’ )
30 ShowImage (Out_Imag , ’ av er ag e f i l t e r e d Image ’ )
31 title( ’5x5 aver age f i l t e r e d Ima ge ’ );
57
Figure 6.7: Scilab code to Perform Average Filtering operation
29 end
58
30 end
31 ma t = gsort (mat);
32 if ( m odulo (N,2)==1)
33 b(i, j) = (ma t(((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, ’ Ori gin al Im ag e ’ )
43 title( ’ Ori gin al Im ag e ’ )
44 figure
45 ShowImage(a, ’ nois y ima ge ’ )
46 title( ’ noi sy ima ge ’ )
47 figure
48 ShowImage(b, ’Me dia n Fi lt ere d Im ag e ’ )
49 title( ’5x5 Median Fil ter ed Imag e ’ )
Func_medianall.sci
Scilab code Exa 6.23 Scilab code to Perform median filtering of colour
image
59
Figure 6.8: Scilab code to Perform median filtering
60
Figure 6.9: Scilab code to Perform median filtering of colour image
20 b = uint8 (b);
21 Out_I mage = uint8 (Out_Image);
22 // ShowCol orImage (b , ’ no is e add ed ’ )
23 // t i t l e ( ’ n o i s e adde d ’ )
24 figure
25 ShowColorImage(Out_Image , ’3 x3 median filtered ’ )
26 title( ’3 x3 median f i l t e r e d ’ )
Scilab code Exa 6.24 Scilab code to Perform Trimmed Average Filter
61
7 c = im re ad( ’ E : \ DIP JAYARAMAN
\ Chapter6 \ len na . jpg ’ ) ; //
SIVP tool box
8 s =1; //s den ote s th e number of values to be l e f t
in th e end
9 r =1;
10 N = 9 ; //3 x3 wi nd ow
11 a = double (imnoise(c, ’ gau ssi an ’ ) ) ;
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 sort ed_m at = gsort (mat);
19 Sum=0;
20 for k=r+s:(N-s)
21 Su m = Su m+ma t(k);
22 end
23 b(i,j)= Su m/(N-r-s);
24 end
25
26 end
a = uint8 (a);
27 b = uint8 (b);
28 // fi gu re
29 // imshow( c )
30 // t i t l e ( ’ O ri gi n al Ima ge ’ )
31 figure
32 ShowImage(a, ’ nois y ima ge ’ )
33 title( ’ noi sy ima ge ’ )
34 figure
35 ShowImage(b, ’Tr immed Ave rag e Fi lt er ed Im ag e ’ )
36 title( ’Tr immed Ave rage Fi lt er ed Im ag e ’ )
62
Figure 6.10: Scilab code to Perform Trimmed Average Filter
63
Chapter 7
Image Segmentation
Scilab code Exa 7.23 Scilab code for Differentiation of Gaussian function
64
Figure 7.1: Scilab code for Differentiation of Gaussian function
65
Figure 7.2: Scilab code for Differentiation of Gaussian function
66
Figure 7.3: Scilab code for Differentiation of Gaussian Filter function
Scilab code Exa 7.25 Scilab code for Differentiation of Gaussian Filter
function
67
Figure 7.4: Scilab code for Differentiation of Gaussian Filter function
10 r = sqrt (i.*i+j.*j);
11 y1 = (1/ ( sig ma1^2))*(( (r.*r)/sigm a1^2) -1 ).* exp (-r.*r
/2*sigma1^2);
12 y2 = (1/ ( sig ma2^2))*(( (r.*r)/sigm a2^2) -1 ).* exp (-r.*r
/2*sigma2^2);
13 y = y1-y2;
14 plot (i,y)
15 xtitle ( ’Sha pe of DOG Fi lt er ’ )
16 // Res ult
17 //En te r th e valu e of si gm a1 : 4
18 //En te r th e valu e of si gm a2 : 1
19 //
68
Scilab code Exa 7.27 Scilab code for Edge Detection using Different Edge
detectors
69
Figure 7.5: Scilab code for Edge Detection using Different Edge detectors
70
Scilab code Exa 7.30 Scilab code to perform watershed transform
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 //C ap t io n : To verify th e gi ve n ma tr ix is a
covaraince mat rix
2 //Probl em 4
3 //page438
4 close ;
5 clear ;
6 clc ;
7 K = [3 7, -1 5;-1 5,37 ];
8 eva ls = spec (K);
9 eva ls = gsort (evals);
10 disp (evals, ’ Ei ge n Val ue s are =’ )
11 if (evals== abs (evals)) then
12 disp (’B oth th e eige n valu es ar e non −negative and
th e giv en ma tr ix is a cov aria nce ma tr ix ’ );
13 else
14 disp ( ’ no n−covariance mat rix ’ )
15 end
Scilab code Exa 8.5 To compute the covariance of the given 2D data
73
1 //Cap ti on : To co mpu te the covariance of the given 2D
data
2 //Probl em 5
3 //page439
4 close ;
5 clear ;
6 clc ;
7 X1 = [2,1 ]’;
8 X2 = [3,2 ]’;
9 X3 = [2,3 ]’;
10 X4 = [1,2 ]’;
11 X = [X 1,X2,X 3,X4];
12 disp (X , ’X= ’ ) ;
13 [M,N ] = size (X); //M=rows , N = col umn s
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 , ’m ean =’ ) ;
20 K = A’ *A ;
21 Kdisp
22 = K/(K(M-
, ’T1);
he Cov ara ince ma ti x is K =’ )
23 // Res ult
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 Cov arai nce ma ti x is 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
75
30 disp (b , ’b = ’ )
31 disp (W1, ’W1= ’ )
32 disp (W2, ’W2= ’ )
33 end
34 end
35 disp ( ’ Fi nal W ei g h ts after o ne iter atio n ar e ’ )
36 disp (b , ’ Bias We igt h 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
77
18 b = y > m ; // the binary block
19 disp (b ,’ Bi na ry all oca tio n ma tr ix is B=’ )
20 K = sum ( sum (b));
21 disp (K , ’nu mber of one s =’ )
22 if ( K ~= blk^ 2 ) & ( K ~= 0)
23 ml = m-s ig* sqrt (K/((blk^2)-K));
24 disp (ml, ’T he va lue of a =’ )
25 mu = m+s ig* sqrt (((blk^2)-K)/K);
26 disp (mu, ’T he va lue of b =’ )
27 x(i: i+(b lk-1 ), j:j+ (bl k -1 )) = b*m u
+( 1- b)*m l;
28 end
29 end
30 end
31 disp ( round (x), ’ Re con str uct ed Bl oc k is x =’ )
32 // Res ult
33 // Origina l Bl oc k is x =
34 //
35// 65. 75. 80. 70.
36// 72. 75. 82. 68.
37// 84. 72. 62. 65.
38// // 6 6 .
39 68. 72. 80.
40 //En t er th e blo ck size :4
41 // mean va lue is m = 72. 25
42 / /St a n d a r d dev iat ion of t he bl oc k is = 6. 62 82 22 5
43 //B in ar y allo catio n ma tr ix is B=
44 //
45 // F T T F
46 // F T T F
47 // T F F F
48 / / F F F T
49 //
50 //n umber of on es = 6
51 / /T he va lu e of a = 67. 11 580 1
52 / /T he va lu e of b = 80 .8 06 998
53 // Rec ons tru cte d Bl oc k is x =
54 //
78
55// 67. 81. 81. 67.
56// 67. 81. 81. 67.
57// 81. 67. 67. 67.
58// 67. 67. 67. 81.
79
Figure 9.1: Program performs Block Truncation Coding
26 end
27 //imsh ow( ui nt 8 (x ) )
28 // t i t l e ( ’ Rec on str uct ed Ima ge ’ )
29 x = uint8 (x);
30 figure (1)
31 imshow(x1)
32 title( ’ Ori gin al Im ag e ’ ) ; //I PD tool box
33 figure (2)
34 ShowImage(x, ’ Reconstr ucted Im ag e ’ ) ; //I PD tool box
35 title( ’ Bl oc k Size = 8 ’ )
80
Chapter 10
Binary Image Processing
Scilab code Exa 10.17 Scilab Code for dilation and erosion process
81
Figure 10.1: Scilab Code for dilation and erosion process
Scilab code Exa 10.19 Scilab Code to perform an opening and closing op-
eration on the image
82
//S IVP tool box
8 // b =[1 ,1 ,1; 1 ,1 ,1; 1 ,1 ,1] ;
9 Struc tureEl ement = Create Struct ureEle ment( ’ squa re ’ ,
3) ;
10 //O pe ni ng is do ne by f i r s t applying erosi on and th en
dila tion operat ions on im ag e
11 b1 = Erod eIma ge(a,Structure Eleme nt);
12 b2 = Dila teIma ge(b1,Structur eElem ent);
13 // Closi ng is done by f i r s t appl ying dil ati on and
th e n erosion oper atio n on i ma g e
14 a1 = Dila teIma ge(a,Structure Elemen t);
15 a2 = Erod eIma ge(a1,Structure Elemen t);
16 // Display ing ori gi na l Image
17 figure (1)
18 ShowImage(a, ’ Ori gin al Im ag e ’ ) ;
19 // Dis pla yin g Op ene d Im ag e
20 figure (2)
21 ShowImage(b2, ’ Ope ne d Image ’ ) ;
22 xtitle ( ’ Ope ned Image ’ )
23 // Displ aying Close d Im ag e
24 figure (3)
25 xtitle
26 ShowImage(a2, Ima ge ’ )Im age ’ ) ;
( ’ Closed ’ Closed
83
Figure 10.2: Scilab Code to perform an opening and closin g 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
85
toolbox
19 title( ’ Origi nal Col or Imag e ’ ) ;
20 figure (2)
21 ShowColorImage(R, ’ Red Com pone nt ’ ) ;
22 figure (3)
23 ShowColorImage(G, ’ Green Co mp one nt ’ ) ;
24 figure (4)
25 ShowColorImage(B, ’ Blu e Comp onen t ’ ) ;
Scilab code Exa 11.12 Read a Colour image and separate the colour im-
age into red green and blue planes
86
Figure 11.1: Read an RGB image and extract the three colour componen ts
red green blue
87
19 ShowColorImage(b1, ’ Gr ee n Missing ’ );
20 figure (4)
21 ShowColorImage(c1, ’ Blue Missing ’ );
Scilab code Exa 11.16 Compute the histogram of the colour image
88
Figure 11.2: Read a Colour image and separate the colour image int o red
green and blue planes
89
25 gre en = length ( find (h > .25 & h <= .4 16 7)) /pi xe ls;
26 cya n = length ( find (h > .4 16 7 & h <= .5 83 3)) /pi xe ls;
27 blu e = length ( find (h > .5 83 3 & h <= .7 5) )/p ix el s;
28 mage nta = length ( find (h > .75 & h <= .9 16 7) )/p ix el s;
29 // Plots histogram
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 n7
47 plot2d2 (n6,white_pixels*
= 7: 0. 1: 8 ones (1 , length (n)),0)
48 plot2d2 (n7,black_pixels* ones (1 , length (n)),5)
Scilab code Exa 11.18 Perform histogram equalisation of the given RGB
image
90
; //S IVP t oolb ox
7 // con ver sio n of RGB to YIQ format
8 b = rgb 2nt sc(a);
9 //Hi st og ra m equa lisa tion of Y co mponent alone
10 b(:,:, 1) =
11 // con ver sio n of YIQ to R GB format
12 c = nts c2r gb(b);
13 figure (1)
14 ShowColorImage(a, ’ Ori gin al Im ag e ’ ) ; //I PD tool box
15 figure (2)
16 ShowColorImage(c, ’ Histo gtra m equa lize d Im ag e ’ ) ;
//I PD tool box
Scilab code Exa 11.21 This program performs median filtering of the colour
image
91
Figure 11.3: This program performs median filtering of the colour image
toolbox
92
2 // Fig . 1 1 . 2 4 : MATLAB Examp le6
3 //page599
4 clc ;
5 close ;
6 a = im re ad( ’ E : \ DIP JAYARAMAN
\ Chapter11 \ pe pp er s . png ’ )
; //S IVP t oolb ox
7 // con ver sio n of RGB to YIQ format
8 yiq = rgb 2nt sc(a);
9 // Extract the Y co mp on en t alone
10 b = yi q(:, :,1 );
11 h = [-1,-1,-1;- 1,8,-1;- 1,-1,-1] ;
12 //Perf orm high pass f i l t e r i n g only on Y co mp on en t
13 c1 = con v2d 2(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 // con ver t YIQ to RGB format
22
23 a1 = nt(1)
figure sc2 rgb(y iq);
24 ShowColorImage(a, ’ Ori gin al Im ag e ’ ) ; //I PD tool box
25 figure (2)
26 ShowColorImage(a1, ’ High Pass f i l t e r e d Imag e ’ ) ; //
IPD toolbox
Scilab code Exa 11.28 Perform gamma correction for the given colour im-
age
93
Figure 11.4: Fitlering only the luminance component
94
Figure 11.5: Perform gamma correction for the given colour image
22 end
23 end
24 figure (1)
25 ShowColorImage(I, ’ Ori gin al Im ag e ’ ) ; //I PD tool box
26 figure (2)
27 ShowColorImage( uint8 (J), ’Gamma Co rr ec te d Image ’ ) ;
//I PD tool box
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)<10 0)
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)>=1 00 & I(i, j)<15 0)
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)>=1 50 & I(i, j)<20 0)
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)>=2 00 & I(i,j) <= 256 )
29 J(i,j,1)=I(i,j)+120;
30 J(i,j,2)=I(i,j)+160;
31
32 end J(i,j,3)=I(i,j)+45;
33 end
34 end
35 figure (1)
36 ShowImage(K, ’ Ori gin al Im ag e ’ ) ; //I PD tool box
37 figure (2)
38 ShowColorImage(J, ’ Pse udo Coloured Ima ge ’ ) ; //IPD
toolbox
Scilab code Exa 11.32 Read an RGB image and segment it using the
threshold method
96
Figure 11.6: Perform Pseudo Colouring Operation
97
Figure 11.7: Read an RGB image and segment it using the threshold method
98
Chapter 12
Wavelet based Image
Processing
99
17 CD = im2 int 8(CD);
18 CA1 = im 2in t8(CA 1);
19 CH1 = im 2in t8(CH 1);
20 CV1 = im 2in t8(CV 1);
21 CD1 = im 2in t8(CD 1);
22 A = [C A,CH;C V,CD];
23 B = [C A1, CH 1;CV 1,CD1];
24 imshow(B)
25 title( ’ Result of Se co nd Leve l Dec omp osi tio n ’ )
Scilab code Exa 12.42 Scilab code to generate different levels of a Gaus-
sian pyramid
100
19 t = t+k (i, j);
20 end
21 end
22 for i = 1:ke rne lsi ze
23 for j = 1:ke rne lsi ze
24 k(i, j) = k(i, j)/t ;
25 end
26 end
27 for t = 1: 1:r f
28 // co nv ol ve i t wi t h th e picture
29 Fil ter edI mg = b;
30 if t==1
31 Filte redIm g = filt er2(k,b)/25 5;
32 else
33 Filte redIm g = filt er2(k,b);
34 end ;
35 //c ompute t he si ze of th e re du ce d i mag e
36 m = m/ 2;
37 n = n/ 2;
38 // create the re duc ed im ag e thr oug h sampl ing
39 b = zeros (m,n);
40
41 for ifor= 1:
j m= 1:n
42 b(i,j) = Filte redIm g(i*2,j*2);
43 end ;
44 end ;
45 end ;
46 figure
47 ShowImage(a, ’ Ori gin al Im ag e ’ )
48 figure
49 ShowImage(b, ’ Dif fer ent Levels of Ga us ai n Pyramid ’ )
50 title( ’ Differ ent Level s of Ga us ai n Pyramid Lev el 2 ’ )
101
Figure 12.1: Scilab code to generate different levels of a Gaussian pyramid
102
Scilab code Exa 12.57 Scilab code to implement watermarking in spatial
domain
1 //Cap ti on : Scila b co de to im pl em en t wa te rm ar ki ng in
spa tia l domain
2 //Fig12 .57
3 //Page662
4 clc
5 close
6 a = im re ad( ’ E : \ DIP JAYARAMAN\ Chapter12 \ cameraman . jp g
’ );
7 figure
8 imshow(a)
9 title( ’ Base Imag e ’ ) ;
10 b = im re ad( ’ E : \ DIP JAYARAMAN\ Chapter12 \ keyima ge . jp g ’
);
11 b = rgb 2gr ay(b);
12 b = imr esi ze(b,[ 32 32 ], ’ bic ubi c ’ ) ;
13 [m1 n1] = size (b);
14 figure
15 imshow(b)
16
17 title(
[m Image ’ ) ;
’Mar k(a);
n] = size
18 i1 = 1;
19 j1 = 1;
20 p =1;
21 c =a;
22 ii i = 1;
23 jj j = 1;
24 a = uint8 (a);
25 b = uint8 (b);
26 for f f = 1:8
27 for i = 1:32
28 jjj = 1;
29 for j = j1:j 1+n 1-1
30 a(i, j) = bi ta nd(a( i,j), uint8 (254)); //
LSB of ba se im ag e is set to zer o .
31 te mp = bi ta nd(b( i,jj j), uint8 ((2^ff)-1));
103
Figure 12.2: Scilab code to implement watermarking in spatial domain
Scilab code Exa 12.63 Scilab code to implement wavelet based watermark-
ing
104
2 //Fig12 .63
3 //Page666
4 clc ;
5 close ;
6 // Ori gin al Im ag e
7 img = im re ad( ’ E : \ DIP JAYARAMAN
\ Chapter12 \ cameraman .
jpg ’ );
8 figure
9 imshow(img)
10 title( ’ Ori gin al Im ag e ’ ) ;
11 [p q] = size (img);
12 //Gene rat e the ke y
13 //key = imread ( ’E: \ DIP JAYARAMAN
\ Chapter12 \ keyimg1 .
png ’ ) ;
14 //key = im re si ze ( ke y , [ p q ] ) ;
15 key = im re ad( ’ E : \ DIP JAYARAMAN \ Chapter12 \ keyimage .
jpg ’ );
16 ke y = rgb 2gr ay(ke y);
17 c = 0. 00 1; // I n i t i a l i s e the weig ht of Water markin g
18 figure
19 imshow(key)
20
21 title(vele
//Wa ’ Key ’ ) ;nsf orm
t tra of ori gi na l ima ge ( ba se ima ge )
22 im g = double (img);
23 ke y = double (key);
24 [c a,ch,c v,cd] = dw t2(i mg, ’ db1 ’ ) ; //Co mpute 2D wav el et
transform
25 // Pe rf orm the wat erm ark ing
26 y = [c a ch;c v cd] ;
27 Y = y + c *k ey;
28 p=p/2;
29 q=q/2;
30 for i=1:p
31 for j=1:q
32 nc a(i,j ) = Y(i, j);
33 nc v(i,j ) = Y(i+ p,j);
34 nc h(i,j ) = Y(i, j+q) ;
35 nc d( i,j ) = Y (i +p ,j +q );
105
36 end
37 end
38 // Displa y the Wa te rm ar ke d ima ge
39 wimg = idw t2(nc a, nc h ,nc v ,nc d, ’ db1 ’ ) ;
40 wim g1 = uint8 (wimg);
41 figure
42 imshow(wimg1)
43 title( ’ Wate rmar ked Image ’ )
44 // Extractio n of ke y fr om Wa terma rked im ag e
45 [rc a ,rc h ,rc v ,rcd] = dwt 2(wim g , ’ db1 ’ ) ; // Compu te 2D
wave let transf orm
46 n1=[rca,rch;rcv,rcd];
47 N1=n1-y;
48 N1 = N1* 4;
49 N1 = im2 int 8(N1);
50 figure
51 imshow(N1)
52 title( ’ Extract the ke y fr om wa te rm ar ke d im ag e ’ )
106
Appendix
Scilab code AP 1 2D Fast Fourier Transform
107
25 end
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 th e ele men ts to
find the me dia n
30 if ( m odulo (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)
34 end +1))/2;
35 end
36 end
109
3 // Ou tp ut va ri ab le s i n i t i a l i s a t i o n ( not found in
in pu t variables )
4 bout=[];
5
6 // N umber of ar gu me nt s in function ca ll
7 [%nargou t ,%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 typ eo f (I)== ” ui nt 8 ” then
16 z = (2 55* (0:n- 1))/ n;
17 elseif isa(I, ’ uin t16 ’ ) | isa( I, ’ int 16 ’ ) then
18 z = 655 35* (0: (n-1)) /n;
19 else / / I is d o u b l e o r sing le
20 z = (0 :( n-1 ))/n
21 end ;
22 [m,n ] = size (I);
23 /b/ =L oop
24 zeros
ov (m,n);
e r all inter vals , ex ce pt th e last
25 for i = 1: length (z)-1
26 // j is t h e in d e x v al ue w e will o u t p u t , s o it
de pend upon storage cla ss
27 if ty pe of (b)== ’ uin t8 ’
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 / / T ake ca re of t ha t last inter val
110
39 d = find (I >= z( $)) ;
40 if ~isempty(d) then
41 // j is t h e in d e x v al ue w e will o u t p u t , s o it
de pend upon storage cla ss
42 if ty peo f (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 bou t = double (bout);
51 endfunction
111