Automation of StarDist in imagej macros

Hi Everyone,

I am trying to write a ImageJ macro in which I will use StarDist. Is there a way to automate so that ok is not needed to be pressed every time (windowless version)?

I have found this link but as it is in python I have no idea what to do?

@nacherso is also interested in the answer if there is a way to do this please it will be greatly appreciated!

Thanks,
Megan

2 Likes

Hi Megan,

thanks for using StarDist and welcome to the forum!

You’ve already found the Jython script that allows to automate StarDist in ImageJ (also see this for another example).
I understand that the ImageJ macro language is very popular, but I don’t have much experience with it and don’t know how to call StarDist from it. I’m not even sure if it’s possible, given that StarDist is an ImageJ2 plugin. (Maybe @haesleinhuepf et al. can weigh in on this.)

Sorry for the late reply.

Best,
Uwe

3 Likes

Hi @meganapalmer,

we just released a new version of StarDist for ImageJ, which is macro-recordable and that can also be called from a macro. The syntax is a bit weird though, so it’s best to record with the parameters that you need, and then go from there.

Hope that works for you.

Best,
Uwe

4 Likes

Hi @uschmidt83

Thank you so much, however I have just tried to record and it threw up this error:

[ERROR] Module threw error
java.lang.NoClassDefFoundError: org/apache/commons/text/StringEscapeUtils
at de.csbdresden.CommandFromMacro.getMacroString(CommandFromMacro.java:229)
at de.csbdresden.CommandFromMacro.record(CommandFromMacro.java:169)
at de.csbdresden.CommandFromMacro.record(CommandFromMacro.java:153)
at de.csbdresden.stardist.StarDist2D.run(StarDist2D.java:322)
at org.scijava.command.CommandModule.run(CommandModule.java:199)
at org.scijava.module.ModuleRunner.run(ModuleRunner.java:168)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:127)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:66)
at org.scijava.thread.DefaultThreadService.lambda$wrap$2(DefaultThreadService.java:228)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

Am I missing anything here?

Thanks,
Megan

2 Likes

Hi Megan,

thanks for the bug report! I’ve just updated StarDist to fix this problem. Please try again.

Best,
Uwe

2 Likes

Thank you @uschmidt83 :blush:

2 Likes

Hi,

StarDist works perfectly well on individual images. I would like to use it in a Macro to obtain label images which I’ll feed later in CellProfiler as Objects.

This is the macro:

/* StarDist segmentation to obtain objects for CellProfiler
 */
 
//CLEAR LOG
print("\\Clear");

// CLOSE ALL OPEN IMAGES
while (nImages>0) { 
	selectImage(nImages); 
    close(); 
}

//SET BATCH MODE 
setBatchMode(true);   //true or false, true if you don't want to see the images, which is faster

//START MESSAGE
print("**** STARTING THE MACRO ****");

//INPUT/OUPUT folders
inDir=getDirectory("Choose the input folder"); 
outputDir=getDirectory("And the output folder");
myList=getFileList(inDir);  //an array

for (j = 0 ; j < myList.length ; j++ ){
	path=inDir+myList[j];   //path to each file
	open(path);
	FileName=File.nameWithoutExtension;
	ImageID=File.name;
	print("Processing "+ImageID);
	
	run("Command From Macro", "command=[de.csbdresden.stardist.StarDist2D], args=['input':'"+ImageID+", 'modelChoice':'Versatile (fluorescent nuclei)', 'normalizeInput':'true', 'percentileBottom':'1.0', 'percentileTop':'99.8', 'probThresh':'0.5', 'nmsThresh':'0.4', 'outputType':'Label Image', 'nTiles':'1', 'excludeBoundary':'2', 'roiPosition':'Automatic', 'verbose':'false', 'showCsbdeepProgress':'false', 'showProbAndDist':'false'], process=[false]");
	selectWindow("Label Image");
	saveAs("Tiff", outputDir+ImageID+"_Label.tif");
	close("*");
}

print("All images analysed");
print("***** Macro done *****");

This should be super easy but I’m getting 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; 104MB of 12130MB (<1%)
 
java.lang.RuntimeException: java.util.concurrent.ExecutionException: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 50 path $.
	at net.imagej.legacy.LegacyService.runLegacyCompatibleCommand(LegacyService.java:307)
	at net.imagej.legacy.DefaultLegacyHooks.interceptRunPlugIn(DefaultLegacyHooks.java:166)
	at ij.IJ.runPlugIn(IJ.java)
	at ij.Executer.runCommand(Executer.java:150)
	at ij.Executer.run(Executer.java:68)
	at ij.IJ.run(IJ.java:317)
	at ij.IJ.run(IJ.java:328)
	at ij.macro.Functions.doRun(Functions.java:686)
	at ij.macro.Functions.doFunction(Functions.java:98)
	at ij.macro.Interpreter.doStatement(Interpreter.java:278)
	at ij.macro.Interpreter.doBlock(Interpreter.java:712)
	at ij.macro.Interpreter.doStatement(Interpreter.java:323)
	at ij.macro.Interpreter.doFor(Interpreter.java:634)
	at ij.macro.Interpreter.doStatement(Interpreter.java:305)
	at ij.macro.Interpreter.doStatements(Interpreter.java:264)
	at ij.macro.Interpreter.run(Interpreter.java:160)
	at ij.macro.Interpreter.run(Interpreter.java:93)
	at ij.macro.Interpreter.run(Interpreter.java:104)
	at ij.plugin.Macro_Runner.runMacro(Macro_Runner.java:161)
	at ij.IJ.runMacro(IJ.java:153)
	at ij.IJ.runMacro(IJ.java:142)
	at net.imagej.legacy.IJ1Helper$3.call(IJ1Helper.java:1148)
	at net.imagej.legacy.IJ1Helper$3.call(IJ1Helper.java:1144)
	at net.imagej.legacy.IJ1Helper.runMacroFriendly(IJ1Helper.java:1095)
	at net.imagej.legacy.IJ1Helper.runMacro(IJ1Helper.java:1144)
	at net.imagej.legacy.plugin.IJ1MacroEngine.eval(IJ1MacroEngine.java:145)
	at org.scijava.script.ScriptModule.run(ScriptModule.java:157)
	at org.scijava.module.ModuleRunner.run(ModuleRunner.java:165)
	at org.scijava.module.ModuleRunner.call(ModuleRunner.java:124)
	at org.scijava.module.ModuleRunner.call(ModuleRunner.java:63)
	at org.scijava.thread.DefaultThreadService.lambda$wrap$2(DefaultThreadService.java:225)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.util.concurrent.ExecutionException: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 50 path $.
	at java.util.concurrent.FutureTask.report(FutureTask.java:122)
	at java.util.concurrent.FutureTask.get(FutureTask.java:192)
	at net.imagej.legacy.LegacyService.runLegacyCompatibleCommand(LegacyService.java:303)
	... 34 more
Caused by: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 50 path $.
	at com.google.gson.Gson.fromJson(Gson.java:947)
	at com.google.gson.Gson.fromJson(Gson.java:897)
	at com.google.gson.Gson.fromJson(Gson.java:846)
	at com.google.gson.Gson.fromJson(Gson.java:817)
	at de.csbdresden.CommandFromMacro.run(CommandFromMacro.java:93)
	at org.scijava.command.CommandModule.run(CommandModule.java:196)
	... 8 more
Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 50 path $.
	at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1564)
	at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:492)
	at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:415)
	at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:184)
	at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
	at com.google.gson.Gson.fromJson(Gson.java:932)
	... 13 more

What is the problem?

M.

Hi @MatthieuV, I’m at the beach with my phone and can’t test it, but I think the problem is here:

You seem to be missing a closing single quote after ImageID.

Best,
Uwe

1 Like