Stardist output to center of geometry

I have a label image made with stardist


I would like to use the label image for trackmate.
I have seen people reducing the output of stardist to the geometrical centers of the nuclei then using that as the input for trackmate.

How do you recommend I go from my label image stack to a stack with only the geometric centers of each nucleus?

Thank you very much.


CLIJ2 has an option to determine centroid of labels on GPU. That being said, I didn’t manage to use it on a similar image to yours.

I personally get the following error

(Fiji Is Just) ImageJ 2.0.0-rc-72/1.53c; Java 1.8.0_172 [64-bit]; Windows 10 10.0; 127MB of 55000MB (<1%)
net.haesleinhuepf.clij.clearcl.exceptions.ClearCLTooManyContextsException: Too many contexts have been created and not released
	at net.haesleinhuepf.clij.clearcl.ClearCLDevice.createContext(
	at net.haesleinhuepf.clij.CLIJ.<init>(
	at net.haesleinhuepf.clij.CLIJ.getInstance(
	at net.haesleinhuepf.clij.CLIJ.getInstance(
	at ij.plugin.filter.PlugInFilterRunner.processOneImage(
	at ij.plugin.filter.PlugInFilterRunner.<init>(
	at ij.IJ.runUserPlugIn(
	at ij.IJ.runPlugIn(
	at ij.Executer.runCommand(

Maybe @haesleinhuepf can help?


1 Like

This is a very rare exception. I think I saw it last time in an application where Tensorflow and OpenCL were fighting for the right to use the GPU. @LPUoO could you confirm that this operation works if you didn’t run StarDist in advance in a freshly started Fiji?

Two options:

  1. Directly use the stardist output. The prediction function model.predict_instances does not only return the label image but as well a dictionary with additional information including the center points of the predicted shapes:
labels, polys = model.predict_instances(img) 
centers = poly["points"]
  1. With scikit-image’s regionprops function
from skimage.measure import regionprops

centers = tuple(r.centroid for r in regionprops(labels)) 
1 Like

If you can get the predicted objects into the ROI Manager (e.g. by predicting via the StarDist Fiji plugin), I can recommend this for using them with TrackMate:


1 Like

Thanks @haesleinhuepf. You are correct, if I restart FIJI then it works.
Is there another way of preventing this exception without restarting ? This way if one uses stardist and CLIJ in the same workflow it doesn’t crash?

Also, do you have a trick to read the CentroidOfLabels output and recreate a stack with the Centroids marked?

Thank you

I’m not so much a Tensorflow expert. Pretty sure you can tell Tensorflow to not block the whole GPU and leave some space for CLIJ. Alternatively, you can run either StarDist or CLIJ on the CPU. :hear_no_evil: Or StarDist on the more powerful GPU and CLIJ on the other, potentially integrated GPU (typical laptop setup).

It’s a bit from the back through the chest into the eye (German saying) but doable:


1 Like

Thank you very much everyone! I have a lot to play with now.


In case you are interested, you can generate this kind of file directly using the ZeroCostDL4mic Stardist 2D notebook
Choose “tracking file” in step 6.

If you are using python, you can also check our notebook code.

We also made a small Fiji macro to batch analyse this type of file using TrackMate.




Thanks @Guillaume_Jacquemet

I had a look at it. Is there a way to use the 2D_versatile_Fluo_from_stardist_fiji model as it is on my own data (just for a quick test) ?

Is the 2D_versatile_Fluo_from_stardist_fiji model somewhere in the appropriate format I can download and upload for this step (6.1)?


Yes, it should be possible. The code to download it is in section 3.3 of the notebook.
If you run that section with “Use_pretrained_model” enabled, the model will be downloaded and available in the Google Colab contents folder. Then you should be able to use it directly to make predictions

If you want to use @haesleinhuepf’s macro you just need to replace
line 33

Ext.CLIJ2_create2D(output, width, height, 32);


Ext.CLIJ2_create3D(output, width, height, depth, 32); 

in order to recreate the stack.

I didn’t figure out how, but you also may want to make it binary presumably by changing line 37

Ext.CLIJ2_writeValuesToPositions(coordinates_and_index, output);

with something that sets the appropriate coordinates to 1 rather than “index” (and make it 8 bit)


If you want to achieve this, just replace this line where the values are defined:



Ext.CLIJ2_set(coordinates_and_index, 1);

… to set all pixels at given positions to 1.


1 Like

Hey @LPUoO,

I just realised CLIJ2 has a method for that: pointlistToLabelledSpots :slight_smile:

1 Like