Scilab Textbook Companion for Digital Image Processing by S. Jayaraman, S. Esakkirajan And T.Veerakumar1 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 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 1 Funded 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 1 Introduction to Image Processing System 2 2D Signals and Systems 3 Convolution and Correlation 4 Image Transforms 5 Image Enhancement 6 Image Restoration and Denoising 7 Image Segmentation 8 Object Recognition 9 Image Compression 10 Binary Image Processing 11 Colur Image Processing 12 Wavelet based Image Processing 4 9 12 15 24 34 44 64 73 77 81 85 99 3 . . . . . . . . . . . . . Circular Convolution exspressed as linear convolution plus alias . . . . . . . . . . . .16 3.17 3. . . . . . . .13 Exa Exa Exa Exa Exa Exa Exa Exa 3. . . . . . . Frequency Response . 4 9 10 12 12 15 15 16 17 17 18 18 19 20 20 21 22 22 24 24 25 26 28 32 Exa 3. . . . . . . Linear auto correlation of a 2D matrix . . . . . . . . . . . . . . . . . . . . . .12 Exa 4.3 3. . .5 4. . . . . . . . . . Circular correlation between two signals . . . . . . . . . . .3 Exa Exa Exa Exa Exa Exa Exa Exa Exa 1. . . . .6 Exa 4. . . DFT of 4x4 grayscale image . .10 Exa 4. . . . . . . . . . . . . . 2D Linear Convolution . . . . . . . . . . Program to compute discrete cosine transform .2 3.4 4. . . . . . . . . . .12 Exa 3. . . . . . . Scilab code to intergchange phase information between two images . . . . Circular Convolution between two 2D matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Circular correlation between two signals . . . 2D Linear Convolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2D Linear Convlolution . . . . . . . .12 2. . . . 2D Linear Convolution . Frequency Response . .8 3. . . . . . . . . . . Linear Cross correlation of a 2D matrix . . . . . . .13 2. . . . . . . . . . . . . . . .16 3. . . . .1 3. . 2D DFT of 4X4 grayscale image .11 Program to calculate number of samples required for an image . . . . . . . . . . . . .18 4. 2D Linear Convolution . . . . Linear Convolution of any signal with an impule signal given rise to the same signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15 3. . . . .7 3. . . .13 .14 3. . . . . . . . . . . . False contouring Scilab code . . . Linear Cross correlation of a 2D matrix .List of Scilab Codes Exa 1. . Program to perform KL tranform for the given 2D matrix Program to find the singular value decomposition of given matrix . . . . . . . . . .12 Exa 11. To verify the given matrix is a covaraince matrix . . . . . . . . . . . . . . . . . . . . . . . . . .19 Exa 11. .7 5. . . .9 9. . .4 8. . . . . . . . . . .Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa 5. . . .5 8. .5 6. . Scilab code performs Pseudo inverse filtering . . . . . Scilab code for Contrast Manipulation . . . . . .18 6. . . . . . . Read an RGB image and extract the three colour components red green blue . . Program performs Block Truncation Coding BTC . . . . . . . . . . Scilab code to Perform median filtering of colour image Scilab code to Perform Trimmed Average Filter . . . . . . . . .16 5. . . Program performs gray level slicing without background Scilab code to create motion blur . . Compute the histogram of the colour image . . . . .20 6. . . . . . . . . . . . Scilab code for Differentiation of Gaussian function . . . . . . .13 6. . . . . . . . . 5 34 36 36 38 38 40 44 45 48 50 54 55 57 59 61 64 67 69 71 73 73 75 77 79 81 82 85 86 88 90 91 . . . . .25 7. . . . . . . . . . . . . . . . . Read a Colour image and separate the colour image into red green and blue planes . . . . . .59 10. . .17 10. Scilab code to Perform Average Filtering operation . . .24 7. . . . . . . .4 Exa 11. . . . Program performs Block Truncation Coding . . .18 Exa 11. . . . .30 8. . . . . . . . . . Scilab Code for dilation and erosion process . . . .9 5.9 9. . . . . . . . . . .7 6. . . .13 5. . Scilab code for Differentiation of Gaussian Filter function Scilab code for Edge Detection using Different Edge detectors . . . Scilab code performs inverse filtering . . Scilab code to perform watershed transform . . Perform histogram equalisation of the given RGB image This program performs median filtering of the colour image . .21 6. .23 7. Scilab code performs inverse filtering . . Scilab code for brightness suppression .23 6. . . . .16 Exa 11. . . . . Scilab code to determine image negative . . . . Scilab Code to perform an opening and closing operation on the image . . . . . Develop a perceptron AND function with bipolar inputs and targets . .21 Scilab code for brightness enhancement . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 6. .5 5. . . Scilab code to Perform median filtering . . . Scilab code that performs threshold operation .1 6. . Scilab code to perform wiener filtering of the corrupted image .27 7. . . . . . . . . To compute the covariance of the given 2D data . . .57 Scilab code to implement watermarking in spatial domain Exa 12. . . . . . . . . . . . . . . . . . . . . . . Read an RGB image and segment it using the threshold method . .9 Scilab code to perform wavelet decomposition . . . . . Perform Pseudo Colouring Operation . . . . . . . . . . . . . . . . . .42 Scilab code to generate different levels of a Gaussian pyramid . . . . . . . . . Exa 12.24 11. . . . . . . . . . .30 11. .32 92 93 95 96 99 100 101 104 107 108 108 109 109 6 . . . . . . . . AP 2 2D Inverse FFT . Perform gamma correction for the given colour image . . AP 3 Median Filtering function . . . . . . . 11. . . . . . . . . . . . . . .Exa Exa Exa Exa Fitlering only the luminance component . . . . . . . . . . . . . . . . AP 4 To caculate gray level .63 Scilab code to implement wavelet based watermarking AP 1 2D Fast Fourier Transform .28 11. . . . . . . . . . . . AP 5 To change the gray level of gray image . Exa 12. . . . . Exa 12. . . . . . . . . . . . . . . . . . . . . . . performs inverse filtering . . . . .3 5. . . . Scilab code for Contrast Manipulation . . . . . . . . . . . . . . .8 6.4 6. . performs inverse filtering . . . Scilab code that performs threshold operation . to Perform median filtering of colour image . . . performs Pseudo inverse filtering . . Scilab code for brightness suppression . .2 4. . to Perform median filtering . . . . . . . . . . . . . . . . . . .3 6.5 5. . . . . . . .5 6. . .1 6. . . .3 5. . . . . . . . . . . . . . . .2 6. . . . . . . . . .2 4. . . . . . . . . Scilab code for Differentiation of Gaussian function . . .1 5. . . . . . . . . .6 6. . .1 2. . . . 11 13 14 27 29 30 35 37 39 39 41 43 45 47 49 51 53 56 58 60 61 63 65 66 to create motion blur . . . . . . . . . . . . . . . . . Program performs gray level slicing without background Scilab Scilab Scilab Scilab Scilab Scilab Scilab Scilab Scilab Scilab code code code code code code code code code code . . . . . . . . .1 2. . . . . . . . . . . . . Frequency Response .7 6. . . . . . . Scilab code to determine image negative . . . . . . . . . . . . Program to compute discrete cosine transform . to perform wiener filtering of the corrupted image to Perform Average Filtering operation .List of Figures 1. . . .10 7. . . . . . performs inverse filtering . . . . . . . . . . . . . . .4 5. . . . . . . . . . . . . . . .6 6. . . . .1 7. . .1 4. . . . . Frequency Response . . . . . . . . Scilab code for brightness enhancement . . . . . . . . . . . . . . . . . . .2 False contouring Scilab code . . to Perform Trimmed Average Filter . . Program to compute discrete cosine transform . . Scilab code for Differentiation of Gaussian function .9 6. .2 5. . . . Scilab code to intergchange phase information between two images . . . . 7 . . . . . . . . . . . .5 Perform gamma correction for the given colour image . . . .7 Read an RGB image and segment it using the threshold method 12. . .6 9. .4 7. . . . . . . . . . . . . .2 Scilab Code to perform an opening and closing operation on the image . . . . . . . 67 68 70 72 80 82 84 87 89 92 94 95 97 98 Program performs Block Truncation Coding . . 10. .6 Perform Pseudo Colouring Operation .2 Read a Colour image and separate the colour image into red green and blue planes . . . . . . . . . . . . . . . . . . . . . . for Edge Detection using Different Edge detectors to perform watershed transform . 11.1 Scilab Scilab Scilab Scilab code code code code for Differentiation of Gaussian Filter function . . . . . . .1 Scilab Code for dilation and erosion process . . . . . . . . .3 7. . 11. . . .1 Read an RGB image and extract the three colour components red green blue . . . . for Differentiation of Gaussian Filter function . . . . . . .2 Scilab code to implement watermarking in spatial domain . . . 11. . . . .4 Fitlering only the luminance component .1 Scilab code to generate different levels of a Gaussian pyramid 102 12. . . . 11. .7. . . . .5 7. . . . . . . . 11. . . 104 8 . . . . . . . . . . . 11. 11. . . . . .3 This program performs median filtering of the colour image . . . 10. n = 6. // number o f d o t s p e r i n c h i n b o t h horizontal & vertical Fs = m * N2 * n * N2 . N = 400. // d i m e n s i o n o f t h e image i n i n c h e s m = 4.3 Program to calculate number of samples required for an image 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // C a p t i o n : Program t o c a l c u l a t e number o f s a m p l e s r e q u i r e d f o r an image // Example1 . ’ Number o f s a m p l e s r e u q i r e d t o p r e s e r v e t h e i n f o r m a t i o n i n t h e image= ’ ) // R e s u l t // Number o f s a m p l e s r e u q i r e d t o p r e s e r v e t h e i n f o r m a t i o n i n t h e image= 9 .Chapter 1 Introduction to Image Processing System Scilab code Exa 1. disp ( Fs . // number o f d o t s p e r i n c h i n e a c h d i r e c t i o n N2 = 2* N . 3 // p a g e 12 clc . close . 32) . 1 3 3 // p a g e 13 4 clc . 11 // u s i n g 128 g r a y l e v e l s 12 figure 13 a_128 = grayslice (a . 15 ShowImage ( a_128 . gray_128 ) . ’ Image w i t h 64 g r a y l e v e l s ’ . 14 gray_128 = gray (128) . 10 . gray_64 = gray (64) .64) . 8 figure 9 imshow ( a ) 10 title ( ’ O r i g i n a l image ’ ) . 6 a = ReadImage ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 \ t i g e r p u b . gray_64 ) .128) . ShowImage ( a_64 . 7 a = uint8 ( a ) .13 False contouring Scilab code 1 // C a p t i o n : F a l s e c o n t o u r i n g S c i l a b c o d e 2 // F i g 1 . // u s i n g 32 g r a y l e v e l s figure a_32 = grayslice (a .15 // 1 5 3 6 0 0 0 0 .sci Scilab code Exa 1. j p g ’ ). ’ Image w i t h 128 g r a y 16 17 18 19 20 21 22 23 levels ’. // u s i n g 64 g r a y l e v e l s figure a_64 = grayslice (a .sci check Appendix AP 5 for dependency: grayslice. 5 close . check Appendix AP 4 for dependency: gray. ’ Image w i t h 8 g r a y l e v e l s ’ . 34 ShowImage ( a_8 . ’ Image w i t h 32 g r a y l e v e l s ’ . ’ Image w i t h 16 g r a y l e v e l s ’ . 26 // u s i n g 16 g r a y l e v e l s 27 figure 28 a_16 = grayslice (a . 31 // u s i n g 8 g r a y l e v e l s 32 a_8 = grayslice (a . 29 gray_16 = gray (16) . 11 . gray_8 ) .8) . gray_16 ) . 30 ShowImage ( a_16 .Figure 1.16) . gray_32 ) .1: False contouring Scilab code 24 gray_32 = gray (32) . 25 ShowImage ( a_32 . 33 gray_8 = gray (8) . %pi :.Y . Y ] = meshgrid ( . 7 Z = 2* cos ( X ) +2* cos ( Y ) .09: %pi ) . 9 xgrid (1) Scilab code Exa 2. 6 [X . 5 close .Chapter 2 2D Signals and Systems Scilab code Exa 2. 1 2 3 // p a g e 60 4 clc . Z ) .16 Frequency Response 1 2 3 // C a p t i o n : F r e q u e n c y R e s p o n s e // F i g 2 . 8 surf (X . 1 6 // p a g e 64 12 .12 Frequency Response 1 // C a p t i o n : F r e q u e n c y R e s p o n s e 2 // F i g 2 . 05: %pi ) . Y ] = meshgrid ( .Y . 7 Z = 2 . Z ) . 8 surf (X .1: Frequency Response 4 clc .cos ( Y ) .%pi :. 5 close .Figure 2. 9 xgrid (1) 13 . 6 [X .cos ( X ) . Figure 2.2: Frequency Response 14 . 1.2 2D Linear Convolution 15 . 9.1 2D Linear Convlolution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // C a p t i o n : 2−D L i n e a r C o n v o l u t i o n // Example3 . h = [1.1]. ’ x= ’ ) disp (h . disp (x .6.9].7 . 4 // p a g e 85 & p a g e 107 clc .Chapter 3 Convolution and Correlation Scilab code Exa 3. 1 & Example3 . // 11.8 .5 . ’ L i n e a r 2D c o n v o l u t i o n r e s u l t y = ’ ) // R e s u l t // L i n e a r 2D c o n v o l u t i o n r e s u l t y = // // 4. 6. x =[4 . // 11. 5. disp (y . 13. H ] = conv2d2 (x . 15. ’ h= ’ ) [y . 15. Scilab code Exa 3. 8.X . // 7. 13. h ) . h ) . 1 0 // p a g e 100 & p a g e 109 & p a g e 119 clc .1 .9]. 9. // 12. 5 & Example3 . // 12. h = [1 .2 .7 .6. x =[1 . // 5. ’ L i n e a r 2D c o n v o l u t i o n r e s u l t y = ’ ) // R e s u l t // L i n e a r 2D c o n v o l u t i o n r e s u l t y = // // 3. 24. disp (y . 76. h = [3 . // 11. 3.5].1. disp (y . 94.1.5 . 15. y = conv2d2 (x . 9 // p a g e 91 & p a g e 108 & p a g e 116 clc .4 . h ) .3.8 . 28. 33. 3 & Example3 . 22.1]. 2 & Example3 . // 21. 30. y = conv2d2 (x .1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // C a p t i o n : 2−D L i n e a r C o n v o l u t i o n // Example3 .4 . 31. // Scilab code Exa 3. 18.9].6.3. 3.1 . 52. x =[1 . 58. 45.5 . 17. 16 . 16. 27. 5. ’ L i n e a r 2D c o n v o l u t i o n r e s u l t y = ’ ) // R e s u l t // L i n e a r 2D c o n v o l u t i o n r e s u l t y = // // 1. // 7.3 2D Linear Convolution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // C a p t i o n : 2−D L i n e a r C o n v o l u t i o n // Example3 .4 . 49. 6 & Example3 . 12. 9. 10.2 . 15. 22.8 .7 . 15. h ) . 3. y = conv2d2 (x .Scilab code Exa 3. disp (y . h ) .3 . // 21.4]. ’ L i n e a r 2D c o n v o l u t i o n r e s u l t y = ’ ) // R e s u l t // L i n e a r 2D c o n v o l u t i o n r e s u l t y = // L i n e a r 2D c o n v o l u t i o n r e s u l t y = // // 5. ’ L i n e a r 2D c o n v o l u t i o n r e s u l t y = ’ ) // R e s u l t // L i n e a r 2D c o n v o l u t i o n r e s u l t y = // // 1 .6. 9. h = [1.7 . 12.1.4 .6. y = conv2d2 (x . x =[1 . h = [5 . 52.7 .8 .1]. 17 . // 5.3. 32 Scilab code Exa 3. 7.2.8]. x =[1 .8 2D Linear Convolution 1 2 3 4 5 6 7 8 9 10 11 12 // C a p t i o n : 2−D L i n e a r C o n v o l u t i o n // Example3 .2 . 16.7 2D Linear Convolution 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // C a p t i o n : 2−D L i n e a r C o n v o l u t i o n // Example3 . disp (y .5 . 7 // p a g e 111 clc . 40.9]. 8 // p a g e 113 clc . 2. 60. // 22. 4.4].2. Scilab code Exa 3.11 Linear Convolution of any signal with an impule signal given rise to the same signal 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // C a p t i o n : L i n e a r C O n v o l u t i o n o f any s i g n a l w i t h an impulse s i g n a l gives // r i s e t o t h e same s i g n a l // Example3 . 13. Scilab code Exa 3.3 .2. 2. y = conv2d2 (x .8]. disp (y . 18. 11.12 Circular Convolution between two 2D matrices 1 2 3 4 5 6 // C a p t i o n : C i r c u l a r C o n v o l u t i o n b e t w e e n two 2D matrices // Example3 . x =[1 . 8. h ) .6.7 . ’ L i n e a r 2D c o n v o l u t i o n r e s u l t y = ’ ) // R e s u l t // L i n e a r 2D c o n v o l u t i o n r e s u l t y = // // L i n e a r 2D c o n v o l u t i o n r e s u l t y = // // 1. 15.13 14 15 // // // 12. h = 1. 1 1 // p a g e 121 clc .3 . // 3. h = [5 . x = [1 . 18 . 9. 1 2 // p a g e 122 clc . 7.4]. 15. 40. 68. y2 = [ y1 (1 . // 22.3 . 60.* H .:) ] disp (y .y (: .8].1) + y (: . ’ L i n e a r C o n v o l u t i o n r e s u l t y= ’ ) disp ( y2 . // Element by Element m u l t i p l i c a t i o n y = ifft2d ( Y ) . 32. x = [1 . // 2D FFT o f x m a t r i x H = fft2d ( h ) . ’ c i r c u l a r c o n v o l u t i o n e x p e s s e d a s l i n e a r convolution plus a l i a s =’) // R e s u l t // L i n e a r C o n v o l u t i o n r e s u l t y= // // 5.:) + y1 ($ . Scilab code Exa 3. h = [5 . y1 (2 . 52. 16.13 Circular Convolution exspressed as linear convolution plus alias 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // C a p t i o n : C i r c u l a r C o n v o l u t i o n e x p r e s s e d a s l i n e a r convolution plus a l i a s // Example3 .4]. 12. // // c i r c u l a r c o n v o l u t i o n e x p e s s e d a s l i n e a r 19 . disp (y . y = conv2d (x . // 2D FFT o f h m a t r i x Y = X . // 21.7 . 1 3 // p a g e 123 clc . y1 = [ y (: . // 62.7 8 9 10 11 12 13 14 15 16 X = fft2d ( x ) . ’ C i r c u l a r C o n v o l u t i o n R e s u l t y = ’ ) // R e s u l t // C i r c u l a r C o n v o l u t i o n R e s u l t y = // // 70. $ ) .2.:) . h ) .6.2) ]. 60. h ) disp (y . h2 = h1 (: .4 . // 21.:) .1.5. ’ L i n e a r c r o s s C o r r e l a t i o n r e s u l t y= ’ ) // R e s u l t // L i n e a r c r o s s C o r r e l a t i o n r e s u l t y= // // 9.14 Linear Cross correlation of a 2D matrix 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // C a p t i o n : 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 m a t r i x // Example3 .15 Circular correlation between two signals 1 // C a p t i o n : C i r c u l a r 2 // Example3 . y = conv2d (x .2 . 22. h = h2 ( $ : -1:1 . x = [3 . 1 5 3 // p a g e 131 4 clc .convolution plus a l i a s = 20 21 22 23 // // // // 70.4]. 2. c o r r e l a t i o n b e t w e e n two s i g n a l s 20 .2 .1]. 9. 4. $ : -1:1) . 24. // 10.4]. 9. Scilab code Exa 3.3]. 5 x = [1 . 60. Scilab code Exa 3. 62.5. 68. 6 h = [3 . 1 4 // p a g e 129 clc .2 .2. h1 = [1 . 15 . 330.10.* H .:) . ’ C i r c u l a r C o r r e l a t i o n r e s u l t y= ’ ) // R e s u l t // C i r c u l a r C o r r e l a t i o n r e s u l t y= // // 37. $ : -1:1) . Scilab code Exa 3. 450. $ : -1:1) .12]. y = ifft2d ( Y ) . ’ C i r c u l a r C o r r e l a t i o n r e s u l t y= ’ ) // R e s u l t // C i r c u l a r C o r r e l a t i o n r e s u l t y= // // 300. h = h (: . y = ifft2d ( Y ) . Y = X . h = [3 . h = h ( $ : -1:1 . h = h ( $ : -1:1 . 21 .* H . // 420. disp (y . X = fft2d ( x ) . // 35.9 . X = fft2d ( x ) . H = fft2d ( h ) . x = [5 .16 Circular correlation between two signals 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // C a p t i o n : C i r c u l a r c o r r e l a t i o n b e t w e e n two s i g n a l s // Example3 . H = fft2d ( h ) . 25. disp (y . Y = X . 23.:) . 1 6 // p a g e 134 clc .6.20].7 8 9 10 11 12 13 14 15 16 17 18 h = h (: . 4.1 . 1. 1 7 // p a g e 136 clc . 1. h1 = [1 . ’ L i n e a r a u t o C o r r e l a t i o n r e s u l t x= ’ ) // R e s u l t // L i n e a r a u t o C o r r e l a t i o n r e s u l t x= // // 1.18 Linear Cross correlation of a 2D matrix 1 2 3 4 5 6 7 8 9 10 11 12 // C a p t i o n : 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 m a t r i x // Example3 . x2 = x1 (: .1. x = conv2d ( x1 .:) .1]. x2 = x2 ( $ : -1:1 . ’ L i n e a r c r o s s C o r r e l a t i o n r e s u l t y= ’ ) // R e s u l t // L i n e a r c r o s s C o r r e l a t i o n r e s u l t y= 22 .:) .3 . // 2. // 1. $ : -1:1) .1]. Scilab code Exa 3. x2 ) disp (x . 2.2. 2. 2. x = [1 .Scilab code Exa 3.1.17 Linear auto correlation of a 2D matrix 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // C a p t i o n : l i n e a r a u t o c o r r e l a t i o n o f a 2D m a t r i x // Example3 .4]. h2 = h1 (: .1 . y = conv2d (x . $ : -1:1) . 1 8 // p a g e 141 clc . h = h2 ( $ : -1:1 . h ) disp (y . x1 = [1 . 2.13 14 15 16 // // // // 4. 6. 3. 3. 7. 10. 23 . 1. 4. 1.1 . 0 0 0 // 0 0 0 0 // 0 0 0 0 // 0 0 0 0 Scilab code Exa 4.1 .1.1 .1 . ’ 2D DFT o f g i v e n 2D image = ’ ) // R e s u l t // 2D DFT o f g i v e n 2D image = // // 16.1 .1]. disp (F . N =4.5 2D DFT of 4X4 grayscale image 24 .4 DFT of 4x4 grayscale image 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // C a p t i o n : 2D DFT o f 4 x4 g r a y s c a l e image // Example4 .1 . F = kernel *( f * kernel ’) .1 . f = [1 .1 .Chapter 4 Image Transforms Scilab code Exa 4.1 .1.1 . //4− p o i n t DFT kernel = dft_mtx ( N ) .1 . 4 // p a g e 170 clc . 0. 1. 1.0 . check Appendix AP 1 for dependency: fft2d. 1.0 . F = [16 . // 1.0 . 1.0 .0 . //4− p o i n t DFT kernel = dft_mtx ( N ) . disp (f . N =4.sce check Appendix AP 2 for dependency: ifft2d.6 Scilab code to intergchange phase information between two images 1 2 3 4 5 // C a p t i o n : S c i l a b c o d e t o i n t e r g c h a n g e p h a s e i n f o r m a t i o n b e t w e e n two i m a g e s // Example4 .1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // C a p t i o n : 2D DFT o f 4 x4 g r a y s c a l e image // Example4 . 1.0. 1. 6 // p a g e 174 − 175 clc .0 . 1.0 . close .0 .0 . 25 . f = real ( f ) . 1.0 .0 .0. 5 // p a g e 171 clc . // 1. 1. f = ( kernel *( F * kernel ’) ) /( N ^2) . 1. 1. ’ I n v e r s e 2D DFT o f t h e t r a n s f o r m e d image f = ’ ) // R e s u l t // I n v e r s e 2D DFT o f t h e t r a n s f o r m e d image f = // // 1. 1. // 1.0].sce Scilab code Exa 4. fftb = fft2d ( double ( b ) ) . rec_b = ifft2d ( newfft_b ) . newfft_b = mag_b . ph_b = atan ( imag ( fftb ) . ’ O r i g i n a l l e n a Image ’ ) . png ’ ) . rec_a = ifft2d ( newfft_a ) .10 Program to compute discrete cosine transform 26 . title ( ’ baboon Image a f t e r p h a s e r e v e r s a l ’ ) Scilab code Exa 4. title ( ’ O r i g i n a l baboon Image ’ ) ffta = fft2d ( double ( a ) ) . b = imresize (b . mag_b = abs ( fftb ) . figure (1) ShowImage (a . a = imresize (a . png ’ ) . figure (2) ShowImage (b . ’ l e n a Image a f t e r p h a s e r e v e r s a l ’ ). a = rgb2gray ( a ) . ’ O r i g i n a l baboon Image ’ ) .5) . newfft_a = mag_a . real ( ffta ) ) .0. title ( ’ l e n a Image a f t e r p h a s e r e v e r s a l ’ ) figure (4) ShowImage ( uint8 ( rec_b ) .*( exp ( %i * ph_b ) ) .0. ’ baboon Image a f t e r p h a s e r e v e r s a l ’ ).6 a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 4 \ l e n a . figure (3) ShowImage ( uint8 ( rec_a ) . ph_a = atan ( imag ( ffta ) . // IPD t o o l b o x title ( ’ O r i g i n a l l e n a Image ’ ) .*( exp ( %i * ph_a ) ) .5) . real ( fftb ) ) . mag_a = abs ( ffta ) . 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // SIVP t o o l b o x b = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 4 \ baboon . b = rgb2gray ( b ) . Figure 4.1: Scilab code to intergchange phase information between two images 27 . 5 0. X = [4 . clc . disp (X .7].4 . 1 2 // p a g e 208 clear .5 0. [m .5 − 0.6532815 − 0.2705981 Scilab code Exa 4.6532815 // 0.5 // 0.5 − 0.5 0.7 .2705981 − 0. 28 . //DCT m a t r i x o f o r d e r f o u r X = dct_mtx ( N ) .3 .12 Program to perform KL tranform for the given 2D matrix 1 2 3 4 5 6 7 8 9 // C a p t i o n : Program t o p e r f o r m KL t r a n s f o r m f o r t h e g i v e n 2D m a t r i x // Example4 .5 // 0.6. ’DCT m a t r i x o f o r d e r f o u r ’ ) // R e s u l t //DCT m a t r i x o f o r d e r f o u r // // 0.2705981 − 0. E = [].5 . n ]= size ( X ) .5 . A = [].2705981 − 0. 1 0 // p a g e 198 clc .6 .7.6532815 0.2 .5 .6532815 0.5 0.1 2 3 4 5 6 7 8 9 10 11 12 13 14 // C a p t i o n : Program t o compute d i s c r e t e c o s i n e tranform // Example4 . N =4. 2: Program to compute discrete cosine transform 29 .Figure 4. 3: Program to compute discrete cosine transform 30 .Figure 4. j ) = V (: . i ) = T * X (: . end mx = A / n .8471005 0. E = E + X (: . end disp (x . // mean m a t r i x E = E/n.4460381 − 0.5949473 31 . C = E . D ] = spec ( C ) . i ( j ) ) . i ) * X (: . i ) . ’KL t r a n s f o r m a t i o n o f t h e i n p u t m a t r i x Y = ’ ) // R e c o n s t r u c t i o n for i = 1: n x (: .4384533 0. i ) . end disp (Y .10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 for i =1: n A = A + X (: .2147417 // 0. i ) ’. ’ E i g e n V a l u e s a r e U = ’ ) disp (T . // e i g e n v a l u e s and e i g e n v e c t o r s d = diag ( D ) .mx * mx ’.1929481 0.3002988 // 0.1963372 // 0. i ] = gsort ( d ) .0264211 // The e i g e n v e c t o r m a t r i x T = // 0. i ) .7455591 // − 0 . 7 8 0 2 6 2 0 0. // s o r t i n g t h e e l e m e n t s o f D i n descending order for j = 1: length ( d ) T (: . ’ The e i g e n v e c t o r m a t r i x T = ’ ) disp (T . end T =T ’ disp (d .4951684 0. // d i a g o n a l e l e m e n t s od e i g e n v a l u e s [d . i ) = T ’* Y (: . // c o v a r i a n c e m a t r i x C = E [ xx ’ ] − mx∗ mx ’ [V . ’ The KL t r a n f o r m b a s i s i s = ’ ) //KL t r a n s f o r m for i = 1: n Y (: . ’ R e c o n s t r u c t m a t r i x o f t h e g i v e n s a m p l e matrix X = ’ ) // R e s u l t // E i g e n V a l u e s a r e U = // 6. 2373664 4. // 4.0755729 3.3002988 // 0. -2 . // 5.6437095 4. 7 8 0 2 6 2 0 0. 3. 6. 5.46 47 48 49 50 51 52 53 54 55 56 57 // The KL t r a n f o r m b a s i s i s = // 0.5110551 9. 6. 7.8336957 // R e c o n s t r u c t m a t r i x o f t h e g i v e n s a m p l e m a t r i x x = // 4. ’A m a t r i x from s v d = ’ ) // R e s u l t // U = // 32 .8471005 0. 1 3 // p a g e 210 clear . ’V = ’ ) disp ( A_recon .1929481 0.2 .4951684 0.5312743 4.0198466 1. 7. -1]. Scilab code Exa 4. disp (U . clc . A_recon = U * S *V ’.3 . ’U = ’ ) disp (S .4289635 // 0. 2. ’ S = ’ ) disp (V .0190104 0.5949473 // KL t r a n s f o r m a t i o n o f t h e i n p u t m a t r i x Y = // 6.4460381 − 0.3.9237632 10.662515 // 3.4384533 0. 7.S .6254808 1. 5. V ]= svd ( A ) . A = [1 .7455591 // − 0 .13 Program to find the singular value decomposition of given matrix 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // C a p t i o n : Program t o f i n d t h e s i n g u l a r v a l u e decomposition of given matrix // Example4 . [U . 5962848 33 .7071068 // // S = // // 4. 3.7071068 // 0. 7 0 7 1 0 6 8 0.7453560 0.1622777 // // V = // // 0. 0. // 0.7071068 0. 3. − 2. 6 6 6 6 6 6 7 0. 6 6 6 6 6 6 7 − 2 .2981424 0. 0. 7 7 6D−16 // − 0 .16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 // − 0 .8944272 // 0 .4472136 // // A m a t r i x from s v d = // // 1. 2. − 0. // 3. − 1.2426407 0.3333333 0. Chapter 5 Image Enhancement Scilab code Exa 5. a = rgb2gray ( a ) . 5 3 // p a g e 246 4 clc . b = double ( a ) +50. 6 // a = i m r e a d ( ’ E : \ DIP JAYARAMAN\ C h a p t e r 5 \ p l a t e .5 Scilab code for brightness enhancement 1 // C a p t i o n : S c i l a b c o d e f o r b r i g h t n e s s enhancement 2 // F i g 5 . ’ Enhanced Image ’ ) . title ( ’ Enhanced Image ’ ) 34 . GIF ’ ) . png ’ ) . title ( ’ O r i g i n a l Image ’ ) figure (2) ShowImage (b . b = uint8 ( b ) . ’ O r i g i n a l Image ’ ) . figure (1) ShowImage (a . 5 close . 7 8 9 10 11 12 13 14 15 16 // SIVP t o o l b o x a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 4 \ baboon . 35 . png ’ ) .9 Scilab code for Contrast Manipulation 1 2 3 4 5 6 7 8 9 10 // C a p t i o n : S c i l a b c o d e f o r Contrast Manipulation // F i g 5 . a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 4 \ baboon . b = uint8 ( b ) . png ’ ) .5. 36 . 9 // p a g e 248 clc . b = double ( a ) *0. a = rgb2gray ( a ) . a = rgb2gray ( a ) . close . title ( ’ B r i g h t n e s s S u p r e s s e d Image ’ ) Scilab code Exa 5. ’ O r i g i n a l Image ’ ) . title ( ’ O r i g i n a l Image ’ ) figure (2) ShowImage (b .Scilab code Exa 5. ’ B r i g h t n e s s S u p r e s s e d Image ’ ) . figure (1) ShowImage (a .7 Scilab code for brightness suppression 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // C a p t i o n : S c i l a b c o d e f o r brightness suppression // F i g 5 . b = double ( a ) -50. 7 // p a g e 247 clc . b = uint8 ( b ) c = double ( b ) *2. close . a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 4 \ l e n a . 37 . 1 3 // p a g e 252 clc . ’ D e c r e a s e i n C o n t r a s t ’ ) . a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 5 \ l a b e l . 20 title ( ’ I n c r e a s e i n C o n t r a s t ’ ) Scilab code Exa 5. k = 255 . title ( ’ N e g a t i v e o f O r i g i n a l Image ’ ) Scilab code Exa 5. close .16 Scilab code that performs threshold operation 38 . j p g ’ ) .13 Scilab code to determine image negative 1 2 3 4 5 6 7 8 9 10 11 12 // C a p t i o n : S c i l a b c o d e t o d e t e r m i n e image n e g a t i v e // F i g . 17 title ( ’ D e c r e a s e i n C o n t r a s t ’ ) 18 figure (3) 19 ShowImage (c .11 c = uint8 ( c ) 12 figure (1) 13 ShowImage (a . ’ I n c r e a s e i n C o n t r a s t ’ ) . ’ O r i g i n a l Image ’ ) . title ( ’ O r i g i n a l o n c a Image ’ ) imshow ( k ) . imshow ( a ) . k = uint8 ( k ) . 5 . 14 title ( ’ O r i g i n a l Image ’ ) 15 figure (2) 16 ShowImage (b .double ( a ) . 4: Scilab code to determine image negative 39 .3: Scilab code for Contrast Manipulation Figure 5.Figure 5. title ( ’ O r i g i n a l Image ’ ) figure (2) ShowImage (b . for i = 1: m for j = 1: n if ( a (i . j ) =0. png ’ ) . 1 6 // p a g e 254 clc . close . title ( ’ T h r e s h o l d e d Image ’ ) xlabel ( sprintf ( ’ T h r e s h o l d v a l u e i s %g ’ .t ) ) // R e s u l t // E n t e r t h e t h r e s h o l d p a r a m e t e r 140 Scilab code Exa 5. j ) =255. [ m n ] = size ( a ) . ’ T h r e s h o l d e d Image ’ ) . j ) <t ) b (i . a = rgb2gray ( a ) . else b (i . a = imread ( ’E : \ D i g i t a l I m a g e P r o c e s s i n g J a y a r a m a n \ C h a p t e r 5 \ l e n a .20 Program performs gray level slicing without background 40 .1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // C a p t i o n : S c i l a b c o d e t h a t p e r f o r m s t h r e s h o l d operation // F i g 5 . t = input ( ’ E n t e r t h e t h r e s h o l d p a r a m e t e r ’ ) . ’ O r i g i n a l Image ’ ) . end end end figure (1) ShowImage (a . 41 . x = rgb2gray ( x ) . y = double ( x ) . L = max ( max ( x ) ) . end end end z = uint8 ( z ) . b = L. n ]= size ( y ) . 2 0 // p a g e 2 5 6 clc . a = round ( L /2) .1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // C a p t i o n : Program p e r f o r m s g r a y l e v e l s l i c i n g without background // F i g . for i =1: m for j =1: n if ( y (i . j ) = L . x = imread ( ’E : \ D i g i t a l I m a g e P r o c e s s i n g J a y a r a m a n \ C h a p t e r 5 \ l e n a . [m . else z (i . 5 . title ( ’ O r g i n a l Image ’ ) figure (2) ShowImage (z . ’ O r i g i n a l Image ’ ) . ’ Gray L e v e l S l i c i n g ’ ) . j ) <= b ) z (i . png ’ ) . j ) =0. title ( ’ Gray L e v e l S l i c i n g w i t h o u t p r e s e r v i n g background ’ ) 42 . j ) >= a & y (i . figure (1) ShowImage (x . Figure 5.6: Program performs gray level slicing without background 43 . 0.0.0925 . Motion_Blur = imfilter (a .0.0 .. 0 .0 .0.0 ..0].0..1 Scilab code to create motion blur 1 // C a p t i o n : S c i l a b c o d e t o c r e a t e m o t i o n b l u r 2 // F i g 6 . 0. 0 .0 ..0 .0072.. 1 3 // p a g e 326 4 clc .0. H ) .Chapter 6 Image Restoration and Denoising Scilab code Exa 6.0629 .0629 .0213 ..0. Motion_Blur = uint8 ( Motion_Blur ) .. ’ Motion B l u r r e d Image ’ ) 44 .0.0 .0 .0 .0569 .0. 1 0 .0092 . // 7 8 9 10 11 12 13 14 15 16 17 18 SIVP t o o l b o x // 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 ( ’ motion ’ .0 .0 . j p g ’ ) .0925 . 0 .0449 .0152 .0.0 ..0 .0.0.0. 6 a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 6 \humm .0509 .0. 2 5 ) H =[0 .0.0032 .0032 .0 .0.0865 . 5 close .0.0985 .0 .0.0.0 ..0 .0 .0.0213 . ShowImage (a .0.0152 .0.0.0865 .0509 .0 ..0.0.0 .0569 .0 .0 ...0092 .0072 .0 . ’ o r i g i n a l Image ’ ) title ( ’ o r i g i n a l Image ’ ) figure ShowImage ( Motion_Blur .0 .0449 .0 . 1: Scilab code to create motion blur 19 title ( ’ 10 x25 Motion B l u r r e d Image ’ ) check Appendix AP 1 for dependency: fft2d. close . 45 .sce Scilab code Exa 6.5 Scilab code performs inverse filtering 1 2 3 4 5 6 7 // C a p t i o n : S c i l a b c o d e p e r f o r m s i n v e r s e f i l t e r i n g // Degrade t h e image by means o f a known b l u r // Apply i n v e r s e f i l t e r t o t h e b l u r r e d image and s e e t h e r e s t o r e d image // F i g 6 .Figure 6.sce check Appendix AP 2 for dependency: ifft2d. 5 // p a g e 330 clc . 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 x = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 6 \ f l o w e r 2 . j p g ’ ) ; x = double ( rgb2gray ( x ) ) ; [ M N ]= size ( x ) ; h = zeros (M , N ) ; for i = 1:11 for j = 1:11 h (i , j ) = 1/121; end end sigma = sqrt (4*10^( -7) ) ; freqx = fft2d ( x ) ; // F o u r i e r t r a n s f o r m o f i n p u t image freqh = fft2d ( h ) ; // F o u r i e r t r a n s f o r m o f d e g r a d a t i o n y = real ( ifft2d ( freqh .* freqx ) ) ; freqy = fft2d ( y ) ; powfreqx = freqx .^2/( M * N ) ; 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 freqg = (( freqh . ’) ’) .* abs ( powfreqx ) ./( abs ( freqh .^2) .* abs ( powfreqx ) + alpha * sigma ^2) ; Resfreqx = freqg .* freqy ; Resa = real ( ifft2d ( Resfreqx ) ) ; x = uint8 ( x ) ; y = uint8 ( y ) ; Resa = uint8 ( Resa ) ShowImage (x , ’ O r i g i n a l Image ’ ) title ( ’ O r i g i n a l Image ’ ) figure ShowImage (y , ’ Degraded Image ’ ) title ( ’ Degraded Image ’ ) figure ShowImage ( Resa , ’ R e s t o r e d Image ’ ) title ( ’ R e s t o r e d Image ’ ) check Appendix AP 1 for dependency: fft2d.sce check Appendix AP 2 for dependency: 46 47 ifft2d.sce Scilab code Exa 6.7 Scilab code performs inverse filtering 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // C a p t i o n : S c i l a b c o d e p e r f o r m s i n v e r s e f i l t e r i n g // Degrade t h e image by means o f a known b l u r and white noise // The image i s d e g r a d e d a s w e l l a s c o r r u p t e d by noise // Apply i n v e r s e f i l t e r t o r e s t o r e t h e image // F i g 6 . 7 // p a g e 332 clc ; close ; x = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 6 \ f l o w e r 2 . j p g ’ ) ; x = double ( rgb2gray ( x ) ) ; [ M N ]= size ( x ) ; h = zeros (M , N ) ; for i = 1:11 for j = 1:11 h (i , j ) = 1/121; end end sigma = sqrt (4*10^( -7) ) ; freqx = fft2d ( x ) ; // F o u r i e r t r a n s f o r m o f i n p u t image freqh = fft2d ( h ) ; // F o u r i e r t r a n s f o r m o f d e g r a d a t i o n y = real ( ifft2d ( freqh .* freqx ) ) +10* rand (M ,N , ’ n o r m a l ’ ) ; freqy = fft2d ( y ) ; powfreqx = freqx .^2/( M * N ) ; 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 freqg = (( freqh . ’) ’) .* abs ( powfreqx ) ./( abs ( freqh .^2) .* abs ( powfreqx ) + alpha * sigma ^2) ; Resfreqx = freqg .* freqy ; 48 49 . 29 y = uint8 ( y ) .sce Scilab code Exa 6. j p g ’ ) . 28 x = uint8 ( x ) . close . ’ R e s t o r e d Image ’ ) 38 title ( ’ R e s t o r e d Image ’ ) check Appendix AP 1 for dependency: fft2d. 9 // p a g e 333 clc . ’ O r i g i n a l Image ’ ) 32 title ( ’ O r i g i n a l Image ’ ) 33 figure 34 ShowImage (y .27 Resa = real ( ifft2d ( Resfreqx ) ) . x = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 6 \ f l o w e r 2 . 50 .9 Scilab code performs Pseudo inverse filtering 1 2 3 4 5 6 7 8 9 // C a p t i o n : S c i l a b c o d e p e r f o r m s Pseudo i n v e r s e filtering // Degrade t h e image by means o f a known b l u r and white noise // The image i s d e g r a d e d a s w e l l a s c o r r u p t e d by noise // 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 h e image // F i g 6 . 30 Resa = uint8 ( Resa ) 31 ShowImage (x . ’ Degraded+n o i s e Image ’ ) 35 title ( ’ Degraded+n o i s e Image ’ ) 36 figure 37 ShowImage ( Resa .sce check Appendix AP 2 for dependency: ifft2d. 51 . end end mask_b = ones (11 . Inv_filt = freq_res . z = uint8 ( z ) ShowImage (x . freqx = fft2d ( x ) . Thr_Freq = 0. freq_res = fft2d ( psf ) .^2+ Thr_Freq ) . psf = zeros (M .* freqx ) ) +25* rand (M . // F o u r i e r t r a n s f o r m o f d e g r a d a t i o n y = real ( ifft2d ( freqh .* Inv_filt ) ) . // F o u r i e r t r a n s f o r m o f i n p u t image freqh = fft2d ( h ) .11) /121. N ) . ’ O r i g i n a l Image ’ ) title ( ’ O r i g i n a l Image ’ ) figure ShowImage (y . psf = fftshift ( psf ) . ’ R e s t o r e d Image ’ ) title ( ’ R e s t o r e d Image ’ ) check Appendix AP 1 for dependency: 52 .10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 x = double ( rgb2gray ( x ) ) ./(( abs ( freq_res ) ) .2. z = real ( ifft2d ( freqy . n1 ] = size ( mask_b ) .N . [ M N ]= size ( x ) . N /2+1 -( n1 -1) /2: N /2+1+( n1 -1) /2) = mask_b . psf ( M /2+1 -( m1 -1) /2: M /2+1+( m1 -1) /2 . ’ n o r m a l ’ ) . y = uint8 ( y ) . [ m1 . N ) . j ) = 1/121. for i = 1:11 for j = 1:11 h (i . ’ Degraded+n o i s e Image ’ ) title ( ’ Degraded+n o i s e Image ’ ) figure ShowImage (z . freqy = fft2d ( y ) . x = uint8 ( x ) . h = zeros (M . 53 . fft2d. sigma = 50. 54 . x = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 6 \ f l o w e r 2 . Freqh = fft2d ( h ) . // Adding random n o i s e with normal d i s t r i b u t i o n Freqy = fft2d ( y ) .^2/( M * N ) . end end Freqa = fft2d ( x ) .*( abs ( Freqh ) >0) +1/ Gamma *( abs ( Freqh ) ==0) .* Freqa ) ) // image d e g r a d a t i o n y = y +25* rand (M . // I t i n d i c a t e s Wiener f i l t e r [ M N ]= size ( x ) .sce Scilab code Exa 6. for i = 1:5 for j = 1:5 h (i . clc . N ) . j p g ’ ) .sce check Appendix AP 2 for dependency: ifft2d.N . // SIVP t o o l b o x x = double ( rgb2gray ( x ) ) . Powy = abs ( Freqy ) . j ) = 1/25. Gamma = 1. y = real ( ifft2d ( Freqh . sFreqh = Freqh . alpha = 1.13 Scilab code to perform wiener filtering of the corrupted image 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 // C a p t i o n : S c i l a b c o d e t o p e r f o r m w i e n e r f i l t e r i n g o f t h e c o r r u p t e d image // F i g 6 . h = zeros (M . ” n o r m a l ” ) . 1 3 // Page 339 close . iFreqh = iFreqh ’. x = uint8 ( x ) . 1 8 // p a g e 349 clc . ResFreqa = Freqg . Resa = real ( ifft2d ( ResFreqa ) ) . ’ R e s t o r e d Image ’ ) title ( ’ R e s t o r e d Image ’ ) Scilab code Exa 6./( Powy -(1 .18 Scilab code to Perform Average Filtering operation 1 2 3 4 5 6 7 // C a p t i o n : S c i l a b c o d e t o P e r f o r m A v e r a g e F i l t e r i n g operation // F i g 6 .*( Powy . j p g ’ ) . close . Resa = uint8 ( Resa ) .*( abs ( Freqh ) * Gamma >1) + Gamma * abs ( sFreqh ) * iFreqh *( abs ( sFreqh ) * Gamma <=1) . iFreqh = iFreqh /( max ( max ( abs ( iFreqh ) ) ) ) . 0. // SIVP t o o l b o x a = imnoise (a .* Freqy . Freqg = iFreqh .2) . //Add s a l t &p e p p e r n o i s e t o t h e image 55 . y = uint8 ( y ) . a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 6 \ l e n n a . Powy = Powy . ’ s a l t & p e p p e r ’ . ShowImage (x .sigma ^2) .25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 iFreqh = 1/ sFreqh . ’ Degraded Image ’ ) title ( ’ Degraded Image ’ ) figure ShowImage ( Resa .*( Powy > sigma ^2) + sigma ^2*( Powy <= sigma ^2) . ’ O r i g i n a l Image ’ ) title ( ’ O r i g i n a l Image ’ ) figure ShowImage (y .alpha ) * sigma ^2) . 56 . j + l ) .21 Scilab code to Perform median filtering 1 // C a p t i o n : S c i l a b c o d e t o P e r f o r m median 2 // F i g 6 .Start +1) limit =( N -1) /2.limit : limit .Start +1) for j = Start :( n . ShowImage (a . for l = . Out_Imag = uint8 ( Out_Imag ) . ’ a v e r a g e f i l t e r e d Image ’ ) title ( ’ 5 x5 a v e r a g e f i l t e r e d Image ’ ) . Sum = Sum + a ( i +k . filtering 57 . Sum =0. end end Out_Imag (i . Out_Imag = a . for k = . 9 [ m n ]= size ( a ) . ’ o r i g i n a l Image ’ ) title ( ’ N o i s y Image ’ ) figure ShowImage ( Out_Imag .8 a = double ( a ) .limit : limit . 10 N = input ( ’ e n t e r t h e window s i z e = ’ ) . 5 x 5 e t c Start =( N +1) /2. j ) = Sum /( N * N ) . 2 1 3 // p a g e 352 4 clc . end end a = uint8 ( a ) . Scilab code Exa 6. // The window s i z e 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 can be 3 x3 . for i = Start :( m . 5 close . limit2 = limit1 +1. limit1 = (N -1) /2. // SIVP t o o l b o x N = input ( ’ E n t e r t h e window s i z e ’ ) .0.2) ) . [m . n ] = size ( a ) . if ( modulo (N . ’ s a l t & p e p p e r ’ . for k = .End +1) for j = Start :( n . else Start = N /2. end 58 .Figure 6.2) ==1) Start = ( N +1) /2. j p g ’ 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ) .End +1) I =1. End = Start +1. j +1) I = I +1. b = a. a = double ( imnoise (c .limit1 : limit2 for l = . limit2 = limit1 . limit1 = ( N /2) -1. end for i = Start :( m . End = Start .7: Scilab code to Perform Average Filtering operation 6 c = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 6 \ cameraman .limit1 : limit2 mat ( I ) = a ( i +k . 32 if ( modulo (N . ’ n o i s y image ’ ) 46 title ( ’ n o i s y image ’ ) 47 figure 48 ShowImage (b . 2 3 ( a ) // p a g e 353 clc . 41 figure 42 ShowImage (c .2) ==1) 33 b (i . 40 b = uint8 ( b ) . j ) = ( mat (( N ^2) /2) + mat ((( N ^2) /2) +1) ) /2. 36 end 37 end 38 end 39 a = uint8 ( a ) . ’ O r i g i n a l Image ’ ) 43 title ( ’ O r i g i n a l Image ’ ) 44 figure 45 ShowImage (a . 59 .sci Scilab code Exa 6. ’ Median F i l t e r e d Image ’ ) 49 title ( ’ 5 x5 Median F i l t e r e d Image ’ ) check Appendix AP 3 for dependency: Func_medianall.30 end 31 mat = gsort ( mat ) . 34 else 35 b (i .23 Scilab code to Perform median filtering of colour image 1 2 3 4 5 // C a p t i o n : S c i l a b c o d e t o P e r f o r m median f i l t e r i n g o f c o l o u r image // F i g 6 . j ) = ( mat ((( N ^2) +1) /2) ) . close . 256]) . Out_Image (: . // A p p l y i n g Median f i l t e r to G plane Out_B = Func_medianall (B . [ m n ]= size ( b ) . Out_Image (: .3) . N ) . // A p p l y i n g Median f i l t e r to R plane Out_G = Func_medianall (G .1) .3) = Out_B .: .: . 7 8 9 10 11 12 13 14 15 16 17 18 19 // SIVP t o o l b o x N = input ( ’ e n t e r t h e window s i z e ’ ) .: .Figure 6. G = b (: .1) . // A p p l y i n g Median f i l t e r to B plane Out_Image (: . b = imnoise (b . b = imresize (a . Out_R = Func_medianall (R .: . ’ s a l t & p e p p e r ’ .2) = Out_G .2) . R = b (: .. B = b (: .: . N ) .8: Scilab code to Perform median filtering 6 a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 6 \ p e p p e r s .[256 . 60 .1) = Out_R . N ) .: . png ’ ) . 24 Scilab code to Perform Trimmed Average Filter 1 2 3 4 5 6 // C a p t i o n : S c i l a b c o d e t o P e r f o r m Trimmed A v e r a g e Filter // Alpha trimmed a v e r a g e f i l t e r // F i g 6 . 21 Out_Image = uint8 ( Out_Image ) . 22 // ShowColorImage ( b .9: Scilab code to Perform median filtering of colour image 20 b = uint8 ( b ) . close .Figure 6. 2 4 // p a g e 355 clc . 61 . ’ n o i s e added ’ ) 23 // t i t l e ( ’ n o i s e added ’ ) 24 figure 25 ShowColorImage ( Out_Image . ’ 3 x3 median 26 title ( ’ 3 x3 median f i l t e r e d ’ ) filtered ’) Scilab code Exa 6. j -1) .. for k = r + s :( N . // 3 x3 window a = double ( imnoise (c . sorted_mat = gsort ( mat ) .s ) . n ) . ’ n o i s y image ’ ) title ( ’ n o i s y image ’ ) figure ShowImage (b .a (i -1 . j p g ’ ) . j ) = Sum /( N -r .7 c = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 6 \ l e n n a . for i = 2: m -1 for j = 2: n -1 mat = [ a (i .a (i -1 .a ( i +1 .j -1) . n ] = size ( a ) . [m .a (i . end end a = uint8 ( a ) . b = uint8 ( b ) . j ) . Sum =0. j +1) . // SIVP t o o l b o x 8 s = 1. // f i g u r e // imshow ( c ) // t i t l e ( ’ O r i g i n a l Image ’ ) figure ShowImage (a . b = zeros (m . j +1) ].a ( i +1 . j +1) . j +1) . ’ g a u s s i a n ’ ) ) ..a (i . j ) . ’ Trimmed A v e r a g e F i l t e r e d Image ’ ) title ( ’ Trimmed A v e r a g e F i l t e r e d Image ’ ) 62 . a (i -1 .a (i -1 . // s d e n o t e s t h e number o f 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 v a l u e s t o be l e f t i n t h e end r = 1.s ) Sum = Sum + mat ( k ) . N = 9.. j ) . end b (i . Figure 6.10: Scilab code to Perform Trimmed Average Filter 63 . r = sqrt ( i . close .23 Scilab code for Differentiation of Gaussian function 1 2 3 4 5 6 7 8 9 10 11 12 13 // C a p t i o n : S c i l a b c o d e f o r D i f f e r e n t i a t i o n o f Gaussian f u n c t i o n // F i g 7 .* r ) / sigma ^2) -1) . 2 3 // p a g e 3 8 8 clc .* i + j .1:10.r . y ) legend ( sprintf ( ’ The s i g m a v a l u e i s %g ’ .* j ) . j = -10:. sigma = input ( ’ E n t e r t h e v a l u e o f s i g m a : ’ ) i = -10:.* exp ( . y =(1/( sigma ^2) ) *((( r . sigma ) ) xtitle ( ’ D i f f e r e n t i a t i o n o f G a u s s i a n f u n c t i o n ’ ) 64 .1:10.Chapter 7 Image Segmentation Scilab code Exa 7. plot (i .* r /2* sigma ^2) . 1: Scilab code for Differentiation of Gaussian function 65 .Figure 7. Figure 7.2: Scilab code for Differentiation of Gaussian function 66 . 25 Scilab code for Differentiation of Gaussian Filter function 1 2 3 4 5 6 7 8 9 // C a p t i o n : S c i l a b c o d e f o r D i f f e r e n t i a t i o n o f Gaussian F i l t e r f u n c t i o n // F i g 7 .1:10. j = -10:.3: Scilab code for Differentiation of Gaussian Filter function Scilab code Exa 7. 67 . close .1:10.Figure 7. 2 5 // p a g e 3 8 9 clc . sigma1 = input ( ’ E n t e r t h e v a l u e o f s i g m a 1 : ’ ) sigma2 = input ( ’ E n t e r t h e v a l u e o f s i g m a 2 : ’ ) i = -10:. y2 = (1/( sigma2 ^2) ) *((( r .Figure 7. plot (i . y = y1 .* exp ( .y2 .* r 12 13 14 15 16 17 18 19 /2* sigma1 ^2) . y ) xtitle ( ’ Shape o f DOG F i l t e r ’ ) // R e s u l t // E n t e r t h e v a l u e o f s i g m a 1 : 4 // E n t e r t h e v a l u e o f s i g m a 2 : 1 // 68 .* i + j .* exp ( .* r ) / sigma1 ^2) -1) .* r /2* sigma2 ^2) .r .4: Scilab code for Differentiation of Gaussian Filter function 10 r = sqrt ( i .* r ) / sigma2 ^2) -1) . 11 y1 = (1/( sigma1 ^2) ) *((( r .r .* j ) . d = edge (a . 2 7 // p a g e 3 8 9 close . ’ Canny ’ ) title ( ’ Canny ’ ) 69 . Log [ 4 ] . a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 7 \ s a i l i n g . ’ P r e w i t t ’ ) title ( ’ P r e w i t t ’ ) figure ShowImage (e . f = edge (a . Canny // F i g 7 . clc . P r e w i t t [ 3 ] . c = edge (a .Scilab code Exa 7.27 Scilab code for Edge Detection using Different Edge detectors 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // C a p t i o n : S c i l a b c o d e f o r Edge D e t e c t i o n u s i n g D i f f e r e n t Edge d e t e c t o r s // [ 1 ] . ’ canny ’ ) . ShowImage (a . e = edge (a . S o b e l [ 2 ] . a = rgb2gray ( a ) . ’ O r i g i n a l Image ’ ) title ( ’ O r i g i n a l Image ’ ) figure ShowImage (c . ’ S o b e l ’ ) title ( ’ S o b e l ’ ) figure ShowImage (d . j p g ’ ) . ’ p r e w i t t ’ ) . ’ Log ’ ) title ( ’ Log ’ ) figure ShowImage (f . ’ s o b e l ’ ) . ’ l o g ’ ) . Figure 7.5: Scilab code for Edge Detection using Different Edge detectors 70 . png ’ ) . close . global EDGE_SOBEL . a = rgb2gray ( b ) . ShowImage ( SegmentedImage . figure ShowColorImage (b . EDGE_SOBEL ) .30 Scilab code to perform watershed transform 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // C a p t i o n : S c i l a b c o d e t o p e r f o r m w a t e r s h e d transform // F i g 7 . jetcolormap ( ColorMapLength ) ) . Threshold2 ) . Threshold1 ) . // determine a threshold EdgeImage = ~ SegmentByThreshold ( Gradient . 3 0 // Page396 clc . png ’ ) figure ColorMapLength = length ( unique ( SegmentedImage ) ) . ’ R e s u l t o f Watershed T r a n s f o r m ’ . 71 . Gradient = EdgeFilter (a . DistanceImage = DistanceTransform ( EdgeImage ) . b = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 7 \ t e a s e t . Threshold1 = CalculateOtsuThreshold ( Gradient ) . Threshold2 = CalculateOtsuThreshold ( DistanceImage ) // d e t e r m i n e a t h r e s h o l d ThresholdImage = SegmentByThreshold ( DistanceImage . MarkerImage ) . SegmentedImage = Watershed ( Gradient . MarkerImage = SearchBlobs ( ThresholdImage ) . ’ t e a s e t ’ ) title ( ’ t e a s e t .Scilab code Exa 7. Figure 7.6: Scilab code to perform watershed transform 72 . disp ( evals . K = [37 .37]. clear all . -15.Chapter 8 Object Recognition Scilab code Exa 8. ’ E i g e n V a l u e s a r e = ’ ) if ( evals == abs ( evals ) ) then disp ( ’ Both t h e e i g e n v a l u e s a r e non− n e g a t i v e and the g i v e n matrix i s a c o v a r i a n c e matrix ’ ).4 To verify the given matrix is a covaraince matrix 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // C a p t i o n : To v e r i f y t h e g i v e n m a t r i x i s a covaraince matrix // Problem 4 // p a g e 4 3 8 close . clc . else disp ( ’ non− c o v a r i a n c e m a t r i x ’ ) end Scilab code Exa 8. evals = gsort ( evals ) . -15 . evals = spec ( K ) .5 To compute the covariance of the given 2D data 73 . 5 2. 5 0. X4 ]. X4 = [1 .5 0. X1 = [2 . A (: .2] ’.5 0. X2 .5 // − 0 . // mean = // 1. //M =rows . K = A ’* A .5 − 0. 1.5 0.1] ’. X = [ X1 . disp (m . 3.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // C a p t i o n : To compute t h e c o v a r i a n c e o f t h e g i v e n 2D data // Problem 5 // p a g e 4 3 9 close . N ] = size ( X ) .5 − 0.5 2. ’ mean = ’ ) .5 // 0. 2. K = K /( M -1) .3] ’. disp (K .5 − 0.5 − 0.5 // // The C o v a r a i n c e m a t i x i s K = // 0. ’X= ’ ) . disp (X . ’ The C o v a r a i n c e m a t i x i s K = ’ ) // R e s u l t //X= // 2.5 0. 5 0. 5 − 0 . 2. 3.5 74 . // 1. clear all .2] ’. 5 − 0 . X3 .5 1. N = c o l u m n s for i =1: N m ( i ) = mean ( X (: .5 // − 0 . X2 = [3 . 2. clc . i ) -m ( i ) . end m = m ’. [M . i ) = X (: . X3 = [2 . i ) ) . 1 . b = 0. if ( Yin ( i ) >=1) Y ( i ) =1. -1 . 75 . ’Y= ’ ) if ( Y ( i ) ~= T ( i ) ) b = b + alpha * T ( i ) . -1 . -1]. // B i a s i n g v e c t o r T = [1 . W2 = W2 + alpha * T ( i ) * X2 ( i ) . // b i a s i n i t i a l i z e d alpha = 1. elseif (( Yin ( i ) <1) &( Yin ( i ) >= -1) ) Y ( i ) =0. // l e a r n i n g r a t e for i = 1: length ( X1 ) Yin ( i ) = b + X1 ( i ) * W1 + X2 ( i ) * W2 . // W e i g h t s a r e i n i t i a l i z e d W2 = 0. // b = [ 1 . // T a r g e t v e c t o r f o r AND f u n c t i o n W1 = 0. X1 = [1 . elseif ( Yin ( i ) < -1) Y ( i ) = -1. W1 = W1 + alpha * T ( i ) * X1 ( i ) . clc . -1]. 1 . 1 . -1 .Scilab code Exa 8.9 Develop a perceptron AND function with bipolar inputs and targets 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // C a p t i o n : D e v e l o p a p e r c e p t r o n AND f u n c t i o n w i t h b i p o l a r i n p u t s and t a r g e t s // Problem 9 // p a g e 4 4 1 close . end disp ( Yin ( i ) . clear all .1 . //X1 and X2 a r e i n p u t v e c t o r s t o AND f u n c t i o n X2 = [1 . -1]. -1 . ’ Yin= ’ ) disp ( Y ( i ) . 1 ] . ’ B i a s Weigth b= ’ ) 37 disp ( W1 . ’ b= ’ ) 31 disp ( W1 .30 disp (b . ’W2= ’ ) are ’) 76 . ’W1= ’ ) 32 disp ( W2 . ’W2= ’ ) 33 end 34 end 35 disp ( ’ F i n a l W e i g h t s a f t e r one i t e r a t i o n 36 disp (b . ’W1= ’ ) 38 disp ( W2 . 72 . clear all . 9 // p a g e 5 1 2 close . m = mean ( mean ( y ) ) .9 Program performs Block Truncation Coding BTC 1 2 3 4 5 6 7 // C a p t i o n : Program p e r f o r m s B l o c k T r u n c a t i o n Coding ( BTC) // Example 9 .80].Chapter 9 Image Compression Scilab code Exa 9. disp ( sig .70. ’ S t a n d a r d d e v i a t i o n o f t h e b l o c k i s =’) 77 8 9 10 11 12 13 14 15 16 17 .82 .66 .84 .68 .75 . disp (x . disp (m . blk = input ( ’ E n t e r t h e b l o c k s i z e : ’ ) .68. for i = 1 : blk : m1 for j = 1 : blk : n1 y = x ( i : i +( blk -1) . ’ mean v a l u e i s m = ’ ) sig = std2 ( y ) .65. ’ O r i g i n a l B l o c k i s x = ’ ) [ m1 n1 ]= size ( x ) .75 . x = [65 .j : j +( blk -1) ) .62 . clc .72 .72 .80 . 68. end 78 . 80. j : j +( blk -1) ) = b * mu +(1 . 39 // 40 // E n t e r t h e b l o c k s i z e : 4 41 // mean v a l u e i s m = 7 2 .b ) * ml . disp (K . ’ The v a l u e o f a = ’ ) mu = m + sig * sqrt ((( blk ^2) -K ) / K ) . 72. 68. ’ R e c o n s t r u c t e d B l o c k i s x = ’ ) 32 // R e s u l t 33 // O r i g i n a l B l o c k i s x = 34 // 35 // 65. disp ( mu . 75. ’ The v a l u e o f b = ’ ) x ( i : i +( blk -1) . 37 // 84. 82. 75. // t h e b i n a r y b l o c k disp (b .18 19 20 21 22 23 24 25 26 27 28 29 end 30 end 31 disp ( round ( x ) . 38 // 66. disp ( ml . ’ B i n a r y a l l o c a t i o n m a t r i x i s B= ’ ) K = sum ( sum ( b ) ) . 6 2 8 2 2 2 5 43 // B i n a r y a l l o c a t i o n m a t 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 o n e s = 6 51 // The v a l u e o f a = 6 7 . 1 1 5 8 0 1 52 // The v a l u e o f b = 8 0 . 36 // 72. ’ number o f o n e s = ’ ) if ( K ~= blk ^2 ) & ( K ~= 0) ml = m . 62. 80. 70. 65. 8 0 6 9 9 8 53 // R e c o n s t r u c t e d B l o c k i s x = 54 // b = y > m . 72.sig * sqrt ( K /(( blk ^2) -K ) ) . 2 5 42 // S t a n d a r d d e v i a t i o n o f t h e b l o c k i s = 6 . 5 9 : MATLAB Example1 // p a g e 5 1 4 close . x1 = x . x = imread ( ’E : \ D i g i t a l I m a g e P r o c e s s i n g J a y a r a m a n \ C h a p t e r 9 \ l e n n a . x ( i : i +( blk -1) . 81. // SIVP t o o l b o x // x=i m r e s i z e ( x . b = y > m . 67. 81. 67. 67. 81. mu = m + sig * sqrt ((( blk ^2) -K ) / K ) . 81.b ) * ml . 81. 67. for i = 1 : blk : m1 for j = 1 : blk : n1 y = x ( i : i +( blk -1) .59 Program performs Block Truncation Coding 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // C a p t i o n : Program p e r f o r m s B l o c k T r u n c a t i o n Coding ( BTC) by c h o o s i n g d i f f e r e n t // b l o c k s i z e s // F i g . 67.j : j +( blk -1) ) . sig = std2 ( y ) . blk = input ( ’ E n t e r t h e b l o c k s i z e : ’ ) . 67. 67. 9 . j p g ’ ) . m = mean ( mean ( y ) ) . Scilab code Exa 9. end end 79 . x = double ( x ) . [ m1 n1 ]= size ( x ) . clc . j : j +( blk -1) ) = b * mu +(1 . if ( K ~= blk ^2 ) & ( K ~= 0) ml = m . [ 2 5 6 2 5 6 ] ) .55 56 57 58 // // // // 67. // t h e b i n a r y b l o c k K = sum ( sum ( b ) ) . 81. 67. 67.sig * sqrt ( K /(( blk ^2) -K ) ) . 1: Program performs Block Truncation Coding 26 end 27 // imshow ( u i n t 8 ( x ) ) 28 // t i t l e ( ’ R e c o n s t r u c t e d Image ’ ) 29 x = uint8 ( x ) . // IPD t o o l b o x 35 title ( ’ B l o c k S i z e = 8 ’ ) 80 . ’ R e c o n s t r u c t e d Image ’ ) . 30 figure (1) 31 imshow ( x1 ) 32 title ( ’ O r i g i n a l Image ’ ) .Figure 9. // IPD t o o l b o x 33 figure (2) 34 ShowImage (x . 3) . clc . clear all .Chapter 10 Binary Image Processing Scilab code Exa 10. StructureElement ) . 1 . bmp ’ ) . 1 0 . 1 . 1 . a1 = DilateImage (a . StructureElement ) .17 Scilab Code for dilation and erosion process 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // C a p t i o n : 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 process // F i g . // D i s p l a y i n g o r i g i n a l Image // imshow ( a ) figure (1) ShowImage (a . ’ O r i g i n a l Image ’ ) . 1 . 1 . // D i s p l a y i n g D i l a t e d Image // imshow ( a1 ) figure (2) 81 . 1 ] . // SIVP t o o l b o x // b = [ 1 . 1 . 1 7 // Page553 close . 1 . a2 = ErodeImage (a . a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 0 \ morph1 . StructureElement = CreateStructureElement ( ’ s q u a r e ’ . clear all . 1 9 // Page555 close . clc . a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 0 \ morph2 . 1 0 . xtitle ( ’ Eroded Image ’ ) Scilab code Exa 10. ’ Eroded Image ’ ) . bmp ’ ) . xtitle ( ’ D i l a t e d Image ’ ) // D i s p l a y i n g Eroded Image // imshow ( a2 ) figure (3) ShowImage ( a2 . ’ D i l a t e d Image ’ ) .Figure 10.1: Scilab Code for dilation and erosion process 19 20 21 22 23 24 25 ShowImage ( a1 .19 Scilab Code to perform an opening and closing operation on the image 1 2 3 4 5 6 7 // C a p t i o n : S c i l a b Code t o p e r f o r m an o p e n i n g and c l o s i n g o p e r a t i o n on t h e image // F i g . 82 . StructureElement = CreateStructureElement ( ’ s q u a r e ’ . 1 . 1 ] . // D i s p l a y i n g Opened Image figure (2) ShowImage ( b2 . 1 . // C l o s i n g i s done by f i r s t a p p l y i n g d i l a t i o n and t h e n e r o s i o n o p e r a t i o n on image a1 = DilateImage (a . 1 . ’ Opened Image ’ ) . 1 . 1 . a2 = ErodeImage ( a1 . 3) . 1 . ’ O r i g i n a l Image ’ ) . StructureElement ) . // Opening i s done by f i r s t a p p l y i n g e r o s i o n and t h e n d i l a t i o n o p e r a t i o n s on image b1 = ErodeImage (a . ’ C l o s e d Image ’ ) . StructureElement ) . xtitle ( ’ C l o s e d Image ’ ) 83 . xtitle ( ’ Opened Image ’ ) // D i s p l a y i n g C l o s e d Image figure (3) ShowImage ( a2 . 1 . StructureElement ) . // D i s p l a y i n g o r i g i n a l Image figure (1) ShowImage (a . b2 = DilateImage ( b1 .8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // SIVP t o o l b o x // b = [ 1 . StructureElement ) . Figure 10.2: Scilab Code to perform an opening and closing operation on the image 84 . : . B (: .: .: . g r e e n // and b l u e // F i g . ’ O r i g i n a l C o l o r Image ’ ) . R (: . G (: .Chapter 11 Colur Image Processing Scilab code Exa 11. // SIVP t o o l b o x R = RGB .: .2) =0. RGB = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 1 \ p e p p e r s .2) =0. B (: .1) =0. B = RGB . png ’ ) . figure (1) ShowColorImage ( RGB .4 Read an RGB image and extract the three colour components red green blue 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // C a p t i o n : Read an RGB image and e x t r a c t t h e t h r e e c o l o u r c o m p o n e n ts : red . 4 : MATLAB Example1 // p a g e 5 8 8 clc .: . R (: .1) =0. 1 1 .3) =0. close .3) =0. G = RGB . // IPD 85 . G (: .: . b1 (: .1) =0. RGB = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 1 \ p e p p e r s .: . ’ O r i g i n a l C o l o r Image ’ ) . b1 = RGB . figure (3) 86 . c1 (: .: . // IPD toolbox figure (2) ShowColorImage ( a1 . 1 2 : MATLAB Example2 // p a g e 5 9 2 clc . g r e e n // and b l u e p l a n e s // F i g . Scilab code Exa 11.19 20 21 22 23 24 25 toolbox title ( ’ O r i g i n a l C o l o r Image ’ ) . figure (4) ShowColorImage (B .3) =0. // SIVP t o o l b o x a1 = RGB .12 Read a Colour image and separate the colour image into red green and blue planes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // C a p t i o n : Read a C o l o u r image and s e p a r a t e t h e c o l o u r image i n t o : red .: . a1 (: .2) =0. figure (2) ShowColorImage (R . figure (3) ShowColorImage (G . png ’ ) . close . 1 1 . ’ Red M i s s i n g ’ ) . ’ Green Component ’ ) . figure (1) ShowColorImage ( RGB . c1 = RGB . ’ Red Component ’ ) . ’ B l u e Component ’ ) . 1: Read an RGB image and extract the three colour components red green blue 87 .Figure 11. figure (4) ShowColorImage ( c1 . v = hsv (: .9167 | h <= . ’ B l u e M i s s i n g ’ ) . // F i n d s l o c a t i o n o f b l a c k and w h i t e p i x e l s darks = find (v <0.16 Compute the histogram of the colour image 1 // C a p t i o n : Compute t h e h i s t o g r a m o f t h e c o l o u r image 2 // F i g . 1 6 : MATLAB Example3 3 // p a g e 5 9 5 4 clc . white_pixels = length ( lights ) / pixels .05 & v >0. // IPD t o o l b o x pixels = prod ( size ( index ) ) . 88 .083 & h <= . h ([ darks lights ]) = -1. h = hsv (: . ’ O r i g i n a l C o l o r Image ’ ) . Scilab code Exa 11.25) ) / pixels .083) & h ~= -1) ) / pixels .2) . 1 1 . lights = find (s <0. [ index . hsv = rgb2hsv ( J ) . // Gets t h e number o f a l l p i x e l s f o r e a c h c o l o u r b i n black_pixels = length ( darks ) / pixels . 6 I = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 1 \ l a v e n d e r . // SIVP t o o l b o x figure (1) ShowColorImage (I . 5 close .2) .3) . // IPD toolbox J = im2double ( I ) . red = length ( find (( h > . ’ Green M i s s i n g ’ ) .1) . map ] = RGB2Ind ( I ) . yellow = length ( find ( h > .19 20 21 ShowColorImage ( b1 . s = hsv (: . j p g ’ 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ) .85) . 2: Read a Colour image and separate the colour image into red green and blue planes 89 .Figure 11. magenta = length ( find ( h > .75) ) / pixels . length ( n ) ) . blue = length ( find ( h > .4167) ) / pixels .2) n5 = 5:0. length ( n ) ) . blue * ones (1 .0) n7 = 7:0. cyan = length ( find ( h > . // P l o t s h i s t o g r a m figure (2) a = gca () . plot2d2 ( n5 .7) n2 = 2:0. plot2d2 ( n3 .1:8 plot2d2 ( n7 .8) n3 = 3:0.3) n6 = 6:0.25 & h <= . plot2d2 ( n2 . white_pixels * ones (1 . plot2d2 ( n1 .1] n = 0:0. length ( n ) ) . yellow * ones (1 . close .1:2.9167) ) / pixels . magenta * ones (1 .1:6.5) Scilab code Exa 11. length ( n ) ) .18 Perform histogram equalisation of the given RGB image 1 2 3 4 5 6 // C a p t i o n : P e r f o r m h i s t o g r a m e q u a l i s a t i o n o f t h e g i v e n RGB image // F i g . red * ones (1 . a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 1 \ p e p p e r s .5833 & h <= .1:4.8 .5833) ) / pixels .1:1.25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 green = length ( find ( h > . 1 8 : MATLAB Example4 // p a g e 5 9 6 clc .9) n4 = 4:0.0. black_pixels * ones (1 .1:7. a .1:3. cyan * ones (1 . plot2d2 (n . green * ones (1 .5) n1 = 1:0. plot2d2 ( n4 . plot2d2 ( n6 . length ( n ) ) .75 & h <= . 1 1 . length ( n ) ) .1:5.4167 & h <= . length ( n ) ) . length ( n ) ) . data_bounds =[0 . png ’ ) 90 . : .1) . c (: . 1 1 .: . 0.3) = MedianFilter ( b (: .: . // SIVP t o o l b o x b = imnoise (a . // IPD toolbox figure (3) ShowColorImage (c . ’ O r i g i n a l Image ’ ) . // H i s t o g r a m e q u a l i s a t i o n o f Y component a l o n e b (: . a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 1 \ p e p p e r s .2) . [3 3]) .: . // SIVP t o o l b o x // c o n v e r s i o n o f RGB t o YIQ f o r m a t b = rgb2ntsc ( a ) . ’ H i s t o g t r a m e q u a l i z e d Image ’ ) . c (: . [3 3]) . ’ c o r r u p t e d Image ’ ) .2) .7 8 9 10 11 12 13 14 15 16 . figure (1) ShowColorImage (a .1) = // c o n v e r s i o n o f YIQ t o RGB f o r m a t c = ntsc2rgb ( b ) . close . ’ O r i g i n a l Image ’ ) .3) . 2 1 : MATLAB Example5 // p a g e 5 9 8 clc . ’ s a l t & p e p p e r ’ . c (: .: .: .: . [3 3]) .2) = MedianFilter ( b (: . figure (1) ShowColorImage (a . png ’ ) . // IPD t o o l b o x Scilab code Exa 11. ’ Median F i l t e r e d Image ’ ) .1) = MedianFilter ( b (: .21 This program performs median filtering of the colour image 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // C a p t i o n : T h i s program p e r f o r m s median f i l t e r i n g o f t h e c o l o u r image // F i g . // IPD t o o l b o x figure (2) ShowColorImage (b . // IPD 91 . // IPD t o o l b o x figure (2) ShowColorImage (c . Figure 11.24 Fitlering only the luminance component 1 // C a p t i o n : F i t l e r i n g o n l y t h e l u m i n a n c e component 92 .3: This program performs median filtering of the colour image toolbox Scilab code Exa 11. -1. 5 close . ’ O r i g i n a l Image ’ ) . for i =1: m for j =1: n D (i . figure (1) ShowColorImage (a . // c o n v e r t YIQ t o RGB f o r m a t a1 = ntsc2rgb ( yiq ) .1) .1) = D . n ]= size ( b ) . 2 4 : MATLAB Example6 3 // p a g e 5 9 9 4 clc . // P e r f o r m h i g h p a s s f i l t e r i n g o n l y on Y component c1 = conv2d2 (b . -1. // SIVP t o o l b o x // c o n v e r s i o n o f RGB t o YIQ f o r m a t yiq = rgb2ntsc ( a ) . -1 . -1 . // E x t r a c t t h e Y component a l o n e b = yiq (: . j ) = c1 (i .28 Perform gamma correction for the given colour image 1 // C a p t i o n : P e r f o r m gamma c o r r e c t i o n f o r t h e g i v e n c o l o u r image 93 . end end yiq (: .8 . [m . png ’ ) 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 . -1 . -1].2 // F i g . j ) . ’ High P a s s f i l t e r e d Image ’ ) .: .: . -1 . 1 1 . // IPD t o o l b o x figure (2) ShowColorImage ( a1 . 6 a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 1 \ p e p p e r s . h = [ -1 . // IPD t o o l b o x Scilab code Exa 11. h ) . j .*(([0: max_intensity ]. k ) ) . end 94 . J = zeros ( I ) . 6 clc . 2 8 : MATLAB Example7 3 // p a g e 6 0 3 4 close . // f o r u i n t 8 image 10 // Look up t a b l e c r e a t i o n 11 LUT = max_intensity . // Mapping o f i n p u t p i x e l s i n t o l o o k u p t a b l e v a l u e s K = double ( I ) +1. p ]= size ( K ) . [m .4: Fitlering only the luminance component 2 // F i g .j . 5 clear all . LUT = floor ( LUT ) . for i = 1: m for j =1: n for k = 1: p J (i .5./ 12 13 14 15 16 17 18 19 20 21 max_intensity ) .n . 7 I = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 1 \ a r a r a u n a .^ gamma_Value ) . 9 max_intensity = 255.Figure 11. png ’ ). 1 1 . k ) = LUT ( K (i . // SIVP t o o l b o x 8 gamma_Value = 0. 3 0 3 // p a g e 6 0 4 4 close . // SIVP t o o l b o x 8 [m . 7 K = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 1 \ l e n n a .5: Perform gamma correction for the given colour image 22 end 23 end 24 figure (1) 25 ShowColorImage (I .30 Perform Pseudo Colouring Operation 1 // C a p t i o n : P e r f o r m Pseudo − C o l o u r i n g O p e r a t i o n 2 // F i g .Figure 11. j p g ’ ) . 1 1 . 6 clc . ’ O r i g i n a l Image ’ ) . 95 . 9 I = uint8 ( K ) . // IPD t o o l b o x Scilab code Exa 11. ’Gamma C o r r e c t e d Image ’ ) . n ]= size ( K ) . 5 clear all . // IPD t o o l b o x 26 figure (2) 27 ShowColorImage ( uint8 ( J ) . 3) = I (i . j ) +10.1) = I (i . j ) +100. j ) >=150 & I (i .2) = I (i .32 Read an RGB image and segment it using the threshold method 96 .3) = I (i . j ) >=50 & I (i . 14 J (i . j ) +152. 32 end 33 end 34 end 35 figure (1) 36 ShowImage (K .1) = I (i .2) = I (i . ’ O r i g i n a l Image ’ ) . j ) +128. j ) <50) 13 J (i .2) = I (i .1) = I (i . 30 J (i .3) = I (i . j ) >=100 & I (i . 28 elseif ( I (i . j ) >=200 & I (i .1) = I (i .10 for i = 1: m 11 for j =1: n 12 if ( I (i . // IPD t o o l b o x 37 figure (2) 38 ShowColorImage (J . j ) +50. 16 elseif ( I (i . j ) +120. j ) <100) 17 J (i . j ) <200) 25 J (i .2) = I (i . j ) +15. j ) <150) 21 J (i . 19 J (i .j . j ) +50.j . 31 J (i . 27 J (i . j ) +140.j .j . // IPD toolbox Scilab code Exa 11. 18 J (i .j . ’ Pseudo C o l o u r e d Image ’ ) .j . j ) +10. 26 J (i . j ) +130. 24 elseif ( I (i . j ) +35. j ) +160. 15 J (i .j . j ) +25.j .1) = I (i . 20 elseif ( I (i .j . j ) <=256) 29 J (i .3) = I (i .j .j . j ) +45. 23 J (i .j .j . 22 J (i .3) = I (i . j ) >=0 & I (i .j .2) = I (i .j . // IPD t o o l b o x figure (2) ShowImage ( mask . ’ O r i g i n a l Image ’ ) . figure (1) ShowColorImage (I .Figure 11.6: Perform Pseudo Colouring Operation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // C a p t i o n : Read an RGB image and s e g m e n t i t u s i n g t h e t h r e s h o l d method // F i g 1 1 . // T h r e s h o l d i s a p p l i e d o n l y t o Cb component mask = b (: .n .2) >120. 3 2 // Page605 close . clc . png ’ ) . p ]= size ( b ) . b = uint8 ( b ) . // SIVP t o o l b o x [m . // SIVP t o o l b o x // C o n v e r s i o n o f RGB t o YCbCr b = rgb2ycbcr_1 ( I ) . I = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 1 \ a r a r a u n a . // IPD t o o l b o x 97 .: . ’ Segmented Image ’ ) . 7: Read an RGB image and segment it using the threshold method 98 .Figure 11. close . CD ]= dwt2 (x .9 Scilab code to perform wavelet decomposition 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // C a p t i o n : S c i l a b c o d e t o p e r f o r m w a v e l e t decomposition // F i g 1 2 . CA = im2int8 ( CA ) . ’ db1 ’ ) . CD1 ]= dwt2 ( CA . CV1 . j p g ’ ). 99 . CH1 . x = ReadImage ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 2 \ l e n n a . // S e c o n d l e v e l d e c o m p o s i t i o n [ CA1 . CH = im2int8 ( CH ) . // The image i n u n s i g n e d i n t e g e r o r d o u b l e h a s t o be converted into normalized // d o u b l e f o r m a t x = im2double ( x ) .Chapter 12 Wavelet based Image Processing Scilab code Exa 12. CV . 1 0 // Page624 clc . ’ db1 ’ ) . CV = im2int8 ( CV ) . CH . // F i r s t L e v e l d e c o m p o s i t i o n [ CA . CH1 = im2int8 ( CH1 ) . 100 . CD1 = im2int8 ( CD1 ) . // R o u t i n e t o g e n e r a t e G a u s s i a n k e r n e l k = zeros ( kernelsize .17 18 19 20 21 22 23 24 25 CD = im2int8 ( CD ) . bmp ’ ) . CV1 . CV1 = im2int8 ( CV1 ) . a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 2 \ a p p l e 3 .42 Scilab code to generate different levels of a Gaussian pyramid 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // C a p t i o n : S c i l a b c o d e t o g e n e r a t e d i f f e r e n t l e v e l s o f a G a u s s i a n pyramid // F i g 1 2 . CA1 = im2int8 ( CA1 ) . CD ]. for i = 1: kernelsize for j =1: kernelsize k (i . CV .kernelsize /2) . A = [ CA . sd = input ( ’ E n t e r t h e s t a n d a r d d e v i a t i o n o f h t e G a u s s i a n window : ’ ) . B = [ CA1 . t = 0. b = a.^2) . kernelsize = input ( ’ E n t e r t h e s i z e o f t h e k e r n e l : ’ ) . rf = input ( ’ E n t e r t h e R e d u c t i o n F a c t o r : ’ ) . kernelsize ) . [ m n ] = size ( b ) .^2) ) /(2* %pi * sd . CH1 . a = rgb2gray ( a ) . CH . close . imshow ( B ) title ( ’ R e s u l t o f S e c o n d L e v e l D e c o m p o s i t i o n ’ ) Scilab code Exa 12.^2) /(2* sd . 4 2 // Page651 clc . j ) = exp ( -(( i .^2+( j kernelsize /2) . CD1 ]. j ) = FilteredImg ( i *2 . for i = 1: m for j = 1: n b (i . figure ShowImage (a . end end for t = 1:1: rf // c o n v o l v e i t w i t h t h e p i c t u r e FilteredImg = b . end . j ) . b ) /255. b ) . j *2) . j ) = k (i . ’ O r i g i n a l Image ’ ) figure ShowImage (b . else FilteredImg = filter2 (k . j ) / t . n = n /2. end . end . ’ D i f f e r e n t L e v e l s o f G a u s a i n Pyramid ’ ) title ( ’ D i f f e r e n t L e v e l s o f G a u s a i n Pyramid L e v e l 2 ’ ) 101 . n ) . end .19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 t = t + k (i . // c r e a t e t h e r e d u c e d image t h r o u g h s a m p l i n g b = zeros (m . end end for i = 1: kernelsize for j = 1: kernelsize k (i . // compute t h e s i z e o f t h e r e d u c e d image m = m /2. if t ==1 FilteredImg = filter2 (k . 1: Scilab code to generate different levels of a Gaussian pyramid 102 .Figure 12. Scilab code Exa 12.57 Scilab code to implement watermarking in spatial domain 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // C a p t i o n : S c i l a b c o d e t o i m p l e m e n t w a t e r m a r k i n g i n s p a t i a l domain // F i g 1 2 . 5 7 // Page662 clc close a = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 2 \ cameraman . j p g ’ ); figure imshow ( a ) title ( ’ Base Image ’ ) ; b = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 2 \ k e y i m a g e . j p g ’ ); b = rgb2gray ( b ) ; b = imresize (b ,[32 32] , ’ b i c u b i c ’ ) ; [ m1 n1 ]= size ( b ) ; figure imshow ( b ) title ( ’ Mark Image ’ ) ; [ m n ]= size ( a ) ; i1 = 1; j1 = 1; p = 1; c = a; iii = 1; jjj = 1; a = uint8 ( a ) ; b = uint8 ( b ) ; for ff = 1:8 for i = 1:32 jjj = 1; for j = j1 : j1 + n1 -1 a (i , j ) = bitand ( a (i , j ) , uint8 (254) ) ; // LSB o f b a s e image i s s e t t o z e r o . 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 h e mark i s e x t r a c t e d . temp = temp /((2^ ff ) -1) ; c (i , j ) = bitor ( a (i , j ) , uint8 ( temp ) ) ; //MSB o f mark i s i n e r t e d i n t o t h e %LSB o f the base jjj = jjj +1; 32 33 34 35 end 36 end 37 j1 = j1 +32; 38 end 39 imshow ( c ) 40 title ( ’ Marked Image ’ ) ; 41 imwrite (c , ’E : \ DIP JAYARAMAN\ C h a p t e r 1 2 \ markimg . j p g ’ ) ; Scilab code Exa 12.63 Scilab code to implement wavelet based watermarking 1 // C a p t i o n : S c i l a b c o d e t o i m p l e m e n t w a v e l e t − b a s e d watermarking 104 2 // F i g 1 2 . 6 3 3 // Page666 4 clc ; 5 close ; 6 // O r i g i n a l Image 7 img = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 2 \ cameraman . jpg ’ ); 8 figure 9 imshow ( img ) 10 title ( ’ O r i g i n a l Image ’ ) ; 11 [ p q ] = size ( img ) ; 12 // G e n e r a t e t h e key 13 // key = i m r e a d ( ’ E : \ DIP JAYARAMAN\ C h a p t e r 1 2 \ keyimg1 . png ’ ) ; 14 // key = i m r e s i z e ( key , [ p q ] ) ; 15 key = imread ( ’E : \ DIP JAYARAMAN\ C h a p t e r 1 2 \ k e y i m a g e . 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 jpg ’ ); key = rgb2gray ( key ) ; c = 0.001; // I n i t i a l i s e t h e w e i g h t o f Watermarking figure imshow ( key ) title ( ’ Key ’ ) ; // Wavelet t r a n s f o r m o f o r i g i n a l image ( b a s e image ) img = double ( img ) ; key = double ( key ) ; [ ca , ch , cv , cd ] = dwt2 ( img , ’ db1 ’ ) ; // Compute 2D w a v e l e t transform // P e r f o r m t h e w a t e r m a r k i n g y = [ ca ch ; cv cd ]; Y = y + c * key ; p = p /2; q = q /2; for i =1: p for j =1: q nca (i , j ) = Y (i , j ) ; ncv (i , j ) = Y ( i +p , j ) ; nch (i , j ) = Y (i , j + q ) ; ncd (i , j ) = Y ( i +p , j + q ) ; 105 figure imshow ( N1 ) title ( ’ E x t r a c t t h e key from w a t e r m a r k e d image ’ ) 106 . nch . rcd ] = dwt2 ( wimg . rcv .36 end 37 end 38 // D i s p l a y t h e Watermarked image 39 wimg = idwt2 ( nca . ’ db1 ’ ) . N1 = N1 *4. N1 = im2int8 ( N1 ) .y . ’ db1 ’ ) . // Compute 2D 46 47 48 49 50 51 52 wavelet transform n1 =[ rca . ncv . rcv . 40 wimg1 = uint8 ( wimg ) . N1 = n1 . rcd ]. ncd . 41 figure 42 imshow ( wimg1 ) 43 title ( ’ Watermarked Image ’ ) 44 // E x t r a c t i o n o f key from Watermarked image 45 [ rca . rch . rch . i ) end // f o u r i e r t r a n s f o r m a l o n g t h e c o l u m n s for j =1: m a2temp = exp ( -2* %i * %pi *(0: n -1) ’.:) ) . j ) ) ) <0.*.0001) a2 (i .0001) ) a2 (i . j ) = 0+ %i * imag ( a2 (i . j ) ) ) <0. elseif ( abs ( real ( a2 (i .0001) a2 (i .(0: m -1) / m ) * a (: . j ) ) +0.:) = a2temp .2) // f o u r i e r t r a n s f o r m a l o n g t h e r o w s for i =1: n a1 (: . j ) ) ) <0. ’ end for i = 1: m for j = 1: n if (( abs ( real ( a2 (i .(0: n -1) / n ) *( a1 (j . j ) = real ( a2 (i .*.’ a2 (j . i ) = exp ( -2* %i * %pi *(0: m -1) ’. elseif ( abs ( imag ( a2 (i . j ) ) ) <0.1) n = size (a . j ) ) . j ) =0. end end 107 .Appendix Scilab code AP 1 2D Fast Fourier Transform 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 function [ a2 ] = fft2d ( a ) // a = any r e a l o r c o m p l e x 2D m a t r i x // a2 = 2D−DFT o f 2D m a t r i x ’a ’ m = size (a .0001) &( abs ( imag ( a2 ( i . 1) n = size ( a2 . if ( modulo (N . Out_Imag = a .*. end if ( modulo (N . i ) = exp (2* %i * %pi *(0: m -1) ’. i ) end // I n v e r s e f o u r i e r t r a n s f o r m a l o n g t h e c o l u m n s for j =1: m atemp = exp (2* %i * %pi *(0: n -1) ’.*.:) = atemp . [ m n ]= size ( a ) .2) ==1) 108 .(0: m -1) / m ) * a2 (: . ’ a (j . End = Start +1.2) // I n v e r s e F o u r i e r t r a n s f o r m a l o n g t h e r o w s for i =1: n a1 (: .25 end Scilab code AP 2 2D Inverse FFT 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function [ a ] = ifft2d ( a2 ) // a2 = 2D−DFT o f any r e a l o r c o m p l e x 2D m a t r i x // a = 2D−IDFT o f a2 m = size ( a2 . ’ end a = a /( m * n ) a = real ( a ) endfunction Scilab code AP 3 Median Filtering function 1 2 3 4 5 6 7 8 9 10 11 12 13 // The i n p u t t o t h e f u n c t i o n a r e t h e c o r r u p t e d image a and t h e d i m e n s i o n function [ Out_Imag ] = Func_medianall (a .(0: n -1) / n ) *( a1 (j .2) ==1) Start =( N +1) /2.:) ) . else Start = N /2. N ) a = double ( a ) . End = Start . End +1) . 24 for l = . 26 I = I +1.limit1 : limit2 . // S o r t t h e e l e m e n t s t o 30 31 32 33 34 35 end 36 end f i n d t h e median if ( modulo (N . j ) =( mat (( N ^2) /2) + mat ((( N ^2) /2) +1) ) /2. j + l ) .limit1 : limit2 . end Scilab code AP 4 To caculate gray level 1 function [ g ] = gray ( m ) 2 g = (0: m -1) ’/ max (m -1 . 25 mat ( I ) = a ( i +k . 27 end 28 end 29 mat = gsort ( mat ) .2) ==1) Out_Imag (i . 22 I =1. else Out_Imag (i .1) 3 g = [g g g] 4 endfunction Scilab code AP 5 To change the gray level of gray image 1 2 function [ bout ] = grayslice (I . 23 for k = . 15 limit2 = limit1 . 21 for j = Start :( n .End +1) . 18 limit2 = limit1 +1. 16 else 17 limit1 =( N /2) -1. z ) 109 .14 limit1 =( N -1) /2. 19 end 20 for i = Start :( m . j ) =( mat ((( N ^2) +1) /2) ) . b = zeros (m . else // I i s d o u b l e o r s i n g l e z = (0:( n -1) ) / n end . if typeof ( I ) == ” u i n t 8 ” then z = (255*(0: n -1) ) / n . else j = i. n ] = size ( I ) . b(d) = j. %nargin ] = argn (0) if %nargin ==1 then z = 10. if ~ isempty ( d ) . end end // Take c a r e o f t h a t l a s t i n t e r v a l 110 . n ) . // Loop o v e r a l l i n t e r v a l s .// Output v a r i a b l e s input v a r i a b l e s ) 4 bout =[]. [m . n = z. elseif isa (I . ’ u i n t 1 6 ’ ) | isa (I . end d = find (I >= z ( i ) & I < z ( i +1) ) . e x c e p t t h e l a s t for i = 1: length ( z ) -1 // j i s t h e i n d e x v a l u e we w i l l o u t p u t . ’ i n t 1 6 ’ ) then z = 65535*(0:( n -1) ) / n . s o i t depend upon s t o r a g e c l a s s if typeof ( b ) == ’ u i n t 8 ’ j = i -1. elseif ~ type ( z ) ==1 then z = double ( z ) . end . 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 i n i t i a l i s a t i o n ( not found in // Number o f a r g u m e n t s i n f u n c t i o n c a l l [ %nargout . s o it depend upon s t o r a g e c l a s s 42 if typeof ( b ) == ” u i n t 8 ” then 43 j = length ( z ) -1. 44 else 45 j = length ( z ) . 50 bout = double ( bout ) .39 d = find ( I >= z ( $ ) ) . 46 end . 49 bout = b . 40 if ~ isempty ( d ) then 41 // j i s t h e i n d e x v a l u e we w i l l o u t p u t . 48 end . 51 endfunction 111 . 47 b(d) = j.
Report "Digital Image Processing_S. Jayaraman, S. Esakkirajan and T. Veerakumar"