1
+ # coding: utf8
2
+ # Python implementation of basic image processing
3
+ # Author: Caio Cesar Viana da Silva
4
+ # Install scikit-image: pip install scikit-image
5
+
6
+ # import skimage.io as io
7
+ # from matplotlib import pyplot as plt
8
+ # import numpy as np
9
+ # from skimage.transform import AffineTransform, warp
10
+ # import skimage.transform as ski
11
+
12
+ #OPENING AN IMAGE
13
+
14
+ def open_img (img_path ):
15
+ import skimage .io as io
16
+
17
+ img = io .imread (img_path )
18
+ io .imshow (img )
19
+ io .show ()
20
+ return img
21
+
22
+
23
+ #VISUALIZE HISTOGRAM
24
+
25
+ def histogram_img (img_path ):
26
+ import skimage .io as io
27
+ from matplotlib import pyplot as plt
28
+
29
+ img = io .imread (img_path )
30
+ plt .hist (img .ravel (),256 ,[0 ,256 ])
31
+ plt .show ()
32
+
33
+ #RGB HISTOGRAM
34
+
35
+ def histogram_rgb_img (img_path ):
36
+ import skimage .io as io
37
+ from matplotlib import pyplot as plt
38
+
39
+ img = io .imread (img_path )
40
+ color = [ 'r' ,'g' ,'b' ]
41
+ for i , c in enumerate (color ) :
42
+ plt .hist (img [:,:,i ].flatten (),256 , color = c )
43
+ plt .show ()
44
+
45
+
46
+ #RGB TO GRAYSCALE
47
+
48
+ def rgb_2_gray (img_path ):
49
+ import numpy as np
50
+ import matplotlib .pyplot as plt
51
+
52
+ img = open_img (img_path )
53
+ gray = np .dot (img [...,:3 ], [0.299 , 0.587 , 0.114 ])
54
+ plt .imshow (gray , cmap = plt .get_cmap ('gray' ))
55
+ plt .show ()
56
+
57
+ #SCALING IMAGE
58
+
59
+ def scaling_img (img_path ):
60
+ import skimage .io as io
61
+ import matplotlib .pyplot as plt
62
+
63
+ fig = plt .figure (figsize = (1 ,8 ))
64
+ img = io .imread (img_path )
65
+ plt .imshow (img )
66
+ plt .show ()
67
+
68
+
69
+ #TRANSLATING IMAGE
70
+
71
+ def translating_img (img_path , vector ):
72
+ import matplotlib .pyplot as plt
73
+ from skimage .transform import AffineTransform , warp
74
+
75
+ img = open_img (img_path )
76
+ transform = AffineTransform (translation = vector )
77
+ shifted = warp (img , transform , mode = 'constant' , preserve_range = True )
78
+ plt .imshow (shifted )
79
+ plt .show ()
80
+
81
+ #ROTATING IMAGE
82
+
83
+ def rotating_img (img_path , degree ):
84
+ import skimage .io as io
85
+ import skimage .transform as ski
86
+
87
+ img = open_img (img_path )
88
+ imgR = ski .rotate (img ,degree )
89
+ io .imshow (imgR )
90
+ io .show ()
91
+
92
+
93
+ def main ():
94
+
95
+ open_img ('test.jpg' )
96
+ histogram_img ('test.jpg' )
97
+ histogram_rgb_img ('test.jpg' )
98
+ rgb_2_gray ('test.jpg' )
99
+ scaling_img ('test.jpg' )
100
+ translating_img ('test.jpg' ,[- 100 , - 100 ])
101
+ rotating_img ('test.jpg' ,45 )
102
+
103
+
104
+
105
+
106
+ if __name__ == "__main__" :
107
+ main ()
0 commit comments