-
Notifications
You must be signed in to change notification settings - Fork 0
/
swc2centeredNRRD.py
51 lines (38 loc) · 1.59 KB
/
swc2centeredNRRD.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
import numpy as np
import sys, os
import nrrd
scale=100
if (len(sys.argv) < 1):
print('Error: missing arguments!')
print('e.g. python swc2nrrd.py neuron.swc Image.nrrd [scale]')
else:
Iswc = str(sys.argv[1])
Iout = str(sys.argv[2])
if (len(sys.argv) < 2):
scale=np.int32(sys.argv[3])
print(scale)
print('Loading %s...'% (Iswc))
with open(Iswc) as fI:
swcIn = fI.readlines()
lineDict = {}
for thisLine in swcIn:
if thisLine[0]!='#':
splitLine = thisLine.split(" ")
lineDict[int(splitLine[0])] = {'position':np.array([splitLine[2],splitLine[3],splitLine[4]],dtype=np.float),
'radius':splitLine[5],
'parent':int(splitLine[6])}
maxExtent=[1000,1000,1000]
for i in range(3):
maxExtent[i] = np.max([(np.max([x['position'][i] for x in lineDict.values()])/scale).astype(np.int)])
minExtent=[0,0,0]
for i in range(3):
minExtent[i] = np.min([(np.min([x['position'][i] for x in lineDict.values()])/scale).astype(np.int)])
print(minExtent)
print(maxExtent)
outputImg = np.zeros(maxExtent,dtype=np.uint8)
w = 3
for thisDict in lineDict.values():
p = np.round(np.divide(thisDict['position'],scale)).astype(np.int)
outputImg[p[0]-w:p[0]+w+1,p[1]-w:p[1]+w+1,p[2]-w:p[2]+w+1]=np.uint8(255)
nrrd.write(Iout, np.uint8(outputImg[minExtent[0]:maxExtent[0],minExtent[1]:maxExtent[1],minExtent[2]:maxExtent[2]]))
print('saved to ' + Iout)