-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid_seg.py
executable file
·187 lines (112 loc) · 4.19 KB
/
grid_seg.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
'''
Name: color_segmentation.py
Version: 1.0
Summary: grid segmentation.
Author: suxing liu
Author-email: [email protected]
Created: 2018-05-29
USAGE:
python3 grid_seg.py -p ~/plant-image-analysis/test/ -ft jpg -r 6 -c 5
'''
# import the necessary packages
import os
import glob
import numpy as np
import argparse
import cv2
import concurrent.futures
import multiprocessing
from multiprocessing import Pool
from contextlib import closing
MBFACTOR = float(1<<20)
# generate foloder 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
def segmentation(image_file):
abs_path = os.path.abspath(image_file)
filename, file_extension = os.path.splitext(image_file)
file_size = os.path.getsize(image_file)/MBFACTOR
print("Segmenting image : {0} \n".format(str(filename)))
# load original image
image = cv2.imread(image_file)
img_height, img_width, img_channels = image.shape
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# make the folder to store the results
#current_path = abs_path + '/'
base_name = os.path.splitext(os.path.basename(filename))[0]
# save folder construction
mkpath = os.path.dirname(abs_path) +'/' + base_name
mkdir(mkpath)
save_path = mkpath + '/'
print("results_folder: {0}\n".format(str(save_path)))
if (file_size > 5.0):
print("It will take some time due to large file size {0} MB".format(str(int(file_size))))
else:
print("Segmenting plant image into blocks... ")
#make backup image
orig = image.copy()
#number of rows
nRows = args['nRows']
# Number of columns
mCols = args['mCols']
# Dimensions of the image
sizeX = img_width
sizeY = img_height
#print(img.shape)
for i in range(0, nRows):
for j in range(0, mCols):
roi = orig[int(i*sizeY/nRows):int(i*sizeY/nRows) + int(sizeY/nRows),int(j*sizeX/mCols):int(j*sizeX/mCols) + int(sizeX/mCols)]
result_file = (save_path + str(i+1) + str(j+1) + '.' + ext)
cv2.imwrite(result_file, roi)
#return thresh
#trait_img
if __name__ == '__main__':
ap = argparse.ArgumentParser()
#ap.add_argument('-i', '--image', required = True, help = 'Path to image file')
ap.add_argument("-p", "--path", required = True, help="path to image file")
ap.add_argument("-ft", "--filetype", required=True, help="Image filetype")
ap.add_argument("-r", "--nRows", required=True, type = int, help="number of rows")
ap.add_argument("-c", "--mCols", required=True, type = int, help="number of columns")
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))
for image in imgList:
segmentation(image)
#current_img = imgList[0]
#(thresh, trait_img) = segmentation(current_img)
'''
# get cpu number for parallel processing
#agents = psutil.cpu_count()
agents = multiprocessing.cpu_count()
print("Using {0} cores to perform 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(segmentation, imgList)
pool.terminate()
'''