-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobject_remove.py
executable file
·235 lines (145 loc) · 6.27 KB
/
object_remove.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
Version: 1.5
Summary: Process all skeleton graph data in each individual folders
Author: suxing liu
Author-email: [email protected]
USAGE:
python3 object_remove.py -p ~/example/molly_HLN_models_skeleton/Models/HighN/
"""
import subprocess, os
import sys
import argparse
import numpy as np
import pathlib
import os
import glob
import shutil
from pathlib import Path
from scipy.spatial import KDTree
import cv2
import imutils
import psutil
import concurrent.futures
import multiprocessing
from multiprocessing import Pool
from contextlib import closing
# generate folder to store the output results
def mkdir(path):
# import module
import os
# remove space at the beginning
path=path.strip()
# remove slash at the end
path=path.rstrip("\\")
# path exist? # True # False
isExists=os.path.exists(path)
# process
if not isExists:
# construct the path and folder
#print path + ' folder constructed!'
# make dir
os.makedirs(path)
return True
else:
# if exists, return
print (path+' path exists!')
return False
#find the closest points from a points sets to a fix point using Kdtree, O(log n)
def closest_point(point_set, anchor_point):
kdtree = KDTree(point_set)
(d, i) = kdtree.query(anchor_point)
#print("closest point:", point_set[i])
return (i, point_set[i])
def object_remove(image_file):
abs_path = os.path.abspath(image_file)
filename, file_extension = os.path.splitext(abs_path)
base_name = os.path.splitext(os.path.basename(filename))[0]
image_file_name = Path(image_file).name
folder_cur = os.path.basename(abs_path)
mkpath = os.path.dirname(abs_path) + '/plant_result'
mkdir(mkpath)
save_path = mkpath + '/'
#print("folder_cur : {0}\n".format(str(folder_cur)))
# make the folder to store the results
#current_path = abs_path + '/'
print("Exacting traits for image : {0}\n".format(str(image_file_name)))
# load the image, convert it to grayscale, blur it slightly, and threshold it
image = cv2.imread(image_file)
# get the dimension of the image
img_height, img_width, img_channels = image.shape
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
# find contours in the thresholded image
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
sorted_contours= sorted(cnts, key=cv2.contourArea, reverse= True)
largest_item = sorted_contours[0]
img_mask = np.zeros([img_height, img_width], dtype="uint8")
img_mask = cv2.drawContours(image = img_mask, contours = [largest_item], contourIdx = -1, color = (255,255,255), thickness = cv2.FILLED)
'''
################################################################
center_img = (int(img_height*0.5), int(img_width*0.5))
point_set = np.zeros((len(cnts), 2))
img_mask = np.zeros([img_height, img_width], dtype="uint8")
if len(cnts) > 0:
# loop over the contours
for idx, c in enumerate(cnts):
# compute the center of the contour
M = cv2.moments(c)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
#point_set.append[(cY, cX)]
point_set[idx,0] = cY
point_set[idx,1] = cX
(index_cp, value_cp) = closest_point(point_set, center_img)
print(index_cp, value_cp)
if 0 <= index_cp < len(cnts):
img_mask = cv2.drawContours(image = img_mask, contours = [cnts[index_cp]], contourIdx = -1, color = (255,255,255), thickness = cv2.FILLED)
else:
print("No contour was found!\n")
################################################################################
'''
# save segmentation result
result_file = (save_path + base_name + '_seg' + file_extension)
#print(filename)
cv2.imwrite(result_file, img_mask)
return img_mask
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", required = True, help="path to image file")
ap.add_argument("-ft", "--filetype", required=True, help="Image filetype")
args = vars(ap.parse_args())
# setting path to model file
file_path = args["path"]
ext = args['filetype']
#accquire image file list
filetype = '*.' + ext
image_file_path = file_path + filetype
#accquire image file list
imgList = sorted(glob.glob(image_file_path))
#print((imgList))
#global save_path
n_images = len(imgList)
result_list = []
'''
#loop execute
for image_id, image in enumerate(imgList):
(filename, QR_data, area, solidity, longest_axis, n_leaves, hex_colors, color_ratio, diff_level_1, diff_level_2, diff_level_3, color_diff_a, color_diff_b, color_diff_c) = extract_traits(image)
result_list.append([filename, QR_data, area, solidity, longest_axis, n_leaves,
hex_colors[0], color_ratio[0], diff_level_1[0], diff_level_2[0], diff_level_3[0],
hex_colors[1], color_ratio[1], diff_level_1[1], diff_level_2[1], diff_level_3[1],
hex_colors[2], color_ratio[2], diff_level_1[2], diff_level_2[2], diff_level_3[2],
color_diff_a, color_diff_b, color_diff_c])
'''
########################################################################
# get cpu number for parallel processing
agents = psutil.cpu_count()-2
#agents = multiprocessing.cpu_count()
#agents = 8
print("Using {0} cores to perfrom parallel processing... \n".format(int(agents)))
# Create a pool of processes. By default, one is created for each CPU in the machine.
# extract the bouding box for each image in file list
with closing(Pool(processes = agents)) as pool:
result = pool.map(object_remove, imgList)
pool.terminate()