4
4
5
5
def extractImage (path ):
6
6
# error handller if the intended path is not found
7
- image = cv2 .imread (path , cv2 .IMREAD_GRAYSCALE );
7
+ image = cv2 .imread (path , cv2 .IMREAD_GRAYSCALE )
8
8
return image
9
9
10
10
def checkImage (image ):
@@ -18,27 +18,23 @@ def checkImage(image):
18
18
19
19
"""
20
20
if len (image .shape ) > 2 :
21
- print ("ERROR: non-binary image (RGB)" );
22
- sys .exit ();
21
+ print ("ERROR: non-binary image (RGB)" ); sys .exit ();
23
22
24
- smallest = image .min (axis = 0 ).min (axis = 0 ); # lowest pixel value; should be 0 (black)
25
- largest = image .max (axis = 0 ).max (axis = 0 ); # highest pixel value; should be 1 (white)
23
+ smallest = image .min (axis = 0 ).min (axis = 0 ) # lowest pixel value: 0 (black)
24
+ largest = image .max (axis = 0 ).max (axis = 0 ) # highest pixel value: 1 (white)
26
25
27
26
if (smallest == 0 and largest == 0 ):
28
- print ("ERROR: non-binary image (all black)" );
29
- sys .exit ();
27
+ print ("ERROR: non-binary image (all black)" ); sys .exit ()
30
28
elif (smallest == 255 and largest == 255 ):
31
- print ("ERROR: non-binary image (all white)" );
32
- sys .exit ();
29
+ print ("ERROR: non-binary image (all white)" ); sys .exit ()
33
30
elif (smallest > 0 or largest < 255 ):
34
- print ("ERROR: non-binary image (grayscale)" );
35
- sys .exit ();
31
+ print ("ERROR: non-binary image (grayscale)" ); sys .exit ()
36
32
else :
37
33
return True
38
34
39
35
class Toolbox :
40
36
def __init__ (self , image ):
41
- self .image = image ;
37
+ self .image = image
42
38
43
39
@property
44
40
def printImage (self ):
@@ -60,15 +56,15 @@ def displayImage(self):
60
56
Display the image on a window
61
57
Press any key to exit
62
58
"""
63
- cv2 .imshow ('Displayed Image' , self .image );
64
- cv2 .waitKey (0 );
65
- cv2 .destroyAllWindows ();
59
+ cv2 .imshow ('Displayed Image' , self .image )
60
+ cv2 .waitKey (0 )
61
+ cv2 .destroyAllWindows ()
66
62
67
63
def saveImage (self , title , extension ):
68
64
"""
69
65
Save as a specific image format (bmp, png, or jpeg)
70
66
"""
71
- cv2 .imwrite ("{}.{}" .format (title ,extension ), self .image );
67
+ cv2 .imwrite ("{}.{}" .format (title ,extension ), self .image )
72
68
73
69
def morph_open (self , image , kernel ):
74
70
"""
@@ -102,13 +98,11 @@ def trimap(image, name, size, number, erosion=False):
102
98
the last argument is optional; i.e., how many iterations will the image get eroded
103
99
Output : a trimap
104
100
"""
105
- checkImage (image );
106
-
107
- row = image .shape [0 ];
108
- col = image .shape [1 ];
109
-
110
- pixels = 2 * size + 1 ; ## Double and plus 1 to have an odd-sized kernel
111
- kernel = np .ones ((pixels ,pixels ),np .uint8 ) ## How many pixel of extension do I get
101
+ checkImage (image )
102
+ row = image .shape [0 ]
103
+ col = image .shape [1 ]
104
+ pixels = 2 * size + 1 ## Double and plus 1 to have an odd-sized kernel
105
+ kernel = np .ones ((pixels ,pixels ),np .uint8 ) ## Pixel of extension I get
112
106
113
107
if erosion is not False :
114
108
erosion = int (erosion )
@@ -117,8 +111,8 @@ def trimap(image, name, size, number, erosion=False):
117
111
image = np .where (image > 0 , 255 , image ) ## Any gray-clored pixel becomes white (smoothing)
118
112
# Error-handler to prevent entire foreground annihilation
119
113
if cv2 .countNonZero (image ) == 0 :
120
- print ("ERROR: foreground has been entirely eroded" );
121
- sys .exit ();
114
+ print ("ERROR: foreground has been entirely eroded" )
115
+ sys .exit ()
122
116
123
117
dilation = cv2 .dilate (image , kernel , iterations = 1 )
124
118
@@ -137,33 +131,32 @@ def trimap(image, name, size, number, erosion=False):
137
131
for i in range (0 ,row ):
138
132
for j in range (0 ,col ):
139
133
if (remake [i ,j ] != 0 and remake [i ,j ] != 255 ):
140
- remake [i ,j ] = 127 ;
134
+ remake [i ,j ] = 127
141
135
142
- path = "./images/results/" ## Change the directory
143
- new_name = '{}px_' .format (size ) + name + '_{}.png' .format (number );
144
- cv2 .imwrite (os .path .join (path , new_name ) , remake )
136
+ path = "./images/results/" ## Change the directory
137
+ new_name = '{}px_' .format (size ) + name + '_{}.png' .format (number )
138
+ cv2 .imwrite (os .path .join (path , new_name ) , remake )
145
139
146
140
147
141
#############################################
148
142
### TESTING SECTION ###
149
143
#############################################
150
144
if __name__ == '__main__' :
151
-
152
- path = "./images/test_images/test_image_11.png" ;
145
+ path = "./images/test_images/test_image_11.png"
153
146
image = extractImage (path )
154
147
155
- size = 10 ;
156
- number = path [- 5 ];
148
+ size = 10
149
+ number = path [- 5 ]
157
150
title = "test_image"
158
151
159
152
unit01 = Toolbox (image );
160
- kernel1 = np .ones ( (11 ,11 ), np .uint8 );
161
- unit01 .displayImage ;
153
+ kernel1 = np .ones ( (11 ,11 ), np .uint8 )
154
+ unit01 .displayImage
162
155
163
- opening = unit01 .morph_close (image ,kernel1 );
164
- trimap (opening , title , size , number , erosion = False );
165
- unit02 = Toolbox (opening );
166
- unit02 .displayImage ;
156
+ opening = unit01 .morph_close (image ,kernel1 )
157
+ trimap (opening , title , size , number , erosion = False )
158
+ unit02 = Toolbox (opening )
159
+ unit02 .displayImage
167
160
168
161
########################################################
169
162
## Default instruction (no binary opening or closing ##
0 commit comments