Skip to content

Commit a279562

Browse files
committed
New modules on feature_extraction.py included
1 parent dd9a924 commit a279562

File tree

5 files changed

+36
-39
lines changed

5 files changed

+36
-39
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ The illustration starts with zero erosion/dilation; followed with one, three, fi
9898
</table>
9999
</p><br />
100100

101+
102+
**4 Image Processing using Feature Extraction Module (feature_extraction.py)** <br/>
103+
**Coming Soon**
104+
101105
## References ##
102106
1. Vikas Gupta and Shanmuganathan Raman. (2017). "Automatic Trimap Generation for Image Matting". Indian Institute of Technology, Gandhinagar, IND [download](https://arxiv.org/pdf/1707.00333.pdf)
103107
2. Olivier Juan and Reanud Keriven. (2005). "Trimap Segmentation for Fast and User-Friendly Alpha Matting". FRA [download](http://imagine.enpc.fr/publications/papers/05vlsm_c.pdf)

images/results/10px_test_image_1.png

1 KB
Loading

requirements.txt

Whitespace-only changes.

binarymask.py src/binarymask.py

File renamed without changes.

trimap_module.py

+32-39
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
def extractImage(path):
66
# 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)
88
return image
99

1010
def checkImage(image):
@@ -18,27 +18,23 @@ def checkImage(image):
1818
1919
"""
2020
if len(image.shape) > 2:
21-
print("ERROR: non-binary image (RGB)");
22-
sys.exit();
21+
print("ERROR: non-binary image (RGB)"); sys.exit();
2322

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

2726
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()
3028
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()
3330
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()
3632
else:
3733
return True
3834

3935
class Toolbox:
4036
def __init__(self, image):
41-
self.image = image;
37+
self.image = image
4238

4339
@property
4440
def printImage(self):
@@ -60,15 +56,15 @@ def displayImage(self):
6056
Display the image on a window
6157
Press any key to exit
6258
"""
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()
6662

6763
def saveImage(self, title, extension):
6864
"""
6965
Save as a specific image format (bmp, png, or jpeg)
7066
"""
71-
cv2.imwrite("{}.{}".format(title,extension), self.image);
67+
cv2.imwrite("{}.{}".format(title,extension), self.image)
7268

7369
def morph_open(self, image, kernel):
7470
"""
@@ -102,13 +98,11 @@ def trimap(image, name, size, number, erosion=False):
10298
the last argument is optional; i.e., how many iterations will the image get eroded
10399
Output : a trimap
104100
"""
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
112106

113107
if erosion is not False:
114108
erosion = int(erosion)
@@ -117,8 +111,8 @@ def trimap(image, name, size, number, erosion=False):
117111
image = np.where(image > 0, 255, image) ## Any gray-clored pixel becomes white (smoothing)
118112
# Error-handler to prevent entire foreground annihilation
119113
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()
122116

123117
dilation = cv2.dilate(image, kernel, iterations = 1)
124118

@@ -137,33 +131,32 @@ def trimap(image, name, size, number, erosion=False):
137131
for i in range(0,row):
138132
for j in range (0,col):
139133
if (remake[i,j] != 0 and remake[i,j] != 255):
140-
remake[i,j] = 127;
134+
remake[i,j] = 127
141135

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

146140

147141
#############################################
148142
### TESTING SECTION ###
149143
#############################################
150144
if __name__ == '__main__':
151-
152-
path = "./images/test_images/test_image_11.png";
145+
path = "./images/test_images/test_image_11.png"
153146
image = extractImage(path)
154147

155-
size = 10;
156-
number = path[-5];
148+
size = 10
149+
number = path[-5]
157150
title = "test_image"
158151

159152
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
162155

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
167160

168161
########################################################
169162
## Default instruction (no binary opening or closing ##

0 commit comments

Comments
 (0)