Dip Lab Reports
Dip Lab Reports
Information Technology,
Nagpur
Submitted By :
Abhay Ganvir (BT19ECE017)
Semester 6
Electronics and Communication Engineering Dept.
Submitted To :
Dr. Rashmi Pandhare
Course Instructor
Contents
1
BT19ECE017 Abhay Ganvir
Code:
1 from m a t p l o t l i b import p y p l o t a s p l t
2 import m a t p l o t l i b . image a s mpimg
3
4 #r e a d image
5 img = mpimg . imread ( ' t e s t . j p g ' )
6
7 #o b t a i n t h e red , b l u e and g r e e n l a y e r s
8 R, G, B = img [ : , : , 0 ] , img [ : , : , 1 ] , img [ : , : , 2 ]
9 imgGray = (R+G+B) /3
10
11 #show r e d l a y e r
12 p l t . imshow (R, cmap= ' Reds ' )
13 p l t . show ( )
14
15 #show b l u e l a y e r
16 p l t . imshow (B, cmap= ' B l u e s ' )
17 p l t . show ( )
18
19 #show g r e e n l a y e r
20 p l t . imshow (G, cmap= ' Greens ' )
21 p l t . show ( )
22
23 #show g r a y s c a l e image
24 p l t . imshow ( imgGray , cmap= ' gray ' )
25 p l t . show ( )
26
27 #l o o p t o c o n v e r t t o b i n a r y
28 #a l l p i x e l s with v a l u e o v e r 10 w i l l be 1 and o t h e r z e r o
29 b i n a r y = (R+G+B) /3
30 f o r i in range (4526) :
31 f o r j in range (10000) :
32 i f b i n a r y [ i , j ] >10:
33 b i n a r y [ i , j ]=1
34 else :
35 b i n a r y [ i , j ]=0
36 #d i s p l a y b i n a r y image
37 p l t . imshow ( binary , cmap= ' gray ' )
38 p l t . show ( )
39
40 #add b i n a r y and g r a y s c a l e
2
BT19ECE017 Abhay Ganvir
Output:
3
BT19ECE017 Abhay Ganvir
4
BT19ECE017 Abhay Ganvir
5
BT19ECE017 Abhay Ganvir
Code:
1 from m a t p l o t l i b import p y p l o t a s p l t
2 import m a t p l o t l i b . image a s mpimg
3
4 #r e a d images
5 img = mpimg . imread ( ' t e s t . j p g ' )
6 img new = mpimg . imread ( ' t e s t 1 . j p g ' )
7
8 #o b t a i n t h e f i n a l image by s u b t r a c t i n g t h e o r i g i n a l and e d i t e d ...
image
9 i m g f i n a l= img − img new
10
11 #d i s p l a y t h e f i n a l r e s u l t
12 p l t . imshow ( i m g f i n a l )
13 p l t . show ( )
6
BT19ECE017 Abhay Ganvir
Output:
7
BT19ECE017 Abhay Ganvir
Code:
1 from m a t p l o t l i b import p y p l o t a s p l t
2 import m a t p l o t l i b . image a s mpimg
3 import numpy a s np
4 import o p e r a t o r
5
6 #r e a d images
7 i m g c i r c l e = mpimg . imread ( ' c i r c l e . j p g ' )
8 i m g r e c t a n g l e = mpimg . imread ( ' r e c t a n g l e . j p g ' )
9
10
23
24 #code below i s t o c o n v e r t c i r c l e image t o b i n a r y
25 R, G, B = i m g r e c t a n g l e [ : , : , 0 ] , i m g r e c t a n g l e [ : , : , 1 ] , ...
img rectangle [ : , : , 2 ]
26 b i n a r y r e c t a n g l e = (R+G+B) /3
27 f o r i in range (500) :
28 f o r j in range (500) :
29 i f b i n a r y r e c t a n g l e [ i , j ] >10:
30 b i n a r y r e c t a n g l e [ i , j ]=1
31 else :
32 b i n a r y r e c t a n g l e [ i , j ]=0
33 p l t . imshow ( b i n a r y r e c t a n g l e , cmap= ' gray ' )
34 p l t . show ( )
35
36
37 #l o g i c a l and
38 img and = b i n a r y c i r c l e . a s t y p e ( i n t ) & b i n a r y r e c t a n g l e . a s t y p e ( i n t ...
)
39 p l t . imshow ( img and , cmap= ' gray ' )
40 p l t . show ( )
8
BT19ECE017 Abhay Ganvir
41
42 #l o g i c a l o r
43 img or = b i n a r y c i r c l e . astype ( i n t ) | b i n a r y r e c t a n g l e . astype ( i n t )
44 p l t . imshow ( img or , cmap= ' gray ' )
45 p l t . show ( )
46
47 #l o g i c a l xor
48 i m g x o r = b i n a r y c i r c l e . a s t y p e ( i n t ) ˆ b i n a r y r e c t a n g l e . a s t y p e ( i n t ...
)
49 p l t . imshow ( img xor , cmap= ' gray ' )
50 p l t . show ( )
51
52 #l o g i c a l nand
53 img nand = ¬img and
54 p l t . imshow ( img nand , cmap= ' gray ' )
55 p l t . show ( )
56
57 #l o g i c a l nor
58 img nor = ¬i m g o r
59 p l t . imshow ( img nor , cmap= ' gray ' )
60 p l t . show ( )
61
62 #l o g i c a l xnor
63 img xnor = ¬i m g x o r
64 p l t . imshow ( img xnor , cmap= ' gray ' )
65 p l t . show ( )
9
BT19ECE017 Abhay Ganvir
Output:
10
BT19ECE017 Abhay Ganvir
11
BT19ECE017 Abhay Ganvir
12
BT19ECE017 Abhay Ganvir
Experiment-4: Separate image layer into red green and blue layers
Code:
1 from m a t p l o t l i b import p y p l o t a s p l t
2 import m a t p l o t l i b . image a s mpimg
3
4 #r e a d image
5 img = mpimg . imread ( ' t e s t . j p g ' )
6
7 #r e a d t h e red , g r e e n and b l u e c o l o u r s p a c e s d i f f e r e n t l y
8 R, G, B = img [ : , : , 0 ] , img [ : , : , 1 ] , img [ : , : , 2 ]
9
10 #d i s p l a y t h e images s e p a r a t e l y
11 p l t . imshow (R, cmap= ' Reds ' )
12 p l t . show ( )
13
14 p l t . imshow (B, cmap= ' B l u e s ' )
15 p l t . show ( )
16
13
BT19ECE017 Abhay Ganvir
Output:
14
BT19ECE017 Abhay Ganvir
15
BT19ECE017 Abhay Ganvir
Code:
8 #o b t a i n t h e red , g r e e n and b l u e c o l o u r s p a c e s s e p a r a t e l y
9 R, G, B = img [ : , : , 0 ] , img [ : , : , 1 ] , img [ : , : , 2 ]
10
11
12 r e d h i s = [ 0 ] ∗ 256
13 #o b t a i n t h e r e d h i s t o g r a m
14 f o r i in range (410) :
15 f o r j in range (728) :
16 r e d h i s [R[ i , j ] ] = r e d h i s [R[ i , j ] ] + 1
17 #p l o t t h e h i s t o g r a m
18 p l t . t i t l e ( ” Red Histogram ” )
19 p l t . x l a b e l (” Pixel I n t e n s i t y ”)
20 p l t . y l a b e l ( ” P i x c e l Frequency ” )
21 p l t . p l o t ( r a n g e ( 2 5 6 ) , r e d h i s , c o l o r =”r e d ” )
22 plt.show ()
23
24
25 b l u e h i s = [ 0 ] ∗ 256
26 #o b t a i n t h e b l u e h i s t o g r a m
27 f o r i in range (410) :
28 f o r j in range (728) :
29 b l u e h i s [B[ i , j ]]= b l u e h i s [B[ i , j ] ] + 1
30 #p l o t t h e h i s t o g r a m
31 p l t . t i t l e ( ” Blue Histogram ” )
32 p l t . x l a b e l (” Pixel I n t e n s i t y ”)
33 p l t . y l a b e l ( ” P i x c e l Frequency ” )
34 p l t . p l o t ( r a n g e ( 2 5 6 ) , b l u e h i s , c o l o r =”b l u e ” )
35 plt.show ()
36
37
38 g r e e n h i s = [ 0 ] ∗ 256
39 #o b t a i n t h e g r e e n h i s t o g r a m
40 f o r i in range (410) :
41 f o r j in range (728) :
16
BT19ECE017 Abhay Ganvir
42 g r e e n h i s [G[ i , j ] ] = g r e e n h i s [G[ i , j ] ] + 1
43 #p l o t t h e h i s t o g r a m
44 p l t . t i t l e ( ” Green Histogram ” )
45 p l t . x l a b e l (” Pixel I n t e n s i t y ”)
46 p l t . y l a b e l ( ” P i x c e l Frequency ” )
47 p l t . p l o t ( r a n g e ( 2 5 6 ) , g r e e n h i s , c o l o r =”g r e e n ” )
48 plt.show ()
5 #r e a d image
6 img = mpimg.imread ( ' t e s t . j p g ' )
7
8 #o b t a i n t h e red , g r e e n and b l u e c o l o u r s p a c e s s e p a r a t e l y
9 R, G, B = img [ : , : , 0 ] , img [ : , : , 1 ] , img [ : , : , 2 ]
10
28
29 p = [ 0 ] ∗ 256
30 f o r i in range (256) :
31 p [ i ] = his [ i ]/(410∗728)
32
33
34 c d f = [ 0 ] ∗ 256
35 f o r i in range (256) :
36 f o r j i n r a n g e (1+ i ) :
37 cdf [ i ] = cdf [ i ] + his [ j ] ;
38
17
BT19ECE017 Abhay Ganvir
39
40 cdf min = cdf [ 0 ] ;
41 f o r i in range (256) :
42 i f ( cdf [ i ] < cdf min ) :
43 cdf min = cdf [ i ] ;
44
45
46
47 h = [ 0 ] ∗ 256
48 f o r i in range (256) :
49 h [ i ] = round ( ( ( c d f [ i ]− c d f m i n ) / ( ( 4 1 0 ∗ 7 2 8 )−c d f m i n ) ) ∗ 2 5 5 )
50
51
52 l e n h i s=l e n ( h i s )
53 p l t . t i t l e ( ” O r i g i n a l Histogram ” )
54 p l t . x l a b e l (” Pixel I n t e n s i t y ”)
55 p l t . y l a b e l ( ” P i x e l Frequency ” )
56 p l t . p l o t ( r a n g e ( l e n h i s ) , h i s , c o l o r =”b l a c k ” )
57 plt.show ()
58
59 l e n h=l e n ( h )
60 p l t . t i t l e ( ” E q u a l i z e d Values ” )
61 p l t . x l a b e l (” O r i g i n a l Pixel I n t e n s i t y ”)
62 p l t . y l a b e l (” Equalized Pixel I n t e n s i t y ”)
63 p l t . p l o t ( r a n g e ( l e n h ) , h , c o l o r =”b l a c k ” )
64 plt.show ()
65
66 y=x
67 f o r i in range (410) :
68 f o r j in range (728) :
69 f o r k in range (256) :
70 i f x [ i , j ] == k :
71 y[ i , j ] = h[k]
72
73 p l t . i m s h o w ( y , cmap= ' gray ' )
74 plt.show ()
18
BT19ECE017 Abhay Ganvir
Output:
19
BT19ECE017 Abhay Ganvir
20
BT19ECE017 Abhay Ganvir
Code:
1 from m a t p l o t l i b import p y p l o t a s p l t
2 import m a t p l o t l i b . image a s mpimg
3 import numpy a s np
4
5 #r e a d image
6 img = mpimg . imread ( ' img . j p g ' )
7
8
9 #I n c r e a s e d c o n t r a s t
10 c o n i n c = img ∗2
11
12 p l t . t i t l e ( ' Increased Contrast ' )
13 p l t . imshow ( c o n i n c , cmap= ' gray ' )
14 p l t . show ( )
15
16
17 #d e c r e a s e d c o n t r a s t
18 c o n d e c = img /2
19
20 p l t . t i t l e ( ' Decreased Contrast ' )
21 p l t . imshow ( co n de c , cmap= ' gray ' )
22 p l t . show ( )
21
BT19ECE017 Abhay Ganvir
Output:
22
BT19ECE017 Abhay Ganvir
23
BT19ECE017 Abhay Ganvir
Code:
1 % c l e a r i n g t h e output s c r e e n
2 clc ;
3
4 % r e a d i n g image ' s p i x e l i n c
5 c = imread ( ' img . j p g ' ) ;
6
7 %c o n v e r t i n g t o g r a y s c a l e :
8 img = r g b 2 g r a y ( c ) ;
9
10 % e x t r a c t i n g a l l b i t one by one
11 % from 1 s t t o 8 th i n v a r i a b l e
12 % from c1 t o c8 r e s p e c t i v e l y
13 for i = 1:1440
14 for j = 1:665
15 temp1 = d e c 2 b i n ( img ( i , j ) , 8 ) ;
16 temp2 = num2str ( temp1 ) ;
17 c1 ( i , j ) = str2num ( temp2 ( 8 ) ) ;
18 c2 ( i , j ) = str2num ( temp2 ( 7 ) ) ;
19 c3 ( i , j ) = str2num ( temp2 ( 6 ) ) ;
20 c4 ( i , j ) = str2num ( temp2 ( 5 ) ) ;
21 c5 ( i , j ) = str2num ( temp2 ( 4 ) ) ;
22 c6 ( i , j ) = str2num ( temp2 ( 3 ) ) ;
23 c7 ( i , j ) = str2num ( temp2 ( 2 ) ) ;
24 c8 ( i , j ) = str2num ( temp2 ( 1 ) ) ;
25 end
26 end
27
28 % combining image a g a i n t o form e q u i v a l e n t t o o r i g i n a l g r a y s c a l e ...
image
29 c c = ( 2 ∗ ( 2 ∗ ( 2 ∗ ( 2 ∗ ( 2 ∗ ( 2 ∗ ( 2 ∗ c8 + c7 ) + c6 ) + c5 ) + c4...
) + c3 ) + c2 ) + c1 ) ;
30
31 % p l o t t i n g o r i g i n a l image i n f i r s t s u b p l o t
32 subplot (2 , 5 , 1) ;
33 imshow ( img ) ;
34 t i t l e ( ' O r i g i n a l Image ' ) ;
35
36 % p l o t t i n g b i n a r y image having e x t r a c t e d b i t from 1 s t t o 8 th
37 % i n s u b p l o t from 2nd t o 9 th
38 subplot (2 , 5 , 2) ;
39 imshow ( c1 ) ;
40 t i t l e ( ' B i t Plane 1 ' ) ;
24
BT19ECE017 Abhay Ganvir
41 subplot (2 , 5 , 3) ;
42 imshow ( c2 ) ;
43 t i t l e ( ' B i t Plane 2 ' ) ;
44 subplot (2 , 5 , 4) ;
45 imshow ( c3 ) ;
46 t i t l e ( ' B i t Plane 3 ' ) ;
47 subplot (2 , 5 , 5) ;
48 imshow ( c4 ) ;
49 t i t l e ( ' B i t Plane 4 ' ) ;
50 subplot (2 , 5 , 6) ;
51 imshow ( c5 ) ;
52 t i t l e ( ' B i t Plane 5 ' ) ;
53 subplot (2 , 5 , 7) ;
54 imshow ( c6 ) ;
55 t i t l e ( ' B i t Plane 6 ' ) ;
56 subplot (2 , 5 , 8) ;
57 imshow ( c7 ) ;
58 t i t l e ( ' B i t Plane 7 ' ) ;
59 subplot (2 , 5 , 9) ;
60 imshow ( c8 ) ;
61 t i t l e ( ' B i t Plane 8 ' ) ;
62
63 % p l o t t i n g recombined image i n 10 th s u b p l o t
64 subplot (2 , 5 , 10) ;
65 imshow ( u i n t 8 ( c c ) ) ;
66 t i t l e ( ' Recombined Image ' ) ;
25
BT19ECE017 Abhay Ganvir
Output:
26
BT19ECE017 Abhay Ganvir
27
BT19ECE017 Abhay Ganvir
Code:
1 %Shannon−Fano Encoding
2 clc ;
3 clear all ;
4 close all ;
5
6 d i s p ( ' Enter t h e p r o b a b i l i t i e s : ' ) ;
7 ss =[0.4 0.2 0.12 0.08 0.08 0.08 0 . 0 4 ]
8
9 %o u t p u t s = s t r i n g o f codewords , a v e r a g e codeword l e n g t h
10
28
BT19ECE017 Abhay Ganvir
39 e s f=s f ( r ) ;
40 f o r p=1: s i l i n g ( r )
41 e s f=mod( e s f , 1 ) ∗ 2 ;
42 h ( p )=e s f −mod( e s f , 1 ) ; %c o n v e r t i n g Pk i n t o a b i n a r y number
43 end
44 hh ( r )=h ( 1 ) ∗ 1 0 ˆ ( s i l i n g ( r ) −1) ; %i n i t i a l i z t i o n f o r making t h e ...
b i n a r y a whole number
45 f o r t =2: s i l i n g ( r )
46 hh ( r )=hh ( r )+h ( t ) ∗ 1 0 ˆ ( s i l i n g ( r )−t ) ; %making t h e b i n a r y ...
a whole number
47 end %e . g . 0 . 1 1 0 1 ==> ...
1101
48 end
49
29
BT19ECE017 Abhay Ganvir
81 B=[ f l i p u d ( r o t 9 0 ( s s ) ) , f l i p u d ( r o t 9 0 ( s i l i n g ) ) , f l i p u d ( r o t 9 0 ( i n f o ) ) ] ;
82 disp ( [ ' Probability ' , ' Length ' , ' Information ' ] )
83 d i s p (B)
84 d i s p ( [ ' Entropy H(X) = ' , num2str (Hx) , ' b i t s / symbol ' ] )
85 d i s p ( [ ' Average l e n g t h , L = ' , num2str (T) , ' b i t s / symbol ' ] )
86 e f f =((Hx/T) ∗ 1 0 0 ) ; %Coding e f f i c i e n c y
87 d i s p ( [ ' E f f i c i e n c y= ' , num2str ( e f f ) , '%' ] )
88 redu=100− e f f ; %Redundancy
89 d i s p ( [ ' Redundancy= ' , num2str ( redu ) , '%' ] )
Output:
30
BT19ECE017 Abhay Ganvir
Code:
1 import cv2
2 import m a t p l o t l i b . p y p l o t a s p l t
3 import numpy a s np
4
31
BT19ECE017 Abhay Ganvir
42
43 # s e l e c c t i n g the neighbours of t a r g e t p i x e l
44
45 # in x axis direction
46 i f grad ang≤ 2 2 . 5 :
47 n e i g h b 1 x , n e i g h b 1 y=i x −1, i y
48 neighb 2 x , neighb 2 y = i x + 1 , i y
49
50 # top−r i g h t ( d i g o n a l 1 ) direction
51 e l i f grad ang >22.5 and grad ang≤ ( 2 2 . 5 + 45) :
52 neighb 1 x , neighb 1 y = i x −1, i y −1
53 neighb 2 x , neighb 2 y = i x + 1, i y + 1
54
55 # In y−a x i s d i r e c t i o n
56 e l i f grad ang >(22.5 + 4 5 ) and g r a d a n g ≤ ( 2 2 . 5 + 9 0 ) :
57 n e i g h b 1 x , n e i g h b 1 y = i x , i y −1
58 neighb 2 x , neighb 2 y = i x , i y + 1
59
60 # top l e f t ( d i a g o n a l −2) d i r e c t i o n
61 e l i f grad ang >(22.5 + 9 0 ) and g r a d a n g ≤ ( 2 2 . 5 + 1 3 5 ) :
62 n e i g h b 1 x , n e i g h b 1 y = i x −1, i y + 1
63 n e i g h b 2 x , n e i g h b 2 y = i x + 1 , i y −1
64
65 # Now i t r e s t a r t s t h e c y c l e
66 e l i f grad ang >(22.5 + 1 3 5 ) and g r a d a n g ≤ ( 2 2 . 5 + 1 8 0 ) :
67 n e i g h b 1 x , n e i g h b 1 y = i x −1, i y
68 neighb 2 x , neighb 2 y = i x + 1 , i y
69
70 # Non−maximum s u p p r e s s i o n s t e p
71 i f width>n e i g h b 1 x ≥ 0 and h e i g h t >n e i g h b 1 y ≥ 0 :
72 i f mag [ i y , i x ]<mag [ n e i g h b 1 y , n e i g h b 1 x ] :
73 mag [ i y , i x ]= 0
74 continue
75
76 i f width>n e i g h b 2 x ≥ 0 and h e i g h t >n e i g h b 2 y ≥ 0 :
77 i f mag [ i y , i x ]<mag [ n e i g h b 2 y , n e i g h b 2 x ] :
78 mag [ i y , i x ]= 0
79
80 w e a k i d s=np . z e r o s l i k e ( img )
81 s t r o n g i d s=np . z e r o s l i k e ( img )
82 i d s=np . z e r o s l i k e ( img )
83
84
85 # double t h r e s h o l d i n g s t e p s
86 f o r i x i n r a n g e ( width ) :
87 f o r i y in range ( height ) :
88 grad mag=mag [ i y , i x ]
89
90 i f grad mag<weak th :
32
BT19ECE017 Abhay Ganvir
91 mag [ i y , i x ]= 0
92 e l i f s t r o n g t h >grad mag ≥ weak th :
93 i d s [ i y , i x ]= 1
94 else :
95 i d s [ i y , i x ]= 2
96
97 r e t u r n mag
98
99 canny img=C a n n y d e t e c t o r ( g r a y i m g )
100
101 p l t . imshow ( canny img , cmap= ' gray ' )
102 p l t . t i t l e ( ” Edges ” )
103 p l t . show ( )
33
BT19ECE017 Abhay Ganvir
Output:
34
BT19ECE017 Abhay Ganvir
35
BT19ECE017 Abhay Ganvir
Code:
1 '''
2 The a l g o r i t h m f o l l o w s t h r e e s t e p p r o c e s s :
3 1 . p r o b a b i l i t y c a l c u l a t i o n and o r d e r i n g t h e symbols
4 2 . Binary t r e e t r a n s f o r m a t i o n
5 3 . A s s i g n i n g c o d e s t o t h e symbols
6 '''
7
8
9 # p r o b a b i l i t y c a l c u l a t i o n and o r d e r i n g t h e symbols
10 data=”AAAAAAABCCCCCCDDEEEEE”
11 c l a s s Node :
12 def i n i t ( s e l f , prob , symbol , l e f t =None , r i g h t=None ) :
13 # p r o b a b i l i t y o f symbol
14 s e l f . prob=prob
15 # symbol
16 s e l f . symbol=symbol
17 # l e f t node
18 s e l f . l e f t=l e f t
19 # r i g h t node
20 s e l f . r i g h t=r i g h t
21 # tree d ir ect io n (0/1)
22 s e l f . code= ' '
23
24
25 c o d e s=d i c t ( )
26
27
28 d e f C a l c u l a t e C o d e s ( node , v a l= ' ' ) :
29 # huffman code f o r c u r r e n t node
30 newVal = v a l + s t r ( node . code )
31
32 i f ( node . l e f t ) :
33 C a l c u l a t e C o d e s ( node . l e f t , newVal )
34 i f ( node . r i g h t ) :
35 C a l c u l a t e C o d e s ( node . r i g h t , newVal )
36
37 i f ( not node . l e f t and not node . r i g h t ) :
38 c o d e s [ node . symbol ] = newVal
39
40 return codes
41
42
36
BT19ECE017 Abhay Ganvir
43
44 # f u n c t i o n t o c a l c u l a t e t h e p r o b a b i l i t i e s o f symbols i n g i v e n ...
data
45 d e f C a l c u l a t e P r o b a b i l i t y ( data ) :
46 symbols = d i c t ( )
47 f o r e l e m e n t i n data :
48 i f symbols . g e t ( e l e m e n t ) == None :
49 symbols [ e l e m e n t ] = 1
50 else :
51 symbols [ e l e m e n t ] += 1
52 r e t u r n symbols
53
54
55
56 # h e l p e r f u n c t i o n t o o b t a i n t h e encoded output
57 d e f Output Encoded ( data , c o d i n g ) :
58 encoding output = [ ]
59 f o r c i n data :
60 # p r i n t ( c o d i n g [ c ] , end = ' ' )
61 e n c o d i n g o u t p u t . append ( c o d i n g [ c ] )
62
63 s t r i n g = ' ' . j o i n ( [ s t r ( item ) f o r item i n e n c o d i n g o u t p u t ] )
64 return string
65
66
67
80
81 d e f Huffman Encoding ( data ) :
82 s y m b o l w i t h p r o b s = C a l c u l a t e P r o b a b i l i t y ( data )
83 symbols = s y m b o l w i t h p r o b s . k e y s ( )
84 p r o b a b i l i t i e s = symbol with probs . values ()
85 p r i n t ( ” symbols : ” , symbols )
37
BT19ECE017 Abhay Ganvir
88 nodes = [ ]
89
90 # c o n v e r t i n g symbols and p r o b a b i l i t i e s i n t o huffman t r e e ...
nodes
91 f o r symbol i n symbols :
92 nodes . append ( Node ( s y m b o l w i t h p r o b s . g e t ( symbol ) , symbol ) )
93
94 w h i l e l e n ( nodes ) > 1 :
95 # s o r t a l l t h e nodes i n a s c e n d i n g o r d e r based on t h e i r ...
probability
96 nodes = s o r t e d ( nodes , key=lambda x : x . prob )
97 # f o r node i n nodes :
98 # p r i n t ( node . symbol , node . prob )
99
100 # p i c k 2 s m a l l e s t nodes
101 r i g h t = nodes [ 0 ]
102 l e f t = nodes [ 1 ]
103
104 l e f t . code = 0
105 r i g h t . code = 1
106
107 # combine t h e 2 s m a l l e s t nodes t o c r e a t e new node
108 newNode = Node ( l e f t . prob+r i g h t . prob , l e f t . symbol+r i g h t . ...
symbol , l e f t , r i g h t )
109
110 nodes . remove ( l e f t )
111 nodes . remove ( r i g h t )
112 nodes . append ( newNode )
113
114 h u f f m a n e n c o d i n g = C a l c u l a t e C o d e s ( nodes [ 0 ] )
115 p r i n t ( ” symbols with c o d e s ” , h u f f m a n e n c o d i n g )
116 T o t a l G a i n ( data , h u f f m a n e n c o d i n g )
117 e n c o d e d o u t p u t = Output Encoded ( data , h u f f m a n e n c o d i n g )
118 r e t u r n e n c od e d o ut p u t , nodes [ 0 ]
119
120
121
122 d e f Huffman Decoding ( e n co d ed d at a , h u f f m a n t r e e ) :
123 tree head = huffman tree
124 decoded output = [ ]
125 f o r x in encoded data :
126 i f x == ' 1 ' :
127 huffman tree = huffman tree . right
128 e l i f x == ' 0 ' :
129 huffman tree = huffman tree . l e f t
130 try :
131 i f h u f f m a n t r e e . l e f t . symbol == None and h u f f m a n t r e e . ...
38
BT19ECE017 Abhay Ganvir
r i g h t . symbol == None :
132 pass
133 except AttributeError :
134 d e c o d e d o u t p u t . append ( h u f f m a n t r e e . symbol )
135 huffman tree = tree head
136
137 s t r i n g = ' ' . j o i n ( [ s t r ( item ) f o r item i n d e c o d e d o u t p u t ] )
138 return string
139
140
141
142 encoding , t r e e=Huffman Encoding ( data )
143
144 p r i n t ( ” Encoded output ” , e n c o d i n g )
145 p r i n t ( ” Decoded Output ” , Huffman Decoding ( encoding , t r e e ) )
Output:
39
BT19ECE017 Abhay Ganvir
Code:
1 % input s t r i n g
2 s t r = 'ASSDDDASASD ' ;
3 f p r i n t f ( ' The e n t e r e d s t r i n g i s : %s \n ' , s t r ) ;
4
5 % length of the s t r i n g
6 len = length ( str ) ;
7 f p r i n t f ( ' The l e n g t h o f t h e s t r i n g i s : %d\n ' , l e n ) ;
8
9 % g e t unique c h a r a c t e r s from t h e s t r i n g
10 u = unique ( s t r ) ;
11 f p r i n t f ( ' The unique c h a r a c t e r s a r e : %s \n ' , u ) ;
12
13 % l e n g t h o f t h e unique c h a r a c t e r s s t r i n g
14 len unique = length (u) ;
15 f p r i n t f ( ' The l e n g t h o f unique c h a r a c t e r s t r i n g i s : %d\n ' , ...
len unique ) ;
16
17 % G e n e r a l lookup t a b l e
18
19 % g e t z e r o s o f l e n g t h o f unique c h a r a c t e r s
20 z = zeros (1 , len unique ) ;
21 p = zeros (1 , len unique ) ;
22
23 for i = 1 : len unique
24
25 % i n ' z ' v a r i a b l e we w i l l f i n d t h e
26 % o c c u r r e n c e o f each c h a r a c t e r s from ' s t r '
27 z ( i ) = length ( f i n d s t r ( str , u( i ) ) ) ;
28
29 % i n ' p ' v a r i a b l e we w i l l g e t
30 % probability of those occurrences
31 p( i ) = z ( i ) / len ;
32 end
33 display ( z ) ;
34 display (p) ;
35
36 % i n ' cpr ' v a r i a b l e we w i l l g e t t h e c u m u l a t i v e
37 % summation o f ' p ' from ' 1 ' t i l l l a s t v a l u e o f ' p '
38 c p r = cumsum ( p ) ;
39
40
BT19ECE017 Abhay Ganvir
42 newcpr = [ 0 c p r ] ;
43
44 d i s p l a y ( cpr ) ;
45 d i s p l a y ( newcpr ) ;
46
47 % make t a b l e t i l l ' l e n u n i q u e ' s i z e
48 for i = 1 : len unique
49
50 % i n f i r s t column we a r e then
51 % p l a c i n g ' newcpr ' v a l u e s
52 i n t e r v a l ( i , 1 ) = newcpr ( i ) ;
53
54 % i n s e c o n d column we a r e
55 % p l a c i n g ' cpr ' v a l u e s
56 i n t e r v a l ( i , 2) = cpr ( i ) ;
57 end
58
59 % D i s p l a y i n g t h e lookup t a b l e
60 d i s p l a y ( ' The l o o k i p t a b l e i s : ' )
61 display ( interval ) ;
62
63 % Encoder Table
64
65 low = 0 ;
66 high = 1 ;
67 for i = 1 : len
68 for j = 1 : len unique
69
70 % i f t h e v a l u e from ' s t r '
71 % matches with ' u ' then
72 i f s t r ( i ) == u ( j ) ;
73 pos = j ;
74 j = j + 1;
75
76 % d i s p l a y i n g t h e matched l e n g t h
77 % o f unique c h a r a c t e r s
78 d i s p l a y ( pos ) ;
79
41
BT19ECE017 Abhay Ganvir
91 % d i s p l a y i n g tag value
92 t a g = low ;
93 d i s p l a y ( tag ) ;
Output:
42
BT19ECE017 Abhay Ganvir
Code:
43
BT19ECE017 Abhay Ganvir
Output:
44
BT19ECE017 Abhay Ganvir
45