Skip to content

Instantly share code, notes, and snippets.

@maweigert
Last active July 12, 2023 12:41
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save maweigert/8dd6ef139e1cd37b2307b35fb50dee4a to your computer and use it in GitHub Desktop.
Save maweigert/8dd6ef139e1cd37b2307b35fb50dee4a to your computer and use it in GitHub Desktop.
#@ DatasetIOService io
#@ CommandService command
""" This example runs stardist on all tif files in a folder
Full list of Parameters:
res = command.run(StarDist2D, False,
"input", imp, "modelChoice", "Versatile (fluorescent nuclei)",
"modelFile","/path/to/TF_SavedModel.zip",
"normalizeInput",True, "percentileBottom",1, "percentileTop",99.8,
"probThresh",0.5, "nmsThresh", 0.3, "outputType","Label Image",
"nTiles",1, "excludeBoundary",2, "verbose",1, "showCsbdeepProgress",1, "showProbAndDist",0).get();
"""
from de.csbdresden.stardist import StarDist2D
from glob import glob
import os
# run stardist on all tiff files in <indir> and save the label image to <outdir>
indir = os.path.expanduser("~/Tmp/images/")
outdir = os.path.expanduser("~/Tmp/labels/")
for f in sorted(glob(os.path.join(indir,"*.tif"))):
print "processing ", f
imp = io.open(f)
res = command.run(StarDist2D, False,
"input", imp,
"modelChoice", "Versatile (fluorescent nuclei)",
).get()
label = res.getOutput("label")
io.save(label, os.path.join(outdir,"label."+os.path.basename(f)))
@Cocomolch4000
Copy link

hi there,
how can one apply all the settings (like "excludeBoundary" or "nmsThresh") with your current syntax, e.g.

model = StarDist2D.from_pretrained('2D_versatile_he')
img = normalize(X[image_ind], 1.8,97.5, axis=axis_norm)
labels, details = model.predict_instances(img)
coord, points, prob = details['coord'], details['points'], details['prob']

?
If it is not possible, where can one download the "de.csbdresden.stardist" module, i did not find it.
thanks in advance.
best
coco

@uschmidt83
Copy link

how can one apply all the settings (like "excludeBoundary" or "nmsThresh") with your current syntax

You can see it in the comment here.

@afrendeiro
Copy link

where can one download the "de.csbdresden.stardist" module, i did not find it.

This is an ImageJ plugin - even though it's Python - it is intended to be run by ImageJ after installing the stardist plugin.

@abdusalamApps
Copy link

@uschmidt83 Hi,
I'm trying to run the code above, but I need the get out the ROI (number of cells and the area) from the results.
I tried to find what strings I can give the getOutput() https://gist.github.com/maweigert/8dd6ef139e1cd37b2307b35fb50dee4a#file-stardist_script-py-L34 method, but I couldn't find any info about that!
I see that I can give it "label", but what else?

@uschmidt83
Copy link

I'm trying to run the code above, but I need the get out the ROI (number of cells and the area) from the results.

If you use "outputType", "ROI Manager", StarDist will populate ImageJ's Roi Manager, which can give you access to number of cells and some measurements of your choice. However, this will not show up as something that you can access with getOutput() and I don't know how to automate this in a script. I think it can be done, and I'd ask at the image.sc forum about it.

@Bassetyabrag
Copy link

Hello!
I am trying to use this code to run stardist on all the pics in the directory but I am getting this error,
SyntaxError: no viable alternative at input ‘=’

does anyone know how to fix it.

thank you

`#@ DatasetIOService io
#@ CommandService command

theinput = "C:\Users\abdel\Downloads\8bitgreen";
output = "C:\Users\abdel\Downloads\try";
print (theinput)

setBatchMode(true);
list = getFileList(theinput);
print (list)

for (i = 0; i < lengthof(list); i+=){
IJ.run(list[i], "8-bit", "");
run("Command From Macro", "command=[de.csbdresden.stardist.StarDist2D], args=['input':'list[i]', 'modelChoice':'Versatile (fluorescent nuclei)', 'normalizeInput':'true', 'percentileBottom':'1.0', 'percentileTop':'99.8', 'probThresh':'0.15000000000000002', 'nmsThresh':'0.3', 'outputType':'Both', 'modelFile':'C:\\Users\\abdel\\Downloads\\dsb2018_heavy_augment.zip', 'nTiles':'5', 'excludeBoundary':'2', 'roiPosition':'Automatic', 'verbose':'false', 'showCsbdeepProgress':'false', 'showProbAndDist':'false'], process=[false]");
rm.runCommand(list[i],"Measure");
IJ.saveAs("Results", output + filename);
}
setBatchMode(false);
`

@uschmidt83
Copy link

@Bassetyabrag You cross-posted this also at the image.sc forum, where I replied to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment