ImageJ SNT; how to convert Paths in Path Manager to a segmented mask image?

Hi everyone,

I have a set of neuronal images that I have manually traced object paths using SNT that is saved in the SWC format.
I am trying to generate a mask image (that has different pixel values for each neuron in an image) using the SWC files.

What I have tried so far:

  1. Load image, open SNT, import a set of SWCs for the image.
  2. highlight one set of paths for one neuron at a time
  3. Path Manager - Fill - Fill out ---- pause after a few seconds
  4. Convert the fill to a binary image ---- save it

I couldn’t find any plugin in ImageJ that takes in a set of binary images to merge them all together while assigning unique pixel values for each object.
So, this is what I am planning to do:

  1. I will write a script that loops through the set of binary images to assign a unique pixel value for each image and merge them into one mask image.

This method sounds too time-consuming, and I was wondering if any of you know a better way to handle this.
Any help would be greatly appreciated!!!

I don’t have an immediate answer other than combining the binary images (each multiplied by the correct value) seems like a good option, but only if you have no overlapping cells. In your image, you have overlapping cells. How do you plan to handle pixels where more than one cell is present?

ah hmmmmmm
I didn’t actually think about that.
I guess I will just have to use the binary images individually instead of combining them.

I did combine the set of binary images that I manually extracted, but I just ignored the overlapping regions.
I don’t think this would be good practice for further analysis.

Ok, so I think I will use the binary images individually, but do you happen to know what I could do to handle the ImageJ - SNT - Fill - Binay work?
My memory filled up after generating fills for the 5th or 6th neuron, and I had to restart ImageJ.
I am hoping that doing it over macro in headless would save me some memory.

I looked at Overview (Fiji Javadocs 2.0.0-SNAPSHOT API) for available APIs, but I couldn’t find anything that does “Fill Out” and converting the fills to a binary mask image.

Thank you for your reply!

@citypalmtree, You are right that the filling API is not that complete right now (we don’t really use it, so we naturally ‘forget’ about it). But there are several methods to obtain skeletonized labels directly from SNT. Would that work? What is the ultimate goal of such labeled image?

1 Like

Also, did you try the auto-segmentation feature of SNT on your image? The density/simplicity of cells is such, that it looks as if it would work. (@arshadic, do you agree?)

The auto-segmentation could be an interesting thing to try, although there appear to be some overlapping neurites from different cells. These might get fused together by AnalyzeSkeleton since the image looks to be 2D (i.e., no Z-separation between the overlapping neurites at the intersection points). The signal in the image also seems on the dimmer side, so obtaining continuous processes could require some image processing elbow grease (a pre-trained classifier wouldn’t hurt :slight_smile: )

Thank you for the reply, Ferreira!
Yes! That would work!!
How could I use SNT to obtain a segmented mask image?

Also, would you mind explaining a bit more about how I could use the auto-segmentation feature in SNT?
From looking at Arshadi’s reply, I am guessing the name of the feature is AnalyzeSkeleton, but I cannot find a such feature in SNT.
Is it one of the sample macro scripts?

Thank you, Arshadi!

What does “elbow grease” mean?
The image I used in SNT for this process was preprocessed to have flattened noise.
Do you mean there needs more correction to be done?
what did you also mean for the pre-trained classifier?

Lastly, about SNT and the auto-segmentation feature, I do plan to have neuronal images with z-stacks. Would that be easier for the images with z-stacks to be used in SNT for the auto-segmentation feature?

The procedure would be: 1) create a zero-filled 16-bit placeholder image with suitable dimensions to receive all of the cells, then 2) skeletonize in place each tree using a unique intensity. The API for this is very succinct. Here is an example in (j)python to be run from the script editor. It assumes that your original image is open, and traces have been loaded:

#@SNTService snt

from ij import IJ

# Documentation Resources: https://imagej.net/SNT:_Scripting
# Latest SNT API: https://morphonets.github.io/SNT/

if snt.isActive():

    # Get a list of all the Trees (traced cells) in PathManager
    trees = snt.getPathAndFillManager().getTrees()
    # Get a reference to the image being traced
    imp = snt.getPlugin().getImagePlus()
 
    if imp and trees:
        # Obtain an empty 16-bit image with the same dimensions of the image being traced
        imp = snt.getPlugin().getImagePlus()
        labels_imp = IJ.createImage("Labels image", imp.getWidth(), imp.getHeight(), imp.getNSlices(), 16);
        labels_imp.copyScale(imp)
    
        # Skeletonize each tree in place using list indices as intensities
        for index, tree in enumerate(trees):
            tree.skeletonize(labels_imp, index+1)
            print("Skeletonized {} with intensity {}".format(tree.getLabel(), index+1))

        # Display labels image
        labels_imp.show()
        labels_imp.resetDisplayRange()
        IJ.run(labels_imp, "glasbey on dark", "")  # apply LUT

    else:
        print("Error: Tracings and image required.")


else:
    print("Error: SNT does not seem to be running.")

Here is an example of a labeled image produced by the script for 4 pyramidal cells:

In the main dialog look for Utilities>Extract Paths from Segmented Image…

The Extract Paths from Segmented Image… command extracts paths from a segmented (thresholded/binary) image. The conversion depends on the quality of the segmentation. What @arshadic, is alluding to is that your image may require some extra effort (or extra “sophistication”) to segment. The “pre-trained classifier” is a reference to segmentation based on machine-learning, e.g., using Fiji’s Trainable Weka Segmentation. You could try to segment the image using whatever approach you feel comfortable with, and attempt the conversion. If the results do not look promising, it may not be worth the effort.

Do let us know how it goes.

1 Like

Thank you @tferr!

The script works really well to convert a set of paths to a single mask image with the objects appropriately segmented.
I wanted to use the “Fill” feature to create a binary image that closely resembles the original image (shapes and such) so that I can use it to train in ML, but so far nothing has worked out.
Do you know of any open-source algorithm that can train and predict the segmentation of neurons? I have been trying with the nucleus segmentation programs, but I guess it does not work on neurons.

Also, the auto-segmentation feature looks very interesting!
Although it didn’t quite work well with the image that I shared in the question, and I was thinking it’s because neither binary nor 3D.
I tried another experimental image (with z-stacks but not preprocessed), and it looks to be working, but I do want to try it again after the preprocessing steps.
I will let you know how it goes.
Although, I did test the feature with a mask image that I manually did “Fill” and generated a binary (segmented), and it worked near perfectly!


The rightmost neuron has become a part of the top neuron from the auto-segmentation in SNT.

Thank you for your help again!