Converting SWC to binary skeletonized volume

I have a set of .swc files representing traces of neurons. I would like to convert these to a binary volume, where each voxel is 0 if it is not in the trace, and 1 otherwise. Any 3D representation of the voxels would be fine for my use case, as long as it can be read into Python downstream.

It seems that the Simple Neurite Tracer plugin for Fiji previously supported this type of functionality – in fact, this exact procedure is described in the first paragraph of the Methods section in the following paper .

However, it does not seem that the functionality described in this paper still exists any longer in the SNT plugin. I am using Fiji with ImageJ 1.52p, the SNT plugin as packaged in the NeuroAnatomy plugin (as recommended in the SNT dialogue). I am able to import and view the SWC file via SNT, but cannot see any way to “rasterize” it (convert from a network of vertex coordinates to a set of active pixels/voxels). How can I do this?

Below is the description of the methods used in the paper mentioned above (which refers to functionality no longer available in the SNT plugin):

"We downloaded the olfactory projection neuron 1 (OP-1) model as a SWC file from DIADEM’s website at, along with its corresponding 3D TIFF image stack. We then rasterised the model (i.e., converted it from a network of vertex coordinates to a set of active pixels) by using the Simple Neurite Tracer (Longair, Baker & Armstrong, 2011) plugin for Fiji, function “Analysis > Render/Analyze Skeletonized Paths.” This produces a 6-connected skeleton path, which we needed to convert to a (thinner) 26-connected path, so we further skeletonized the raster with the morphology.skeletonize3d function from scikit-image (Van der Walt et al., 2014), and saved it as a compressed TIFF file. "
this is a liberary to build skeleton image to graph, and return a networkx object.
then you can count many representation by networkx.

Hi @jpgard, if I correctly understand you want this (.swc, from the Diadem challenge, loaded in Aivia)

turned into this (we have an export to binary option):

It looks like you would like to do that on a daily basis, but if not, let me know, I can send you a demo license for you to generate all your binary files :wink:


As far as I can tell, sknw goes in the opposite direction only: image --> graph. I would like to go graph --> image. Does that functionality exist in sknw? It seems other similar packages (e.g. skan ) also don’t support this direction and only convert image --> graph.

Yes @pmascalchi , based on the animations it does seem that is what I’m looking for, assuming the export to binary produces a 3D file. Awesome! I will check out Aivia. How is that specific functionality accessed in the software?

The functionality is by default in the software :slight_smile:

look at sknw.draw

# draw the graph
def draw_graph(img, graph, cn=255, ce=128):
    acc = np.cumprod((1,)+img.shape[::-1][:-1])[::-1]
    img = img.ravel()
    for idx in graph.nodes():
        pts = graph.node[idx]['pts']
        img[, acc)] = cn
    for (s, e) in graph.edges():
        eds = graph[s][e]
        for i in eds:
            pts = eds[i]['pts']
            img[, acc)] = ce

every node and edge object has a ‘pts’ member, you can use the ndarray to set value.

I just tried and the Analysis > Render/Analyze Skeletonized Paths command exists in an up-to-date Fiji, after starting the plugin via Plugins › Segmentation › Simple Neurite Tracer:


To follow-up on @imagejan comment: The new SNT API makes it extremely simple to obtain a skeleton from an SWC file. This functionality has been in place for more than an year, so I am really sorry for not following up earlier. You can do so using SNT’s GUI (see this post for an updated snapshot), or programmatically using any of Fiji’s scripting languages.

Here is an example in jython:

from sc.fiji import Tree
Tree('/path/to/SWC/file.swc').getSkeleton().show()  # 3D stack
Tree('/path/to/SWC/file.swc').getSkeleton2D().show()  # 2D projection

and another one in groovy, that renders a demo reconstruction provided by SNTService:

#@SNTService snt
tree = snt.demoTrees().get(0)

which outputs:

There are also convenience methods to render multiple files in a single image. E.g., the Render Cell Collection (Skeletonized Montage) demo script (that you can run from the Templates> Neuroanatomy> Render> menu in Fiji’s script editor) exemplifies some of this functionality, by rendering four pyramidal cells in a common stack:


Currently you have to subscribe to the Neuroanatomy update site. to install SNT.

1 Like