0% found this document useful (0 votes)
50 views

Digital Image Processing - DR - B.chandra Mohan

The document describes experiments on digital image processing using Scilab. Experiment 2 discusses kernel processing on images to perform image enhancement. It implements mean and median filtering on a noisy input image using 3x3 kernels. The original noisy image and the enhanced images obtained after applying mean and median filtering are displayed.

Uploaded by

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

Digital Image Processing - DR - B.chandra Mohan

The document describes experiments on digital image processing using Scilab. Experiment 2 discusses kernel processing on images to perform image enhancement. It implements mean and median filtering on a noisy input image using 3x3 kernels. The original noisy image and the enhanced images obtained after applying mean and median filtering are displayed.

Uploaded by

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

Scilab Manual for

Digital Image Processing


by Dr B.Chandra Mohan
Electronics Engineering
Bapatla Engineering College1

Solutions provided by
Mr R.Senthilkumar- Assistant Professor
Electronics Engineering
Institute of Road and Transport Technology

March 25, 2022

1 Funded by a grant from the National Mission on Education through ICT,


http://spoken-tutorial.org/NMEICT-Intro. This Scilab Manual and Scilab codes
written in it can be downloaded from the ”Migrated Labs” section at the website
http://scilab.in
1
Contents

List of Scilab Solutions 4

1 Histogram display and histogram equalization 6

2 Kernel processing on images leading to image enhancement 10

3 Display of 2D filters frequency responses and processing the


images using these filters 14

4 Implementation of Airthmetic Coding for images 18

5 Basic JPEG algorithm implementation 20

6 DPCM encoding and decoding of images 25

7 Simple image watermarking algorithms using LSB substi-


tution 30

8 Simple content based image retrieval using various distance


metrics 36

9 Image segmentation algorithms using Snakes 41

10 Color images manipulations, reading and writing of color


images 44

11 Color image enhancements 47

12 Color image histogram manipulation 51

2
13 LOG Masks implementation for gray and color images 55

14 Special effects implementation on grey and color images 57

15 Simple video reading and writing .avi formats and manipu-


lation of video frames 62

3
List of Experiments

Solution 1.1 Exp1 . . . . . . . . . . . . . . . . . . . . . . . . . 6


Solution 2.1 Exp2 . . . . . . . . . . . . . . . . . . . . . . . . . 10
Solution 3.1 Exp3 . . . . . . . . . . . . . . . . . . . . . . . . . 14
Solution 4.1 Exp4 . . . . . . . . . . . . . . . . . . . . . . . . . 18
Solution 5.1 Exp5 . . . . . . . . . . . . . . . . . . . . . . . . . 20
Solution 6.1 Exp6 . . . . . . . . . . . . . . . . . . . . . . . . . 25
Solution 7.1 Exp7 . . . . . . . . . . . . . . . . . . . . . . . . . 30
Solution 8.1 Exp8 . . . . . . . . . . . . . . . . . . . . . . . . . 36
Solution 9.1 Exp9 . . . . . . . . . . . . . . . . . . . . . . . . . 41
Solution 10.1 Exp10 . . . . . . . . . . . . . . . . . . . . . . . . 44
Solution 11.1 Exp11 . . . . . . . . . . . . . . . . . . . . . . . . 47
Solution 12.1 Exp12 . . . . . . . . . . . . . . . . . . . . . . . . 51
Solution 13.1 Exp13 . . . . . . . . . . . . . . . . . . . . . . . . 55
Solution 14.1 Exp14 . . . . . . . . . . . . . . . . . . . . . . . . 57
Solution 15.1 Exp15a . . . . . . . . . . . . . . . . . . . . . . . . 62
Solution 15.2 Exp15b . . . . . . . . . . . . . . . . . . . . . . . . 63
AP 1 Rotate Image 90 degree . . . . . . . . . . . . . . . 66
AP 2 Histogram of Gray images . . . . . . . . . . . . . 67
AP 3 Image Enhancement . . . . . . . . . . . . . . . . 67
AP 4 Inverse Zig Zag scanning of pixels . . . . . . . . . 69
AP 5 Zig Zag Scanning of Pixels . . . . . . . . . . . . . 70
AP 6 2D Fast Fourier Trasnform . . . . . . . . . . . . . 70
AP 7 Inverse 2D Fast Fourier Transform . . . . . . . . . 71

4
List of Figures

1.1 Exp1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2 Exp1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.1 Exp2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.2 Exp2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.1 Exp3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2 Exp3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

6.1 Exp6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
6.2 Exp6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

7.1 Exp7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
7.2 Exp7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

8.1 Exp8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
8.2 Exp8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

11.1 Exp11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
11.2 Exp11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

12.1 Exp12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
12.2 Exp12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

14.1 Exp14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
14.2 Exp14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

5
Experiment: 1

Histogram display and


histogram equalization

Scilab code Solution 1.1 Exp1

1 // Program 1 H i s t o g r a m d i s p l a y and h i s t o g r a m
equalization
2 // S o f t w a r e v e r s i o n
3 //OS Windows7
4 // S c i l a b 5 . 4 . 1
5 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
6 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
7 clc ;
8 clear ;
9 close ;
10 // a=i m r e a d ( ’ C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
t i r e . t i f ’ ) ; / / Image Path
11 a = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
t i r e . jpeg ’)
12 [ m n ]= size ( a ) ;
13 for i =1:256

6
14 b ( i ) = length ( find ( a ==( i -1) ) ) ;
15 end
16 pbb = b /( m * n ) ;
17 pb (1) = pbb (1) ;
18 for i =2:256
19 pb ( i ) = pb (i -1) + pbb ( i ) ;
20 end
21
22 s = pb *255;
23 sb = uint8 ( round ( s ) ) ;
24 index =0;
25 for i =1: m
26 for j =1: n
27 index = double ( a (i , j ) ) +1; // c o n v e r t i t t o
double
28 // o t h e r w i s e i n d e x = 255+1 =0
29 hea (i , j ) = sb ( index ) ; // h i s t o g r a m e q u a l i z a t i o n
30 end
31 end
32 figure ,
33 ShowImage (a , ’ O r i g i n a l Image ’ ) // IPD t o o l b o x
34 title ( ’ O r i g i n a l Image ’ )
35 figure
36 plot2d3 ( ’ gnn ’ ,[1:256] , b )
37 title ( ’ H i s t o g r a m o f t h e Image ’ )
38 figure
39 ShowImage ( hea , ’ Image a f t e r H i s t o g r a m e q u a l i z a t i o n ’ )
// IPD t o o l b o x
40 title ( ’ Image a f t e r H i s t o g r a m e q u a l i z a t i o n ’ )

7
Figure 1.1: Exp1

8
Figure 1.2: Exp1

9
Experiment: 2

Kernel processing on images


leading to image enhancement

Scilab code Solution 2.1 Exp2

1 // Program 2 . K e r n e l p r o c e s s i n g on i m a g e s l e a d i n g t o
image enhancement .
2 // S o f t w a r e v e r s i o n
3 //OS Windows7
4 // S c i l a b 5 . 4 . 1
5 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
6 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
7 clc
8 clear
9 close
10 a = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
c k t n o i s e . j p e g ’ ) ; // SIVP t o o l b o x
11 ks = input ( ’ e n t e r t h e s i z e o f t h e k e r n e l 1 f o r 1 1 3
for 3 3 . . . ’ ) ; // k e r n e l s i z e 3 x3
12 [ m n ]= size ( a ) ;
13
14 a1 = zeros ( m + ks -1 , n + ks -1) ;

10
15 [ m1 n1 ]= size ( a1 ) ;
16 x = floor ( ks /2) ;
17 a1 (1+ x : m1 -x ,1+ x : n1 - x ) = a ;
18 b =[];
19 c =[];
20
21 for i =1+ x : m1 - x
22 for j =1+ x : n1 - x
23 t = a1 (i - x : i +x ,j - x : j + x ) ;
24 men = sum ( sum ( t ) ) /( ks * ks ) ;
25 med = median ( t (:) ) ;
26 b (i -x ,j - x ) = men ;
27 c (i -x ,j - x ) = med ;
28 end
29 end
30
31 figure
32 ShowImage (a , ’ N o i s e d image ( b e f o r e enhancement ) ’ ) ; //
IPD t o o l b o x
33 title ( ’ N o i s e d image ( b e f o r e enhancement ) ’ ) ;
34 figure
35 ShowImage ( uint8 ( b ) , ’ enhancement w i t h mean f i l t e r i n g ’
) ; // IPD t o o l b o x
36 title ( ’ enhancement w i t h mean f i l t e r i n g ’ ) ;
37 figure
38 ShowImage ( uint8 ( c ) , ’ enhancement w i t h median
f i l t e r i n g ’ ) ; // IPD t o o l b o x
39 title ( ’ enhancement w i t h median f i l t e r i n g ’ ) ;
40 //RESULT
41 // e n t e r t h e s i z e o f t h e k e r n e l 1 f o r 1 1 3 f o r 3 3
...3

11
Figure 2.1: Exp2

12
Figure 2.2: Exp2

13
Experiment: 3

Display of 2D filters frequency


responses and processing the
images using these filters

check Appendix AP 6 for dependency:


fft2d.sce
check Appendix AP 7 for dependency:
ifft2d.sce

Scilab code Solution 3.1 Exp3

1 // Program 3 : D i s p l a y o f 2D f i l t e r s f r e q u e n c y
r e s p o n s e s and p r o c e s s i n g t h e i m a g e s u s i n g t h e s e
filters
2 // R e f e r e n c e : ” D i g i t a l Image P r o c e s s i n g ” , Dr . S .
Jayaraman , S . E s a k k i r a j a n , T . Veerakumar ,TMH, 2 0 1 1
3 // Note : The i n − b u i l t s c i l a b f u n c t i o n s f f t 2 d and
i f f t 2 d are not working p r o p e r l y
4 // I t g i v e wrong r e s u l t s .
5 // Use My f u n c t i o n s f o r 2D−FFT and 2D−IFFT .
6 // S o f t w a r e v e r s i o n

14
7 //OS Windows7
8 // S c i l a b 5 . 4 . 1
9 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
10 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
11 clc ;
12 close ;
13 clear ;
14 exec ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
fft2d . sce ’)
15 exec ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
i f f t 2 d . sce ’)
16 im1 = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
b a l l o o n s n o i s y . png ’ ) ; // c o l o u r n o i s e image
17 im = rgb2gray ( im1 ) ; // g r a y n o i s e image
18 fc = 100; // c u t o f f f r e q u e n c y −more f e a t u r e s c h o o s e
high c u t o f f frequency
19 n = 1; // f i l t e r o r d e r =1
20 [ co , ro ]= size ( im ) ;
21 cx = round ( co /2) ; // c e n t r e o f t h e image
22 cy = round ( ro /2) ;
23 IM = fft2d ( double ( im ) ) ;
24 imf = fftshift ( IM ) ;
25 H = zeros ( co , ro ) ;
26 for i = 1: co
27 for j = 1: ro
28 d = (i - cx ) .^2+( j - cy ) .^2;
29 H (i , j ) = 1/(1+(( d / fc / fc ) .^(2* n ) ) ) ; //Low
P a s s B u t t e r w o r t h F i r s t Order f i l t e r
30 end
31 end
32 out_im = imf .* H ;
33 out = abs ( ifft2d ( out_im ) ) ;
34 out = uint8 ( out ) ;
35 figure

15
Figure 3.1: Exp3

36 ShowColorImage ( im1 , ’ C o l o u r N o i s y Image ’ )


37 figure
38 ShowImage ( im , ’ Gray N o i s e Image ’ )
39 figure
40 ShowImage (H , ’ Low P a s s F i l t e r F r e q u e n c y R e s p o n s e ’ )
41 figure
42 ShowImage ( out , ’ F i l t e r e d Image ’ )

16
Figure 3.2: Exp3

17
Experiment: 4

Implementation of Airthmetic
Coding for images

Scilab code Solution 4.1 Exp4

1 // Program 4 . I m p l e m e n t a t i o n o f a r i t h m e t i c c o d i n g
f o r images
2 // Note 1 : I n o r d e r t o run t h i s program download
Huffman t o o l b o x from
3 // s c i l a b atoms
4 // Note 2 : The Huffman atom i s u s e d t o e n c o d e i m a g e s
of small s i z e only
5 // S o f t w a r e v e r s i o n
6 //OS Windows7
7 // S c i l a b 5 . 4 . 1
8 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
9 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
10 clear ;
11 clc ;
12 close ;
13 //A=t e s t m a t r i x ( ’ f r k ’ , 1 0 ) +1;
14 a = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \

18
cameraman . j p e g ’ ) ;
15 A = imresize (a ,[16 16]) ; // Only Image o f s m a l l s i z e
i s p o s s i b l e to c a l l huffcode
16 B = size ( A ) ;
17 A = A (:) . ’;
18 A = double ( A ) ;
19 [ QT , QM ]= huffcode ( A ) ; // Huffman E n c o d i n g
20 disp ( ’ c o m p r e s s e d B i t s e q u e n c e : ’ ) ;
21 disp ( QT ) ;
22 disp ( ’ Code T a b l e : ’ ) ;
23 disp ( QM ) ;
24 // Now , t h e r e v e r s e o p e r a t i o n
25 C = huffdeco ( QT , QM ) ; // Huffman D e c o d i n g
26 for i =1: B (1)
27 E (i ,1: B (2) ) = C (( i -1) * B (2) +1: i * B (2) ) ;
28 end
29 D = E ’;
30 E = imresize (D ,[32 ,32]) ;
31 figure
32 ShowImage (a , ’ O r i g i n a l cameraman Image 256 x256 ’ )
33 figure
34 ShowImage (E , ’ R e c o n s t r u c t e d cameraman Image 256 x256 ’ )
;

19
Experiment: 5

Basic JPEG algorithm


implementation

check Appendix AP 4 for dependency:


izigzag5.sci
check Appendix AP 5 for dependency:
zigzag5.sci

Scilab code Solution 5.1 Exp5

1 // Program 5 . B a s i c JPEG a l g o r i t h m i m p l e m e n t a t i o n
2 // S o f t w a r e v e r s i o n
3 //OS Windows7
4 // S c i l a b 5 . 4 . 1
5 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
6 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
7 close
8 clear ;
9 clc ;
10 exec ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
zigzag 5 . sci ’)

20
11 exec ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
izigzag 5 . sci ’)
12 I = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
cameraman . j p e g ’ ) ; // 256 x256 image
13 I = imresize (I ,0.25) ; // r e d u c e d t o 64 x64 image [ i n
order to reduce the computation time ]
14 [m , n ]= size ( I ) ; // F i n d i n g t h e d i m e n s i o n s o f t h e image
file .
15 I = double ( I ) ;
16 q = [16 11 10 16 24 40 51 61;
17 12 12 14 19 26 58 60 55;
18 14 13 16 24 40 57 69 56;
19 14 17 22 29 51 87 80 62;
20 18 22 37 56 68 109 103 77;
21 24 35 55 64 81 104 113 92;
22 49 64 78 87 103 121 120 101;
23 72 92 95 98 112 100 103 99];
24 N =8; // B l o c k s i z e f o r which
DCT i s Computed .
25 M =8;
26 I_Trsfrm . block = zeros (N , M ) ; // I n i t i a l i s i n g t h e DCT
C o e f f i c i e n t s S t r u c t u r e Matrix ” I Trsfrm ” with the
required dimensions .
27 for a =1: m / N
28 for b =1: n / M
29 for k =1: N
30 for l =1: M
31 Mean_Sum =0;
32 // 2D−D i s c r e t e C o s i n e T r a n s f o r m
///////
33 for i =1: N
34 for j =1: M
35 Mean_Sum = Mean_Sum + double ( I
( N *( a -1) +i , M *( b -1) + j ) ) *
cos ( %pi *( k -1) *(2* i -1) /(2*
N ) ) * cos ( %pi *( l -1) *(2* j -1)

21
/(2* M ) ) ;
36 end
37 end
38 // / / / / / / / / / / / / /
39 if k ==1
40 Mean_Sum = Mean_Sum * sqrt (1/ N ) ;
41 else
42 Mean_Sum = Mean_Sum * sqrt (2/ N ) ;
43 end
44 if l ==1
45 Mean_Sum = Mean_Sum * sqrt (1/ M ) ;
46 else
47 Mean_Sum = Mean_Sum * sqrt (2/ M ) ;
48 end
49 I_Trsfrm (a , b ) . block (k , l ) = Mean_Sum ;
50 end
51 end
52 // N o r m a l i z i n g t h e DCT M a t r i x and Q u a n t i z i n g
the r e s u l t i n g values .
53 I_Trsfrm (a , b ) . block = round ( I_Trsfrm (a , b ) .
block ./ q ) ;
54 end
55 end
56 I_zigzag . block = zeros (N , M ) ;
57 for a = 1: m / N
58 for b = 1: n / M
59 I_zigzag (a , b ) . block = zigzag_5 ( I_Trsfrm (a , b )
. block ) ;
60 end
61 end
62 I_rec_Trnsfm . block = zeros (N , M ) ;
63 for a = 1: m / N
64 for b = 1: n / M
65 I_rec_Trnsfm (a , b ) . block = izigzag_5 ( I_zigzag
(a , b ) . block ) ;
66 end
67 end
68 // D e n o r m a l i z i n g t h e R e c o n s t r u c t e d Tranform m a t r i x

22
u s i n g t h e same
69 // N o r m a l i z a t i o n m a t r i x .
70 for a =1: m / N
71 for b =1: n / M
72 I_rec_Trnsfm (a , b ) . block =( I_rec_Trnsfm (a , b ) .
block ) .* q ;
73 end
74 end
75 // I n v e r s e 2D−DCT
76 for a =1: m / N
77 for b =1: n / M
78 for i =1: N
79 for j =1: M
80 Mean_Sum =0;
81 for k =1: N
82 for l =1: M
83 if k ==1
84 temp = double ( sqrt (1/2) *
I_rec_Trnsfm (a , b ) .
block (k , l ) ) * cos ( %pi *( k
-1) *(2* i -1) /(2* N ) ) * cos
( %pi *( l -1) *(2* j -1) /(2*
M));
85 else
86 temp = double (
I_rec_Trnsfm (a , b ) .
block (k , l ) ) * cos ( %pi *(
k -1) *(2* i -1) /(2* N ) ) *
cos ( %pi *( l -1) *(2* j -1)
/(2* M ) ) ;
87 end
88 if l ==1
89 temp = temp * sqrt (1/2) ;
90 end
91 Mean_Sum = Mean_Sum + temp ;
92 end
93 end
94 Mean_Sum = Mean_Sum *(2/ sqrt ( M * N ) ) ;

23
95 I_rec (( a -1) * N +i ,( b -1) * M + j ) = Mean_Sum
;
96 end
97 end
98 end
99 end
100 // D i s p l a y i n g t h e R e c o n s t r u c t e d Image .
101 diff_image = im2double ( I ) *255 - I_rec ;
102 diff_image = diff_image / max ( max ( diff_image ) ) ;
103 diff_image = im2uint8 ( diff_image ) ;
104 I_rec = I_rec / max ( max ( I_rec ) ) ;
105 I_rec = im2uint8 ( I_rec ) ;
106 figure
107 ShowImage ( I_rec , ’ R e c o v e r e d Image ’ ) ;
108 figure
109 ShowImage ( diff_image , ’ D i f f e r e n c e Image ’ )
110 figure
111 imhist ( I_rec ) ;
112 figure
113 imhist ( diff ) ;

24
Experiment: 6

DPCM encoding and decoding


of images

Scilab code Solution 6.1 Exp6

1 // Program 6 DPCM e n c o d i n g and d e c o d i n g o f i m a g e s


2 // S o f t w a r e v e r s i o n
3 //OS Windows7
4 // S c i l a b 5 . 4 . 1
5 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
6 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
7 clc
8 clear
9 // F u n c t i o n t o f i n d number o f e l e m e n t s i n an image
10 function [ N ] = numel ( X )
11 //X−i n p u t image
12 //N− number o f e l e m e n t s i n image X
13 [m , n ]= size ( X ) ;
14 N = m*n;
15 endfunction
16 //
//////////////////////////////////////////////////////////////////

25
17 // F u n c t i o n t o c a l c u l a t e peak s i g n a l t o n o i s e r a t i o
18 function [ psnr , mse , maxerr ] = psnr_mse_maxerr (X , Xapp )
19 //PSNR MSE MAXERR Peak s i g n a l t o n o i s e r a t i o
20 //X − o r i g i n a l Image
21 // Xapp − r e c o n s t r u c t e d image
22 // p s n r − peak s i g n a l t o n o i s e r a t i o
23 // mse − mean s q u a r e e r r o r
24 // maxerr − maximum e r r o r
25 X = double ( X ) ;
26 Xapp = double ( Xapp ) ;
27 absD = abs (X - Xapp ) ;
28 A = absD .^2;
29 mse = sum ( A (:) ) / numel ( X ) ;
30 psnr = 10* log10 (255*255/ mse ) ;
31 maxerr = round ( max ( absD (:) ) ) ;
32 endfunction
33 //
//////////////////////////////////////////////////////////////////

34 a = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
cameraman . j p e g ’ ) ;
35 a = double ( a ) ;
36 [ m n ]= size ( a ) ;
37 pre =0;
38 q = input ( ’ e n t e r t h e q u a n t i z a t i o n v a l u e ’ ) ;
39 for i =1: m
40 for j =1: n
41 t1 = a (i , j ) - pre ;
42 tq = round ( t1 / q ) ;
43 pre = pre + tq * q ;
44 b (i , j ) = tq ;
45 end
46 end
47 repre =0;
48 for i =1: m
49 for j =1: n
50 ret = b (i , j ) ;

26
51 inq = ret * q ;
52 repre = repre + inq ;
53 c (i , j ) = repre ;
54 end
55 end
56 figure
57 ShowImage (a , ’ Image B e f o r e Q u a n t i z a t i o n ’ )
58 figure
59 ShowImage (b , ’ Q u a n t i z e d Image ’ )
60 figure
61 ShowImage (c , ’ R e c o n s t r u c t e d Image From Q u a n t i z e d
Image ’ )
62 psnr = psnr_mse_maxerr (a , c ) ;
63 disp ( psnr , ’PSNR i n dB= ’ )
64 //RESULT
65 // e n t e r t h e q u a n t i z a t i o n v a l u e 2
66 //PSNR i n dB = 5 1 . 1 6 5 5 5 9
67
68 // e n t e r t h e q u a n t i z a t i o n v a l u e 8
69 //PSNR i n dB = 4 0 . 6 9 8 1 6 4
70 //

27
Figure 6.1: Exp6

28
Figure 6.2: Exp6

29
Experiment: 7

Simple image watermarking


algorithms using LSB
substitution

Scilab code Solution 7.1 Exp7

1 // Program 7 . S i m p l e image w a t e r m a r k i n g a l g o r i t h m s
u s i n g LSB s u b s t i t u t i o n
2 // Note 1 : The i m r e a d f u n c t i o n i n SIVP t o o l b o x r e a d
t h e b i n a r y image a s g r a y
3 // s c a l e image . D u r i n g b i t s e t i t w i l l c r e a t e p r o b l e m s
.
4 // The g r a y s c a l e image can be c o n v e r t e d i n t o b i n a r y
image u s i n g t h e f u n c t i o n
5 // g r a y 2 b i n ( )
6 // Note 2 : The f u n c t i o n s b i t s e t and b i t g e t a r e
written i n o r d e r to save the
7 // s c i l a b w o r k s p a c e memory d u r i n g e x e c u t i o n
8 // S o f t w a r e v e r s i o n
9 //OS Windows7
10 // S c i l a b 5 . 4 . 1
11 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
12 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x

30
0.5.3.1 −2
13 clc
14 clear
15 close
16 // F u n c t i o n t o f i n d number o f e l e m e n t s i n an image
17 function [ N ] = numel ( X )
18 //X−i n p u t image
19 //N− number o f e l e m e n t s i n image X
20 [m , n ]= size ( X ) ;
21 N = m*n;
22 endfunction
23 // F u n c t i o n t o c a l c u l a t e peak s i g n a l t o n o i s e r a t i o
24 function [ psnr , mse , maxerr ] = psnr_mse_maxerr (X , Xapp )
25 //PSNR MSE MAXERR Peak s i g n a l t o n o i s e r a t i o
26 //X − o r i g i n a l Image
27 // Xapp − r e c o n s t r u c t e d image
28 // p s n r − peak s i g n a l t o n o i s e r a t i o
29 // mse − mean s q u a r e e r r o r
30 // maxerr − maximum e r r o r
31 X = double ( X ) ;
32 Xapp = double ( Xapp ) ;
33 absD = abs (X - Xapp ) ;
34 A = absD .^2;
35 mse = sum ( A (:) ) / numel ( X ) ;
36 psnr = 10* log10 (255*255/ mse ) ;
37 maxerr = round ( max ( absD (:) ) ) ;
38 endfunction
39 //
//////////////////////////////////////////////////////////////////

40 function [ A ]= gray2bin ( B )
41 [m , n ] = size ( B )
42 for i = 1: m
43 for j = 1: n
44 if ( B (i , j ) >200)
45 A (i , j ) = 1;
46 else
47 A (i , j ) =0;

31
48 end
49
50 end
51
52 end
53 endfunction
54 //
//////////////////////////////////////////////////////////////////

55 function [ c ]= bit_set (c , b )
56 [m , n ] = size ( c ) ;
57 for i =1: m
58 for j =1: n
59 c (i , j ) = bitset ( c (i , j ) ,1 , b (i , j ) ) ;
60 end
61 end
62 endfunction
63 //
//////////////////////////////////////////////////////////////////

64 function [ d ] = bit_get ( c )
65 [m , n ] = size ( c ) ;
66 for i =1: m
67 for j =1: n
68 d (i , j ) = bitget ( c (i , j ) ,1) ;
69 end
70 end
71 endfunction
72 //
//////////////////////////////////////////////////////////////////

73 a = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
cameraman . j p e g ’ ) ; // o r i g i n a l image
74 b = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \ wat
. jpg ’ ); // watermark image
75 b = gray2bin ( b ) ;

32
76 [ m n ] = size ( a ) ;
77 a = double ( a ) ;
78 c = a;
79 c = bit_set (c , b ) ;
80 d = bit_get ( c ) ;
81
82 figure
83 ShowImage (a , ’ O r i g i n a l image ’ ) ;
84 title ( ’ O r i g i n a l image ’ ) ;
85 figure
86 ShowImage (b , ’ watermark image ’ ) ;
87 title ( ’ watermark image ’ ) ;
88 figure
89 ShowImage ( uint8 ( c ) , ’ w a t e r m a r k e d image ’ ) ;
90 title ( ’ w a t e r m a r k e d image ’ ) ;
91 figure
92 ShowImage (d , ’ e x t r a c t e d watermark ’ ) ;
93 title ( ’ e x t r a c t e d watermark ’ ) ;
94 psnr = psnr_mse_maxerr (a , c ) ;
95 correlation = corr2 (b , d ) ;
96 disp ( correlation , ’ c o r r e l a t i o n b e t w e e n watermark
image and e x t r a c t e d watermark= ’ )

33
Figure 7.1: Exp7

34
Figure 7.2: Exp7

35
Experiment: 8

Simple content based image


retrieval using various distance
metrics

Scilab code Solution 8.1 Exp8

1 // Program 8 : S i m p l e c o n t e n t b a s e d image r e t r i e v a l
using various distance metrics .
2 // Based on S i m i l a r i t y m a t r i x
3 // U s i n g Colormaps o f d i f f e r e n t i m a g e s
4 // Note 1 : Other methods l i k e w a v e l e t b a s e d
decomposition along with Euclidean d i s t a n c e
5 // c o m p a r i s o n o f sub i m a g e s can be u s e d f o r image
retrieval
6 // Note 2 : P r i n c i p a l Component A n a l y s i s (PCA) i n b u i l t
f u n c t i o n i s a v a i l a b l e to
7 // g e t e i g e n v e c t o r s and e i g e n v a l u e s f o r image
retrieval
8 // S o f t w a r e v e r s i o n
9 //OS Windows7
10 // S c i l a b 5 . 4 . 1
11 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
12 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x

36
0.5.3.1 −2
13 clear ;
14 clc ;
15 close ;
16 I1 = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
P i c t u r e 1 . png ’ ) ; // 257 x 2 5 7 x 3 .
17 I1 = imresize ( I1 ,0.5) ;
18 [ IndexedImage_I1 , ColorMap ] = RGB2Ind ( I1 ) ; // IPD
toolbox
19 I = ColorMap ; // 6 6 0 4 9 x3
20 J1 = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
P i c t u r e 2 . png ’ ) ; // 257 x 2 5 7 x 3 .
21 J1 = imresize ( J1 ,0.5) ;
22 [ IndexedImage_J1 , ColorMap ] = RGB2Ind ( J1 ) ; // IPD
toolbox
23 J = ColorMap ; // 6 6 0 4 9 x3
24 // S i m i l a r i t y M a t r i x Method
25 [r , c ]= size ( I ) ;
26 A = [];
27 I = double ( I ) ;
28 J = double ( J ) ;
29 for i = 1: r
30 for j = 1: c
31 M1 (i , j ) = ( I (i ,2) * sin ( I (i ,1) ) -J (j ,2) * sin ( J (j
,1) ) ) ^2;
32 M2 (i , j ) = ( I (i ,2) * cos ( I (i ,1) ) -J (j ,2) * cos ( J (j
,1) ) ) ^2;
33 M3 (i , j ) = ( I (i ,3) -J (i ,3) ) ^2;
34 M (i , j ) = sqrt ( M1 (i , j ) + M2 (i , j ) + M3 (i , j ) ) ;
35 A (i , j ) = 1 - M (i , j ) / sqrt (5) ;
36 end
37 end
38 I1_rec = Ind2RGB ( IndexedImage_I1 , A )
39 I1_rec = imresize ( I1_rec ,2) ;
40 J1_rec = Ind2RGB ( IndexedImage_J1 , A )
41 J1_rec = imresize ( J1_rec ,2) ;

37
42 figure
43 ShowColorImage ( I1 , ’ o r i g i n a l f i r s t image ’ ) ;
44 figure
45 ShowColorImage ( I1_rec , ’ R e c o n s t r u c t e d f i r s t image ’ ) ;
46 figure
47 ShowColorImage ( J1 , ’ o r i g i n a l s e c o n d image ’ ) ;
48 figure
49 ShowColorImage ( J1_rec , ’ R e c o n s t r u c t e d s e c o n d image ’ ) ;

38
Figure 8.1: Exp8

39
Figure 8.2: Exp8

40
Experiment: 9

Image segmentation algorithms


using Snakes

Scilab code Solution 9.1 Exp9

1 // Program 9 . Image s e g m e n t a t i o n a l g o r i t h m s u s i n g
snakes .
2 // Note : I n c o m p l e t e .
3 // So many f u n c t i o n s a r e n o t a v i l a b l e i n S c i l a b
4 // Image s e g m e n t a t i o n a l g o r i t h m s u s i n g s n a k e s i s
i m p o s s i b l e with c u r r e n t
5 // v e r s i o n o f s c i l a b and s c i l a b image p r o c e s s i n g
atoms .
6 // I t r i e d my b e s t
7 // S o f t w a r e v e r s i o n
8 //OS Windows7
9 // S c i l a b 5 . 4 . 1
10 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
11 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
12 close ;
13 clear ;
14 clc ;
15 J = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \

41
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
b i n a r y i m a g e . jpg ’ );
16 J = rgb2gray ( J ) ;
17 J = imresize (J ,[256 ,256]) ;
18 J = double ( J ) ;
19 [h , w ] = size ( J ) ;
20 for i = 1: h
21 for j = 1: w
22 if ( J (i , j ) >200)
23 J (i , j ) = 1;
24 else
25 J (i , j ) =0;
26 end
27 end
28 end
29 I = imfilter (J , fspecial ( ’ g a u s s i a n ’ ,[17 17] ,3) ) ;
30 figure
31 ShowImage (I , ’ S n a k e s ’ )
32 N =500; // number o f s n a k e p o i n t s
33 alpha =1;
34 tstep =1;
35 N_iter =500;
36 f =50;
37 global EDGE_SOBEL ;
38 gradient = EdgeFilter (I , EDGE_SOBEL ) ;
39 [m , n ] = size ( gradient ) ;
40 Ix = gradient (: ,:) ;
41 Iy = gradient (: ,:) ’;
42 S = -f *( Ix .* Ix + Iy .* Iy ) ;
43 gradient = EdgeFilter (S , EDGE_SOBEL ) ;
44 Sx = gradient (: ,:) ;
45 Sy = gradient (: ,:) ’;
46 eps = 2.2204 e -016;
47 Smag = sqrt ( Sx .^2 + Sy .^2) + eps ;
48 Sx (:) = Sx ./ Smag ;
49 Sy (:) = Sy ./ Smag ;
50 D =[ - tstep * alpha * ones (N ,1) (1+2* tstep * alpha ) * ones (N
,1) - tstep * alpha * ones (N ,1) ];

42
51 D (2 ,3) = D (2 ,3) - tstep * alpha ;
52 D ($ -1 ,1) = D ($ -1 ,1) - tstep * alpha ;
53 theta = linspace (0 ,2* %pi , N ) ;
54 theta = theta (:) ;
55 x = w /2 + 10 + ( h /3) * cos ( theta ) ;
56 y = h /2 - 10 + ( h /4) * sin ( theta ) ;
57 plot (x ,y , ’ r ’ ) ;

43
Experiment: 10

Color images manipulations,


reading and writing of color
images

Scilab code Solution 10.1 Exp10

1 // Program 1 0 . C o l o r i m a g e s m a n i p u l a t i o n s , r e a d i n g and
w r i t i n g of c o l o r images
2 // S o f t w a r e v e r s i o n
3 //OS Windows7
4 // S c i l a b 5 . 4 . 1
5 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
6 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
7 clc
8 clear
9 close
10 // Showing RGB c o m p o ne n t s o f a c o l o r RGB image .
11 // S p l i t t i n g t h e c o l o r image (RGB Image ) i n t o t h r e e
planes
12 a = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D I P S c i l a b P r o g r a m s \ p e p p e r s .
png ’ ) ; // t h i s image i s 348 x 5 1 2 x 3 s i z e

44
13 figure
14 ar = a (: ,: ,1) ;
15 ShowImage ( ar , ’RED M a t r i x ’ )
16 figure
17 ag = a (: ,: ,2) ;
18 ShowImage ( ag , ’GREEN M a t r i x ’ )
19 figure
20 ab = a (: ,: ,3) ;
21 ShowImage ( ab , ’BLUE M a t r i x ’ )
22 // R e c o n s t r u c t i o n o f o r i g i n a l c o l o r image from t h r e e
RGB p l a n e s
23
24 RGB = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D I P S c i l a b P r o g r a m s \ p e p p e r s .
png ’ ) ; // SIVP t o o l b o x
25 RGB_128 = RGB /2;
26 RGB_128 = round ( RGB_128 )
27 [X , map ] = RGB2Ind ( RGB_128 ) ;
28 figure
29 ShowImage (X , ’ I n d e x e d Image ’ , map )
30 // L i m i t i n g no o f c o l o u r s t o 8 w i t h o u t d i t h e r i n g
31 figure
32 RGB_8 = RGB /7;
33 RGB_8 = round ( RGB_8 )
34 [ X1 , map1 ]= RGB2Ind ( RGB ) ;
35 ShowImage ( X1 , ’ Without D i t h e r ’ , map1 )
36
37 figure
38 ShowColorImage ( RGB , ’RGB C o l o r Image ’ )
39 YIQ = rgb2ntsc ( RGB ) ;
40 figure
41 ShowColorImage ( YIQ , ’NTSC image YIQ ’ )
42 RGB = ntsc2rgb ( YIQ ) ;
43 YCC = rgb2ycbcr ( RGB ) ;
44 figure
45 ShowColorImage ( YCC , ’ e q u i v a l e n t HSV image YCbCr ’ )
46 RGB = ycbcr2rgb ( YCC ) ;
47 HSV = rgb2hsv ( RGB ) ;

45
48 figure
49 ShowColorImage ( HSV , ’ e q u i v a l e n t HSV image ’ )
50 RGB = hsv2rgb ( HSV ) ;

46
Experiment: 11

Color image enhancements

check Appendix AP 3 for dependency:


imgenh11.sci

Scilab code Solution 11.1 Exp11

1 // Program 1 1 . C o l o r image e n h a n c e m e n t s
2 // S o f t w a r e v e r s i o n
3 //OS Windows7
4 // S c i l a b 5 . 4 . 1
5 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
6 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
7 clc
8 clear
9 close
10 a = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
b a l l o o n s n o i s y . png ’ ) ;
11 ks = input ( ’ e n t e r t h e s i z e o f t h e k e r n e l 1 f o r 1 1 3
for 3 3 . . . ’ );
12 exec ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
imgenh 11 . s c i ’ )

47
13 for i =1:3
14 b (: ,: , i ) = imgenh11 ( a (: ,: , i ) , ks ) ;
15 end
16
17 figure
18 ShowColorImage (a , ’ N o i s e d image ( b e f o r e enhancement ) ’ )
;
19 title ( ’ N o i s e d image ( b e f o r e enhancement ) ’ ) ;
20 figure
21 ShowColorImage ( uint8 ( b ) , ’ enhancement w i t h mean
f i l t e r i n g ’ );
22 title ( ’ enhancement w i t h mean f i l t e r i n g ’ ) ;
23 //RESULT
24 // e n t e r t h e s i z e o f t h e k e r n e l 1 f o r 1 1 3 f o r 3 3
...3
25
26 //NOTE: s i n c e t h e image i s l a r g e [ 4 8 0 640] i t will
t a k e some t i m e t o
27 // show t h e r e s u l t

48
Figure 11.1: Exp11

49
Figure 11.2: Exp11

50
Experiment: 12

Color image histogram


manipulation

check Appendix AP 2 for dependency:


histbw12.sci

Scilab code Solution 12.1 Exp12

1 // Program 12 C o l o r image h i s t o g r a m m a n i p u l a t i o n
2 // S o f t w a r e v e r s i o n
3 //OS Windows7
4 // S c i l a b 5 . 4 . 1
5 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
6 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
7 clc
8 close
9 a = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
p e p p e r s . png ’ ) ;
10 a1 = uint8 ( a ) ;
11 exec ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
histbw 12 . s c i ’)

51
12 for i =1:3
13 b (: ,: , i ) = histbw12 ( a1 (: ,: , i ) ) ;
14 end
15 figure
16 ShowColorImage (a , ’ o r i g i n a l c o l o r image ’ ) ;
17 title ( ’ o r i g i n a l c o l o r image ’ ) ;
18 figure
19 ShowColorImage (b , ’ h i s t o g r a m e q u a l i z a t i o n o f color
image ’ ) ;
20 title ( ’ h i s t o g r a m e q u a l i z a t i o n o f c o l o r image ’ ) ;
21 rgbhist_12 ( a ) ;
22 // e x e c ( ’ C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
rgbhist 12 . sci ’)

52
Figure 12.1: Exp12

53
Figure 12.2: Exp12

54
Experiment: 13

LOG Masks implementation for


gray and color images

Scilab code Solution 13.1 Exp13

1 // Program 1 3 . LOG Masks i m p l e m e n t a t i o n f o r g r a y and


c o l o r images
2 // S o f t w a r e v e r s i o n
3 //OS Windows7
4 // S c i l a b 5 . 4 . 1
5 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
6 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
7 clc
8 clear
9 close
10 a = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
cameraman . j p e g ’ ) ;
11 a = double ( a ) ;
12 logmask =[0 1 1 2 2 2 1 1 0;1 2 4 5 5 5 4 2 1;1 4 5 3
0 3 5 4 1;2 5 3 -12 -24 -12 3 5 2;2 5 0 -24 -40
-24 0 5 2;
13 2 5 3 -12 -24 -12 3 5 2;1 4 5 3 0 3 5 4 1;1

55
2 4 5 5 5 4 2 1;0 1 1 2 2 2 1 1 0];
14 [ m n ]= size ( a ) ;
15 [ m1 n1 ]= size ( logmask ) ;
16 b = zeros ( m + m1 -1 , n + n1 -1) ;
17 m2 = floor ( m1 /2) ;
18 n2 = floor ( n1 /2) ;
19 b ( m2 +1: m + m2 , n2 +1: n + n2 ) = a ;
20 for i = m2 +1: m + m2
21 for j = n2 +1: n + n2
22 c = b (i - m2 : i + m2 ,j - n2 : j + n2 ) ;
23 d = sum ( sum ( c .* logmask ) ) ;
24 if d >150
25 e (i - m2 ,j - n2 ) =0;
26 else
27 e (i - m2 ,j - n2 ) =1;
28 end
29
30 end
31 end
32 title ( ’ Camerman image a f t e r LOG masked ’ )
33 imshow ( e ) // SIVP t o o l b o x

56
Experiment: 14

Special effects implementation


on grey and color images

check Appendix AP 1 for dependency:

rot90f.sci

Scilab code Solution 14.1 Exp14

1 // Program 1 4 . S p e c i a l e f f e c t s i m p l e m e n t a t i o n on g r a y
and c o l o r i m a g e s
2 // Note : The f u n c t i o n s l i k e e n t r o p f i l t ( ) .m a r e n o t
available in s c i l a b
3 // But s i m i l a r e f f e c t s can be p r o d u c e d i n s c i l a b
u s i n g o t h e r methods .
4 // S o f t w a r e v e r s i o n
5 //OS Windows7
6 // S c i l a b 5 . 4 . 1
7 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
8 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
9 clc ;
10 clear ;
11 close ;

57
12 exec ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
rot90 f . sci ’)
13 img1 = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
m a n d r i l l . j p e g ’ ) ; // c o l o u r image
14 img2 = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
t w o z e b r a s . j p g ’ ) ; // c o l o u r image
15 img3 = imread ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
cameraman . j p e g ’ ) ; // g r a y image
16 filter1 = fspecial ( ’ s o b e l ’ ) ;
17 img1_filter = imfilter ( img1 , filter1 ) ;
18 img2_filter = imfilter ( img2 , filter1 ) ;
19 ShowColorImage ( img1 , ’ o r i g i n a l image 1 ’ ) ;
20 figure
21 ShowColorImage ( img1_filter , ’ S p e c i a l e f f e c t i n
M a n d r i l l Image ’ )
22 figure
23 ShowColorImage ( img2 , ’ o r i g i n a l image 2 ’ ) ;
24 figure
25 ShowColorImage ( img2_filter , ’ S p e c i a l e f f e c t i n
t w o z e b r a s Image ’ )
26 img3_negative = 255 - double ( img3 ) ; // image n e g a t i v e
27 img3_rotate = rot90f ( img3 ,3) ;
28 // Image c o n t r a t a d j u s t m e n t
29 [m , n ] = size ( img3 ) ;
30 for i = 1: m
31 for j = 1: n
32 if img3 (i , j ) >70 then
33 img3_adjust (i , j ) = img3 (i , j ) +(255 - img3 (i
,j));
34 else
35 img3_adjust (i , j ) = img3 (i , j ) ;
36 end
37
38 end

58
39 end
40 figure
41 ShowImage ( img3 , ’ Cameraman o r i g i n a l Image ’ ) ;
42 figure
43 ShowImage ( img3_negative , ’ Cameraman N e g a t i v e Image ’ )
44 figure
45 ShowImage ( img3_rotate , ’ 270 d e g r e e r o t a t i o n o f
camerman image ’ )
46 figure
47 ShowImage ( img3_adjust , ’ Cameraman Image C o n t r a s t
Adjustment ’ )

59
Figure 14.1: Exp14

60
Figure 14.2: Exp14

61
Experiment: 15

Simple video reading and


writing .avi formats and
manipulation of video frames

Scilab code Solution 15.1 Exp15a

1 // Program 1 5 . S i m p l e v i d e o r e a d i n g and w r i t i n g . a v i
f o r m a t s and m a n i p u l a t i o n o f v i d e o f r a m e s .
2 // Note 1 : I n s t a l l x v i d c o d e c f o r r e a d and w r i t e
v i d e o f i l e s from
3 // h t t p : / /www . x v i d . o r g / Downloads . 1 5 . 0 . html
4 // Note 2 : v e r y l a r g e can n o t be r e a d by s c i l a b
5 // Note 3 : s h u t t l e . a v i i s a l a r g e f i l e more 100
frames . use shuttlenew . avi f i l e
6 // f o r v i d e o p r o c e s s i n g a p p l i c a t i o n s
7 // U s i n g SIVP Atom
8 // S o f t w a r e v e r s i o n
9 //OS Windows7
10 // S c i l a b 5 . 4 . 1
11 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
12 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
13 clear ;

62
14 clc ;
15 close ;
16 // n = a v i o p e n ( SCI + ’/ c o n t r i b / s i v p / i m a g e s / v i d e o . a v i ’ ) ;
17 n = aviopen ( ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \ red
−c a r −v i d e o . a v i ’ ) ;
18 im = avireadframe ( n ) ; // g e t a f r a m e
19 imshow ( im ) ;
20 avilistopened ()
21 aviclose ( n ) ;

Scilab code Solution 15.2 Exp15b

1 // Program 1 5 . S i m p l e v i d e o r e a d i n g and w r i t i n g . a v i
f o r m a t s and m a n i p u l a t i o n o f v i d e o f r a m e s .
2 // Note 1 : I n s t a l l x v i d c o d e c f o r r e a d and w r i t e
v i d e o f i l e s from
3 // h t t p : / /www . x v i d . o r g / Downloads . 1 5 . 0 . html
4 // Note 2 : v e r y l a r g e can n o t be r e a d by s c i l a b
5 // Note 3 : s h u t t l e . a v i i s a l a r g e f i l e more 100
f r a m e s . u s e red −c a r −v i d e o . a v i f i l e
6 // f o r v i d e o p r o c e s s i n g a p p l i c a t i o n s
7 // U s i n g Image P r o c e s s i n g D e s i g n Atom ( IPD )
8 // S o f t w a r e v e r s i o n
9 //OS Windows7
10 // S c i l a b 5 . 4 . 1
11 // Image P r o c e s s i n g D e s i g n T o o l b o x 8 . 3 . 1 − 1
12 // S c i l a b Image and V i d e o P r o c c e s s i n g t o o l b o x
0.5.3.1 −2
13 clear ;
14 clc ;
15 close ;
16 VideoPath = ’C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \ red
−c a r −v i d e o . a v i ’ ;

63
17
18 VideoInfo = GetVideoStruct ( ’C : \ U s e r s \ s e n t h i l k u m a r \
D e s k t o p \ Chandra Mohan LAB \
D i g i t a l I m a g e P r o c e s s i n g L a b \ red −c a r −v i d e o . a v i ’ ) ;
19
20 VideoFilePointer = OpenVideoFile ( ’C : \ U s e r s \
s e n t h i l k u m a r \ D e s k t o p \ Chandra Mohan LAB \
D i g i t a l I m a g e P r o c e s s i n g L a b \ red −c a r −v i d e o . a v i ’ ) ;
21
22 figure () ;
23
24 for n = 1 : VideoInfo . NumberOfFrames
25
26 RGB = ReadImage ( VideoFilePointer ) ;
27
28 ShowColorImage ( RGB , VideoPath ) ;
29
30 end ;
31
32 CloseVideoFile ( VideoFilePointer ) ;

64
Appendix

Scilab code AP 11 function [ B ] = rot90f (A , k )


2 [ %nargout , %nargin ] = argn (0)
3 //ROT90 R o t a t e m a t r i x 90 d e g r e e s .
4 // ROT90 (A) i s t h e 90 d e g r e e c o u n t e r c l o c k w i s e
r o t a t i o n of matrix A.
5 // ROT90 (A, K) i s t h e K∗90 d e g r e e r o t a t i o n o f A, K
= + −1 ,+ −2 ,...
6 [m , n ] = size ( A ) ;
7 if %nargin ==1 then
8 k = 1;
9 else
10 k = k - fix ( k /4) .*4;
11 if (k <0) then
12 k = k +4;
13 end
14 end
15 if k == 1
16 A = A . ’;
17 B = A ( n : -1:1 ,:) ;
18 elseif k == 2
19 B = A ( m : -1:1 , n : -1:1) ;
20 elseif k == 3
21 B = A ( m : -1:1 ,:) ;
22 B = B . ’;
23 else
24 B = A;
25 end

65
26 endfunction
Rotate Image 90 degree

Scilab code AP 12 function [ hea , b ]= histbw12 ( a )


2 // a=i m r e a d ( ’ C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b
\ t i r e . jpeg ’ )
3 // a− o r i g i n a l image
4 // b− h i s t o g r a m
5 // hea− h i s t o g r a m e q u a l i z e d image
6 [ m n ]= size ( a ) ;
7 for i =1:256
8 b ( i ) = length ( find ( a ==( i -1) ) ) ;
9 end
10 pbb = b /( m * n ) ;
11 pb (1) = pbb (1) ;
12 for i =2:256
13 pb ( i ) = pb (i -1) + pbb ( i ) ;
14 end
15
16 s = pb *255;
17 sb = uint8 ( round ( s ) ) ;
18 index =0;
19 for i =1: m
20 for j =1: n
21 index = double ( a (i , j ) ) +1; // c o n v e r t i t t o
double
22 // o t h e r w i s e i n d e x = 255+1 =0
23 hea (i , j ) = sb ( index ) ; // h i s t o g r a m
equalization
24 end
25 end
26 endfunction
27 // n o t e :
28 // F i r s t run t h i s f u n c t i o n
29 // t y p e t h e f o l l o w i n g commands i n s c i l a b c o n s o l e
window

66
30 // a=i m r e a d ( ’ C : \ U s e r s \ s e n t h i l k u m a r \ D e s k t o p \
Chandra Mohan LAB \ D i g i t a l I m a g e P r o c e s s i n g L a b \
t i r e . jpeg ’ )
31 // [ hea , b ] = h i s t b w 1 2 ( a ) ;
32 // f i g u r e ,
33 // ShowImage ( a , ’ O r i g i n a l Image ’ ) // IPD t o o l b o x
34 // t i t l e ( ’ O r i g i n a l Image ’ )
35 // f i g u r e
36 // p l o t 2 d 3 ( ’ gnn ’ , [ 1 : 2 5 6 ] , b )
37 // t i t l e ( ’ H i s t o g r a m o f t h e Image ’ )
38 // f i g u r e
39 // ShowImage ( hea , ’ Image a f t e r H i s t o g r a m e q u a l i z a t i o n
’ ) // IPD t o o l b o x
40 // t i t l e ( ’ Image a f t e r H i s t o g r a m e q u a l i z a t i o n ’ )
Histogram of Gray images

Scilab code AP 13 function [ out ] = imgenh11 (a , ks )


2 [ m n ]= size ( a ) ;
3 a1 = zeros ( m + ks -1 , n + ks -1) ;
4 [ m1 n1 ]= size ( a1 ) ;
5 x = floor ( ks /2) ;
6 a1 (1+ x : m1 -x ,1+ x : n1 - x ) = a ;
7 out =[];
8
9 for i =1+ x : m1 - x
10 for j =1+ x : n1 - x
11 t = a1 (i - x : i +x ,j - x : j + x ) ;
12 med = median ( t (:) ) ;
13 out (i -x ,j - x ) = med ;
14 end
15 end
16 endfunction
Image Enhancement

Scilab code AP 14 function [ result ] = izigzag5 ( data )


2 // i n v e r s e Z i g Z a g s c a n n i n g o f i n p u t d a t a

67
3 N = sqrt ( size ( data ,1) ) ;
4 z = 1;
5 count = 0;
6 row = 1;
7 col = 0;
8 for ( x = 2:2* N ) ,
9 if ( x <= N +1) ,
10 y = x + 1;
11 if ( modulo (x ,2) == 0)
12 col = col + 1;
13 else
14 row = row + 1;
15 end
16 else
17 y = N +1;
18 if ( modulo (x ,2) == 0)
19 row = row - 1;
20 col = col + 2;
21 else
22 row = row + 2;
23 col = col - 1;
24 end
25 end
26
27 while (( row < y ) &( col < y ) &( row > 0) &( col > 0) )
28 result ( row , col ) = data ( z ) ;
29 z = z + 1;
30
31 if ( modulo (x ,2) == 0)
32 row = row - 1;
33 col = col + 1;
34 else
35 row = row + 1;
36 col = col - 1;
37 end
38 end
39 end
40 endfunction

68
Inverse Zig Zag scanning of pixels

Scilab code AP 15 function [ result ] = zigzag5 ( data )


2 // Z i g Z a g s c a n n i n g o f i n p u t d a t a
3 N = size ( data ,1) ;
4 z = 1;
5 count = 0;
6 row = 1;
7 col = 0;
8
9 for ( x = 2:2* N ) ,
10 if ( x <= N +1)
11 y = x + 1;
12 if ( modulo (x ,2) == 0)
13 col = col + 1;
14 else
15 row = row + 1;
16 end
17 else
18 y = N +1;
19 if ( modulo (x ,2) == 0)
20 row = row - 1;
21 col = col + 2;
22 else
23 row = row + 2;
24 col = col - 1;
25 end
26 end
27
28 while (( row < y ) &( col < y ) &( row > 0) &( col > 0) )
29 result ( z ) = data ( row , col ) ;
30 z = z + 1;
31 if ( modulo (x ,2) == 0)
32 row = row - 1;
33 col = col + 1;
34 else
35 row = row + 1;

69
36 col = col - 1;
37 end
38 end
39 end
40 endfunction
Zig Zag Scanning of Pixels

Scilab code AP 16 function [ a2 ] = fft2d ( a )


2 // a = any r e a l o r c o m p l e x 2D m a t r i x
3 // a2 = 2D−DFT o f 2D m a t r i x ’a ’
4 m = size (a ,1)
5 n = size (a ,2)
6 // 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
7 for i =1: n
8 a1 (: , i ) = exp ( -2* %i * %pi *(0: m -1) ’.*.(0: m -1) / m ) * a (: , i )
9 end
10 // f o u r i e r t r a n s f o r m a l o n g t h e c o l u m n s
11 for j =1: m
12 a2temp = exp ( -2* %i * %pi *(0: n -1) ’.*.(0: n -1) / n ) *( a1 (j ,:) )
.’
13 a2 (j ,:) = a2temp . ’
14 end
15 for i = 1: m
16 for j = 1: n
17 if (( abs ( real ( a2 (i , j ) ) ) <0.0001) &( abs ( imag ( a2 (
i , j ) ) ) <0.0001) )
18 a2 (i , j ) =0;
19 elseif ( abs ( real ( a2 (i , j ) ) ) <0.0001)
20 a2 (i , j ) = 0+ %i * imag ( a2 (i , j ) ) ;
21 elseif ( abs ( imag ( a2 (i , j ) ) ) <0.0001)
22 a2 (i , j ) = real ( a2 (i , j ) ) +0;
23 end
24 end
25 end
2D Fast Fourier Trasnform

70
Scilab code AP 17 function [ a ] = ifft2d ( a2 )
2 // 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
3 // a = 2D−IDFT o f a2
4 m = size ( a2 ,1)
5 n = size ( a2 ,2)
6 // 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
7 for i =1: n
8 a1 (: , i ) = exp (2* %i * %pi *(0: m -1) ’.*.(0: m -1) / m ) * a2 (: , i )
9 end
10 // I 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
11 for j =1: m
12 atemp = exp (2* %i * %pi *(0: n -1) ’.*.(0: n -1) / n ) *( a1 (j ,:) ) . ’
13 a (j ,:) = atemp . ’
14 end
15 a = a /( m * n )
16 a = real ( a )
17 endfunction
Inverse 2D Fast Fourier Transform

71

You might also like