Batch Processing Time Series with StarDist in Fiji

Hi All,

I am finally getting around to polay with StarDist in ernest and have hit a snag in batch processing in Fiji with it.

The data is 3 channel time series of celltrack/PI/AnnexinV for looking at appoptosis. We have a Fiji macro that has served us well over the years but StarDist is much faster and more robust at pulling out the nuclei.

I am hitting a few issues when trying to batch process it however.

I am using Stardist with default settings and if i run without activating batch mode it all works fine with Label image output or ROI manager output.

Once in batch mode i have the following issues

  • If output is set to Label Image, no Label Images are created. Cant then use them to make masks
  • If output is set to ROI Manager a blank window titled ROI Manager is created after each run of StarDist by the macro and the created rois can’t be used to generate masks as the ROI list isn’t addressed properly, i assume due to the extra roi windows.
  • If i run the first stack through without batchmode enabled and then turn it on at teh end of the first loop it works fine with the ROI manager with no extra windows.

Any ideas on what coudl be going on? I have my code working now with the last point on the issues but it woudl be nice to have it truly headless.

If batchmode isn;t enabled, everything

1 Like

Hi Cameron,

thanks for trying StarDist and welcome to the forum!

Sorry for the late reply, but sparked by your post I was experimenting with batch mode and related issues over the last few days.

I haven’t managed to fix this, and I’m not sure I can justify spending more time on this. Sorry.

I hope that this is fixed now. At least in my tests, I could use the ROI Manager in batch mode.

PS: You need to update StarDist in Fiji to get my changes. I just made a new release.

Best,
Uwe

2 Likes

Brilliant, thanks heaps for looking into it.

A functional ROI manager is fine as i can use that to generate masks with the fill command.

I will give it a go now :slight_smile:

1 Like

Hey guys @uschmidt83 @Cameron.Nowell ,
I was very happy to find this thread as I was thinking to run StarDist in a loop to segment a Z-stack. Now I am wondering if that is possible. I am trying to call StarDist from a macro for loop:

for (i=1; i<=nSlices; i++) {
setSlice(i);

run("Command From Macro", "command=[de.csbdresden.stardist.StarDist2D], args=['input':'Slice', 'modelChoice':'Versatile (fluorescent nuclei)', 'normalizeInput':'true', 'percentileBottom':'1.0', 'percentileTop':'99.8', 'probThresh':'0.5', 'nmsThresh':'0.4', 'outputType':'ROI Manager', 'nTiles':'1', 'excludeBoundary':'2', 'roiPosition':'Automatic', 'verbose':'false', 'showCsbdeepProgress':'false', 'showProbAndDist':'false'], process=[false]");
roiManager("Fill");

}
which does not work (I guess since StarDist does not work on stacks). If I change the image properties to make a time series and not a Z-stack it blows every segment for every frame. Is it possible to look a function that only accepts 2D to analyze 3D? What I am missing?

I have successfully ran the jython script recommended on other pages and that works on single images on a folder, but still normalization is applied per image and the final result is not proper segmentation on the whole stack. I guess no surprises there… and I guess the answer to my question is yes -I can loop through a stack by analyzing individual slices but cannot run StarDist in 3D by running single slices. No cheap tricks allowed ;-p.

Hi, thanks for trying StarDist.

I don’t understand what you mean.

You could normalize the stack yourself and then turn off the normalization in StarDist.

Howdy

For my time series stuff i just give it the time stack and StarDist processes it all (code example below for that function in Fiji macro). I have it generate ROIs only and associate them with the frame position. Then dump them into a blank temp image with the same dimensions and paint them all (ssemed to be the only way to get it to work in batch mode).

Your z stacks shoudl work the same but you might need to flip the Z for time first

function starDistSegment(){
	newImage("Temp", "8-bit black", width, height, frames);

	selectWindow(currentChannelName);
	run("Command From Macro", "command=[de.csbdresden.stardist.StarDist2D], args=['input':'"+currentChannelName+"', 'modelChoice':'Versatile (fluorescent nuclei)', 'normalizeInput':'true', 'percentileBottom':'1.0', 'percentileTop':'99.8', 'probThresh':'0.4', 'nmsThresh':'0.6000000000000001', 'outputType':'ROI Manager', 'nTiles':'1', 'excludeBoundary':'2', 'roiPosition':'Automatic', 'verbose':'false', 'showCsbdeepProgress':'false', 'showProbAndDist':'false', 'label':''], process=[false]");
	
	selectWindow("Temp");
	setColor(255);
	roiManager("Show All without labels");
	roiManager("Associate", "true");
	roiManager("Fill");
	setThreshold(1, 255);
	run("Analyze Particles...", "  show=Masks stack");
	selectWindow("Mask of Temp");
	run("Invert LUT");
	rename(currentChannelName+" Mask");
	run("Watershed", "stack");
	selectWindow(currentChannelName);
	close();
	selectWindow("Temp");
	close();

}
2 Likes

Hi @uschmidt83, I am also unable to use StarDist in batch mode in ImageJ macros. My files are large stacks that will take a looong time to process if they are not done in batch mode. The following is a simple test macro. When batch mode is “False” it works fine, but when batch mode is “True”, it comes with the Macro Error, ‘No window with the title “Label Image” found.’, so that nothing can be done with the output image in batch mode.

// Open sample file and obtain DAPI channel
run("Fluorescent Cells");
Stack.setDisplayMode("color");
run("Duplicate...", "duplicate channels=3");

// Run StarDist and do something to the output image
setBatchMode(true);
run("Command From Macro", "command=[de.csbdresden.stardist.StarDist2D], args=['input':'FluorescentCells-1.tif', 'modelChoice':'Versatile (fluorescent nuclei)', 'normalizeInput':'true', 'percentileBottom':'1.0', 'percentileTop':'99.8', 'probThresh':'0.5', 'nmsThresh':'0.4', 'outputType':'Both', 'nTiles':'1', 'excludeBoundary':'2', 'roiPosition':'Automatic', 'verbose':'false', 'showCsbdeepProgress':'false', 'showProbAndDist':'false'], process=[false]");
selectWindow("Label Image");
rename("nuclei");
setBatchMode(false);

In batch mode, the “Label Image (V)” image produced by StarDist doesn’t seem to exist. I guess the “(V)” suffix means that it is a virtual image? I was wondering if the problem could be solved either by producing an actual (non-virtual) image, or by including a manual destination option for the image/file produced by StarDist in its options interface?

I am using Fiji with ImageJ Version 2.1.0/1.53e on a Mac.

Guy

I have no experience with batch mode, but is it really that much faster? Can you disable batch mode just before running StarDist and turn it back on afterwards?

This is a known issue (see first post in this thread and my reply) and I have given up on fixing this. However, you can use the ROI Manager in batch mode and presumably create a label image based on it (see this Jython script, but the same is also possible in Macro). Would this help you?

Best,
Uwe

Thanks for responding, Uwe. Yes, I am doing as you suggested - everything except StarDist is in batch mode. I don’t know how much faster batch mode would be without being able to try it; I just assumed it would speed things up by not having to open up images on the screen.

My macro uses StarDist2D on stack slices to get 3D semantic segmentations of nuclei in corneas, which have a variety of nuclear shapes (oblate, prolate and near-spheroid) depending on the cell type. It does a pretty good job, with just a few nuclei needing manual adjustment by splitting or joining, but probably not as good as StarDist3D would do. Would you be interested in having my curated segmented+raw data sets as a ground truth+training set so that a StarDist 3D Fiji plugin could be created?

We’d have to take a look at the dataset, but we might be able to train a model from it, such that other users can benefit from it.

Adding 3D support to the Fiji plugin is unfortunately quite a bit more work. It looks like we currently don’t have the time to do that. :slightly_frowning_face:

Best,
Uwe