forked from ma-tech/PyWoolzScripts
/
WlzMakeITKSnapSegImage.py
executable file
·299 lines (288 loc) · 9.76 KB
/
WlzMakeITKSnapSegImage.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/python
##
# \file WlzMakeITKSnapSegImage.py
# \author Bill Hill
# \date April 2017
# \version $Id$
# \par
# Address:
# MRC Human Genetics Unit,
# MRC Institute of Genetics and Molecular Medicine,
# University of Edinburgh,
# Western General Hospital,
# Edinburgh, EH4 2XU, UK.
# \par
# Copyright (C), [2015],
# The University Court of the University of Edinburgh,
# Old College, Edinburgh, UK.
#
# 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.
# \brief Exports Woolz domains as an ITK-SnAP (indexed) segmentation
# image corresponding to the given ITK-SnAP label description
# file.
##
from __future__ import print_function
import os
import re
import sys
import ctypes as c
import argparse
import subprocess
import Wlz as w
libc = c.CDLL('libc.so.6')
libc.fopen.restype = c.POINTER(w.FILE)
class WlzError(Exception):
pass
def ErrorMsg(msg): #{
print(prog + ': ' + msg, file=sys.stderr)
exit(1)
#}
def VerbMsg(msg): #{
if(args.verbose): #{
print(prog + ': ' + msg)
#}
#}
def ParseArgs(): #{
parser = argparse.ArgumentParser(description = \
'Exports Woolz domains as an ITK-SnAP (indexed) segmentation image ' + \
'corresponding to the given ITK-SnAP label description file. All ' + \
'files are both read and written using the Woolz file format, use ' + \
'WlzExtFFConvert to convert them to NIfTI as required.')
parser.add_argument('-c', '--compound', \
action='store_true', default=False, \
help='output compound object instead of index object.')
parser.add_argument('-d', '--domaindir', \
type=str, default='.', \
help='directory containing the Woolz domains.')
parser.add_argument('-o', '--output', \
type=str, default='-', \
help='output segmentation image.')
parser.add_argument('-r', '--reference', \
type=str, default='', \
help='reference image (used to ensure index object is the same size' + \
'the reference object).')
parser.add_argument('-v', '--verbose', \
action='store_true', default=False, \
help='verbose output (mainly useful for debugging).')
parser.add_argument('ldf', help = \
'input ITK-SnAP label description file in which label text must ' + \
'correspond to domain file names with a .wlz extension.')
args = parser.parse_args()
return(args)
#}
def ExportDomainsToIndexObj(): #{
# Get domain indices and names from the label description file
# adding the domain at each step.
cpd_obj = None
idx_obj = None
max_idx = 0
obj_type = w.WLZ_NULL
dom_objs = []
dom_idxs = []
with open(args.ldf) as ldf: #{
for rec in ldf: #{
VerbMsg('rec = ' + rec)
if not re.match(r'^[ \t]*#', rec): #{
rec = rec.lstrip()
dom_idx = int(re.split('\s+', rec)[0])
dom_name = re.split('"', rec)[1]
VerbMsg('idx = ' + str(dom_idx) + ' dom = ' + dom_name)
if(dom_idx > 0): #{
try: #{
if dom_idx > max_idx: #{
max_idx = dom_idx
#}
dom_idxs.append(dom_idx)
errNum = w.enum__WlzErrorNum(w.WLZ_ERR_FILE_OPEN)
dom_file = args.domaindir + '/' + dom_name + '.wlz'
fp = libc.fopen(dom_file, 'rb')
if not bool(fp): #{
raise IOError()
#}
obj = w.WlzAssignObject( \
w.WlzReadObj(fp, c.byref(errNum)), None)
if(bool(errNum)): #{
raise WlzError()
#}
p_lst = w.WlzMakePropertyList(None)
p_nam = w.WlzMakeNameProperty(dom_name, c.byref(errNum))
if(bool(errNum)): #{
raise WlzError()
#}
ft = c.CFUNCTYPE(c.c_void_p, c.c_void_p)\
(w.WlzFreePropertyListEntry)
alcerr = w.AlcDLPListEntryAppend(p_lst.contents.list, None, \
p_nam, ft)
if(bool(alcerr)): #{
raise WlzError()
#}
obj.contents.plist = w.WlzAssignPropertyList(p_lst, None)
dom_objs.append(obj)
libc.fclose(fp)
if(bool(errNum)): #{
raise WlzError()
#}
VerbMsg('domain object type (idx == ' + str(dom_idx) + ') ' + \
str(w.WlzStringFromObjTypeValue( \
obj.contents.type, None)))
if(obj_type == w.WLZ_NULL): #{
if((obj.contents.type == w.WLZ_2D_DOMAINOBJ) or \
(obj.contents.type == w.WLZ_3D_DOMAINOBJ)): #{
obj_type = obj.contents.type
#}
#}
except IOError: #}{
ErrorMsg('Failed to read domain from file ' + dom_file + \
' (' + w.WlzStringFromErrorNum(errNum, None) + ')')
except WlzError: #}{
ErrorMsg('Failed to process domain from file ' + dom_file + \
' (' + w.WlzStringFromErrorNum(errNum, None) + ')')
#}
#}
#}
#}
#}
VerbMsg('Object type ' + str(w.WlzStringFromObjTypeValue(obj_type, None)))
if((max_idx < 1) or
((not obj_type == w.WLZ_2D_DOMAINOBJ) and
(not obj_type == w.WLZ_3D_DOMAINOBJ))): #{
ErrorMsg('Require at least one 2 or 3D domain with a positive index.')
#}
try: #{
VerbMsg('Creating compound array object of domains.')
cpd_obj = w.WlzMakeCompoundArray( \
w.enum__WlzObjectType(w.WLZ_COMPOUND_ARR_2), \
1, max_idx + 1, None, obj_type, c.byref(errNum))
if(bool(errNum)): #{
raise WlzError()
#}
for i in range(0, len(dom_objs)): #{
obj = dom_objs[i]
idx = dom_idxs[i]
cpd_obj.contents.o[idx] = obj
#}
except: #}{
ErrorMsg('Failed to create compound object from domains (' + \
w.WlzStringFromErrorNum(errNum, None) + ')')
#}
if not args.compound: #{
try: #{
VerbMsg('Creating index object from compound of domains.')
idx_obj = w.WlzAssignObject( \
w.WlzIndexObjFromCompound(cpd_obj, c.byref(errNum)), None)
if(bool(errNum)): #{
raise WlzError()
#}
except: #}{
ErrorMsg('Failed to create index object from compound of domains (' + \
w.WlzStringFromErrorNum(errNum, None) + ')')
#}
#}
if bool(args.reference) and (not args.compound): #{
try: #{
VerbMsg('Cutting index object to the same bounding box as the\n' + \
'reference object (read from ' + args.reference + ').')
fp = None
errNum = w.enum__WlzErrorNum(w.WLZ_ERR_FILE_OPEN)
if args.reference == '-': #{
fp = libc.stdin
else: #}{
fp = libc.fopen(args.reference, 'rb')
#}
if not bool(fp): #{
raise IOError()
#}
obj = w.WlzAssignObject( \
w.WlzReadObj(fp, c.byref(errNum)), None)
if not args.reference == '-': #{
libc.fclose(fp)
#}
if(bool(errNum)): #{
raise WlzError()
#}
box = w.WlzBoundingBox3I(obj, c.byref(errNum))
if(not bool(errNum)): #{
gtype = w.WlzGreyTypeFromObj(idx_obj, c.byref(errNum))
#}
w.WlzFreeObj(obj)
if(bool(errNum)): #{
raise WlzError()
#}
VerbMsg('Using bounding box ' + \
'(' + str(box.xMin) + ',' + str(box.xMax) + '),' + \
'(' + str(box.yMin) + ',' + str(box.yMax) + '),' + \
'(' + str(box.zMin) + ',' + str(box.zMax) + ')')
VerbMsg('Preserving grey type ' + \
w.WlzStringFromGreyType(gtype, None) + \
'.')
obj = w.WlzCutObjToBox3D(idx_obj, box, gtype, 0, 0.0, 0.0, \
c.byref(errNum))
if(bool(errNum)): #{
raise WlzError()
#}
w.WlzFreeObj(idx_obj)
idx_obj = obj
except IOError: #}{
ErrorMsg('Failed to read the reference object read from ' + \
args.reference + '.')
except WlzError: #}{
ErrorMsg('Failed to cut index object to the bounding box of the\n' + \
'reference object read from (' + args.reference + ') (' + \
w.WlzStringFromErrorNum(errNum, None) + ')')
#}
#}
try: #{
ft = 'index'
if args.compound: #{
ft = 'compound'
#}
VerbMsg('Writing ' + ft + ' object to output file (' + args.output + ').')
errNum = w.enum__WlzErrorNum(w.WLZ_ERR_FILE_OPEN)
if args.output == '-': #{
fp = libc.stdout
else: #}{
fp = libc.fopen(args.output, 'wb')
#}
if not bool(fp): #{
raise IOError()
#}
if args.compound: #{
errNum = w.WlzWriteObj(fp, cpd_obj)
else: #}{
errNum = w.WlzWriteObj(fp, idx_obj)
#}
if not args.output == '-': #{
libc.fclose(fp)
#}
if(bool(errNum)): #{
raise WlzError()
#}
except: #}{
ErrorMsg('Failed to write ' + ft + ' object to file (' + \
w.WlzStringFromErrorNum(errNum, None) + ')')
#}
#}
if __name__ == '__main__': #{
# Process the command line
args = ParseArgs()
prog = sys.argv[0];
errNum = w.enum__WlzErrorNum(w.WLZ_ERR_NONE)
if(args.verbose): #{
print(prog + ': args = ' + str(args))
#}
ExportDomainsToIndexObj()
#}