-
Notifications
You must be signed in to change notification settings - Fork 0
/
labelObjects.py
executable file
·127 lines (101 loc) · 3.86 KB
/
labelObjects.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
import sys
import numpy as np
from scipy import ndimage
import nrrd
def labelObj(imagefile, labelfile, t=20, ms=1000, sl=np.ones((3, 3, 3))):
print('Loading image %s...' % (imagefile))
data1, header1 = nrrd.read(imagefile)
header1.pop("endian", None)
print('Labeling objects with any voxel intensity above %s' % str(t))
data1[data1 < t] = 0
print('identifying distinct objects...')
labels, features = ndimage.label(data1, structure=sl)
print(str(features) + ' distinct objects found')
val, lab = np.histogram(labels, bins=range(1, features + 1))
print('Removing any objects with a volume of less than ' + str(ms) + ' voxels.')
print('New label(s):')
data = np.zeros(np.shape(data1))
v = 1
for i in range(0, features - 1):
if val[i] > ms:
data[labels == lab[i]] = v
print(str(v) + ' = ' + str(lab[i]))
v = v + 1
print(str(v - 1) + ' distinct objects still indexed')
print("Saving result to " + labelfile)
header1['encoding'] = 'gzip'
if v > 256:
header1['type'] = 'uint16'
nrrd.write(labelfile, np.uint16(data), header=header1)
else:
header1['type'] = 'uint8'
nrrd.write(labelfile, np.uint8(data), header=header1)
return np.uint8(np.unique(data))
def cutObj(imagefile, labelfile, labels=None):
if labels is None:
print('no labels to be cut')
else:
print('Loading image %s...' % (imagefile))
data1, header1 = nrrd.read(imagefile)
print('Loading mask %s...' % (labelfile))
data2, header2 = nrrd.read(labelfile)
print('Cutting objects with label(s) %s' % str(labels))
for i in labels:
try:
print('Cutting object ' + str(i))
data1[data2 == int(i)] = np.uint8(0)
except:
print('---')
v = np.max(data1)
print("Saving result over " + imagefile)
header1['encoding'] = 'gzip'
if v > 256:
header1['type'] = 'uint16'
nrrd.write(imagefile, np.uint16(data1), header=header1)
else:
header1['type'] = 'uint8'
nrrd.write(imagefile, np.uint8(data1), header=header1)
def cropObj(imagefile, labelfile, labels=None):
if labels is None:
print('no labels to crop to')
else:
print('Loading image %s...' % (imagefile))
data1, header1 = nrrd.read(imagefile)
print('Loading mask %s...' % (labelfile))
data2, header2 = nrrd.read(labelfile)
print('Croping to objects with label(s) %s' % str(labels))
mask = np.ones(np.shape(data1), dtype=bool)
for i in labels:
try:
print('Cropping to object ' + str(i))
mask[data2 == int(i)] = False
except:
print('---')
data1[mask] = 0
v = np.max(data1)
print("Saving result over " + imagefile)
header1['encoding'] = 'gzip'
if v > 256:
header1['type'] = 'uint16'
nrrd.write(imagefile, np.uint16(data1), header=header1)
else:
header1['type'] = 'uint8'
nrrd.write(imagefile, np.uint8(data1), header=header1)
if __name__ == "__main__":
if (len(sys.argv) < 3):
print('Error: missing arguments!')
print('e.g. python labelObjects.py image.nrrd indexlabels.nrrd [intensity_threshold] [min_size] [connection_cube]')
else:
imagefile = str(sys.argv[1])
labelfile = str(sys.argv[2])
t = 20
ms = 1000
sl = np.ones((3, 3, 3))
if (len(sys.argv) > 3):
t = np.uint16(sys.argv[3])
if (len(sys.argv) > 4):
ms = np.array(sys.argv[4], dtype=np.uint16)
if (len(sys.argv) > 5):
sl = np.array(sys.argv[5], dtype=np.uint16)
labelObj(imagefile, labelfile, t=t, ms=ms, sl=sl)
print('done.')