Hello helpful @smcardle !
I’m trying to add this part to my script but I keep getting an error and I understand very little of what’s going wrong here. Could you please help?
This is my current script for cell detection (+ removing artifacts with your method):

import qupath.ext.stardist.StarDist2D
// Specify the model file (you will need to change this!)
def pathModel = 'L:/Basic/divc/ALC/WMD/Bonnie/Viral transduction OSC/Viral Trandsuction Human OSC/Immunofluorescence/QuPath_Script_for_counting/dsb2018_heavy_augment.pb' 
def stardist = StarDist2D.builder(pathModel)
        .threshold(0.50)              // Probability (detection) threshold
        .channels('DAPI')            // Specify detection channel
        .normalizePercentiles(1, 99) // Percentile normalization
        .pixelSize(0.5)              // Resolution for detection
        .measureShape()              // Add shape measurements
        .measureIntensity()          // Add cell measurements (in all compartments)
        .build()
// Run detection for the selected objects
def imageData = getCurrentImageData()
def pathObjects = getSelectedObjects()
if (pathObjects.isEmpty()) {
    Dialogs.showErrorMessage("StarDist", "Please select a parent object!")
    return
stardist.detectObjects(imageData, pathObjects)
def pixelSize= getCurrentServer().getPixelCalibration().getAveragedPixelSize()
smallthresh = 8
largethresh = 50
smalls=getDetectionObjects().findAll{it.getROI().getArea()*pixelSize()*pixelSize<smallthresh}
removeObjects(smalls,true)
bigs=getDetectionObjects().findAll{it.getROI().getArea()*pixelSize()*pixelSize>largethresh}
removeObjects(bigs,true)
println 'Done! '
              

Thanks! This is what it says, if I try to run just this part of the script:

def pixelSize= getCurrentServer().getPixelCalibration().getAveragedPixelSize()
smallthresh = 8
largethresh = 50
smalls=getDetectionObjects().findAll{it.getROI().getArea()*pixelSize()*pixelSize<smallthresh}
removeObjects(smalls,true)
bigs=getDetectionObjects().findAll{it.getROI().getArea()*pixelSize()*pixelSize>largethresh}
removeObjects(bigs,true)

I get:

ERROR: startup failed:
QuPathScript: 2: Unexpected input: 'def' @ line 1, column 52.
   erver().getPixelCalibration() def pixelW
1 error
 in QuPathScript at line number 1
org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:292)
    org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:148)
    org.apache.groovy.parser.antlr4.AstBuilder.collectSyntaxError(AstBuilder.java:4753)
    org.apache.groovy.parser.antlr4.AstBuilder.access$100(AstBuilder.java:169)
    org.apache.groovy.parser.antlr4.AstBuilder$3.syntaxError(AstBuilder.java:4764)
    groovyjarjarantlr4.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:44)
    groovyjarjarantlr4.v4.runtime.Parser.notifyErrorListeners(Parser.java:543)
    groovyjarjarantlr4.v4.runtime.DefaultErrorStrategy.notifyErrorListeners(DefaultErrorStrategy.java:154)
    org.apache.groovy.parser.antlr4.internal.DescriptiveErrorStrategy.reportInputMismatch(DescriptiveErrorStrategy.java:104)
    org.apache.groovy.parser.antlr4.internal.DescriptiveErrorStrategy.recover(DescriptiveErrorStrategy.java:55)
    org.apache.groovy.parser.antlr4.internal.DescriptiveErrorStrategy.recoverInline(DescriptiveErrorStrategy.java:68)
    groovyjarjarantlr4.v4.runtime.Parser.match(Parser.java:213)
    org.apache.groovy.parser.antlr4.GroovyParser.compilationUnit(GroovyParser.java:368)
    org.apache.groovy.parser.antlr4.AstBuilder.buildCST(AstBuilder.java:243)
    org.apache.groovy.parser.antlr4.AstBuilder.buildCST(AstBuilder.java:221)
    org.apache.groovy.parser.antlr4.AstBuilder.buildAST(AstBuilder.java:262)
    org.apache.groovy.parser.antlr4.Antlr4ParserPlugin.buildAST(Antlr4ParserPlugin.java:58)
    org.codehaus.groovy.control.SourceUnit.buildAST(SourceUnit.java:256)
    java.base/java.util.Iterator.forEachRemaining(Unknown Source)
    java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
    java.base/java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
    org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:663)
    groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:373)
    groovy.lang.GroovyClassLoader.lambda$parseClass$2(GroovyClassLoader.java:316)
    org.codehaus.groovy.runtime.memoize.StampedCommonCache.compute(StampedCommonCache.java:163)
    org.codehaus.groovy.runtime.memoize.StampedCommonCache.getAndPut(StampedCommonCache.java:154)
    groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:314)
    groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:298)
    groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258)
    org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getScriptClass(GroovyScriptEngineImpl.java:350)
    org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:159)
    qupath.lib.gui.scripting.languages.DefaultScriptLanguage.execute(DefaultScriptLanguage.java:234)
    qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:1166)
    qupath.lib.gui.scripting.DefaultScriptEditor$3.run(DefaultScriptEditor.java:1534)
    java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    java.base/java.util.concurrent.FutureTask.run(Unknown Source)
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.base/java.lang.Thread.run(Unknown Source)
For help interpreting this error, please search the forum at https://forum.image.sc/tag/qupath
You can also start a new discussion there, including both your script & the messages in this log.
              

The error looks like it’s from a different script to the one posted…

It looks like you had a previous version that used getPixelWidth() and you still have a def pixelW somewhere after your pixel calibration.

Check all the lines in the qupath script window to make sure there isn’t a random def (possibly at the end of line 1)

You’re right! i tried it again and this is what I get:

ERROR: It looks like you've tried to access a method that doesn't exist.
ERROR: No signature of method: java.lang.Double.call() is applicable for argument types: () values: []
Possible solutions: wait(), any(), abs(), abs(), wait(long), any(groovy.lang.Closure) in Script Counting 60 treshold working.groovy at line number 30
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:72)
    org.codehaus.groovy.vmplugin.v8.IndyGuardsFiltersAndSignatures.unwrap(IndyGuardsFiltersAndSignatures.java:163)
    org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
    Script_Counting_60_treshold_working$_run_closure1.doCall(Script Counting 60 treshold working.groovy:30)
    java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(Unknown Source)
    java.base/java.lang.reflect.Method.invoke(Unknown Source)
    org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:343)
    groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:328)
    org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:279)
    groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1008)
    org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39)
    org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    org.codehaus.groovy.runtime.callsite.BooleanReturningMethodInvoker.invoke(BooleanReturningMethodInvoker.java:49)
    org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper.call(BooleanClosureWrapper.java:52)
    org.codehaus.groovy.runtime.DefaultGroovyMethods.findMany(DefaultGroovyMethods.java:5100)
    org.codehaus.groovy.runtime.DefaultGroovyMethods.findAll(DefaultGroovyMethods.java:4954)
    org.codehaus.groovy.runtime.DefaultGroovyMethods.findAll(DefaultGroovyMethods.java:4941)
    org.codehaus.groovy.runtime.dgm$251.doMethodInvoke(Unknown Source)
    org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
    Script_Counting_60_treshold_working.run(Script Counting 60 treshold working.groovy:31)
    org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:331)
    org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:161)
    qupath.lib.gui.scripting.languages.DefaultScriptLanguage.execute(DefaultScriptLanguage.java:234)
    qupath.lib.gui.scripting.DefaultScriptEditor.executeScript(DefaultScriptEditor.java:1166)
    qupath.lib.gui.scripting.DefaultScriptEditor$3.run(DefaultScriptEditor.java:1534)
    java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    java.base/java.util.concurrent.FutureTask.run(Unknown Source)
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.base/java.lang.Thread.run(Unknown Source)
For help interpreting this error, please search the forum at https://forum.image.sc/tag/qupath
You can also start a new discussion there, including both your script & the messages in this log.

And the script that I run is the following:

import qupath.ext.stardist.StarDist2D
// Specify the model file (you will need to change this!)
def pathModel = 'L:/Basic/divc/ALC/WMD/Bonnie/Viral transduction OSC/Viral Trandsuction Human OSC/Immunofluorescence/QuPath_Script_for_counting/dsb2018_heavy_augment.pb' 
def stardist = StarDist2D.builder(pathModel)
        .threshold(0.50)              // Probability (detection) threshold
        .channels('DAPI')            // Specify detection channel
        .normalizePercentiles(1, 99) // Percentile normalization
        .pixelSize(0.5)              // Resolution for detection
        .measureShape()              // Add shape measurements
        .measureIntensity()          // Add cell measurements (in all compartments)
        .build()
// Run detection for the selected objects
def imageData = getCurrentImageData()
def pathObjects = getSelectedObjects()
if (pathObjects.isEmpty()) {
    Dialogs.showErrorMessage("StarDist", "Please select a parent object!")
    return
stardist.detectObjects(imageData, pathObjects)
def pixelSize= getCurrentServer().getPixelCalibration().getAveragedPixelSize()
smallthresh = 8
largethresh = 50
smalls=getDetectionObjects().findAll{it.getROI().getArea()*pixelSize()*pixelSize<smallthresh}
removeObjects(smalls,true)
bigs=getDetectionObjects().findAll{it.getROI().getArea()*pixelSize()*pixelSize>largethresh}
removeObjects(bigs,true)
println 'Done! '