Image Processing MR Biren Patel
Image Processing MR Biren Patel
Image Processing
by Mr Biren Patel
Others
Dharmsinh Desai University1
Solutions provided by
Mr Biren Patel
Others
D.D. University
July 5, 2021
5 Image Restoration 37
7 Image Compression 63
9 Image Segmentation 76
10 Wavelets 81
2
List of Experiments
3
Experiment: 1
1
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 //
6 //
7 clc // t o c l e a r command window .
8 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
9 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
10
11
12
13 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
14 i = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; // R e a d i n g
from sub−d i r e c t o r y o f i m a g e s .
15
16 b = rgb2gray ( i ) ; // t o c o n v e r t an image i n t o grayscale
image .
4
17 imwrite (b , ’ 3 . j p g ’ ) ; // t o w r i t e an image .
18 info = imfinfo ( ’ 3 . j p g ’ ) ; // t o g e t i n f o r m a t i o n l i k e s i z e
, width , h e i g h t e t c u s i n g i m f i n f o ( ) f u n c t i o n .
19 disp ( info . Width ) ; // i m f i n f o s t o r e d a t a i n t o i n f o
v a r i a b l e and d i s p ( ) i s f u n c t i o n t o u s e f o r
d i s p l a y i n g c o n t e n t o f argument i n S c i l a b c o n s o l e .
20 disp ( info . Filename ) ;
21 disp ( info . FileSize ) ;
22 disp ( info . Width ) ;
23 disp ( info . Height ) ;
24 disp ( info . BitDepth ) ;
25 disp ( info . ColorType ) ;
26
27 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
28 i = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; // R e a d i n g
from sub−d i r e c t o r y o f i m a g e s
29 ShowColorImage (i , ’ O r i g i n a l Image ’ ) ; //To show a c o l o r
image i n g r a p h i c a l window u s i n g ” ShowColorImage
( ) ” f u n c t i o n . one can u s e imshow f u n c t i o n which
u s e s t k image show w i d g e t f o r d i s p l a y i n g an image
.
30 title ( ’ O r i g i n a l Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
31
32
33 im = imresize (i ,1.3) ; //To r e s i z e an image , 1 . 3 i s
scale factor
34 ShowColorImage ( im , ’ R e s i z e d Image ’ ) ; //To show an
image i n g r a p h i c a l window u s i n g ” ShowImage ( ) ”
f u n c t i o n . one can u s e imshow f u n c t i o n which u s e
t k image show w i d g e t f o r d i s p l a y i n g an image .
35 title ( ’ R e s i z e d Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
36
37 im1 = imresize (i ,1.3 , ’ b i l i n e a r ’ ) ; // t o r e s i z e an image
. T h i r d p a r a m e t e r i s u s e d f o r s e l e c t i n g one
5
method .
38 ShowColorImage ( im1 , ’ R e s i z e d Image u s i n g b i l i n e a r
i n t e r p o l a t i o n ’ ) ; //To show an image i n g r a p h i c a l
window u s i n g ” ShowImage ( ) ” f u n c t i o n . one can u s e
imshow f u n c t i o n which u s e t k image show w i d g e t
f o r d i s p l a y i n g an image .
39 title ( ’ R e s i z e d Image u s i n g b i l i n e a r i n t e r p o l a t i o n ’ ) ;
// t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
40
41
42 im2 = imresize (i ,1.3 , ’ b i c u b i c ’ ) ; // t o r e s i z e an image
43 ShowColorImage ( im2 , ’ R e s i z e d Image u s i n g b i c u b i c
i n t e r p o l a t i o n ’ ) ; //To show an image i n g r a p h i c a l
window u s i n g ” ShowImage ( ) ” f u n c t i o n . one can u s e
imshow f u n c t i o n which u s e t k image show w i d g e t
f o r d i s p l a y i n g an image .
44 title ( ’ R e s i z e d Image u s i n g b i c u b i c i n t e r p o l a t i o n ’ ) ;
// t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
45
46
47 im3 = rgb2gray ( i ) ; //To c o n v e r t an image i n t o
g r a y s c a l e image .
48 figure , ShowImage ( im3 , ’ G r a y s c a l e image ’ ) ; // f i g u r e i s
u s e d t o d i s p l a y i m a g e s i n s e p a r a t e window .
49 title ( ’ G r a y s c a l e Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
50
51 subimage = imcrop ( im2 ,[1 ,1 ,256 ,256]) ; // s u b i m a g e i s
image l e f t a f t e r c r o p p i n g a t 1 , 1 t o p l e f t c o r n e r .
Width o f an image i s 256 and h e i g h t o f an image
i s 256.
52 figure , ShowColorImage ( subimage , ’ Cropped image ’ ) ; //
f i g u r e i s used to d i s p l a y images in s e p a r a t e
window .
53 title ( ’ Cropped Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
6
7
Experiment: 2
21.tif
log.tif
pollen.tif
pollensmall.tif
1
2 //
3 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
4 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
5 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
8
6 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
7 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
8 //OS : Windows 7
9 //
10 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
11 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
12 clc // t o c l e a r command window .
13 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
14 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
15
16
17 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
18 rgb = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; //
R e a d i n g from sub−d i r e c t o r y o f i m a g e s .
19 figure , ShowColorImage ( rgb , ’ O r i g i n a l c o l o r image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
20 title ( ’ O r i g i n a l C o l o r Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
21
22 grayimage = rgb2gray ( rgb ) ; // t o c o n v e r t a RGB image
i n t o g r a y s c a l e image .
23
24 figure , ShowImage ( grayimage , ’ G r a y s c a l e image ’ ) ; //
ShowImage ( ) i s u s e d t o show image , f i g u r e i s
command t o v i e w i m a g e s i n s e p a r a t e window .
25 title ( ’ G r a y s c a l e image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
26
27 function [ negative ]= imagenegative ( b ) // i m a g e n e g a t i v e
( ) i s f u n c t i o n t o f i n d n e g a t i v e o f an image .
28 [ nr nc ]= size ( b ) // t o f i n d d i m e n s i o n o f a
g r a y s c a l e image .
9
29 for i = 1: nr
30 for j = 1: nc
31 negative (i , j ) =255 - b (i , j ) ;
32 end ;
33 end
34 endfunction
35
36 negative = imagenegative ( grayimage ) ;
37 figure , ShowImage ( negative , ’ N e g a t i v e image ’ ) ; //
ShowImage ( ) i s u s e d t o show image , f i g u r e i s
command t o v i e w i m a g e s i n s e p a r a t e window .
38 title ( ’ N e g a t i v e image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
39
40 aaa = ReadImage ( ’ 2 1 . t i f ’ ) ; // ReadImage ( ) is function
d e f i n e d i n IPD t o o l b o x .
41 figure , ShowImage ( aaa , ’ I n t e n s i t y Ramp Image ’ ) ; //
ShowImage ( ) i s u s e d t o show image , figure is
command t o v i e w i m a g e s i n s e p a r a t e window .
42 title ( ’ I n t e n s i t y Ramp Image ’ ) ; // t i t l e ( ) i s used f o r
p r o v i d i n g a t i t l e t o an image .
43
44 graydouble = double ( aaa ) ; // t o c o n v e r t image i n t o
double p r e c i s i o n .
45
46 function [ g1 ]= imadjust (b , lowin , lowout , highin , highout
, gamma1 ) // i m a d j u s t ( ) i s u s e d f o r gamma c o r r e c t i o n
.
47 [ nr nc ]= size ( b ) ;
48 for i = 1: nr
49 for j = 1: nc
50 g1 (i , j ) = lowout +( highout - lowout ) *(( b (i , j )
- lowin ) /( highin - lowin ) ) ^ gamma1 ;
51 end ;
52 end
53 endfunction
54
55 s1 = imadjust ( graydouble ,0.0392 ,0.7843 ,0.589 ,1 ,2) ;
10
56 d = mat2gray ( s1 ) ; // t o c o n v e r t m a t r i x i n t o grayscale
image .
57 figure , ShowImage (d , ’ R e s u l t o f e n h a n c i n g image w i t h
gamma=2 ’ ) ; // ShowImage ( ) i s u s e d t o show image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
58 title ( ’ R e s u l t o f e n h a n c i n g image w i t h gamma=2 ’ ) ; //
t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
59
60
61 aaa = ReadImage ( ’ l o g . t i f ’ ) ; // ReadImage ( ) is function
d e f i n e d i n IPD t o o l b o x .
62 figure , ShowImage ( aaa , ’ Image ’ ) ; // ShowImage ( ) i s u s e d
t o show image , f i g u r e i s command t o v i e w i m a g e s
i n s e p a r a t e window .
63 title ( ’ Image ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a
t i t l e t o an image .
64
65 graydouble = double ( aaa ) ; // t o c o n v e r t image i n t o
double p r e c i s i o n .
66 function [ log1 ]= logtransform (b , c ) ; // t o p e r f o r m l o g
t r a n s f o r m a t i o n s on an image
67 [ nr nc ]= size ( b ) ;
68 for i = 1: nr
69 for j = 1: nc
70 log1 (i , j ) = c * log (1+ b (i , j ) ) ;
71 end
72 end
73 endfunction
74
75 s2 = logtransform ( graydouble ,1) ;
76
77 ans1 =255* uint8 ( s2 ) ;
78 figure , ShowImage ( ans1 , ’ R e s u l t o f a p p l y i n g l o g
t r a n s f o r m a t i o n s w i t h c=1 ’ ) ; // ShowImage ( ) i s u s e d
t o show image , f i g u r e i s command t o v i e w i m a g e s
i n s e p a r a t e window .
11
79 title ( ’ R e s u l t o f a p p l y i n g l o g t r a n s f o r m a t i o n s w i t h c
=1 ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o
an image .
80
81
82
83
84 aaa = ReadImage ( ’ p o l l e n . t i f ’ ) ; // ReadImage ( ) is
f u n c t i o n d e f i e n e d i n IPD t o o l b o x .
85 figure , ShowImage ( aaa , ’ w a s h e d o u t p o l l e n i m a g e ’ ) ; //
ShowImage ( ) i s u s e d t o show image , f i g u r e i s
command t o v i e w i m a g e s i n s e p a r a t e window .
86 title ( ’ w a s h e d o u t p o l l e n i m a g e ’ ) ; // t i t l e ( ) i s u s e d
f o r p r o v i d i n g a t i t l e t o an image .
87 bb = double ( aaa ) ;
88
89 function [ ans ]= contraststretch (b , r1 , s1 , r2 , s2 ) ; // t o
p e r f o r m c o n t r a s t s t r e t c h i n g on a image .
90 [ nr nc ]= size ( b ) ;
91 m1 = s1 / r1 ;
92 m2 =( r2 - r1 ) /( s2 - s1 ) ;
93 m3 =(255 - s2 ) /(255 - r2 ) ;
94 for i = 1: nr
95 for j = 1: nc
96 if ( b (i , j ) <= r1 ) then
97 ans (i , j ) = m1 * b (i , j ) ;
98 end
99
100 if ( b (i , j ) > r1 ) then
101 if ( b (i , j ) <= r2 ) then
102 ans (i , j ) = m2 * b (i , j ) +( r2 * s1 - r1 * s2 ) /( r2 -
r1 ) ;
103 end
104 end
105 if ( b (i , j ) > r2 ) then
106 ans (i , j ) = m3 * b (i , j ) +(255* s2 - r2 *255)
/(255 - r2 ) ;
107 end
12
108 end
109 end
110 endfunction
111 s2 = contraststretch ( bb ,70 ,40 ,150 ,200) ;
112 ans2 = mat2gray ( s2 ) ;
113 figure , ShowImage ( ans2 , ’ R e s u l t o f c o n t r a s t stretching
’ ) ; // ShowImage ( ) i s u s e d t o show image , f i g u r e i s
command t o v i e w i m a g e s i n s e p a r a t e window .
114 title ( ’ R e s u l t o f c o n t r a s t s t r e t c h i n g ’ ) ; // t i t l e ( ) i s
u s e d f o r p r o v i d i n g a t i t l e t o an image .
115
116
117 aa = ReadImage ( ’ p o l l e n s m a l l . t i f ’ ) ; //To r e a d an image .
118 a = rgb2gray ( aa ) ; // t o c o n v e r t an image i n t o g r a y s c a l e
119 b = double ( a ) ;
120 [ nr , nc ]= size ( a )
121
122
123 [ count , cells ]= imhist ( a ) ; // i m h i s t ( ) i s u s e d t o
obtain histogram .
124 scf (10) ; imhist (a ,256 , ’ ’ ) ; // s c f ( ) i s u s e d t o s e t
c u r r e n t g r a p h i c window .
125
126
127
128 bit =8; // l i s v a r i a b l e t o u s e d f i n d
p o s s i b l e i n t e n s i t y l e v e l s o f an image .
129 l =2^ bit ;
130
131 r = zeros (1 , l ) ; // rK i s used f o r input i n t e n s i t y l e v e l s
f o r an image .
132 n = zeros (1 , l ) ; //nK i s u s e d f o r number o f p i x e l s ,
which have d i f f e r e n t i n t e n s i t y l e v e l s .
133 p = zeros (1 , l ) ; // pk i s p r o b a b i l i t y o f o c c u r r e n c e o f
i n t e n s i t y l e v e l r k i n a d i g i t a l image .
134
135 for i = 1: l // f o r
loop i s used to d e f i n e d i f f e r e n t
intensity levels .
13
136 r (1 , i ) =i -1;
137 end
138
139
140 for k =1: l // f o r l o o p i s used to f i n d o c c u r r e n c e o f rk
intensity level
141 for i = 1: nr
142 for j = 1: nc
143 if a (i , j ) == r (1 , k ) then
144 n (1 , k ) = n (1 , k ) +1;
145 end
146 end
147 end
148 end
149
150 p = n /( nr * nc ) ; // t o f i n d probability of occurrence of
i n t e n s i t y l e v e l r k i n a d i g i t a l image .
151
152
153 for k =1: l // f o r loop i s used to f i n d transformation ’ s
result .
154 temp =0;
155 for j =1: k
156 temp = temp +255* p (1 , j ) ;
157 end
158 if ( ceil ( temp ) - temp >0.5) then
159 s (1 , k ) = floor ( temp ) ;
160 else
161 s (1 , k ) = ceil ( temp ) ;
162 end
163 end
164
165 for k =1: l // f o r
l o o p i s u s e d t o a s s i g n new i n t e n s i t y
levels .
166 for i = 1: nr
167 for j = 1: nc
168 if ( b (i , j ) == r (1 , k ) ) then
169 dd (i , j ) = s (1 , k ) ;
14
170 end
171 end
172 end
173 end
174 ddd = uint8 ( dd ) ;
175 [ count , cells ]= imhist ( ddd ,256) ; // i m h i s t ( ) i s used to
g e t 256 b i n s o f h i s t o g r a m .
176 clf ;
177 scf (11) ; imhist ( ddd ,256) ; //To show h i s t o g r a m
178 scf (12) ; ShowImage ( ddd , ’ R e s u l t o f h i s t o g r a m
e q u a l i z a t i o n ’ ) ; // ShowImage ( ) i s u s e d t o show
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
179 title ( ’ R e s u l t o f h i s t o g r a m e q u a l i z a t i o n ’ ) ; // t i t l e ( )
i s u s e d f o r p r o v i d i n g a t i t l e t o an image .
15
Experiment: 3
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 clc // t o c l e a r command window .
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14
15
16 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
17 rgb = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; //
16
R e a d i n g from sub−d i r e c t o r y o f i m a g e s .
18 figure , ShowColorImage ( rgb , ’ O r i g i n a l c o l o r image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
19 title ( ’ O r i g i n a l C o l o r Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
20
21 im = rgb2gray ( rgb ) ; // t o c o n v e r t a RGB image i n t o
g r a y s c a l e image .
22
23 figure , ShowImage ( im , ’ G r a y s c a l e image ’ ) ; // ShowImage ( )
i s u s e d t o show image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
24 title ( ’ G r a y s c a l e image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
25
26 filter = fspecial ( ’ a v e r a g e ’ ,3) ; // f s p e c i a l ( ) i s u s e d t o
create special f i l t e r s .
27 imf = imfilter ( im , filter ) ;
28 figure , ShowImage ( imf , ’ F i l t e r e d G r a y s c a l e image u s i n g
3∗3 a v e r a g e mask ’ ) ; // ShowImage ( ) i s u s e d t o show
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
29 title ( ’ F i l t e r d G r a y s c a l e image u s i n g 3∗3 a v e r a g e
mask ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
30
31
32 filter = fspecial ( ’ a v e r a g e ’ ,9) ; // f s p e c i a l ( ) i s u s e d t o
create special f i l t e r s .
33 imf = imfilter ( im , filter ) ;
34 figure , ShowImage ( imf , ’ F i l t e r d G r a y s c a l e image u s i n g
9∗9 a v e r a g e mask ’ ) ; // ShowImage ( ) i s u s e d t o show
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
35 title ( ’ F i l t e r d G r a y s c a l e image u s i n g 9∗9 a v e r a g e
mask ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
17
t o an image .
36
37
38 filter = fspecial ( ’ a v e r a g e ’ ,16) ; // f s p e c i a l ( ) i s u s e d
to c r e a t e s p e c i a l f i l t e r s .
39 imf = imfilter ( im , filter ) ;
40 figure , ShowImage ( imf , ’ F i l t e r d G r a y s c a l e image u s i n g
1 6 ∗ 1 6 a v e r a g e mask ’ ) ; // ShowImage ( ) i s u s e d t o
show image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
41 title ( ’ F i l t e r d G r a y s c a l e image u s i n g 1 6 ∗ 1 6 a v e r a g e
mask ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
42
43
44 imn = imnoise ( im , ’ g a u s s i a n ’ ,0.01 ,0.02) ; // i m n o i s e ( )
i s u s e d t o add n o i s e i n an image . ’ g a u s s i a n ’ i s
u s e d a s a s e c o n d argument t o f u n c t i o n f o r a d d i n g
Gasussian noise
45 figure , ShowImage ( imn , ’ Image c o r r u p t e d by G a u s s i a n
n o i s e ’ ) ; // ShowImage ( ) i s u s e d t o show image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
46 title ( ’ Image c o r r u p t e d by G a u s s i a n n o i s e ’ ) ; // t i t l e
( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an image .
47
48
49 imn = imnoise ( im , ’ s p e c k l e ’ ) ; // i m n o i s e ( ) i s used to
add n o i s e i n an image . ’ s p e c k l e ’ i s u s e d a s a
s e c o n d argument t o f u n c t i o n f o r a d d i n g S p e c k l e
n o i s e . T h i r d argument i s u s e d f o r v a r i o u s .
50 figure , ShowImage ( imn , ’ Image c o r r u p t e d by S p e c k l e
n o i s e ’ ) ; // ShowImage ( ) i s u s e d t o show image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
51 title ( ’ Image c o r r u p t e d by s p e c k l e n o i s e ’ ) ; // t i t l e ( )
i s u s e d f o r p r o v i d i n g a t i t l e t o an image .
52
18
53 imns = imnoise ( im , ’ s a l t & p e p p e r ’ ) ; // i m n o i s e ( ) i s
u s e d t o add n o i s e i n an image . ’ g a u s s i a n ’ i s u s e d
a s a s e c o n d argument t o f u n c t i o n f o r a d d i n g S a l t
& Pepper n o i s e . T h i r d argument i s u s e d f o r n o i s e
density .
54 figure , ShowImage ( imns , ’ Image c o r r u p t e d by s a l t and
p e p p e r n o i s e ’ ) ; // ShowImage ( ) i s u s e d t o show
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
55 title ( ’ Image c o r r u p t e d by s a l t and p e p p e r n o i s e ’ ) ;
// t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
56
57 filter = fspecial ( ’ a v e r a g e ’ ,3) ; // f s p e c i a l ( ) i s u s e d t o
create special f i l t e r s .
58 imf = imfilter ( imns , filter ) ;
59 figure , ShowImage ( imf , ’ F i l t e r d G r a y s c a l e image u s i n g
3∗3 a v e r a g e mask ’ ) ; // ShowImage ( ) i s u s e d t o show
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
60 title ( ’ F i l t e r d G r a y s c a l e image u s i n g 1 6 ∗ 1 6 a v e r a g e
mask ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
61
62 subim = imcrop ( imns , [1 , 1 , 300 , 300]) ; // i m c r o p ( ) i s
u s e d t o c r o p an image .
63 function [ resimg ]= median2 ( image , filtersize , type1 ) //
median2 ( ) i s f u n c t i o n t o f i l t e r an image .
64 size1 = filtersize ;
65 [ nr , nc ]= size ( image ) ;
66 if type1 == ” z e r o ” then
67 temp = zeros ( nr +2* floor ( size1 /2) , nc +2* floor (
size1 /2) ) ;
68 end
69 temp = zeros ( nr +2* floor ( size1 /2) , nc +2* floor ( size1
/2) ) ;
70
71 temp ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil ( size1
19
/2) : nc + ceil ( size1 /2) -1) = subim (1: $ ,1: $ )
72 for i = ceil ( size1 /2) : nr + ceil ( size1 /2) -1
73 for j = ceil ( size1 /2) : nc + ceil ( size1 /2) -1
74 t = temp (i - floor ( size1 /2) :1: i + floor ( size1
/2) ,j - floor ( size1 /2) :1: j + floor ( size1
/2) )
75 y = gsort ( t ) ;
76 temp (i , j ) = median ( y ) ;
77 end
78 end
79
80 nn = temp ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil (
size1 /2) : nc + ceil ( size1 /2) -1)
81 resimg = mat2gray ( nn )
82
83 endfunction
84
85 nnn = median2 ( subim ,3 , ” z e r o ” ) ; // median2 ( ) i s a 2−D
median f i l t e r , s e c o n d argument i s u s e d f o r f i l t e r
s i z e . I t s h o u l d be odd i n t e g e r . T h i r d argument
i s used f o r z e r o padding .
86 figure , ShowImage ( nnn , ’ F i l t e r e d image u s i n g 3∗3
median f i l t e r ’ ) ; // ShowImage ( ) i s u s e d t o show
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
87 title ( ’ F i l t e r e d image u s i n g 3∗3 median f i l t e r ’ ) ; //
t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
88
89
90
91 F = fspecial ( ’ s o b e l ’ ) // f s p e c i a l ( ’ s o b e l ’ ) r e t u r n s a 3 x3
horizontal edges sobel filter .
92 imf = imfilter ( im , F ) ;
93 figure , ShowImage ( imf , ’ F i l t e r d G r a y s c a l e image u s i n g
3∗3 a v e r a g e mask ’ ) ; // ShowImage ( ) i s u s e d t o show
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
20
94 title ( ’ F i l t e r d G r a y s c a l e image u s i n g 3∗3 a v e r a g e
mask ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
95
96 F = fspecial ( ’ p r e w i t t ’ ) // f s p e c i a l ( ’ p r e w i t t ’ ) returns a
3 x3 h o r i z o n t a l e d g e s p r e w i t t f i l t e r .
97 imf = imfilter ( im , F ) ;
98 figure , ShowImage ( imf , ’ F i l t e r i n g G r a y s c a l e image
u s i n g 3 x3 h o r i z o n t a l e d g e s p r e w i t t f i l t e r . ’ ) ; //
ShowImage ( ) i s u s e d t o show image , f i g u r e i s
command t o v i e w i m a g e s i n s e p a r a t e window .
99 title ( ’ F i l t e r i n g G r a y s c a l e image u s i n g 3 x3
h o r i z o n t a l e d g e s p r e w i t t f i l t e r . ’ ) ; // t i t l e ( ) i s
u s e d f o r p r o v i d i n g a t i t l e t o an image .
100
101 F = fspecial ( ’ g a u s s i a n ’ ) // f s p e c i a l ( ’ g a u s s i a n ’ , hsize ,
sigma ) r e t u r n s a Gaussian lowpass f i l t e r .
102 imf = imfilter ( im , F ) ;
103 figure , ShowImage ( imf , ’ F i l t e r i n g G r a y s c a l e image
u s i n g G a u s s i a n l o w p a s s f i l t e r . ’ ) ; // ShowImage ( )
i s u s e d t o show image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
104 title ( ’ F i l t e r i n g G r a y s c a l e image u s i n g G a u s s i a n
l o w p a s s f i l t e r . ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
105
106 F = fspecial ( ’ l a p l a c i a n ’ ) // f s p e c i a l ( ’ l a p l a c i a n ’ , a l p h a )
r e t u r n s a 3−by−3 L a p l a c i a n f i l t e r . The r e t u r n e d
f i l t e r i s [ a l p h a , 1−a l p h a , a l p h a ; 1−a l p h a , −4 , 1−
a l p h a ; a l p h a , 1−a l p h a , a l p h a ] / ( a l p h a +1) . The
d e f a u l t value f o r alpha i s 0 . 2 .
107 imf = imfilter ( im , F ) ;
108 figure , ShowImage ( imf , ’ F i l t e r i n g G r a y s c a l e image
u s i n g 3 x3 L a p l a c i a n f i l t e r . ’ ) ; // ShowImage ( ) i s
u s e d t o show image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
109 title ( ’ F i l t e r i n g G r a y s c a l e image u s i n g 3 x3
L a p l a c i a n f i l t e r . ’ ) ; // t i t l e ( ) i s u s e d f o r
21
providing a t i t l e t o an image .
110
111 F = fspecial ( ’ l o g ’ ) // f s p e c i a l ( ’ l o g ’ , h s i z e , s i g m a )
r e t u r n s a L a p l a c i a n o f G a u s s i a n f i l t e r . The s i z e
o f r e t u r n e d f i l t e r i s d e t e r m i n e d by p a r a m e t e r
h s i z e . h s i z e can be a 1 x2 v e c t o r which i n d i c a t e
t h e r o w s and c o l u m n s o f F . I f h s i z e i s a s c a l a r ,
F i s a s q u a r e m a t r i x . The d e f a u l t v a l u e f o r h s i z e
i s [ 5 , 5 ] ; the d e f a u l t v a l u e f o r sigma i s 0 . 5 .
112 imf = imfilter ( im , F ) ;
113 figure , ShowImage ( imf , ’ F i l t e r d G r a y s c a l e image u s i n g
L a p l a c i a n o f G a u s s i a n f i l t e r . ’ ) ; // ShowImage ( ) i s
u s e d t o show image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
114 title ( ’ F i l t e r d G r a y s c a l e image u s i n g L a p l a c i a n o f
G a u s s i a n f i l t e r . ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
22
Experiment: 4
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 clc // t o c l e a r command window .
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14
15 function [ H ]= lowpassfilter ( type1 ,M ,N , D0 , n ) //
l o w p a s s f i l t e r i s u s e d t o f i l t e r an image .
16 u =0:( M -1) ;
23
17 v =0:( N -1) ;
18 idx = find (u > M /2) ;
19 u ( idx ) = u ( idx ) -M ;
20 idy = find (v > N /2) ;
21 v ( idy ) = v ( idy ) -N ;
22 [U , V ]= meshgrid (v , u ) ;
23 D = sqrt ( U .^2+ V .^2) ;
24 select type1
25 case ’ ideal ’ then
26 H = double (D <= D0 ) ;
27 case ’ butterworth ’ then
28 if argn (2) ==4 then
29 n =1;
30 end
31 t1 = D ./ D0
32 t2 = t1 .^(2* n )
33 t3 =1+ t2 ;
34 [ nr1 , nc1 ]= size ( t3 ) ;
35 a = ones ( nr1 , nc1 ) ;
36 H = a ./ t3 ;
37 case ’ gaussian ’ then
38 H = exp ( -( D .^2) ./(2*( D0 ^2) ) ) ;
39 else
40 disp ( ’ U n k n o w n f i l t e r t y p e . ’ )
41 end
42 endfunction
43
44 function [ H1 ]= highpassfilter ( type2 ,M ,N , D0 , n ) //
h i g h p a s s f i l t e r ( ) i s u s e d t o f i l t e r an image .
45 if argn (2) ==4 then
46 n =1;
47 end
48 h = lowpassfilter ( type2 ,M ,N , D0 , n ) ;
49 H1 =1 - h ;
50 endfunction
51
52 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
24
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
53 rgb = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; //
R e a d i n g from sub−d i r e c t o r y o f i m a g e s .
54 figure , ShowColorImage ( rgb , ’ O r i g i n a l C o l o r image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
55 title ( ’ O r i g i n a l C o l o r Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
56
57 im = rgb2gray ( rgb ) ; // t o c o n v e r t a RGB image i n t o
g r a y s c a l e image .
58
59
60 f = double ( im ) ;
61 [M , N ]= size ( f ) ;
62
63 h = fft2 ( f ) ; // f f t 2 ( ) i s u s e d t o f i n d 2− D i m e n s i o n a l
F a s t F o u r i e r T r a n s f o r m o f an m a t r i x
64 i = log (1+ abs ( h ) ) ;
65 in = fftshift ( i ) ; // f f t s h i f t ( ) i s u s e d t o r e a r r a n g e t h e
f f t o u t p u t , moving t h e z e r o f r e q u e n c y t o t h e
c e n t e r o f the spectrum .
66 inm = mat2gray ( in )
67 figure , ShowImage ( inm , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
68 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
69
70
71 filt = lowpassfilter ( ’ i d e a l ’ ,M ,N ,5) ;
72 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
73 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
74 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
25
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
75 filt2 = mat2gray ( filt1 ) ;
76 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
77 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
78
79 mm = mat2gray ( n1 ) ;
80 figure , ShowImage ( mm , ’ I d e a l Lowpass f i l t e r e d i m a g e [
c u t o f f f r e q =5] ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
81 title ( ’ I d e a l L o w p a s s f i l t e r e d image [ c u t o f f f r e q =5] ’ ) ;
// t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
82
83
84
85 filt = lowpassfilter ( ’ i d e a l ’ ,M ,N ,50) ;
86 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
87 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
88 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
89 filt2 = mat2gray ( filt1 ) ;
90 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
91 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
92
93 mm = mat2gray ( n1 ) ;
26
94 figure , ShowImage ( mm , ’ I d e a l Lowpass f i l t e r e d image [
c u t o f f f r e q =5] ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
95 title ( ’ I d e a l Lowpass f i l t e r e d image [ c u t o f f f r e q =50] ’ )
; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
96
97
98 filt = lowpassfilter ( ’ i d e a l ’ ,M ,N ,200) ;
99 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
100 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
101 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
102 filt2 = mat2gray ( filt1 ) ;
103 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
104 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
105
106 mm = mat2gray ( n1 ) ;
107 figure , ShowImage ( mm , ’ I d e a l Lowpass f i l t e r e d image [
c u t o f f f r e q =200] ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
108 title ( ’ I d e a l Lowpass f i l t e r e d image [ c u t o f f f r e q =200] ’
) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
109
110
111
112
113 filt = lowpassfilter ( ’ b u t t e r w o r t h ’ ,M ,N ,5) ;
27
114 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
115 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
116 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
117 filt2 = mat2gray ( filt1 ) ;
118 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
119 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
120
121 mm = mat2gray ( n1 ) ;
122 figure , ShowImage ( mm , ’ b u t t e r w o r t h Lowpass filtered
image [ c u t o f f f r e q =5] ’ ) ; // ShowColorImage ( ) i s u s e d
t o show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
123 title ( ’ b u t t e r w o r t h Lowpass f i l t e r e d image [ c u t o f f f r e q
=5] ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o
an image .
124
125
126
127 filt = lowpassfilter ( ’ b u t t e r w o r t h ’ ,M ,N ,50) ;
128 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
129 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
130 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
131 filt2 = mat2gray ( filt1 ) ;
132 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
28
window .
133 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
134
135 mm = mat2gray ( n1 ) ;
136 figure , ShowImage ( mm , ’ b u t t e r w o r t h Lowpass
f i l t e r e d i m a g e [ c u t o f f f r e q =5] ’ ) ; // ShowColorImage ( )
i s u s e d t o show c o l o r image , f i g u r e i s command t o
v i e w i m a g e s i n s e p a r a t e window .
137 title ( ’ b u t t e r w o r t h Lowpass f i l t e r e d i m a g e [ c u t o f f f r e q
=50] ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
138
139
140 filt = lowpassfilter ( ’ b u t t e r w o r t h ’ ,M ,N ,200) ;
141 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
142 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
143 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
144 filt2 = mat2gray ( filt1 ) ;
145 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
146 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
147
148 mm = mat2gray ( n1 ) ;
149 figure , ShowImage ( mm , ’ b u t t e r w o r t h Lowpass
f i l t e r e d i m a g e [ c u t o f f f r e q =200] ’ ) ; // ShowColorImage
( ) i s u s e d t o show c o l o r image , f i g u r e i s command
t o v i e w i m a g e s i n s e p a r a t e window .
150 title ( ’ b u t t e r w o r t h Lowpass f i l t e r e d i m a g e [ c u t o f f f r e q
=200] ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
29
151
152
153 filt = lowpassfilter ( ’ b u t t e r w o r t h ’ ,M ,N ,5) ;
154 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
155 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
156 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
157 filt2 = mat2gray ( filt1 ) ;
158 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
159 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
160
161 mm = mat2gray ( n1 ) ;
162 figure , ShowImage ( mm , ’ g a u s s i a n Lowpass f i l t e r e d image
[ c u t o f f f r e q =5] ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
163 title ( ’ g a u s s i a n Lowpass f i l t e r e d image [ c u t o f f f r e q =5]
’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
164
165
166
167 filt = lowpassfilter ( ’ g a u s s i a n ’ ,M ,N ,50) ;
168 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
169 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
170 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
171 filt2 = mat2gray ( filt1 ) ;
30
172 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
173 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
174
175 mm = mat2gray ( n1 ) ;
176 figure , ShowImage ( mm , ’ g a u s s i a n Lowpass f i l t e r e d image
[ c u t o f f f r e q =5] ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
177 title ( ’ g a u s s i a n Lowpass f i l t e r e d image [ c u t o f f f r e q
=50] ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
178
179
180 filt = lowpassfilter ( ’ g a u s s i a n ’ ,M ,N ,200) ;
181 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
182 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
183 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
184 filt2 = mat2gray ( filt1 ) ;
185 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
186 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
187
188 mm = mat2gray ( n1 ) ;
189 figure , ShowImage ( mm , ’ g a u s s i a n Lowpass f i l t e r e d image
[ c u t o f f f r e q =200] ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
31
190 title ( ’ g a u s s i a n Lowpass f i l t e r e d image [ c u t o f f f r e q
=200] ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
191
192
193
194 clc // t o c l e a r command window .
195 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
196 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
197
198 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
199 rgb = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; //
R e a d i n g from sub−d i r e c t o r y o f i m a g e s .
200 figure , ShowColorImage ( rgb , ’ O r i g i n a l c o l o r image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
201 title ( ’ O r i g i n a l C o l o r Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
202
203 im = rgb2gray ( rgb ) ; // t o c o n v e r t a RGB image i n t o
g r a y s c a l e image .
204
205
206 f = double ( im ) ;
207 [M , N ]= size ( f ) ;
208
209 h = fft2 ( f ) ; // f f t 2 ( ) i s u s e d t o f i n d 2− D i m e n s i o n a l
F a s t F o u r i e r T r a n s f o r m o f an m a t r i x
210 i = log (1+ abs ( h ) ) ;
211 in = fftshift ( i ) ;
212 inm = mat2gray ( in )
213 figure , ShowImage ( inm , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
32
window .
214 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
215
216
217 filt = highpassfilter ( ’ i d e a l ’ ,M ,N ,5) ;
218 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
219 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
220 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
221 filt2 = mat2gray ( filt1 ) ;
222 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
223 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
224
225 mm = mat2gray ( n1 ) ;
226 figure , ShowImage ( mm , ’ I d e a l highpass filteredimage [
c u t o f f f r e q =5] ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
227 title ( ’ I d e a l h i g h p a s s f i l t e r e d i m a g e [ c u t o f f f r e q =5] ’ ) ;
// t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
228
229
230
231 filt = highpassfilter ( ’ i d e a l ’ ,M ,N ,50) ;
232 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
233 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
234 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
33
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
235 filt2 = mat2gray ( filt1 ) ;
236 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
237 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
238
239 mm = mat2gray ( n1 ) ;
240 figure , ShowImage ( mm , ’ I d e a l highpass filteredimage [
c u t o f f f r e q =50] ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
241 title ( ’ I d e a l h i g h p a s s f i l t e r e d i m a g e [ c u t o f f f r e q =50] ’ )
; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
242
243
244 filt = highpassfilter ( ’ i d e a l ’ ,M ,N ,200) ;
245 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
246 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
247 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
248 filt2 = mat2gray ( filt1 ) ;
249 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
250 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
251
252 mm = mat2gray ( n1 ) ;
253 figure , ShowImage ( mm , ’ I d e a l highpass filteredimage [
34
c u t o f f f r e q =200] ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
254 title ( ’ I d e a l h i g h p a s s f i l t e r e d i m a g e [ c u t o f f f r e q =200] ’
) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
255
256
257
258 filt = highpassfilter ( ’ b u t t e r w o r t h ’ ,M ,N ,200) ;
259 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
260 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
261 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
262 filt2 = mat2gray ( filt1 ) ;
263 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
264 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
265
266 mm = mat2gray ( n1 ) ;
267 figure , ShowImage ( mm , ’ b u t t e r w o r t h h i g h p a s s filtered
image [ c u t o f f f r e q =200] ’ ) ; // ShowColorImage ( ) i s
u s e d t o show c o l o r image , f i g u r e i s command t o
v i e w i m a g e s i n s e p a r a t e window .
268 title ( ’ b u t t e r w o r t h h i g h p a s s f i l t e r e d image [
c u t o f f f r e q =200] ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g
a t i t l e t o an image .
269
270
271
272 filt = highpassfilter ( ’ g a u s s i a n ’ ,M ,N ,200) ;
273
35
274 n = filt .* h ; // c o n v o l v i n g an image w i t h a two
dimensional f i l t e r .
275 n1 = real ( ifft ( n ) ) ; // i f f t ( ) i s u s e d t o f i n d i n v e r s e 2−
D i m e n s i o n a l F a s t f o u r i e r t r a n s f o r m o f an m a t r i x
276 filt1 = fftshift ( filt ) ; // f f t s h i f t ( ) i s u s e d t o
r e a r r a n g e t h e f f t o u t p u t , moving t h e z e r o
frequency to the c e n t e r o f the spectrum .
277 filt2 = mat2gray ( filt1 ) ;
278 figure , ShowImage ( filt2 , ’ F r e q u e n c y Spectrum ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
279 title ( ’ F r e q u e n c y Spectrum ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
280
281 mm = mat2gray ( n1 ) ;
282 figure , ShowImage ( mm , ’ g a u s s i a n h i g h p a s s filtered
image [ c u t o f f f r e q =200] ’ ) ; // ShowColorImage ( ) i s
u s e d t o show c o l o r image , f i g u r e i s command t o
v i e w i m a g e s i n s e p a r a t e window .
283 title ( ’ g a u s s i a n h i g h p a s s f i l t e r e d image [ c u t o f f f r e q
=200] ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
36
Experiment: 5
Image Restoration
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 clc // t o c l e a r command window .
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14
15 function imn = imnoise22 ( im , noise_type , param1 ,
param2 )
16 imtype = typeof ( im (1) ) ;
17 im = im2double ( im ) ;
18 im = matrix ( im (:) , size ( im ) ) ;
37
19 // G a u s s i a n n o i s e
20 if ( noise_type == ’ g a u s s i a n ’ | noise_type == ’
G a u s s i a n ’ ) then
21 if ~ exists ( ’ param1 ’ , ’ l o c a l ’ )
22 m =0
23 else
24 m = param1
25 end
26 if ~ exists ( ’ param2 ’ , ’ l o c a l ’ )
27 v =0.01
28 else
29 v = param2
30 end
31
32 old_rand_gen = rand ( ’ i n f o ’ ) ;
33 rand ( ’ n o r m a l ’ ) ;
34
35 imn = im + sqrt ( v ) * rand ( im ) + m ;
36
37 rand ( old_rand_gen ) ;
38
39
40 elseif noise_type == ’ l o c a l v a r ’
41 if argn (2) < 3 then
42 error ( ’ Too f e w a r g u m e n t s f o r n o i s e t y p e ’ ’
l o c a l v a r ’ ’ . ’ );
43 elseif argn (2) == 3 then
44 if ( or ( size ( im ) <> size ( param1 ) ) ) then
45 error ( ” The f i r s t p a r a m e t e r f o r ’ ’ l o c a l v a r ’ ’
s h o u l d have t h e same ” +...
46 ” s i z e w i t h t h e i n p u t image . ” ) ;
47 end
48
49 old_rand_gen = rand ( ’ i n f o ’ ) ;
50 rand ( ’ n o r m a l ’ ) ;
51 imn = matrix ( im (:) , size ( im ) ) + sqrt ( param1 ) .*
rand ( im ) ;
52 rand ( old_rand_gen ) ;
38
53
54 elseif argn (2) == 4 then
55 if ( or ( size ( param1 ) <> size ( param2 ) ) ) then
56 error ( ” The two p a r a m e t e r s f o r ’ ’ l o c a l v a r ’ ’
s h o u l d have t h e same s i z e . ” ) ;
57 end
58
59 minp1 = min ( param1 ) ;
60 maxp1 = max ( param1 ) ;
61 imn = min ( max ( im (:) , minp1 ) , maxp1 ) ; //max ( im ,
minp1 ) can ’ t work
62 // b e c a u s e
im i s a
hypermat
.
63 imn = matrix ( interp1 ( param1 (:) , param2 (:) , imn ) ,
size ( im ) ) ;
64
65 old_rand_gen = rand ( ’ i n f o ’ ) ;
66 rand ( ’ n o r m a l ’ ) ;
67 imn = im + sqrt ( imn ) .* rand ( im ) ;
68 rand ( old_rand_gen ) ;
69 end
70
71 // s a l t & p e p p e r n o i s e
72 elseif noise_type == ’ s a l t & p e p p e r ’ | noise_type
== ’ s a l t and p e p p e r ’
73 if ~ exists ( ’ param1 ’ , ’ l o c a l ’ )
74 d =0.05
75 else
76 d = param1
77
78 if ( d < 0 | d > 1) then
79 error ( ” The p a r a m e t e r f o r ’ ’ s a l t & p e p p e r ’ ’ n o i s e
should i n range [ 0 , 1 ] . ”);
80 end
81 end
82
39
83 old_rand_gen = rand ( ’ i n f o ’ ) ;
84 rand ( ’ u n i f o r m ’ ) ;
85 prob = rand ( im ) ;
86 rand ( old_rand_gen ) ;
87
88 imn = im ;
89 imn ( prob < d /2) = 0;
90 imn ( prob >= d /2 & prob < d ) = 1;
91
92 elseif noise_type == ’ s p e c k l e ’
93 if ~ exists ( ’ param1 ’ , ’ l o c a l ’ )
94 v =0.04
95 else
96 v = param1
97 if ( v < 0) then
98 error ( ” The p a r a m e t e r f o r ’ ’ s p e c k l e ’ ’ n o i s e
s h o u l d >=0.” ) ;
99 end
100 end
101
102 old_rand_gen = rand ( ’ i n f o ’ ) ;
103 rand ( ’ u n i f o r m ’ ) ;
104 imn = im + im .* ( sqrt ( v ) * ( rand ( im ) -0.5) ) ;
105 rand ( old_rand_gen ) ;
106
107 elseif noise_type == ’ p o i s s o n ’
108 error ( ’ Not y e t i m p l e m e n t e d ’ ) ;
109 else
110 error ( ’ I n v a l i d n o i s e t y p e . ’ ) ;
111 end
112
113 // c o n v e r t h e o u t p u t image t o t h e same t y p e a s t h e
i n p u t image
114
115 select imtype
116 case ’ u i n t 8 ’ then
117 imn = im2uint8 ( imn ) ;
118 case ’ i n t 8 ’ then
40
119 imn = im2int8 ( imn ) ;
120 case ’ u i n t 1 6 ’ then
121 imn = im2uint16 ( imn ) ;
122 case ’ i n t 1 6 ’ then
123 imn = im2int16 ( imn ) ;
124 case ’ i n t 3 2 ’ then
125 imn = im2int32 ( imn ) ;
126 case ’ c o n s t a n t ’ then
127 imn = im2double ( imn ) ;
128 else
129 error ( ” Data t y p e ” + imtype + ” i s n o t s u p p o r t e d
. ”);
130 end
131
132 endfunction
133
134
135 //
136 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
137 rgb = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; //
R e a d i n g from sub−d i r e c t o r y o f i m a g e s .
138 figure , ShowColorImage ( rgb , ’ O r i g i n a l c o l o r image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
139 title ( ’ O r i g i n a l C o l o r Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
140 im = rgb2gray ( rgb ) ; // t o c o n v e r t a RGB image i n t o
g r a y s c a l e image .
141
142 k = imnoise ( im , ’ s a l t & p e p p e r ’ , 0.02) ;
143 figure , ShowImage (k , ’ S a l t & p e p p e r n o i s e corrupted
image ’ ) ; // ShowColorImage ( ) i s u s e d t o show c o l o r
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
144 title ( ’ S a l t & p e p p e r n o i s e c o r r u p t e d image ’ ) ; // t i t l e
41
( ) i s used f o r p r o v i d i n g a t i t l e t o an image .
145
146
147 [ count , cells ]= imhist (k ,256) ; // i m h i s t ( ) i s used to
f i n d h i s t o g r a m o f an image .
148 figure ; imhist (k ,255 , ’ ’ ) ;
149 title ( ’ H i s t o g r a m o f S a l t & p e p p e r n o i s e c o r r u p t e d
image ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
150
151 k = imnoise ( im , ’ g a u s s i a n ’ ,0.02 , 0.02) ;
152 figure , ShowImage (k , ’ G a u s s i a n n o i s e c o r r u p t e d image ’
) ; // ShowColorImage ( ) i s u s e d t o show c o l o r image
, f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
153 title ( ’ G a u s s i a n n o i s e c o r r u p t e d image ’ ) ; // t i t l e ( ) i s
u s e d f o r p r o v i d i n g a t i t l e t o an image .
154
155 [ count , cells ]= imhist (k ,256) ; // i m h i s t ( ) i s u s e d t o
f i n d h i s t o g r a m o f an image .
156 figure ; imhist (k ,255 , ’ ’ ) ;
157 title ( ’ H i s t o g r a m o f G a u s s i a n n o i s e c o r r u p t e d image ’
) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
158
159
160 k = imnoise22 ( im , ’ s p e c k l e ’ ,0.8) ; // minor e r r o r i n
original function
161 figure , ShowImage (k , ’ S p e c k l e n o i s e c o r r u p t e d image ’ )
; // ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
162 title ( ’ S p e c k l e n o i s e c o r r u p t e d image ’ ) ; // t i t l e ( ) i s
u s e d f o r p r o v i d i n g a t i t l e t o an image .
163 [ count , cells ]= imhist (k ,256) ; // i m h i s t ( ) i s u s e d t o
f i n d h i s t o g r a m o f an image .
164 figure ; imhist (k ,255 , ’ ’ ) ;
165 title ( ’ H i s t o g r a m o f S p e c k l e n o i s e c o r r u p t e d image ’ )
42
; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 clc // t o c l e a r command window .
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14
15 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
16 rgb = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; //
R e a d i n g from sub−d i r e c t o r y o f i m a g e s .
17 figure , ShowColorImage ( rgb , ’ O r i g i n a l c o l o r image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
18 title ( ’ O r i g i n a l C o l o r Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
19 im = rgb2gray ( rgb ) ; // t o c o n v e r t a RGB image i n t o
g r a y s c a l e image .
20
43
21 function [ f ]= alphatrim (g ,m ,n , d ) // a l p h a t r i m ( ) i s u s e d
t o f i l t e r an image u s i n g a l p h a −trimmed mean
filter
22 size1 = m ;
23 [ nr , nc ]= size ( g ) ;
24 temp = zeros ( nr +2* floor ( size1 /2) , nc +2* floor ( size1
/2) ) ;
25 temp ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil ( size1
/2) : nc + ceil ( size1 /2) -1) = g (1: $ ,1: $ )
26
27 for i = ceil ( size1 /2) : nr + ceil ( size1 /2) -1
28 for j = ceil ( size1 /2) : nc + ceil ( size1 /2) -1
29 t = temp (i - floor ( size1 /2) :1: i + floor ( size1
/2) ,j - floor ( size1 /2) :1: j + floor ( size1
/2) )
30 y = gsort ( t ) ;
31 a = y (:)
32 b =a ’;
33 t1 = b (1+ d /2: $ - d /2) ;
34 temp2 (i , j ) = mean ( t1 ) ;
35 end
36 end
37 nn = temp2 ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil (
size1 /2) : nc + ceil ( size1 /2) -1)
38 f = mat2gray ( nn )
39 endfunction
40
41
42 function [ f ]= gmean1 (g ,m , n ) ; // gmean1 ( ) i s u s e d t o
f i l t e r an image u s i n g G e o m e t r i c mean f i l t e r
43 size1 = m ;
44 q=m*n;
45 g = double ( g ) ;
46 [ nr , nc ]= size ( g ) ;
47 temp = zeros ( nr +2* floor ( size1 /2) , nc +2* floor ( size1
/2) ) ;
48 temp ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil ( size1
/2) : nc + ceil ( size1 /2) -1) = g (1: $ ,1: $ )
44
49 temp = temp +1;
50 for i = ceil ( size1 /2) : nr + ceil ( size1 /2) -1
51 for j = ceil ( size1 /2) : nc + ceil ( size1 /2) -1
52 t = temp (i - floor ( size1 /2) :1: i + floor ( size1
/2) ,j - floor ( size1 /2) :1: j + floor ( size1
/2) ) ;
53 temp2 (i , j ) = prod ( t ) ;
54 end
55 end
56 temp3 = temp2 .^(1/ q ) ;
57 nn = temp3 ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil (
size1 /2) : nc + ceil ( size1 /2) -1)
58 f1 = nn -1;
59 f = mat2gray ( f1 )
60
61 endfunction
62
63
64
65 function [ f ]= gmean2 (g ,m , n ) ; // gmean2 ( ) i s u s e d t o
f i l t e r an image u s i n g G e o m e t r i c mean f i l t e r
66 size1 = m ;
67 q=m*n;
68 [ nr , nc ]= size ( g ) ;
69 temp = zeros ( nr +2* floor ( size1 /2) , nc +2* floor ( size1
/2) ) ;
70
71 temp ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil ( size1
/2) : nc + ceil ( size1 /2) -1) = g (1: $ ,1: $ )
72
73 for i = ceil ( size1 /2) : nr + ceil ( size1 /2) -1
74 for j = ceil ( size1 /2) : nc + ceil ( size1 /2) -1
75 t = temp (i - floor ( size1 /2) :1: i + floor ( size1
/2) ,j - floor ( size1 /2) :1: j + floor ( size1
/2) ) ;
76 temp2 (i , j ) = geomean ( t ) ;
77 end
78 end
45
79 nn = temp2 ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil (
size1 /2) : nc + ceil ( size1 /2) -1)
80 f = mat2gray ( nn )
81 endfunction
82
83 function [ f ]= harmean1 (g ,m , n ) // harmean1 ( ) i s u s e d t o
f i l t e r an image u s i n g Harmonic mean f i l t e r .
84 size1 = m ;
85 d=m*n;
86 g = double ( g ) ;
87 [ nr , nc ]= size ( g ) ;
88 temp = zeros ( nr +2* floor ( size1 /2) , nc +2* floor ( size1
/2) ) ;
89 temp ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil ( size1
/2) : nc + ceil ( size1 /2) -1) = g (1: $ ,1: $ )
90
91 for i = ceil ( size1 /2) : nr + ceil ( size1 /2) -1
92 for j = ceil ( size1 /2) : nc + ceil ( size1 /2) -1
93 t = temp (i - floor ( size1 /2) :1: i + floor ( size1
/2) ,j - floor ( size1 /2) :1: j + floor ( size1
/2) ) ;
94 t1 = ones (m , n ) ./( t + %eps )
95 t2 = sum ( t1 ) ;
96 temp2 (i , j ) = d / t2 ;
97 // temp2 ( i , j )=harmean ( t ) ;
98 end
99 end
100 nn = temp2 ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil (
size1 /2) : nc + ceil ( size1 /2) -1)
101 f = mat2gray ( nn ) ;
102 endfunction
103
104 function [ f ]= charmean1 (g ,m ,n , q ) // charmean1 ( ) i s u s e
t o f i l t e r an image u s i n g C o n t r a Harmonic mean
filter
105 size1 = m ;
106 d=m*n;
107 g = double ( g ) ;
46
108 [ nr , nc ]= size ( g ) ;
109 temp = zeros ( nr +2* floor ( size1 /2) , nc +2* floor ( size1
/2) ) ;
110 temp ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil ( size1
/2) : nc + ceil ( size1 /2) -1) = g (1: $ ,1: $ )
111 disp ( q )
112 for i = ceil ( size1 /2) : nr + ceil ( size1 /2) -1
113 for j = ceil ( size1 /2) : nc + ceil ( size1 /2) -1
114 t = temp (i - floor ( size1 /2) :1: i + floor ( size1
/2) ,j - floor ( size1 /2) :1: j + floor ( size1
/2) ) ;
115 d1 =( t + %eps ) .^ q
116 n1 =( t + %eps ) .^( q +1)
117 d2 = sum ( d1 ) ;
118 n2 = sum ( n1 ) ;
119 temp2 (i , j ) = n2 /( d2 ) ;
120 end
121 end
122 nn = temp2 ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil (
size1 /2) : nc + ceil ( size1 /2) -1)
123 f = nn ;
124 endfunction
125
126
127
128 function [ resimg ]= ordfilt2 ( image ,m ,n , type1 ) //
o r d f i l t 2 ( ) i s u s e d t o f i l t e r an image u s i n g min
o r max f i l t e r .
129 size1 = m ;
130 [ nr , nc ]= size ( image ) ;
131 temp = zeros ( nr +2* floor ( size1 /2) , nc +2* floor ( size1
/2) ) ;
132 temp ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil ( size1
/2) : nc + ceil ( size1 /2) -1) = image (1: $ ,1: $ )
133 select type1
134 case ’ min ’ then
135 for i = ceil ( size1 /2) : nr + ceil ( size1 /2) -1
136 for j = ceil ( size1 /2) : nc + ceil ( size1 /2) -1
47
137 t = temp (i - floor ( size1 /2) :1: i + floor (
size1 /2) ,j - floor ( size1 /2) :1: j +
floor ( size1 /2) )
138 y = gsort ( t ) ;
139 temp2 (i , j ) = min ( y ) ;
140 end
141 end
142 case ’ max ’ then
143 for i = ceil ( size1 /2) : nr + ceil ( size1 /2) -1
144 for j = ceil ( size1 /2) : nc + ceil ( size1 /2) -1
145 t = temp (i - floor ( size1 /2) :1: i + floor (
size1 /2) ,j - floor ( size1 /2) :1: j +
floor ( size1 /2) )
146 y = gsort ( t ) ;
147 temp2 (i , j ) = max ( y ) ;
148 end
149 end
150 end
151 nn = temp2 ( ceil ( size1 /2) : nr + ceil ( size1 /2) -1 , ceil (
size1 /2) : nc + ceil ( size1 /2) -1)
152 resimg = mat2gray ( nn )
153 endfunction
154
155
156
157 function [ f ] = spfilt (g , type1 , m ,n , parameter )
158 if argn (2) ==2 then
159 m =3; n =3; Q =1.5; d =2;
160 elseif argn (2) ==5 then
161 Q = parameter ; d = parameter ;
162 elseif argn (2) ==4 then
163 Q =1.5; d =2;
164 else
165 disp ( ’ wrong number o f i n p u t s ’ ) ;
166 end
167 select type1
168 case ’ amean ’ then
169 filtersize = m
48
170 w = fspecial ( ’ a v e r a g e ’ , filtersize ) ;
171 f = imfilter (g , w ) ;
172 case ’ gmean1 ’ then
173 f = gmean1 (g ,m , n ) ;
174 case ’ gmean2 ’ then
175 f = gmean2 (g ,m , n ) ;
176 case ’ hmean ’ then
177 f = harmean1 (g ,m , n ) ;
178
179 case ’ charmean ’ then
180 f = charmean1 (g ,m ,n , Q ) ;
181 case ’ median ’ then
182 filtersize = [ m n ];
183 f = MedianFilter (g , filtersize ) ;
184 // f=m e d f i l t 2 ( g , [ m n ] , ’ s y m m e t r i c ’ ) ;
185
186 case ’ max ’ then
187 f = ordfilt2 (g ,m ,n , ’ max ’ ) ;
188
189 case ’ min ’ then
190 f = ordfilt2 (g ,m ,n , ’ min ’ ) ;
191
192 case ’ m i d p o i n t ’ then
193 f1 = ordfilt2 (g ,m ,n , ’ max ’ ) ;
194 f2 = ordfilt2 (g ,m ,n , ’ min ’ ) ;
195 f = imlincomb (0.5 , f1 ,0.5 , f2 ) ;
196
197 case ’ atrimmed ’ then
198 if (d <0) |( d /2 ~= round ( d /2) ) then
199 disp ( ’ d must be a n o n n e g a t i v e , e v e n
i n t e g e r ’ );
200 end
201 disp ( d )
202 d =2;
203 f = alphatrim (g ,m ,n , d ) ;
204 else
205 disp ( ’ Unknown f i l t e r t y p e . ’ ) ;
206 end
49
207 endfunction
208
209
210 v = imnoise ( im , ’ s a l t & p e p p e r ’ ,0.02)
211 figure , ShowImage (v , ’ S a l t & p e p p e r n o i s e corrupted
image ’ ) ; // ShowColorImage ( ) i s u s e d t o show c o l o r
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
212 title ( ’ S a l t & p e p p e r n o i s e c o r r u p t e d image ’ ) ; //
t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
213
214
215 h = spfilt (v , ’ median ’ ,3 ,3)
216 figure , ShowImage (h , ’ R e s u l t o f f i l t e r i n g with a
median f i l t e r o f s i z e 3∗3 ’ ) ; // ShowColorImage ( ) i s
u s e d t o show c o l o r image , f i g u r e i s command t o
v i e w i m a g e s i n s e p a r a t e window .
217 title ( ’ R e s u l t o f f i l t e r i n g w i t h a median f i l t e r o f
s i z e 3∗3 ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a
t i t l e t o an image .
218
219
220
221 h = spfilt (v , ’ amean ’ ,3)
222 figure , ShowImage (h , ’ R e s u l t o f f i l t e r i n g w i t h an
a r i t h m e t i c mean f i l t e r o f s i z e 3∗3 ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
223 title ( ’ R e s u l t o f f i l t e r i n g w i t h an a r i t h m e t i c mean
f i l t e r o f s i z e 3∗3 ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
224
225
226
227 i = spfilt (v , ’ max ’ ,3 ,3)
228 figure , ShowImage (i , ’ R e s u l t o f f i l t e r i n g w i t h a max
50
f i l t e r o f s i z e 3∗3 ’ ) ; // ShowColorImage ( ) i s u s e d
t o show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
229 title ( ’ R e s u l t o f f i l t e r i n g w i t h a median f i l t e r o f
s i z e 3∗3 ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a
t i t l e t o an image .
230
231 // F i l t e r i n g t h e c o r r u p t e d image w i t h m i d p o i n t f i l t e r
232 j = spfilt (v , ’ m i d p o i n t ’ )
233 figure , ShowImage (j , ’ R e s u l t o f f i l t e r i n g w i t h a
M i d p o i n t f i l t e r o f s i z e 3∗3 ’ ) ; // ShowColorImage ( )
i s u s e d t o show c o l o r image , f i g u r e i s command t o
v i e w i m a g e s i n s e p a r a t e window .
234 title ( ’ R e s u l t o f f i l t e r i n g w i t h a M i d p o i n t f i l t e r o f
s i z e 3∗3 ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a
t i t l e t o an image .
235
236 j = spfilt (v , ’ min ’ ,3 ,3)
237 figure , ShowImage (j , ’ R e s u l t o f f i l t e r i n g with a Min
f i l t e r o f s i z e 3∗3 ’ ) ; // ShowColorImage ( ) i s used
t o show c o l o r image , f i g u r e i s command t o view
i m a g e s i n s e p a r a t e window .
238 title ( ’ R e s u l t o f f i l t e r i n g w i t h a Min f i l t e r of size
3∗3 ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a title
t o an image .
239
240
241 km = spfilt (v , ’ atrimmed ’ )
242 figure , ShowImage ( km , ’ R e s u l t o f f i l t e r i n g with a
Alpha−trimmed mean f i l t e r o f s i z e 3∗3 ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
243 title ( ’ R e s u l t o f f i l t e r i n g w i t h a Alpha−trimmed mean
f i l t e r o f s i z e 3∗3 ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
244
245 s = imcrop (v , [1 , 1 , 300 , 300]) ;
51
246 s = v ;
247
248 l = spfilt (s , ’ gmean1 ’ ,3 ,3)
249 figure , ShowImage (l , ’ R e s u l t o f f i l t e r i n g with a
G e o m e t r i c mean f i l t e r o f s i z e 3∗3 ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
250 title ( ’ R e s u l t o f f i l t e r i n g w i t h a G e o m e t r i c mean
f i l t e r o f s i z e 3∗3 ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image . ;
251
252 l = spfilt (s , ’ gmean2 ’ ,3 ,3)
253 figure , ShowImage (l , ’ R e s u l t o f f i l t e r i n g with a
G e o m e t r i c mean f i l t e r o f s i z e 3∗3 ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
254 title ( ’ R e s u l t o f f i l t e r i n g w i t h a G e o m e t r i c mean
f i l t e r o f s i z e 3∗3 ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image . ;
255
256 m = spfilt (s , ’ hmean ’ ,3 ,3)
257 figure , ShowImage (m , ’ R e s u l t o f f i l t e r i n g with a
Harmonic mean f i l t e r o f s i z e 3∗3 ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
258 title ( ’ R e s u l t o f f i l t e r i n g w i t h a Harmonic mean
f i l t e r o f s i z e 3∗3 ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image . ;
259
260
261
262 n = spfilt (v , ’ charmean ’ ,3 ,3 ,1)
263 figure , ShowImage (n , ’ R e s u l t o f f i l t e r i n g with a
C o n t r a Harmonic mean f i l t e r o f s i z e 3∗3 and o r d e r
o f f i l t e r i s 1 ’ ) ; // ShowColorImage ( ) i s u s e d t o
52
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
264 title ( ’ R e s u l t o f f i l t e r i n g w i t h a C o n t r a Harmonic
mean f i l t e r o f s i z e 3∗3 and o r d e r o f f i l t e r i s 1 ’
) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
53
Experiment: 6
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 clc // t o c l e a r command window
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14
15
16
17 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
54
18 rgb = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; //
R e a d i n g from sub−d i r e c t o r y o f i m a g e s .
19 figure , ShowColorImage ( rgb , ’ O r i g i n a l c o l o r image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
20 title ( ’ O r i g i n a l C o l o r Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
21
22
23 R = rgb (: ,: ,1) ; // S e p a r a t i o n o f r e d component from
image
24 figure , ShowImage (R , ’ Red component s e p a r a t i o n from
o r i g i n a l image ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
25 title ( ’ Red component s e p a r a t i o n from o r i g i n a l image ’
) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
26
27
28
29 G = rgb (: ,: ,2) ; // S e p a r a t i o n o f g r e e n component from
image
30 figure , ShowImage (R , ’ Green comonent s e p a r a t i o n from
o r i g i n a l image ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
31 title ( ’ Green component s e p a r a t i o n from o r i g i n a l
image ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
32
33
34 B = rgb (: ,: ,3) ; // S e p a r a t i o n o f b l u e component from
image
35 figure , ShowImage (R , ’ B l u e component s e p a r a t i o n from
o r i g i n a l image ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
55
i m a g e s i n s e p a r a t e window .
36 title ( ’ B l u e component s e p a r a t i o n from o r i g i n a l image
’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
37
38 [k , map ]= RGB2Ind ( rgb ) ; //RGB image i s c o n v e r t e d t o an
i n d e x e d image
39 figure , ShowImage (k , ’ I n d e x e d image ’ , map ) ; //map m a t r i x
i s x o l o r m a p m a t r i x o f an RGB image
40
41 rg = Ind2RGB (k , map ) ; // t o c o n v e r t i n d e x e d image t o t r u e
c o l o r image .
42 figure , ShowColorImage ( rg , ’RGB image i s converted
from an i n d e x e d image ’ ) ; // ShowColorImage ( ) i s
u s e d t o show c o l o r image , f i g u r e i s command t o
v i e w i m a g e s i n s e p a r a t e window .
43 title ( ’RGB image i s c o n v e r t e d from an i n d e x e d image ’
) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
44
45 function [ y ]= ind2gray (x , map )
46 rgb = Ind2RGB (x , map ) ; // t o c o n v e r t an i n d e x e d image
i n t o RGB image
47 yiq = rgb2ntsc ( rgb ) ; //RGB image i s c o n v e r t e d i n t o NTSC
c o l o r system .
48 y = yiq (: ,: ,1) ;
49 endfunction
50
51 t = ind2gray (k , map ) ;
52 figure , ShowImage (t , ’ I n d e x e d image i s converted to
g r a y s c a l e ’ ) ; // ShowColorImage ( ) i s u s e d t o show
c o l o r image , f i g u r e i s command to view images in
s e p a r a t e window .
53 title ( ’ I n d e x e d image i s c o n v e r t e d t o g r a y s c a l e ’ ) ; //
t i t l e ( ) i s used f o r p r o v i d i n g a t i t l e t o an
image .
54
55
56
56 yiq = rgb2ntsc ( rgb ) ; //YIQ c o m po n e n t s a r e o b t a i n e d from
RGB c o m p o n e n t s o f an image .
57 figure , ShowColorImage ( yiq , ’ Luminance (Y) , Hue ( I ) and
S a t u r a t i o n (Q) c o m po n e n t s o f a RGB image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
58 title ( ’ Luminance (Y) , Hue ( I ) and S a t u r a t i o n (Q)
c o m p o n e n ts o f a RGB image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
59
60 rgb1 = ntsc2rgb ( yiq ) ; //RGB c o m p o n e n t s a r e o b t a i n e d
from YIQ c o m p o n e n t s o f an image .
61 figure , ShowColorImage ( rgb1 , ’RGB c o m p o n en t s a r e
o b t a i n e d from NTSC f o r m a t ’ ) ; // ShowColorImage ( ) i s
u s e d t o show c o l o r image , f i g u r e i s command t o
v i e w i m a g e s i n s e p a r a t e window .
62 title ( ’RGB c o m p o n en t s a r e o b t a i n e d from NTSC f o r m a t ’
) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
63
64 ycbcr = rgb2ycbcr ( rgb ) ; //YCbCr c o m p o n e n t s a r e o b t a i n e d
from RGB c o m p on e n t s o f an image .
65 figure , ShowColorImage ( ycbcr , ’ Luminance (Y) , two c o l o r
d i f f e r e n c e c o m p o n e n t s Cb and Cr c o m p o n e n t s o f a
RGB image ’ ) ; // ShowColorImage ( ) i s u s e d t o show
c o l o r image , f i g u r e i s command t o v i e w i m a g e s i n
s e p e r a t e window .
66 title ( ’ Luminance (Y) , two c o l o r d i f f e r e n c e c om p o n e n t s
Cb and Cr c om p o n e n t s o f a RGB image ’ ) ; // t i t l e ( )
i s u s e d f o r p r o v i d i n g a t i t l e t o an image .
67
68 rgb2 = ycbcr2rgb ( ycbcr ) ;
69 figure , ShowColorImage ( rgb2 , ’RGB c o m p o n en t s a r e
o b t a i n e d from YCbCR c o l o r s p a c e ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
57
70 title ( ’RGB c o m p o ne n t s a r e o b t a i n e d from YCbCr c o l o r
s p a c e ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 clc // t o c l e a r command window .
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14 //
15
16 // s t a c k s i z e ( ) i s u s e d t o i n c r e a s e s t a c k s i z e t o
a c h i e v e maximum p e r f o r m a n c e . R e s t a r t S c i l a b i f
e r r o r no 1 0 0 0 1 ( c a n n o t a l l o c a t e memory ) i s o c c u r e d
.
17 stacksize ( ’ max ’ )
18
19
20 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
21 rgb = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; //
R e a d i n g from sub−d i r e c t o r y o f i m a g e s .
58
22 figure , ShowColorImage ( rgb , ’ O r i g i n a l c o l o r image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
23 title ( ’ O r i g i n a l C o l o r Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
24
25
26 hsv = rgb2hsv ( rgb ) ; //HSV co m p o n e n t s a r e o b t a i n e d from
RGB c o m p o n e n t s o f an image u s i n g r g b 2 h s v ( ) .
27 figure , ShowColorImage ( hsv , ’HSV c o m p o n e n t s o f a RGB
image ’ ) ; // ShowColorImage ( ) i s u s e d t o show c o l o r
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
28 title ( ’HSV c o m p o n e n t s o f a RGB image ’ ) ; // t i t l e ( ) i s
u s e d f o r p r o v i d i n g a t i t l e t o an image .
29
30 rgb3 = hsv2rgb ( hsv ) ; //RGB c o m p o n e n t s a r e o b t a i n e d from
HSV c o l o r s y s t e m u s i n g h s v 2 r g b ( ) .
31 figure , ShowColorImage ( rgb3 , ’RGB componenta a r e
o b t a i n e d from HSV c o l o r s y s t e m ’ ) ; // ShowColorImage
( ) i s u s e d t o show c o l o r image , f i g u r e i s command
t o v i e w i m a g e s i n s e p a r a t e window .
32 title ( ’RGB componenta a r e o b t a i n e d from HSV c o l o r
s y s t e m ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
33
34 cmy = imcomplement ( rgb ) ; // cyan magenta and y e l l o w
c o l o r s a r e o b t a i n e d from an image u s i n g
imcomplement ( ) .
35 figure , ShowColorImage ( cmy , ’ cyan magenta and y e l l o w
a r e o b t a i n e d from t h e RGB image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
36 title ( ’ cyan magenta and y e l l o w a r e o b t a i n e d from t h e
RGB image ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a
t i t l e t o an image .
59
37
38 rgb4 = imcomplement ( cmy ) ; //RGB c o m p o n e n t s a r e o b t a i n e d
from cyan magenta and y e l l o w c o m p o n e n t s i f an
image .
39 figure , ShowColorImage ( rgb4 , ’RGB c o m p o n en t s a r e
o b t a i n e d from cyan magenta and y e l l o w c o mp o n e n t s ’
) ; // ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
40 title ( ’RGB c o m p o n en t s a r e o b t a i n e d from cyan magenta
and y e l l o w c o m p o n e n t s ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
41
42
43
44 rgb = im2double ( rgb )
45 function [ hsi ]= rgb2hsi ( rgb ) // r g b 2 h s i ( ) i s user
d e f i n e d f u n c t i o n , which i s u s e d f o r c o n v e r t i n g
RGB f o r m a t t o HSI c o l o r model .
46
47 r = rgb (: , : , 1) ; // s e p a r a t i n g Red , Green and
B l u e c o m p on e n t s from t h e RGB image .
48 g = rgb (: , : , 2) ;
49 b = rgb (: , : , 3) ;
50 num = 0.5*(( r - g ) + ( r - b ) ) ;
51 den1 =( r - g ) .^2 + ( r - b ) .*( g - b )
52 den = den1 .^0.5;
53 theta = acos ( num ./( den + %eps ) ) ; // %eps i s minimum
number t o a v o i d d i v i d e by z e r o e x c e p t i o n .
54 H = theta ;
55 H ( b > g ) = 2* %pi - H ( b > g ) ;
56 H = H /(2* %pi ) ;
57
58 num = min ( min (r , g ) , b ) ;
59 den = r + g + b ;
60 den ( den == 0) = %eps ;
61 S = 1 - 3.* num ./ den ;
62
60
63 H ( S == 0) = 0;
64
65 I = ( r + g + b ) /3;
66
67 hsi = rgb ;
68 hsi (: ,: ,1) = H ;
69 hsi (: ,: ,2) = S ;
70 hsi (: ,: ,3) = I ;
71
72 endfunction
73 hsi = rgb2hsi ( rgb ) ;
74
75 figure , ShowColorImage ( hsi , ’ H S I i m a g e ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
76 title ( ’ H S I i m a g e ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g
a t i t l e t o an image .
77
78 function [ rgb ]= hsi2rgb ( hsi ) // h s i 2 r g b ( ) i s u s e r
d e f i n e d f u n c t i o n , which i s u s e d f o r c o n v e r t i n g HSI
f o r m a t t o RGB c o l o r model .
79
80
81 H = hsi (: , : , 1) * 2 * %pi ;
82 S = hsi (: , : , 2) ;
83 I = hsi (: , : , 3) ;
84
85 [ nr nc ]= size ( H ) // t o f i n d d i m e n s i o n o f an image .
86
87
88 R = zeros ( nr , nc ) ;
89 G = zeros ( nr , nc ) ;
90 B = zeros ( nr , nc ) ;
91
92
93 idx = find ( (0 <= H ) & ( H < 2* %pi /3) ) ;
94 B ( idx ) = I ( idx ) .* (1 - S ( idx ) ) ;
61
95 R ( idx ) = I ( idx ) .* (1 + S ( idx ) .* cos ( H ( idx ) ) ./
cos ( %pi /3 - H ( idx ) ) ) ;
96 G ( idx ) = 3* I ( idx ) - ( R ( idx ) + B ( idx ) ) ;
97
98
99 idx = find ( (2* %pi /3 <= H ) & ( H < 4* %pi /3) ) ;
100 R ( idx ) = I ( idx ) .* (1 - S ( idx ) ) ;
101 G ( idx ) = I ( idx ) .* (1 + S ( idx ) .* cos ( H ( idx ) -
2* %pi /3) ./ cos ( %pi - H ( idx ) ) ) ;
102 B ( idx ) = 3* I ( idx ) - ( R ( idx ) + G ( idx ) ) ;
103
104
105 idx = find ( (4* %pi /3 <= H ) & ( H <= 2* %pi ) ) ;
106 G ( idx ) = I ( idx ) .* (1 - S ( idx ) ) ;
107 B ( idx ) = I ( idx ) .* (1 + S ( idx ) .* cos ( H ( idx ) -
4* %pi /3) ./ cos (5* %pi /3 - H ( idx ) ) ) ;
108 R ( idx ) = 3* I ( idx ) - ( G ( idx ) + B ( idx ) ) ;
109
110
111 rgb = hsi ;
112 rgb (: ,: ,1) = R ;
113 rgb (: ,: ,2) = G ;
114 rgb (: ,: ,3) = B ;
115 rgb = max ( min ( rgb , 1) , 0) ;
116
117 endfunction
118
119
120 rgb = hsi2rgb ( hsi ) ;
121
122 figure , ShowColorImage ( rgb , ’RGB c o m p o ne n t s a r e
o b t a i n e d from h s i c o l o r s y s t e m ’ ) ; // ShowColorImage
( ) i s u s e d t o show c o l o r image , f i g u r e i s command
t o v i e w i m a g e s i n s e p a r a t e window .
123 title ( ’RGB c o m p o n en t s a r e o b t a i n e d from HSI c o l o r
s y s t e m ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e
t o an image .
62
Experiment: 7
Image Compression
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 clc // t o c l e a r command window .
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14
15 SIVP_PATH = getSIVPpath () ; // t o l o c a t e a d i r e c t o r y
i n which SIVP t o o l b o x i s i n s t a l l e d . g e t S I V P p a t h ( )
i s pre −d e f i n e d f u n c t i o n i n SIVP t o o l b o x .
16 rgb = imread ( SIVP_PATH + ’ i m a g e s / l e n a . png ’ ) ; //
R e a d i n g from sub−d i r e c t o r y o f i m a g e s .
63
17 figure , ShowColorImage ( rgb , ’ O r i g i n a l c o l o r image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
18 title ( ’ O r i g i n a l C o l o r Image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
19
20 im = rgb2gray ( rgb ) ; // t o c o n v e r t a RGB image i n t o
g r a y s c a l e image .
21
22 M =8;
23 N =8;
24 [ nr , nc ]= size ( im ) ;
25
26
27 p = imcrop ( im ,[1 ,1 ,8 ,8]) ; // image i s d i v i d e d i n t o 8∗8
b l o c k s f o r c o m p u t a t i o n o f DCT.
28 p = double ( p ) ;
29 p1 =p -128;
30 f = dct ( p1 ) ; // d c t ( ) i s u s e d t o f i n d DCT
transformations .
31 // q i s Luminance q u a n t i z a t i o n m a t r i x .
32 q =[16 11 10 16 24 40 51 61
33 12 12 14 19 26 58 60 55
34 14 13 16 24 40 57 69 56
35 14 17 22 29 51 87 80 62
36 18 22 37 56 68 109 103 77
37 24 35 55 64 81 104 113 92
38 49 64 78 87 103 121 120 101
39 72 92 95 98 112 100 103 99];
40 // Each DCT c o e f f i c i e n t f ( u , v ) i s d i v i d e d by t h e
c o r r e s p o n d i n g q u a n t i z e r step −s i z e parameter q ( u , v
) i n t h e q u a n t i z a t i o n m a t r i x and r o u n d e d t o t h e
nearest integer
41 t = f ./ q ;
42 g = floor ( t )
43
44
64
45 function [ out ]= zigzag ( in ) // z i g z a g ( ) i s f u n c t i o n t o
f i n d a v e c t o r s o r t e d by t h e c r i t e r i a o f t h e
spatial frequency
46 [ num_rows num_cols ]= size ( in ) ;
47
48 // I n i t i a l i s e t h e o u t p u t v e c t o r
49 out = zeros (1 , num_rows * num_cols ) ;
50
51 cur_row =1; cur_col =1; cur_index =1;
52
53 // F i r s t e l e m e n t
54 // o u t ( 1 )=i n ( 1 , 1 ) ;
55
56 while cur_row <= num_rows & cur_col <= num_cols
57 if cur_row ==1 & pmodulo ( cur_row + cur_col ,2)
==0 & cur_col ~= num_cols then
58 out ( cur_index ) = in ( cur_row , cur_col ) ;
59 cur_col = cur_col +1;
// move r i g h t a t t h e t o p
60 cur_index = cur_index +1;
61
62 elseif cur_row == num_rows & pmodulo ( cur_row +
cur_col ,2) ~=0 & cur_col ~= num_cols then
63 out ( cur_index ) = in ( cur_row , cur_col ) ;
64 cur_col = cur_col +1;
// move r i g h t a t t h e bottom
65 cur_index = cur_index +1;
66
67 elseif cur_col ==1 & pmodulo ( cur_row + cur_col
,2) ~=0 & cur_row ~= num_rows then
68 out ( cur_index ) = in ( cur_row , cur_col ) ;
69 cur_row = cur_row +1;
// move down a t t h e l e f t
70 cur_index = cur_index +1;
71
72 elseif cur_col == num_cols & pmodulo ( cur_row +
cur_col ,2) ==0 & cur_row ~= num_rows then
73 out ( cur_index ) = in ( cur_row , cur_col ) ;
65
74 cur_row = cur_row +1;
// move down a t t h e r i g h t
75 cur_index = cur_index +1;
76
77 elseif cur_col ~=1 & cur_row ~= num_rows &
pmodulo ( cur_row + cur_col ,2) ~=0 then
78 out ( cur_index ) = in ( cur_row , cur_col ) ;
79 cur_row = cur_row +1; cur_col = cur_col
-1; // move d i a g o n a l l y l e f t down
80 cur_index = cur_index +1;
81
82 elseif cur_row ~=1 & cur_col ~= num_cols &
pmodulo ( cur_row + cur_col ,2) ==0 then
83 out ( cur_index ) = in ( cur_row , cur_col ) ;
84 cur_row = cur_row -1; cur_col = cur_col
+1; // move d i a g o n a l l y r i g h t up
85 cur_index = cur_index +1;
86
87 elseif cur_row == num_rows & cur_col == num_cols
// o b t a i n t h e bottom r i g h t e l e m e n t
88 out ( M * N ) = in (8 ,8) ;
// end o f t h e o p e r a t i o n
89 break
// t e r m i n a t e t h e o p e r a t i o n
90 else
91 end
92 end
93
94 endfunction
95
96 out = zigzag ( g ) ;
97 disp ( out )
66
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 clc // t o c l e a r command window .
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14
15
16 nn =[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0
0 3 0 4 4 4 5 0 0 0 0 5 0 0 0];
17 function [ rle ]= relencoder ( nn ) // run l e n g t h e n c o d e r
18
19 k =1;
20 n = [ nn 1]; // dummy e n d i n g
21
22 t =0;
23 for i = 1: length ( n ) -1
24 if n ( i ) ==0 then
25 t = t +1;
26 if t ==15 then
27 r (k ,1) = t ;
28 r (k ,2) =0;
29 k = k +1;
30 t =0;
31 end
32 else
33 valuecode = n ( i ) ;
34 lengthcode = t ;
35 r (k ,1) = lengthcode ;
67
36 r (k ,2) = valuecode ;
37 k = k +1;
38 t =0;
39 end
40 end
41 rle = r ;
42 rle ( $ +1 ,:) =0;
43 disp ( rle )
44 disp ( size ( rle ) )
45 endfunction
46
47 rr = relencoder ( nn ) ;
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11
12 // t h i s c o d e i s f o r huffman e n c o d i n g u s i n g t o o l b o x .
13 clc // t o c l e a r command window .
14 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
15 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
) .// Generate a Testmatrix
16
17 sp = sparse
([1 ,1;1 ,2;1 ,3;1 ,4;1 ,5;1 ,6] ,[5 ,7 ,10 ,15 ,20 ,45])
68
18 [ SB ,h ,L , QM ]= huffman ( sp ) ;
19 //SB c o n t a i n s t h e s y m b o l s
20 disp ( SB ) ;
21 // h i s t h e n o r m a l i z e d h i s t o g r a m
22 disp ( h ) ;
23 // L c o n t a i n s t h e number o f b i t s u s e d f o r t h e
symbols
24 disp ( L ) ;
25 // QM i s t h e c o m p l e t e c o d e t a b l e ,
26 // c o n t a i n i n g symbol , b i t s and no . o f b i t s
27 disp ( QM ) ;
28 disp ( sp )
69
Experiment: 8
Morphological Image
Processing
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 clc // t o c l e a r command window .
70
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14
15
16 a =[0 0;0 1]
17 b =[0 1;1 1]
18
19
20
21 Image = imread ( ’ wirebondmask . t i f ’ ) ;
22
23 StructureElement = CreateStructureElement ( ’ s q u a r e ’ ,
9) ; // C r e a t e S t r u c t u r e E l e m e n t ( ) i s u s e d t o c r e a t e
s t r u c t u r i n g element . F i r s t parameter i s used to
c r e a t e square s t r u c t u r i n g elemnt of s i z e 9
24
25 ResultImage = ErodeImage ( Image , StructureElement ) ; //
ErodeImage ( ) i s u s e d t o p e r f o r m e r o s i o n o f an
image by s t r u c t u r i n g e l e m e n t .
26 figure , ShowImage ( ResultImage , ’ E r o s i o n o f an image by
s q u a r e s t r u c t u r i n g e l e m e n t . ’ ) ; // ShowColorImage ( )
i s u s e d t o show c o l o r image , f i g u r e i s command
t o v i e w i m a g e s i n s e p a r a t e window .
27 title ( ’ E r o s i o n o f an image by s q u a r e s t r u c t u r i n g
e l e m e n t . ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a
title
28
29
30 Image = imread ( ’ 8 2 . t i f ’ ) ;
31 figure , ShowImage ( Image , ’ Text w i t h b r o k e n c h a r a c t e r s .
’ ) ; // ShowImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
32 title ( ’ Text w i t h b r o k e n c h a r a c t e r s . ’ ) ; // t i t l e ( ) i s
used f o r p r o v i d i n g a t i t l e
33
34 StructureElement1 = CreateStructureElement ( ’ s q u a r e ’ ,
71
3) ; // C r e a t e S t r u c t u r e E l e m e n t ( ) i s u s e d t o c r e a t e
s t r u c t u r i n g element . F i r s t parameter i s used to
c r e a t e square s t r u c t u r i n g elemnt of s i z e 3 .
35 ResultImage1 = DilateImage ( Image , StructureElement1 )
; // D i l a t e I m a g e ( ) i s u s e d t o p e r f o r m e r o s i o n o f an
image by s t r u c t u r i n g e l e m e n t .
36 figure , ShowImage ( ResultImage1 , ’ D i l a t i o n o f an image
by s q u a r e s t r u c t u r i n g e l e m e n t . Broken s e g e m e n t s
w e r e j o i n e d . ’ ) ; // ShowImage ( ) i s u s e d t o show
c o l o r image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
37 title ( ’ D i l a t i o n o f an image by s q u a r e s t r u c t u r i n g
e l e m e n t . Broken s e g e m e n t s w e r e j o i n e d . ’ ) ; // t i t l e ( )
i s used f o r p r o v i d i n g a t i t l e
38
39 StructureElement2 = CreateStructureElement ( ’ s q u a r e ’ ,
3) ; // C r e a t e S t r u c t u r e E l e m e n t ( ) i s u s e d t o c r e a t e
s t r u c t u r i n g element . F i r s t parameter i s used to
c r e a t e square s t r u c t u r i n g elemnt of s i z e 9
40 StructureElement2 . Data =[ %f %t %f ; %t %t %t ; %f %t %f ]
41
42 ResultImage2 = DilateImage ( Image , StructureElement2 ) ;
// D i l a t e I m a g e ( ) i s u s e d t o p e r f o r m e r o s i o n o f an
image by s t r u c t u r i n g e l e m e n t .
43 figure , ShowImage ( ResultImage2 , ’ D i l a t i o n o f an image
by s q u a r e s t r u c t u r i n g e l e m e n t . Broken s e g e m e n t s
w e r e j o i n e d . ’ ) ; // ShowImage ( ) i s u s e d t o show
c o l o r image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
44 title ( ’ D i l a t i o n o f an image by s q u a r e s t r u c t u r i n g
e l e m e n t . Broken s e g e m e n t s w e r e j o i n e d . ’ ) ; // t i t l e ( )
i s used f o r p r o v i d i n g a t i t l e
45
46
47 ResultImage3 = OpenImage ( Image , StructureElement1 ) ; //
// OpenImage ( ) i s u s e d t o open an image by
s t r u c t u r i n g element .
48 figure , ShowImage ( ResultImage3 , ’ Opening o f an image . ’
72
) ; // ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
49 title ( ’ Opening o f an image . ’ ) ; // t i t l e ( ) i s u s e d f o r
providing a t i t l e
50
51 ResultImage4 = CloseImage ( Image , StructureElement1 ) ;
52 figure , ShowImage ( ResultImage4 , ’ C l o s i n g o f an image . ’
) ; // ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
53 title ( ’ C l o s i n g o f an image . ’ ) ; // t i t l e ( ) i s u s e d f o r
providing a t i t l e
54
55
56 image1 = imread ( ’ 8 3 . t i f ’ ) ;
57 image2 = imcomplement ( image1 ) ; // imcomplement ( ) i s u s e d
t o f i n d complement o f an image .
58
59 function [ result ]= hitmiss (i , se1 , se2 ) // h i t m i s s ( ) i s
u s e d t o p e r f o r m h i t and m i s s t r a n s f o r m , which i s
used f o r shape d e t e c t i o n .
60 e = imcomplement ( i ) ; // imcomplement ( ) i s u s e d t o
f i n d complement o f an image .
61 c = ErodeImage (e , se2 ) ; // ErodeImage ( ) i s u s e d t o e r o d e
an image by s t r u t u r i n g e l e m e n t .
62 b = ErodeImage (i , se1 ) // // ErodeImage ( ) i s u s e d t o
e r o d e an image by s t r u t u r i n g e l e m e n t .
63 result1 = b & c ;
64 disp ( result1 )
65 [ nr , nc ]= size ( i ) ;
66 idx = find ( result1 == %t ) ;
67 result = zeros ( nr , nc )
68 result ( idx ) =255;
69 endfunction
70
71 i =[ 0. 0. 0. 0. 0. 0. 0. 0.
0.
73
72 0. 0. 1. 0. 0. 0. 1. 0.
0.
73 0. 1. 1. 1. 0. 1. 1. 1.
0.
74 0. 0. 1. 0. 0. 0. 1. 1.
0.
75 0. 0. 0. 0. 0. 0. 0. 0.
0.
76 0. 0. 0. 0. 0. 0. 0. 0.
0.]
77
78 se1 = CreateStructureElement ( ’ s q u a r e ’ , 3) ;
79 se1 . Data =[ %f %t %f ; %t %t %t ; %f %t %f ]
80
81 se2 = CreateStructureElement ( ’ s q u a r e ’ , 3) ;
82 se2 . Data =[ %t %f %t ; %f %f %f ; %t %f %t ];
83
84 figure , ShowImage (i , ’ O r i g i n a l b i n a r y image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
85 title ( ’ O r i g i n a l b i n a r y image ’ ) ; // t i t l e ( ) i s u s e d f o r
providing a t i t l e
86
87 Result = hitmiss (i , se1 , se2 ) ; // h i t m i s s ( ) i s u s e d t o
p e r f o r m h i t and m i s s t r a n s f o r m , which i s u s e d f o r
shape d e t e c t i o n .
88 figure , ShowImage ( Result , ’ R e s u l t o f H i t and M i s s
T r a n s f o r m . ’ ) ; // ShowColorImage ( ) i s u s e d t o show
c o l o r image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
89 title ( ’ R e s u l t o f H i t and M i s s T r a n s f o r m . ’ ) ; // t i t l e ( )
i s used f o r p r o v i d i n g a t i t l e
90
91 // boundary e x t r a c t i o n
92
93 se1 = CreateStructureElement ( ’ s q u a r e ’ , 3) ;
94 Image = imread ( ’ wirebondmask . t i f ’ ) ;
74
95 result1 = ErodeImage ( Image , se1 ) ;
96 result = Image - result1 ;
97 figure , ShowImage ( Image , ’ O r i g i n a l b i n a r y image ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
98 title ( ’ O r i g i n a l b i n a r y image . ’ ) ; // t i t l e ( ) i s u s e d
for providing a t i t l e
99
100 figure , ShowImage ( result , ’ E x t r a c t e d boundary o f an
image . ’ ) ; // ShowColorImage ( ) i s u s e d t o show c o l o r
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
101 title ( ’ E x t r a c t e d boundary o f an image . ’ ) ; // t i t l e ( )
i s used f o r p r o v i d i n g a t i t l e
75
Experiment: 9
Image Segmentation
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
76
11 clc // t o c l e a r command window .
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14
15 Image = ReadImage ( ’ t u r b i n e b l a d e . t i f ’ ) ;
16 figure , ShowImage ( Image , ’ Gray s c a l e image w i t h a
i s o l a t e d b l a c k p o i n t . ’ ) ; // ShowColorImage ( ) i s
u s e d t o show c o l o r image , f i g u r e i s command t o
v i e w i m a g e s i n s e p a r a t e window .
17 title ( ’ Gray s c a l e image w i t h a i s o l a t e d b l a c k p o i n t .
’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
18
19 image = double ( Image ) ;
20 mask =[ -1 -1 -1; -1 8 -1; -1 -1 -1];
21
22 res = imfilter ( image , mask ) ;
23 g = abs ( res ) ;
24 t = max ( g ) ;
25 g =g >= t ;
26 figure , ShowImage (g , ’ D e t e c t i o n o f p o i n t ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
27 title ( ’ D e t e c t i o n o f p o i n t . ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
28
29
30 hmask =[ -1 -1 -1;2 2 2; -1 -1 -1]; // mask f o r
horizontal line detection .
31 vmask =[ -1 2 -1; -1 2 -1; -1 2 -1]; // mask f o r v e r t i c a l
line detection .
32 dlmask =[ -1 -1 2; -1 2 -1;2 -1 -1]; // mask f o r +45
degree l i n e detection .
33 dlmask2 =[2 -1 -1; -1 2 -1; -1 -1 2]; // mask f o r −45
degree l i n e detection .
34
77
35 Image = ReadImage ( ’ wirebondmask . t i f ’ )
36 figure , ShowImage ( Image , ’ Image o f a w i r e −bond mask ’ ) ;
// ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
37 title ( ’ Image o f a w i r e −bond mask . ’ ) ; // t i t l e ( ) i s
u s e d f o r p r o v i d i n g a t i t l e t o an image .
38 image = double ( Image ) ;
39 g1 = imfilter ( image , hmask ) ;
40 figure , ShowImage ( g1 , ’ R e s u l t o f p r o c e s s i n g w i t h
h o r i z o n t a l l i n e d e t e c t o r mask . ’ ) ; // ShowColorImage
( ) i s u s e d t o show c o l o r image , f i g u r e i s command
t o v i e w i m a g e s i n s e p a r a t e window .
41 title ( ’ R e s u l t o f p r o c e s s i n g w i t h h o r i z o n t a l l i n e
d e t e c t o r mask . ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g
a t i t l e t o an image .
42
43 g2 = imfilter ( image , vmask ) ;
44 figure , ShowImage ( g2 , ’ R e s u l t o f p r o c e s s i n g with
v e r t i c a l d e g r e e l i n e d e t e c t o r mask . ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
45 title ( ’ R e s u l t o f p r o c e s s i n g w i t h v e r t i c a l d e g r e e
l i n e d e t e c t o r mask . ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
46
47 g3 = imfilter ( image , dlmask ) ;
48 figure , ShowImage ( g3 , ’ R e s u l t o f p r o c e s s i n g w i t h +45
d e g r e e l i n e d e t e c t o r mask . ’ ) ; // ShowColorImage ( )
i s u s e d t o show c o l o r image , f i g u r e i s command t o
v i e w i m a g e s i n s e p a r a t e window .
49 title ( ’ R e s u l t o f p r o c e s s i n g w i t h +45 d e g r e e l i n e
d e t e c t o r mask . ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g
a t i t l e t o an image .
50
51 g4 = imfilter ( image , dlmask2 )
52 figure , ShowImage ( g4 , ’ R e s u l t o f p r o c e s s i n g w i t h −45
78
d e g r e e l i n e d e t e c t o r mask . ’ ) ; // ShowColorImage ( )
i s u s e d t o show c o l o r image , f i g u r e i s command t o
v i e w i m a g e s i n s e p a r a t e window .
53 title ( ’ R e s u l t o f p r o c e s s i n g w i t h −45 d e g r e e l i n e
d e t e c t o r mask . ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g
a t i t l e t o an image .
54
55
56 Image = ReadImage ( ’ b u i l d i n g . t i f ’ )
57 figure , ShowImage ( Image , ’ Image o f a B u i l d i n g ’ ) ; //
ShowColorImage ( ) i s u s e d t o show c o l o r image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
58 title ( ’ Image o f a b u i l d i n g . ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
59
60 e = edge ( image , ’ s o b e l ’ ) ; // e d g e ( )i s u s e d t o d e t e c t an
e d g e i n g r a y s c a l e image . S e c o n d argument ’ s o b e l ’ i s
used f o r Sobel edge d e t e c t o r .
61 figure , ShowImage (e , ’ Edge d e t e c t i o n u s i n g S o b e l
a p p r o x i m a t i o n . ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
62 title ( ’ Edge d e t e c t i o n u s i n g S o b e l a p p r o x i m a t i o n . ’ ) ;
// t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
63
64 e = edge ( image , ’ p r e w i t t ’ ) ; // e d g e ( )i s used to d e t e c t
an e d g e i n g r a y s c a l e image . S e c o n d argument ’
prewitt ’ i s used f o r p r e w i t t edge d e t e c t o r .
65 figure , ShowImage (e , ’ Edge d e t e c t i o n u s i n g P r e w i t t
a p p r o x i m a t i o n . ’ ) ; // ShowColorImage ( ) i s u s e d t o
show c o l o r image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
66 title ( ’ Edge d e t e c t i o n u s i n g P r e w i t t a p p r o x i m a t i o n . ’ )
; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
67
79
68 e = edge ( image , ’ canny ’ ) ; // e d g e ( ) i s u s e d t o d e t e c t an
e d g e i n g r a y s c a l e image . S e c o n d argument ’ canny ’ i s
u s e d f o r Canny e d g e d e t e c t o r .
69 figure , ShowImage (e , ’ Edge d e t e c t i o n u s i n g Canny e d g e
d e t e c t o r . ’ ) ; // ShowColorImage ( ) i s u s e d t o show
c o l o r image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
70 title ( ’ Edge d e t e c t i o n u s i n g Canny a p p r o x i m a t i o n . ’ ) ;
// t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
80
Experiment: 10
Wavelets
woman.bmp
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 clc // t o c l e a r command window .
12 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
13 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
14
81
15 // s t a c k s i z e ( ) i s u s e d t o i n c r e a s e s t a c k s i z e t o
a c h i e v e maximum p e r f o r m a n c e . R e s t a r t S c i l a b i f
e r r o r no 1 0 0 0 1 ( c a n n o t a l l o c a t e memory ) i s o c c u r e d
.
16 stacksize ( ’ max ’ )
17
18
19
20 r = ReadImage ( ’ woman . bmp ’ ) ; // ReadImage ( ) i s used to
r e a d an image
21 X = rgb2gray ( r ) ; // r g b 2 g r a y ( ) i s u s e d t o c o n v e r t an
image i n t o g r a y s c a l e .
22 figure , ShowImage (X , ’A s i m p l e t e s t image ’ ) ; //
ShowImage ( ) i s u s e d t o show g r a y s c a l e image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
23 title ( ’A s i m p l e t e s t image ’ ) ; // t i t l e ( ) i s u s e d f o r
p r o v i d i n g a t i t l e t o an image .
24
25 wname = ’ h a a r ’ // wname i s u s e d i n two d i m e n s i o n
multiple l e v e l d i s c r e t e f a s t wavelet transform . ’
haar ’ i s u s e d f o r Haar w a v e l e t .
26 [ nr , nc ]= size ( r ) ;
27 x = zeros ( nr , nc ) ;
28 x = X (1: $ ,1: $ ) ;
29 x = double ( x ) ;
30
31 [ wc , s ] = wavedec2 (x ,2 , wname ) ; // Compute a 2− l e v e l
d e c o m p o s i t i o n o f t h e image u s i n g t h e Haar f i l t e r s
. S e c o n d argument i s u s e d f o r l e v e l .
32
33 // E x t r a c t t h e l e v e l 1 c o e f f i c i e n t s . F o u r t h argument
must be 1 t o f o r e x t r a c t i o n t h e l e v e l 1
c o e f f i c i e n t i n a p p c o e f 2 ( ) and d e t c o e f 2 ( ) f u n c t i o n
.
34 a1 = appcoef2 ( wc ,s , wname ,1) ; // a p p c o e f 2 ( ) i s u s e d t o
e x t r a c t 2−D a p p r o x i m a t i o n c o e f f i c i e n t s .
82
35 h1 = detcoef2 ( ’ h ’ ,wc ,s ,1) ; // d e t c o e f 2 ( ) i s used to
e x t r a c t 2−D d e t a i l c o e f f i c i e n t e x t r a c t i o n . F i r s t
argument ’ h ’ i s u s e d f o r h o r i z o n t a l d e t a i l
coefficeint .
36 v1 = detcoef2 ( ’ v ’ ,wc ,s ,1) ; // d e t c o e f 2 ( ) i s u s e d t o
e x t r a c t 2−D d e t a i l c o e f f i c i e n t e x t r a c t i o n . F i r s t
argument ’ v ’ i s u s e d f o r v e r t i c a l d e t a i l
coefficeint .
37 d1 = detcoef2 ( ’ d ’ ,wc ,s ,1) ; // d e t c o e f 2 ( ) i s u s e d t o
e x t r a c t 2−D d e t a i l c o e f f i c i e n t e x t r a c t i o n . F i r s t
a r g u m e n t d i s used f o r d i a g o n a l d e t a i l
coefficient .
38
39 // E x t r a c t t h e l e v e l 2 c o e f f i c i e n t s . F o u r t h argument
must be 2 t o f o r e x t r a c t i o n t h e l e v e l 2
c o e f f i c i e n t i n a p p c o e f 2 ( ) and d e t c o e f 2 ( ) f u n c t i o n
.
40
41 a2 = appcoef2 ( wc ,s , wname ,2) ;
42 h2 = detcoef2 ( ’ h ’ ,wc ,s ,2) ;
43 v2 = detcoef2 ( ’ v ’ ,wc ,s ,2) ;
44 d2 = detcoef2 ( ’ d ’ ,wc ,s ,2) ;
45
46 // D i s p l a y t h e d e c o m p o s i t i o n up t o l e v e l 1 o n l y .
47 a1 = double ( a1 ) ;
48 cod_a1 = wcodemat ( a1 ,256) ;
49 cod_a1 = double ( cod_a1 ) ;
50 cod_a1 = wkeep ( cod_a1 ,[256 256]) ;
51 cod_h1 = wcodemat ( h1 ,260) ;
52 cod_h1 = double ( cod_h1 ) ;
53 cod_h1 = wkeep ( cod_h1 , [256 256]) ;
54 cod_v1 = wcodemat ( v1 ,260) ;
55 cod_v1 = double ( cod_v1 ) ;
56 cod_v1 = wkeep ( cod_v1 , [256 256]) ;
57 cod_d1 = wcodemat ( d1 ,260) ;
58 cod_d1 = double ( cod_d1 ) ;
59 cod_d1 = wkeep ( cod_d1 , [256 256]) ;
60 ans1 =[ cod_a1 , cod_h1 ; cod_v1 , cod_d1 ];
83
61
62 figure , ShowImage ( ans1 , ’ Wavelet T r a n s f o r m o f an
image ’ ) ; // ShowImage ( ) i s u s e d t o show g r a y s c a l e
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
63 title ( ’ Wavelet T r a n s f o r m o f an image ’ ) ; // t i t l e ( ) i s
u s e d f o r p r o v i d i n g a t i t l e t o an image .
64
65
66
67
68 // D i s p l a y t h e e n t i r e d e c o m p o s i t i o n u p t o l e v e l 2 .
69 cod_a2 = wcodemat ( a2 ,260) ;
70 cod_a2 = double ( cod_a2 ) ;
71 cod_a2 = wkeep ( cod_a2 , [128 128]) ;
72 cod_h2 = wcodemat ( h2 ,260) ;
73 cod_h2 = double ( cod_h2 ) ;
74 cod_h2 = wkeep ( cod_h2 , [128 128]) ;
75 cod_v2 = wcodemat ( v2 ,260) ;
76 cod_v2 = double ( cod_v2 ) ;
77 cod_v2 = wkeep ( cod_v2 , [128 128]) ;
78 cod_d2 = wcodemat ( d2 ,260) ;
79 cod_d2 = double ( cod_d2 ) ;
80 cod_d2 = wkeep ( cod_d2 , [128 128]) ;
81 bb =[[ cod_a2 , cod_h2 ; cod_v2 , cod_d2 ] , cod_h1 ; cod_v1 ,
cod_d1 ];
82
83
84 figure , ShowImage ( bb , ’ Two s c a l e Wavelet t r a n s f o r m an
image ’ ) ; // ShowImage ( ) i s u s e d t o show g r a y s c a l e
image , f i g u r e i s command t o v i e w images in
s e p a r a t e window .
85 title ( ’ Two s c a l e Wavelet t r a n s f o r m an image ’ ) ; //
t i t l e ( ) i s used f o r p r o v i d i n g a t i t l e t o an
image .
86
87
84
88 // R e c o n s t r u c t i o n o f an image u s i n g w r c o e f 2 ( )
f u n c t i o n . L a s t argument i s u s e d f o r l e v e l o f
reconstruction .
89 ra2 = wrcoef2 ( ’ a ’ ,wc ,s , wname ,2) ; // ’ a ’ i s u s e d f o r
approximation c o e f f i c i e n t s .
90 rh2 = wrcoef2 ( ’ h ’ ,wc ,s , wname ,2) ; // ’ h ’ i s u s e d f o r
horizontal detail coefficient .
91 rv2 = wrcoef2 ( ’ v ’ ,wc ,s , wname ,2) ; // ’ v ’ i s u s e d f o r
vertical detail coefficient .
92 rd2 = wrcoef2 ( ’ d ’ ,wc ,s , wname ,2) ; // ’ d ’ i s u s e d f o r
detail coefficient .
93
94 ra1 = wrcoef2 ( ’ a ’ ,wc ,s , wname ,1) ;
95 rh1 = wrcoef2 ( ’ h ’ ,wc ,s , wname ,1) ;
96 rv1 = wrcoef2 ( ’ v ’ ,wc ,s , wname ,1) ;
97 rd1 = wrcoef2 ( ’ d ’ ,wc ,s , wname ,1) ;
98
99 cod_ra2 = wcodemat ( ra2 ,260) ;
100 cod_rh2 = wcodemat ( rh2 ,260) ;
101 cod_rv2 = wcodemat ( rv2 ,260) ;
102 cod_rd2 = wcodemat ( rd2 ,260) ;
103 cod_ra1 = wcodemat ( ra1 ,260) ;
104 cod_rh1 = wcodemat ( rh1 ,260) ;
105 cod_rv1 = wcodemat ( rv1 ,260) ;
106 cod_rd1 = wcodemat ( rd1 ,260) ;
107
108
109
110 // Adding t o g e t h e r t h e r e c o n s t r u c t e d a v e r a g e a t
l e v e l 2 and a l l o f
111 // t h e r e c o n s t r u c t e d d e t a i l s g i v e s t h e f u l l
r e c o n s t r u c t e d image .
112 Xhat = ra2 + rh2 + rv2 + rd2 + rh1 + rv1 + rd1 ;
113 X1 = double ( X ) ;
114 X2 = double ( Xhat ) ;
115 X3 = max ( max ( abs ( X1 - X2 ) ) ) ; //
116 disp ( X3 ) ;
117 disp ( ’ R e c o n s t r u c t i o n e r r o r ’ )
85
118
119 // A n o t h e r way t o r e c o n s t r u c t t h e image .
120
121 XXhat = waverec2 ( wc ,s , wname ) ; // w a v e r e c 2 ( ) i s u s e d
f o r two d i m e n s i o n m u l t i p l e l e v e l i n v e r s e d i s c r e t e
transform .
122 X1 = double ( X ) ;
123 X2 = double ( XXhat ) ;
124 X3 = max ( max ( abs ( X1 - X2 ) ) ) ;
125 disp ( X3 ) ;
126 disp ( ’ R e c o n s t r u c t i o n e r r o r ( u s i n g w a v e r e c 2 ) ’ ) ;
127 // C o m p r e s s i o n can be a c c o m p l i s h e d by a p p l y i n g a
t h r e s h o l d to the wavelet c o e f f i c i e n t s .
128 thr = 20;
129 [ X_comp , wc_comp , s_comp , perf0 , perfL2 ] = wdencmp ( ’ g b l ’
,wc ,s , wname ,2 , thr , ’ h ’ ,1) ; // wdencmp ( ) i s u s e d f o r
de−n o i s i n g or compression using wavelets . h
means u s e h a r d t h r e s o l d i n g . p e r f L 2 i s u s e d t o
find energy r e c o v e r y . p e r f 0 i s used to measure
compression performance .
130
131 cod_X_comp = wcodemat ( X_comp ,260) ;
132
133 figure , ShowImage ( cod_X_comp , ’ Compressed u s i n g
g l o b a l h a r d t h r e s h o l d ’ ) ; // ShowImage ( ) i s u s e d t o
show g r a y s c a l e image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
134 title ( ’ Compressed u s i n g g l o b a l h a r d t h r e s h o l d ’ ) //
t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
135 disp ( ’ Energy r e t a i n e d ’ ) ;
136 disp ( perfL2 ) ;
137
138 disp ( ’ N u l l c o e f f i c i e n t s ’ ) ;
139 disp ( perf0 ) ;
86
Scilab code Solution 10.2 Wavelets
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11 // e d g e d e t e c t i o n
12 clc // t o c l e a r command window .
13 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
14 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
15
16 // s t a c k s i z e ( ) i s u s e d t o i n c r e a s e s t a c k s i z e t o
a c h i e v e maximum p e r f o r m a n c e . R e s t a r t S c i l a b i f
e r r o r no 1 0 0 0 1 ( c a n n o t a l l o c a t e memory ) i s o c c u r e d
.
17 stacksize ( ’ max ’ )
18
19
20 X = ReadImage ( ’ 1 0 1 . t i f ’ ) ; // ReadImage ( ) i s used to read
an image
21 figure , ShowImage (X , ’ O r i g i n a l G r a y s c a l e image ’ ) ; //
ShowImage ( ) i s u s e d t o show g r a y s c a l e image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
22 title ( ’ O r i g i n a l G r a y s c a l e Image ’ ) ; // t i t l e ( ) i s u s e d
f o r p r o v i d i n g a t i t l e t o an image .
87
23
24 [ nr , nc ]= size ( X ) ;
25 x = zeros ( nr , nc ) ;
26 x = X (1: $ ,1: $ ) ;
27 x = double ( x ) ;
28
29 [ CA , CH , CV , CD ]= dwt2 (x , ’ db2 ’ ) ; // one l e v e l
decomposition
30 a =( CA - min ( CA ) ) / max ( CA - min ( CA ) ) ; // n o r m a l i z e t h e
approximation c o e f f i c i e n t s matrix f o r d i s p l a y i n g .
31 h =( CH - min ( CH ) ) / max ( CH - min ( CH ) ) ; // n o r m a l i z e t h e
h o r i z o n t a l d e t a i l c o e f f i c i e n t s matrix f o r
displaying .
32 v =( CV - min ( CV ) ) / max ( CV - min ( CV ) ) ; // n o r m a l i z e t h e
v e r t i c a l d e t a i l c o e f f i c i e n t s matrix f o r
displaying .
33 d =( CD - min ( CD ) ) / max ( CD - min ( CD ) ) ; // n o r m a l i z e t h e
diagonal d e t a i l c o e f f i c i e n t s matrix f o r
displaying .
34 c1 =[ a ’; h ’] ’;
35 c2 =[ v ’; d ’] ’;
36 co =[ c1 ; c2 ];
37 figure , ShowImage ( co , ’ O r i g i n a l G r a y s c a l e image ’ ) ; //
ShowImage ( ) i s u s e d t o show g r a y s c a l e image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
38 title ( ’ O r i g i n a l G r a y s c a l e Image ’ ) ; // t i t l e ( ) i s u s e d
f o r p r o v i d i n g a t i t l e t o an image .
39
40 CA (1: $ ,1: $ ) =0; // z e r o i n g a p p r o x i m a t i o n c o e f f i c i e n t s
41 a = CA ;
42 c1 =[ a ’; h ’] ’;
43 c2 =[ v ’; d ’] ’;
44 co =[ c1 ; c2 ];
45 figure , ShowImage ( co , ’ D e l e t e d a p p r o x i m a t i o n
c o e f f i c i e n t s ’ ) ; // ShowImage ( ) i s u s e d t o show
g r a y s c a l e image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
88
46 title ( ’ D e l e t e d a p p r o x i m a t i o n c o e f f i c i e n t s ’ ) ; // t i t l e
( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an image .
47
48 x1 = idwt2 ( CA , CH , CV , CD , ’ db2 ’ ,[ nr nc ]) ; // i d w t 2 ( ) is
u s e d t o f i n d Two D i m e n s i o n I n v e r s e D i s c r e t e F a s t
Wavelet T r a n s f o r m .
49
50 figure , ShowImage ( x1 , ’ R e c o n s t r u c t e d image a f t e r
d e l e t i n g a p p r o x i m a t i o n c o e f f i c i e n t s ’ ) ; // ShowImage
( ) i s u s e d t o show g r a y s c a l e image , f i g u r e i s
command t o v i e w i m a g e s i n s e p a r a t e window .
51 title ( ’ R e c o n s t r e d image a f t e r d e l e t i n g a p p r o x i m a t i o n
c o e f f i c i e n t s ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g
a t i t l e t o an image .
52
53
54 // h o r i z o n t a l l i n e d e t e c t i o n
55 [ CA , CH , CV , CD ]= dwt2 (x , ’ db2 ’ ) ; // one l e v e l
decomposition
56 a =( CA - min ( CA ) ) / max ( CA - min ( CA ) ) ; // n o r m a l i z e t h e
approximation c o e f f i c i e n t s matrix f o r d i s p l a y i n g .
57 h =( CH - min ( CH ) ) / max ( CH - min ( CH ) ) ; // n o r m a l i z e t h e
h o r i z o n t a l d e t a i l c o e f f i c i e n t s matrix f o r
displaying .
58 v =( CV - min ( CV ) ) / max ( CV - min ( CV ) ) ; // n o r m a l i z e t h e
v e r t i c a l d e t a i l c o e f f i c i e n t s matrix f o r
displaying .
59 d =( CD - min ( CD ) ) / max ( CD - min ( CD ) ) ; // n o r m a l i z e t h e
diagonal d e t a i l c o e f f i c i e n t s matrix f o r
displaying .
60 CA (1: $ ,1: $ ) =0; // z e r o i n g t h e a p p r o x i m a t i o n
c o e f f i c i e n t s matrix .
61 a = CA ;
62 CH (1: $ ,1: $ ) =0; // z e r o i n g t h e h o r i z o n t a l d e t a i l
c o e f f i c i e n t s matrix .
63 h = CH ;
64 c1 =[ a ’; h ’] ’;
65 c2 =[ v ’; d ’] ’;
89
66 co =[ c1 ; c2 ];
67 figure , ShowImage ( co , ’ D e l e t e d a p p r o x i m a t i o n
c o e f f i c i e n t s and h o r i z o n t a l d e t a i l c o e f f i c i e n t s ’
) ; // ShowImage ( ) i s u s e d t o show g r a y s c a l e image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
68 title ( ’ d e l e t e d a p p r o x i m a t i o n c o e f f i c i e n t s and
h o r i z o n t a l d e t a i l c o e f f i c i e n t s ’ ) ; // t i t l e ( ) i s
u s e d f o r p r o v i d i n g a t i t l e t o an image .
69
70 x1 = idwt2 ( CA , CH , CV , CD , ’ db2 ’ ,[ nr nc ]) ; // i d w t 2 ( ) is
u s e d t o f i n d Two D i m e n s i o n I n v e r s e D i s c r e t e F a s t
Wavelet T r a n s f o r m .
71 figure , ShowImage ( x1 , ’ R e c o n s t r u c t e d image a f t e r
d e l e t i n g a p p r o x i m a t i o n c o e f f i c i e n t s and
h o r i z o n t a l d e t a i l c o e f f i c i e n t s ’ ) ; // ShowImage ( )
i s u s e d t o show g r a y s c a l e image , f i g u r e i s
command t o v i e w i m a g e s i n s e p a r a t e window .
72 title ( ’ R e c o n s t r u c t e d image a f t e r d e l e t i n g
a p p r o x i m a t i o n c o e f f i c i e n t s and h o r i z o n t a l d e t a i l
c o e f f i c i e n t s ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g
a t i t l e t o an image .
73
74 // v e r t i c a l l i n e d e t e c t i o n
75
76 [ CA , CH , CV , CD ]= dwt2 (x , ’ db2 ’ ) ; // one l e v e l
decomposition
77 a =( CA - min ( CA ) ) / max ( CA - min ( CA ) ) ; // n o r m a l i z e t h e
approximation c o e f f i c i e n t s matrix f o r d i s p l a y i n g .
78 h =( CH - min ( CH ) ) / max ( CH - min ( CH ) ) ; // n o r m a l i z e t h e
h o r i z o n t a l d e t a i l c o e f f i c i e n t s matrix f o r
displaying .
79 v =( CV - min ( CV ) ) / max ( CV - min ( CV ) ) ; // n o r m a l i z e t h e
v e r t i c a l d e t a i l c o e f f i c i e n t s matrix f o r
displaying .
80 d =( CD - min ( CD ) ) / max ( CD - min ( CD ) ) ; // n o r m a l i z e t h e
diagonal d e t a i l c o e f f i c i e n t s matrix f o r
displaying .
90
81 CA (1: $ ,1: $ ) =0; // z e r o i n g t h e a p p r o x i m a t i o n
c o e f f i c i e n t s matrix .
82 a = CA ;
83 CV (1: $ ,1: $ ) =0; // z e r o i n g t h e vertical detail
c o e f f i c i e n t s matrix .
84 v = CV ;
85 c1 =[ a ’; h ’] ’;
86 c2 =[ v ’; d ’] ’;
87 co =[ c1 ; c2 ];
88 figure , ShowImage ( co , ’ D e l e t e d a p p r o x i m a t i o n
c o e f f i c i e n t s and v e r t i c a l d e t a i l c o e f f i c i e n t s ’ ) ;
// ShowImage ( ) i s u s e d t o show g r a y s c a l e image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
89 title ( ’ D e l e t e d a p p r o x i m a t i o n c o e f f i c i e n t s and
v e r t i c a l d e t a i l c o e f f i c i e n t s ’ ) ; // t i t l e ( ) i s u s e d
f o r p r o v i d i n g a t i t l e t o an image .
90
91 x1 = idwt2 ( CA , CH , CV , CD , ’ db2 ’ ,[ nr nc ]) ; // i d w t 2 ( ) is
u s e d t o f i n d Two D i m e n s i o n I n v e r s e D i s c r e t e F a s t
Wavelet T r a n s f o r m .
92
93 figure , ShowImage ( x1 , ’ R e c o n s t r u c t e d image a f t e r
d e l e t i n g a p p r o x i m a t i o n c o e f f i c i e n t s and v e r t i c a l
d e t a i l c o e f f i c i e n t s ’ ) ; // ShowImage ( ) i s u s e d t o
show g r a y s c a l e image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
94 title ( ’ R e c o n s t r u c t e d image a f t e r d e l e t i n g
a p p r o x i m a t i o n c o e f f i c i e n t s and v e r t i c a l d e t a i l
c o e f f i c i e n t s ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g
a t i t l e t o an image .
pattern.tif
91
Scilab code Solution 10.3 Wavelets
1 //
2 // e n v i r o n m e n t : S c i l a b 5 . 4 . 1
3 // T o o l b o x : Image P r o c e s s i n g D e s i g n 8 . 3 . 1 − 1
4 // T o o l b o x : SIVP 0 . 5 . 3 . 1 − 2
5 // T o o l b o x : S c i l a b Wavelet T o o l b o x 0 . 1 . 1 9 − 1
6 // T o o l b o x : Huffcomp T o o l b o x 1 . 1 . 1
7 //OS : Windows 7
8 //
9 // R e f e r e n c e book name : D i g i t a l Image P r o c e s s i n g
10 // book a u t h o r : R a f a e l C . G o n z a l e z and R i c h a r d E .
Woods
11
12 // w a v e l e t b a s e d image s m o o t h i n g .
13 clc // t o c l e a r command window .
14 clear all // t o k i l l p r e v i o u s l y d e f i n e d v a r i a b l e s .
15 xdel ( winsid () ) // t o c l o s e a l l c u r r e n t l y open f i g u r e ( s
).
16
17 // s t a c k s i z e ( ) i s u s e d t o i n c r e a s e s t a c k s i z e t o
a c h i e v e maximum p e r f o r m a n c e . R e s t a r t S c i l a b i f
e r r o r no 1 0 0 0 1 ( c a n n o t a l l o c a t e memory ) i s o c c u r e d
.
18 stacksize ( ’ max ’ )
19
20
21
22 X = ReadImage ( ’ p a t t e r n . t i f ’ ) ; // ReadImage ( ) i s used to
r e a d an image
23 figure , ShowImage (X , ’ O r i g i n a l G r a y s c a l e image ’ ) ; //
ShowImage ( ) i s u s e d t o show g r a y s c a l e image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
24 title ( ’ O r i g i n a l G r a y s c a l e Image ’ ) ; // t i t l e ( ) i s u s e d
f o r p r o v i d i n g a t i t l e t o an image .
25
26
92
27 dwtmode ( ’ s t a t u s ’ ) // ’ s t a t u s ’ i s u s e d t o d i s p l a y
c u r r e n t DWT E x t e n s i o n mode .
28 dwtmode ( ’ sym ’ ) // ’ sym ’ i s u s e d f o r c h a n g i n g DWT
E x t e n s i o n mode t o h a l f s y m m e t r i s a t i o n
29 wname = ’ h a a r ’ // wname i s u s e d i n two d i m e n s i o n
multiple l e v e l d i s c r e t e f a s t wavelet transform . ’
haar ’ i s u s e d f o r Haar w a v e l e t .
30 [ nr , nc ]= size ( X ) ;
31 x = zeros ( nr , nc ) ;
32 x = X (1: $ ,1: $ ) ;
33 x = double ( x ) ;
34
35 [ wc , s ] = wavedec2 (x ,4 , wname ) ; // Compute a 2− l e v e l
d e c o m p o s i t i o n o f t h e image u s i n g t h e Haar f i l t e r s
. S e c o n d argument i s u s e d f o r l e v e l .
36
37 // E x t r a c t t h e l e v e l 1 c o e f f i c i e n t s . F o u r t h argument
must be 1 t o f o r e x t r a c t i o n t h e l e v e l 1
c o e f f i c i e n t i n a p p c o e f 2 ( ) and d e t c o e f 2 ( ) f u n c t i o n
.
38 a1 = appcoef2 ( wc ,s , wname ,1) ; // a p p c o e f 2 ( ) i s u s e d t o
e x t r a c t 2−D a p p r o x i m a t i o n c o e f f i c i e n t s .
39 h1 = detcoef2 ( ’ h ’ ,wc ,s ,1) ; // d e t c o e f 2 ( ) i s u s e d t o
e x t r a c t 2−D d e t a i l c o e f f i c i e n t e x t r a c t i o n . F i r s t
argument ’ h ’ i s u s e d f o r h o r i z o n t a l d e t a i l
coefficients .
40 v1 = detcoef2 ( ’ v ’ ,wc ,s ,1) ; // d e t c o e f 2 ( ) i s u s e d t o
e x t r a c t 2−D d e t a i l c o e f f i c i e n t e x t r a c t i o n . F i r s t
argument ’ v ’ i s u s e d f o r v e r t i c a l d e t a i l
coefficients .
41 d1 = detcoef2 ( ’ d ’ ,wc ,s ,1) ; // d e t c o e f 2 ( ) i s u s e d t o
e x t r a c t 2−D d e t i a l c o e f f i c i e n t e x t r a c t i o n . F i r s t
a r g u m e n t d i s used f o r d i a g o n a l d e t a i l
coefficients .
42
43 // E x t r a c t t h e l e v e l 2 c o e f f i c i e n t s . F o u r t h argument
must be 2 f o r e x t r a c t i o n t h e l e v e l 2
c o e f f i c i e n t s i n a p p c o e f 2 ( ) and d e t c o e f 2 ( )
93
function .
44
45 a2 = appcoef2 ( wc ,s , wname ,2) ;
46 h2 = detcoef2 ( ’ h ’ ,wc ,s ,2) ;
47 v2 = detcoef2 ( ’ v ’ ,wc ,s ,2) ;
48 d2 = detcoef2 ( ’ d ’ ,wc ,s ,2) ;
49
50 // E x t r a c t t h e l e v e l 2 c o e f f i c i e n t s . F o u r t h argument
must be 3 f o r e x t r a c t i o n t h e l e v e l 2
c o e f f i c i e n t s i n a p p c o e f 2 ( ) and d e t c o e f 2 ( )
function .
51
52 a3 = appcoef2 ( wc ,s , wname ,3) ;
53 h3 = detcoef2 ( ’ h ’ ,wc ,s ,3) ;
54 v3 = detcoef2 ( ’ v ’ ,wc ,s ,3) ;
55 d3 = detcoef2 ( ’ d ’ ,wc ,s ,3) ;
56
57
58 // E x t r a c t t h e l e v e l 2 c o e f f i c i e n t s . F o u r t h argument
must be 4 f o r e x t r a c t i o n t h e l e v e l 2
c o e f f i c i e n t s i n a p p c o e f 2 ( ) and d e t c o e f 2 ( )
function .
59
60 a4 = appcoef2 ( wc ,s , wname ,4) ;
61 h4 = detcoef2 ( ’ h ’ ,wc ,s ,4) ;
62 v4 = detcoef2 ( ’ v ’ ,wc ,s ,4) ;
63 d4 = detcoef2 ( ’ d ’ ,wc ,s ,4) ;
64
65
66
67
68 // D i s p l a y t h e d e c o m p o s i t i o n up t o l e v e l 1 o n l y .
69 a1 = double ( a1 ) ;
70 cod_a1 = wcodemat ( a1 ,256) ;
71 cod_a1 = double ( cod_a1 ) ;
72 cod_a1 = wkeep ( cod_a1 ,[344 344]) ;
73 cod_h1 = wcodemat ( h1 ,260) ;
74 cod_h1 = double ( cod_h1 ) ;
94
75 cod_h1 = wkeep ( cod_h1 , [344 344]) ;
76 cod_v1 = wcodemat ( v1 ,260) ;
77 cod_v1 = double ( cod_v1 ) ;
78 cod_v1 = wkeep ( cod_v1 , [344 344]) ;
79 cod_d1 = wcodemat ( d1 ,260) ;
80 cod_d1 = double ( cod_d1 ) ;
81 cod_d1 = wkeep ( cod_d1 , [344 344]) ;
82 ans1 =[ cod_a1 , cod_h1 ; cod_v1 , cod_d1 ];
83
84 figure , ShowImage ( ans1 , ’ Wavelet T r a n s f o r m o f an
image ’ ) ; // ShowImage ( ) i s u s e d t o show g r a y s c a l e
image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
85 title ( ’ Wavelet T r a n s f o r m o f an image ’ ) ; // t i t l e ( ) i s
u s e d f o r p r o v i d i n g a t i t l e t o an image .
86
87
88
89
90 // D i s p l a y t h e e n t i r e d e c o m p o s i t i o n up t o l e v e l 2 .
91 cod_a2 = wcodemat ( a2 ,260) ;
92 cod_a2 = double ( cod_a2 ) ;
93 cod_a2 = wkeep ( cod_a2 , [172 172]) ;
94 cod_h2 = wcodemat ( h2 ,260) ;
95 cod_h2 = double ( cod_h2 ) ;
96 cod_h2 = wkeep ( cod_h2 , [172 172]) ;
97 cod_v2 = wcodemat ( v2 ,260) ;
98 cod_v2 = double ( cod_v2 ) ;
99 cod_v2 = wkeep ( cod_v2 , [172 172]) ;
100 cod_d2 = wcodemat ( d2 ,260) ;
101 cod_d2 = double ( cod_d2 ) ;
102 cod_d2 = wkeep ( cod_d2 , [172 172]) ;
103 bb =[[ cod_a2 , cod_h2 ; cod_v2 , cod_d2 ] , cod_h1 ; cod_v1 ,
cod_d1 ];
104
105
106 figure , ShowImage ( bb , ’ S e c o n d l e v e l Wavelet t r a n s f o r m
an image ’ ) ; // ShowImage ( ) i s u s e d t o show g r a y
95
s c a l e image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
107 title ( ’ S e c o n d l e v e l Wavelet t r a n s f o r m an image ’ ) ; //
t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
108
109
110 // D i s p l a y t h e e n t i r e d e c o m p o s i t i o n u p t o l e v e l 3 .
111 cod_a3 = wcodemat ( a3 ,260) ;
112 cod_a3 = double ( cod_a3 ) ;
113 cod_a3 = wkeep ( cod_a3 ,[86 86]) ;
114 cod_h3 = wcodemat ( h3 ,260) ;
115 cod_h3 = double ( cod_h3 ) ;
116 cod_h3 = wkeep ( cod_h3 ,[86 86]) ;
117 cod_v3 = wcodemat ( v3 ,260) ;
118 cod_v3 = double ( cod_v3 ) ;
119 cod_v3 = wkeep ( cod_v3 ,[86 86]) ;
120 cod_d3 = wcodemat ( d3 ,260) ;
121 cod_d3 = double ( cod_d3 ) ;
122 cod_d3 = wkeep ( cod_d3 ,[86 86]) ;
123 bbb =[[[ cod_a3 , cod_h3 ; cod_v3 , cod_d3 ] , cod_h2 ; cod_v2 ,
cod_d2 ] , cod_h1 ; cod_v1 , cod_d1 ];
124
125
126 figure , ShowImage ( bbb , ’ T h i r d l e v e l Wavelet t r a n s f o r m
an image ’ ) ; // ShowImage ( ) i s u s e d t o show g r a y
s c a l e image , f i g u r e i s command t o v i e w i m a g e s i n
s e p a r a t e window .
127 title ( ’ T h i r d l e v e l Wavelet t r a n s f o r m an image ’ ) ; //
t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
128
129
130
131 // D i s p l a y t h e e n t i r e d e c o m p o s i t i o n u p t o l e v e l 2 .
132 cod_a4 = wcodemat ( a4 ,260) ;
133 cod_a4 = double ( cod_a4 ) ;
134 cod_a4 = wkeep ( cod_a4 , [43 43]) ;
96
135 cod_h4 = wcodemat ( h4 ,260) ;
136 cod_h4 = double ( cod_h4 ) ;
137 cod_h4 = wkeep ( cod_h4 , [43 43]) ;
138 cod_v4 = wcodemat ( v4 ,260) ;
139 cod_v4 = double ( cod_v4 ) ;
140 cod_v4 = wkeep ( cod_v4 , [43 43]) ;
141 cod_d4 = wcodemat ( d4 ,260) ;
142 cod_d4 = double ( cod_d4 ) ;
143 cod_d4 = wkeep ( cod_d4 , [43 43]) ;
144 bbbb =[[[[ cod_a4 , cod_h4 ; cod_v4 , cod_d4 ] , cod_h3 ; cod_v3 ,
cod_d3 ] , cod_h2 ; cod_v2 , cod_d2 ] , cod_h1 ; cod_v1 ,
cod_d1 ];
145
146
147 figure , ShowImage ( bbbb , ’ F o u r t h l e v e l Wavelet
t r a n s f o r m an image ’ ) ; // ShowImage ( ) i s u s e d t o
show g r a y s c a l e image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
148 title ( ’ F o u r t h l e v e l Wavelet t r a n s f o r m an image ’ ) ; //
t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
149
150 // R e c o n s t r u c t i o n o f an image u s i n g w r c o e f 2 ( )
f u n c t i o n . L a s t argument i s u s e d f o r l e v e l o f
reconstruction .
151
152 ra4 = wrcoef2 ( ’ a ’ ,wc ,s , wname ,4) ;
153 rh4 = wrcoef2 ( ’ h ’ ,wc ,s , wname ,4) ;
154 rv4 = wrcoef2 ( ’ v ’ ,wc ,s , wname ,4) ;
155 rd4 = wrcoef2 ( ’ d ’ ,wc ,s , wname ,4) ;
156
157 ra3 = wrcoef2 ( ’ a ’ ,wc ,s , wname ,3) ;
158 rh3 = wrcoef2 ( ’ h ’ ,wc ,s , wname ,3) ;
159 rv3 = wrcoef2 ( ’ v ’ ,wc ,s , wname ,3) ;
160 rd3 = wrcoef2 ( ’ d ’ ,wc ,s , wname ,3) ;
161
162 ra2 = wrcoef2 ( ’ a ’ ,wc ,s , wname ,2) ; // ’ a ’ i s u s e d f o r
approximation c o e f f i c i e n t s .
97
163 rh2 = wrcoef2 ( ’ h ’ ,wc ,s , wname ,2) ; // ’ h ’ i s used f o r
horizontal detail coefficients .
164 rv2 = wrcoef2 ( ’ v ’ ,wc ,s , wname ,2) ; // ’ v ’ i s used f o r
vertical detail coefficients .
165 rd2 = wrcoef2 ( ’ d ’ ,wc ,s , wname ,2) ; // ’ d ’ i s used f o r
detail coefficients .
166
167 ra1 = wrcoef2 ( ’ a ’ ,wc ,s , wname ,1) ;
168 rh1 = wrcoef2 ( ’ h ’ ,wc ,s , wname ,1) ;
169 rv1 = wrcoef2 ( ’ v ’ ,wc ,s , wname ,1) ;
170 rd1 = wrcoef2 ( ’ d ’ ,wc ,s , wname ,1) ;
171
172
173 cod_ra4 = wcodemat ( ra4 ,260) ;
174 cod_rh4 = wcodemat ( rh4 ,260) ;
175 cod_rv4 = wcodemat ( rv4 ,260) ;
176 cod_rd4 = wcodemat ( rd4 ,260) ;
177
178 cod_ra3 = wcodemat ( ra3 ,260) ;
179 cod_rh3 = wcodemat ( rh3 ,260) ;
180 cod_rv3 = wcodemat ( rv3 ,260) ;
181 cod_rd3 = wcodemat ( rd3 ,260) ;
182
183 cod_ra2 = wcodemat ( ra2 ,260) ;
184 cod_rh2 = wcodemat ( rh2 ,260) ;
185 cod_rv2 = wcodemat ( rv2 ,260) ;
186 cod_rd2 = wcodemat ( rd2 ,260) ;
187
188 cod_ra1 = wcodemat ( ra1 ,260) ;
189 cod_rh1 = wcodemat ( rh1 ,260) ;
190 cod_rv1 = wcodemat ( rv1 ,260) ;
191 cod_rd1 = wcodemat ( rd1 ,260) ;
192
193 // z e r o i n g f i r s t l e v e l d e t a i l coefficients .
194 rh1 (1: $ ,1: $ ) =0;
195 rd1 (1: $ ,1: $ ) =0;
196 rv1 (1: $ ,1: $ ) =0;
197
98
198 Xhat = ra4 + rh4 + rv4 + rd4 + rh3 + rv3 + rd3 + rh2 +
rv2 + rd2 + rh1 + rv1 + rd1 ;
199 figure , ShowImage ( Xhat , ’ R e c o n s t r u c t i o n o f an image
a f t e r z e r o i n g f i r s t l e v e l d e t a i l c o e f f i c i e n t s . ’ );
// ShowImage ( ) i s u s e d t o show g r a y s c a l e image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
200 title ( ’ R e c o n s t r u c t i o n o f an image a f t e r z e r o i n g
f i r s t and s e c o n d l e v e l d e t a i l c o e f f i c i e n t s . ’ ) ; //
t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
201
202 // z e r o i n g f i r s t and s e c o n d l e v e l d e t a i l
coefficients .
203 rh2 (1: $ ,1: $ ) =0;
204 rv2 (1: $ ,1: $ ) =0;
205 rd2 (1: $ ,1: $ ) =0;
206
207 rh1 (1: $ ,1: $ ) =0;
208 rd1 (1: $ ,1: $ ) =0;
209 rv1 (1: $ ,1: $ ) =0;
210 Xhat = ra4 + rh4 + rv4 + rd4 + rh3 + rv3 + rd3 + rh2 +
rv2 + rd2 + rh1 + rv1 + rd1 ;
211 figure , ShowImage ( Xhat , ’ r e c o n s t i o n o f an image a f t e r
z e r o i n g f i r s t and s e c o n d l e v e l d e t a i l
c o e f f i c i e n t s . ’ ) ; // ShowImage ( ) i s u s e d t o show
g r a y s c a l e image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
212 title ( ’ R e c o n s t r u c t i o n o f an image a f t e r z e r o i n g
f i r s t and s e c o n d l e v e l d e t a i l c o e f f i c i e n t s . ’ ) ; //
t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o an
image .
213
214 // z e r o i n g f i r s t , s e c o n d and t h i r d l e v e l d e t a i l
coefficients .
215 rh3 (1: $ ,1: $ ) =0;
216 rv3 (1: $ ,1: $ ) =0;
217 rd3 (1: $ ,1: $ ) =0;
99
218
219 rh2 (1: $ ,1: $ ) =0;
220 rv2 (1: $ ,1: $ ) =0;
221 rd2 (1: $ ,1: $ ) =0;
222
223 rh1 (1: $ ,1: $ ) =0;
224 rd1 (1: $ ,1: $ ) =0;
225 rv1 (1: $ ,1: $ ) =0;
226 Xhat = ra4 + rh4 + rv4 + rd4 + rh3 + rv3 + rd3 + rh2 +
rv2 + rd2 + rh1 + rv1 + rd1 ;
227 figure , ShowImage ( Xhat , ’ R e c o n s t r u c t i o n o f an image
a f t e r z e r o i n g f i r s t , s e c o n d and t h i r d l e v e l
d e t a i l c o e f f i c i e n t s . ’ ) ; // ShowImage ( ) i s u s e d t o
show g r a y s c a l e image , f i g u r e i s command t o v i e w
i m a g e s i n s e p a r a t e window .
228 title ( ’ R e c o n s t r u c t i o n o f an image a f t e r z e r o i n g
f i r s t , s e c o n d and t h i r d l e v e l d e t a i l c o e f f i c i e n t s
. ’ ) ; // t i t l e ( ) i s u s e d f o r p r o v i d i n g a t i t l e t o
an image .
229
230
231 // z e r o i n g a l l l e v e l d e t a i l coefficients .
232 rh4 (1: $ ,1: $ ) =0;
233 rv4 (1: $ ,1: $ ) =0;
234 rd4 (1: $ ,1: $ ) =0;
235
236
237 rh3 (1: $ ,1: $ ) =0;
238 rv3 (1: $ ,1: $ ) =0;
239 rd3 (1: $ ,1: $ ) =0;
240
241
242 rh2 (1: $ ,1: $ ) =0;
243 rv2 (1: $ ,1: $ ) =0;
244 rd2 (1: $ ,1: $ ) =0;
245
246 rh1 (1: $ ,1: $ ) =0;
247 rd1 (1: $ ,1: $ ) =0;
100
248 rv1 (1: $ ,1: $ ) =0;
249 Xhat = ra4 + rh4 + rv4 + rd4 + rh3 + rv3 + rd3 + rh2 +
rv2 + rd2 + rh1 + rv1 + rd1 ;
250 figure , ShowImage ( Xhat , ’ R e c o n s t r u c t i o n o f an image
a f t e r z e r o i n g a l l l e v e l d e t a i l c o e f f i c i e n t s . ’ ) ; //
ShowImage ( ) i s u s e d t o show g r a y s c a l e image ,
f i g u r e i s command t o v i e w i m a g e s i n s e p a r a t e
window .
251 title ( ’ R e c o n s t r u c t i o n o f an image a f t e r z e r o i n g a l l
l e v e l d e t a i l c o e f f i c i e n t s . ’ ) ; // t i t l e ( ) i s u s e d
f o r p r o v i d i n g a t i t l e t o an image .
101
Appendix
102