-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.py
240 lines (221 loc) · 7.91 KB
/
storage.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
236
237
238
239
240
# !/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Syntax: python storage.py <path to folder images> <diameter of scalemarker> <blunder size=4000>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
'''
External library imports
'''
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
import numpy as np
from skimage import io
from skimage.color import label2rgb
from skimage.filters import threshold_otsu
from skimage.measure import label
from skimage.measure import regionprops
from skimage.morphology import medial_axis, opening, closing
from skimage.morphology import square
from skimage.segmentation import clear_border
from skimage.transform import resize
'''
Python library imports
'''
import csv
import os
import sys
'''
Funtions
'''
def extractTraits(labelImg, rootIdx, d, mm):
medialAxisImg = np.zeros_like(labelImg, dtype=float)
medialAxisImg[np.where(labelImg == rootIdx)] = labelImg[np.where(labelImg == rootIdx)]
s = medialAxisImg.shape
medialAxisImgSmall = resize(medialAxisImg.copy(), (100, int(float(s[1]) / float(s[0]) * 100)))
# Compute the medial axis (skeleton) and the distance transform for the root object
medialAxisImg = opening(medialAxisImg)
medialAxisImg = closing(medialAxisImg)
medialAxisImgSmall = opening(medialAxisImgSmall)
medialAxisImgSmall = closing(medialAxisImgSmall)
skel, distance = medial_axis(medialAxisImg, return_distance=True)
skelSmall, distanceSmall = medial_axis(medialAxisImgSmall, return_distance=True)
dist_on_skel = distance * skel
dist_on_skelSmall = resize(distanceSmall * skelSmall, s)
skelList = np.where(dist_on_skelSmall == 0)
dist_on_skel[skelList] = 0
dist_on_skel = pruneMA(dist_on_skel)
diameterPos = np.where(dist_on_skel > 0)
try:
testArr = dist_on_skel[diameterPos]
radius = np.median(dist_on_skel[diameterPos])
radius = (radius / d) * mm
except:
radius = 0.
length = 0.
try:
length = len(dist_on_skel[diameterPos]) + dist_on_skel[diameterPos][0] + dist_on_skel[diameterPos][
len(dist_on_skel[diameterPos]) - 1]
length = (length / d) * mm
except:
length = 0.
radius = 0.
volume = 0.
if length > 0:
height = 1 # relace with scaled pixel value
for i in dist_on_skel[diameterPos]:
try:
volume += np.pi * ((float(i) / d) * mm) * ((float(height) / d) * mm)
except:
volume = 0
print('VOLUME ERROR')
if volume > 0:
volumeArr.append(volume)
lengthArr.append(length)
diameterArr.append(radius)
else:
print(length, radius)
def pruneMA(MAimg):
# positions of the MA
pos = np.where(MAimg > 0)
oneConnectedEnds = []
for i in zip(pos[0], pos[1]):
if MAimg[i[0], i[1]] < np.max(MAimg) / 1.5:
MAimg[i[0], i[1]] = 0
# Loop through MA
for i in zip(pos[0], pos[1]):
count = 0
for j in (-1, 0, 1):
for k in (-1, 0, 1):
try:
if MAimg[i[0] + j, i[1] + k] > 0:
if j != 0 or k != 0:
count += 1
except:
pass
if count == 1:
oneConnectedEnds.append(i)
collect = []
for idx, i in enumerate(oneConnectedEnds):
MAimg[i[0], i[1]] = 0
for j in (-1, 0, 1):
for k in (-1, 0, 1):
try:
if MAimg[i[0] + j, i[1] + k] > 0:
if j != 0 or k != 0:
collect.append((i[0] + j, i[1] + k))
except:
pass
if len(collect) == 1:
oneConnectedEnds.append(collect[0])
collect = []
return MAimg
'''
Execution of the program
'''
print('------------------------------------------------------------------------')
print('DIRT/storage')
print('------------------------------------------------------------------------')
print('Syntax: python storage.py <path > <diameter> <blunder>')
print('------------------------------------------------------------------------')
print('path: path to storage root images')
print('diameter: diameter of the scale marker')
print('blunder: min. number of pixels in connected components (standard = 4000)')
print('------------------------------------------------------------------------')
print('Publication to cite: https://doi.org/10.1002/ppp3.10130')
print('------------------------------------------------------------------------')
f = sys.argv[1]
path = os.path.dirname(os.path.abspath(f))
print('---------')
print(f)
print(path)
print(os.path.dirname(f))
print('---------')
mm = float(sys.argv[2])
skipIt = 4000
try:
skipIt = int(sys.argv[3])
except:
pass
# fileList=glob.glob(path+'/*tubers*')
diameter = 0
volumeArr = []
lengthArr = []
diameterArr = []
tuberID = []
imgName = []
image = io.imread(f, as_gray=True, plugin="matplotlib")
# apply threshold
thresh = threshold_otsu(image)
bw = closing(image > thresh, square(3))
# remove artifacts connected to image border
cleared = bw.copy()
clear_border(cleared)
# label image regions
label_image = label(cleared)
image_label_overlay = label2rgb(label_image, image=image)
regions = regionprops(label_image)
for i in regions:
if i.area < skipIt:
continue
elif i.eccentricity < 0.5: # Circle
diameter = i.equivalent_diameter
print('Diameter of Circle:' + str(diameter))
for i in regions:
if i.area < skipIt:
continue
elif i.eccentricity < 0.8: # Circle
continue
elif np.linalg.norm(i.orientation) < 0.7: # Tag
continue
else:
extractTraits(label_image, i.label, diameter, mm)
tuberID.append(str(i.label))
imgName.append(f)
plt.figure(1)
plt.clf()
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(image_label_overlay)
for region in regions:
# skip small images
if region.area < skipIt:
continue
elif region.eccentricity < 0.8: # Circle
continue
elif np.linalg.norm(region.orientation) < 0.7: # Tag
continue
# draw rectangle around segmented coins
minr, minc, maxr, maxc = region.bbox
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
fill=False, edgecolor='red', linewidth=2)
ax.add_patch(rect)
print("save segmentation")
plt.savefig(os.path.join(os.path.dirname(path), os.path.splitext(os.path.basename(f))[0] + '_segmentation.jpg'))
plt.close()
allValues = [['Length (mean)', 'Length (std)', 'Volume (mean)', 'Volume (std)', 'Diameter (mean)', 'Diameter (std)'],
[np.average(lengthArr), np.std(lengthArr), np.average(volumeArr), np.std(volumeArr, ddof=1),
np.average(diameterArr), np.std(diameterArr, ddof=1)]]
with open(os.path.join(os.path.dirname(path), 'all.csv'), 'w') as csvFile:
writer = csv.writer(csvFile, delimiter=',')
writer.writerows(allValues)
header = ['Image', 'Tuber ID', 'Length', 'Volume', 'Diameter']
with open(os.path.join(os.path.dirname(path), 'allSingle.csv'), 'w') as csvFile:
writer = csv.writer(csvFile, delimiter=',')
writer.writerow(header)
for idx in range(len(lengthArr)):
writer.writerow([imgName[idx], tuberID[idx], lengthArr[idx], volumeArr[idx], diameterArr[idx]])