Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
28 views46 pages

Dip Lab Reports

The document describes experiments performed on digital images to demonstrate various digital image processing techniques. It includes code snippets to separate an image into red, green, and blue color channels. It also shows how to generate a histogram and perform histogram equalization on an image. The experiments cover other techniques like logical operations on images, grayscale conversion, and filtering.

Uploaded by

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

Dip Lab Reports

The document describes experiments performed on digital images to demonstrate various digital image processing techniques. It includes code snippets to separate an image into red, green, and blue color channels. It also shows how to generate a histogram and perform histogram equalization on an image. The experiments cover other techniques like logical operations on images, grayscale conversion, and filtering.

Uploaded by

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

Indian Institute of

Information Technology,
Nagpur

Digital Image Processing


Lab Reports

Submitted By :
Abhay Ganvir (BT19ECE017)
Semester 6
Electronics and Communication Engineering Dept.

Submitted To :
Dr. Rashmi Pandhare
Course Instructor
Contents

1 Experiment-1: Convert image into binary image; separate Image Layer


into red green and blue; convert Image to Grayscale; add 20 to gray
image; add binary image to gray scale . . . . . . . . . . . . . . . . . . 2
2 Experiment-2: Subtract an image with a blacked out section and the
original image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 Experiment-3: Perform logical operations on 2 images . . . . . . . . . 8
4 Experiment-4: Separate image layer into red green and blue layers . . 13
5 Experiment-5: Histogram Generation and Histogram Equalization . . 16
6 Experiment-6: Change contrast of an image . . . . . . . . . . . . . . 21
7 Experiment-7: Bit Plane Slicing . . . . . . . . . . . . . . . . . . . . . 24
8 Experiment-8: Shannon Fano . . . . . . . . . . . . . . . . . . . . . . 28
9 Experiment-9: Edge Detection . . . . . . . . . . . . . . . . . . . . . . 31
10 Experiment-10: Hoffman Coding . . . . . . . . . . . . . . . . . . . . . 36
11 Experiment-11: Arithmetic Coding . . . . . . . . . . . . . . . . . . . 40
12 Experiment-11: Image Segmentation . . . . . . . . . . . . . . . . . . 43

1
BT19ECE017 Abhay Ganvir

Experiment-1: Convert image into binary image; separate Image Layer


into red green and blue; convert Image to Grayscale; add 20 to gray
image; add binary image to gray scale

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

41 img new = b i n a r y + imgGray


42 p l t . imshow ( img new , cmap= ' gray ' )
43 p l t . show ( )
44
45 #add 20 t o g r a y s c a l e
46 g r a y 2 0 = 20 + imgGray
47 p l t . imshow ( g r a y 2 0 , cmap= ' gray ' )
48 p l t . show ( )

Figure 1: Input image

Output:

3
BT19ECE017 Abhay Ganvir

4
BT19ECE017 Abhay Ganvir

5
BT19ECE017 Abhay Ganvir

Experiment-2: Subtract an image with a blacked out section and the


original image

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 ( )

Figure 2: Input image

6
BT19ECE017 Abhay Ganvir

Output:

7
BT19ECE017 Abhay Ganvir

Experiment-3: Perform logical operations on 2 images

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

11 #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


12 R, G, B = i m g c i r c l e [ : , : , 0 ] , i m g c i r c l e [ : , : , 1 ] , i m g c i r c l e [ : , : , 2 ]
13 b i n a r y c i r c l e = (R+G+B) /3
14 f o r i in range (500) :
15 f o r j in range (500) :
16 i f b i n a r y c i r c l e [ i , j ] >10:
17 b i n a r y c i r c l e [ i , j ]=1
18 else :
19 b i n a r y c i r c l e [ i , j ]=0
20 p l t . imshow ( b i n a r y c i r c l e , cmap= ' gray ' )
21 p l t . show ( )
22

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

Figure 3: Input image

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

17 p l t . imshow (G, cmap= ' Greens ' )


18 p l t . show ( )

13
BT19ECE017 Abhay Ganvir

Figure 4: Input image

Output:

14
BT19ECE017 Abhay Ganvir

15
BT19ECE017 Abhay Ganvir

Experiment-5: Histogram Generation and Histogram Equalization

Code:

Listing 1: ”Histogram Generation” language


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 . i m a g e a s mpimg
3 import numpy a s np
4
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
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 ()

Listing 2: ”Histogram Equalization” language


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 . i m a g e a s mpimg
3 import numpy a s np
4

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

11 x = 0 .2989 ∗ R + 0 .5870 ∗ G + 0 .1140 ∗ B


12
13 p l t . i m s h o w ( x , cmap= ' gray ' )
14 plt.show ()
15
16 f o r i in range (410) :
17 f o r j in range (728) :
18 x [ i , j ] = round ( x [ i , j ] )
19
20
21 h i s = [ 0 ] ∗ 256
22 #o b t a i n t h e h i s t o g r a m
23 f o r i in range (410) :
24 f o r j in range (728) :
25 y = int (x [ i , j ])
26 his [ y ] = his [ y ] + 1
27

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

Figure 5: Input image

Output:

19
BT19ECE017 Abhay Ganvir

20
BT19ECE017 Abhay Ganvir

Experiment-6: Change contrast of an image

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

Figure 6: Input image

Output:

22
BT19ECE017 Abhay Ganvir

23
BT19ECE017 Abhay Ganvir

Experiment-7: Bit Plane Slicing

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

Figure 7: Input image

Output:

26
BT19ECE017 Abhay Ganvir

27
BT19ECE017 Abhay Ganvir

Experiment-8: Shannon Fano

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

11 s s=s s . / sum ( s s ) ; %i f o c c u r r e n c e s a r e i n p u t t e d , p r o b a b i l i t i e s a r e ...


gained
12 s s=s o r t ( s s , ' d e s c e n d ' ) ; %t h e p r o b a b i l i t i e s a r e s o r t e d i n ...
descending order
13
14 %s i l i n g= c e i l ( l o g 2 ( 1 / s s ( 1 ) ) ) ; %i n i t i a l l e n g t h i s computed
15
16 s i l i n g =l o g 2 ( 1 / s s ( 1 ) ) ; %i n i t i a l l e n g t h i s computed
17 s i l i n g =round ( s i l i n g , 1 , ' s i g n i f i c a n t ' ) ;
18
19 s f =0;
20 f a n o =0;
21 %i n i t i a l i z a t i o n s f o r Pk
22 n=1;Hx=0; %i n i t i a l i z a t i o n s f o r e n t r o p y H(X)
23
24 f o r i =1: l e n g t h ( s s )
25 Hx=Hx+ s s ( i ) ∗ l o g 2 ( 1 / s s ( i ) ) ; %s o l v i n g f o r e n t r o p y
26 end
27
28 f o r k=1: l e n g t h ( s s )
29 i n f o ( k )=−(l o g 2 ( s s ( k ) ) ) ; %I n f o r m a t i o n
30 end
31
32 f o r j =1: l e n g t h ( s s )−1
33 f a n o=f a n o+s s ( j ) ;
34 s f =[ s f 0 ] + [ z e r o s ( 1 , j ) f a n o ] ; %s o l v i n g f o r I n f o r m a t i o n f o r ...
e v e r y codeword
35 s i l i n g =[ s i l i n g 0 ] + [ z e r o s ( 1 , j ) c e i l ( l o g 2 ( 1 / s s ( j +1) ) ) ] ; %s o l v i n g ...
f o r l e n g t h e v e r y codeword
36 end
37
38 f o r r =1: l e n g t h ( s f )

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

50 c={ ' 0 ' , ' 1 ' } ;


51 d i s p ( ' Codeword ' ) ;
52 f o r i =1: l e n g t h ( hh )
53 u=1; %c o n v e r t i n g t h e ...
codes into a s t r i n g
54 f o r t=s i l i n g ( i ) : −1:1
55 f=f l o o r ( hh ( i ) / 1 0 ˆ ( t −1) ) ; %1001 ==>1 ( g e t t i n g ...
t h e f i r s t h i g h e s t u n i t o f a number )
56 hh ( i )=mod( hh ( i ) , 1 0 ˆ ( t −1) ) ; %1001 ==>001(...
e l i m i n a t i n g t h e f i r s t h i g h e s t u n i t o f a number )
57 i f f==1
58 i f u==1
59 d=c { 2 } ; %c o n v e r s i o n p a r t ( ...
num( 1 0 0 1 ) t o s t r ( 1 0 0 1 ) )
60 else
61 d=[d c { 2 } ] ;
62 end
63 else
64 i f u==1
65 d=c { 1 } ;
66 else
67 d=[d c { 1 } ] ;
68 end
69 end
70 codex { i , : } = { d } ;
71 u=u+1;
72 end
73 disp ( [ d ] )
74 end
75
76 t a o= s i l i n g ( 1 ) ∗ s s ( 1 ) ; %i n i t i a l i z a t i o n f o r codeword l e n g t h
77 f o r u=1: l e n g t h ( s s )−1 %computing f o r codeword l e n g t h
78 t a o=t a o+s i l i n g ( u+1)∗ s s ( u+1) ;
79 end
80 T=t a o /n ; %computing f o r a v e r a g e codeword l e n g t h

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

Experiment-9: Edge Detection

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

5 img = cv2 . imread ( ' img . j p g ' )


6
7 g r a y i m g=cv2 . c v t C o l o r ( img , cv2 .COLOR BGR2GRAY)
8
9 np . shape ( g r a y i m g )
10

11 p l t . imshow ( gray img , cmap= ' gray ' )


12 p l t . t i t l e ( ”Gray Image ” )
13 p l t . show ( )
14
15
16 d e f C a n n y d e t e c t o r ( img , weak th=None , s t r o n g t h=None ) :
17 # n o i c e r e d u c t i o n o f image
18 img=cv2 . G a u s s i a n B l u r ( img , ( 5 , 5 ) , 1 . 4 )
19
20 # c a l c u l a t i n g the gradient
21 gx=cv2 . S o b e l ( np . f l o a t 3 2 ( img ) , cv2 . CV 64F , 1 , 0 , 3 )
22 gy=cv2 . S o b e l ( np . f l o a t 3 2 ( img ) , cv2 . CV 64F , 0 , 1 , 3 )
23
24 # connvertion of c a r t e s i a n coordinate to polar
25 mag , ang=cv2 . c a r t T o P o l a r ( gx , gy , a n g l e I n D e g r e e s=True )
26
27 # s e t t i n g up maximum and minimum t h r e s h o l d
28 # f o r double t h r e s h o l d i n g
29 max mag=np . max(mag)
30 i f not weak th : weak th=max mag ∗ 0 . 1
31 i f not s t r o n g t h : s t r o n g t h=max mag ∗ 0 . 5
32
33 # g e t t i n g t h e d i m e n t i o n s o f i n p u t image
34 h e i g h t , width = img . shape
35
36 # l o o p i n g through e v e r y p i x e l
37 f o r i x i n r a n g e ( width ) :
38 f o r i y in range ( height ) :
39
40 g r a d a n g=ang [ i y , i x ]
41 g r a d a n g=abs ( grad ang −180) i f abs ( g r a d a n g ) >180 e l s e ...
abs ( g r a d a n g )

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

Figure 8: input image

Output:

34
BT19ECE017 Abhay Ganvir

35
BT19ECE017 Abhay Ganvir

Experiment-10: Hoffman Coding

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

68 # f u n c t i o n t o c a l c u l a t e t h e s p a c e d i f f e r e n c e between compressed ...


and non compressed data ”
69 d e f T o t a l G a i n ( data , c o d i n g ) :
70 b e f o r e c o m p r e s s i o n = l e n ( data ) ∗ 8 # t o t a l b i t s p a c e t o s t o r ...
t h e data b e f o r e c o m p r e s s i o n
71 after compression = 0
72 symbols = c o d i n g . k e y s ( )
73 f o r symbol i n symbols :
74 count = data . count ( symbol )
75 a f t e r c o m p r e s s i o n += count ∗ l e n ( c o d i n g [ symbol ] ) #...
c a l c u l a t e how many b i t i s r e q u i r e d f o r t h a t symbol i n t o t a l
76 p r i n t ( ” Space u s a g e b e f o r e c o m p r e s s i o n ( i n b i t s ) : ” , ...
before compression )
77 p r i n t ( ” Space u s a g e a f t e r c o m p r e s s i o n ( i n b i t s ) : ” , ...
after compression )
78
79

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

86 print (” probabilities : ” , probabilities )


87

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

Experiment-11: Arithmetic Coding

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 % i n ' newcpr ' v a r i a b l e we a r e t a k i n g


41 % ' cpr ' from ' 0 ' t i l l l a s t v a l u e o f ' p '

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

80 % g e t t i n g the tag value


81 % o f t h e matched c h a r a c t e r
82 r a n g e = h i g h − low ;
83 h i g h = low + ( r a n g e . ∗ i n t e r v a l ( pos , 2 ) ) ;
84 low = low + ( r a n g e . ∗ i n t e r v a l ( pos , 1 ) ) ;
85 i = i + 1;
86 break
87 end
88 end
89 end
90

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

Experiment-11: Image Segmentation

Code:

1 % This i s a program f o r e x t r a c t i n g o b j e c t s from an image . Written...


f o r v e h i c l e number p l a t e s e g m e n t a t i o n and e x t r a c t i o n
2
3 % U can u s e a t t a c h e d t e s t image f o r t e s t i n g
4 % i n p u t − g i v e t h e image f i l e name a s i n p u t . eg :− c a r 3 . j p g
5 clc ;
6 clear all ;
7 % k=i n p u t ( ' Enter t h e f i l e name ' , ' s ' ) ; % i n p u t image ; c o l o r image
8 % im=imread ( k ) ;
9 im = imread ( ' img . j p g ' ) ;
10 im1=r g b 2 g r a y ( im ) ;
11 im1=m e d f i l t 2 ( im1 , [ 3 3 ] ) ; %Median f i l t e r i n g t h e image t o remove ...
n o i s e%
12 BW = edge ( im1 , ' s o b e l ' ) ; %f i n d i n g e d g e s
13 [ imx , imy]= s i z e (BW) ;
14 msk=[0 0 0 0 0 ;
15 0 1 1 1 0;
16 0 1 1 1 0;
17 0 1 1 1 0;
18 0 0 0 0 0;];
19 B=conv2 ( d o u b l e (BW) , d o u b l e ( msk ) ) ; %Smoothing image t o r e d u c e t h e ...
number o f c o n n e c t e d components
20 L = b w l a b e l (B, 8 ) ;% C a l c u l a t i n g c o n n e c t e d components
21 mx=max(max(L) )
22 % There w i l l be mx c o n n e c t e d components . Here U can g i v e a v a l u e ...
between 1 and mx f o r L o r i n a l o o p you can e x t r a c t a l l ...
c o n n e c t e d components
23 [ r , c ] = f i n d (L==17) ;
24 rc = [ r c ] ;
25 [ sx sy ]= s i z e ( r c ) ;
26 n1=z e r o s ( imx , imy ) ;
27 f o r i =1: sx
28 x1=r c ( i , 1 ) ;
29 y1=r c ( i , 2 ) ;
30 n1 ( x1 , y1 ) =255;
31 end % S t o r i n g t h e e x t r a c t e d image i n an a r r a y
32 f i g u r e , imshow ( im ) ;
33 f i g u r e , imshow ( im1 ) ;
34 f i g u r e , imshow (B) ;
35 f i g u r e , imshow ( n1 , [ ] ) ;

43
BT19ECE017 Abhay Ganvir

Figure 9: Input Image

Output:

44
BT19ECE017 Abhay Ganvir

45

You might also like