Login  Register

Low level use of thresholder (Was: Headless ImageJ2-rc2 - Analyze Particles returns no result)

Posted by Jan Eglinger on Jun 13, 2014; 12:33pm
URL: http://imagej.273.s1.nabble.com/Headless-ImageJ2-rc2-Analyze-Particles-returns-no-result-tp5008127p5008196.html

Dear Paul,

I just noticed the comment in your example code:

On 13.06.2014, 1:24 PM, Paul van Schayck wrote:
 > IJ.run(imp, "Convert to Mask", ""); // How to use ij.plugin.Thresholder?


and wanted to share how I have done it [*1*]:

     import ij.WindowManager;
     import ij.plugin.Thresholder;

     [...]

     private Thresholder thr;

     [...]

     WindowManager.setTempCurrentImage(imp);
     thr.run("mask");
     WindowManager.setTempCurrentImage(null);

If anyone has a more elegant solution, I would be happy to hear it.

Cheers,
Jan

Footnote *1*: The reason why I was looking for a way to avoid calls of
IJ.run is that using IJ.run within an ExtendedPlugInFilter will break
macro recording when using the preview functionality (i.e. macro
commands will be recorded prematurely when IJ.run will be called from a
dialog containing a Preview checkbox). See this bug for background:
http://fiji.sc/bugzilla/show_bug.cgi?id=651



On 13.06.2014, 1:24 PM, Paul van Schayck wrote:

> Dear Johannes,
>
> Thank you for your extensive response.
>
> On Thu, Jun 12, 2014 at 4:03 PM, Johannes Schindelin
> <[hidden email]> wrote:
>>> I'm also trying to rewrite the macro to js, still struggling a bit with
>>> that, should the bug also be visible there?
>>
>> Yes. Both problems (that the image was not properly registered with ImageJ
>> 1.x as well as the 0 particles found problem caused by the parseDouble()
>> method being incorrect in headless mode) would persist.
>
> Hmm.. this is contratary to what I found using this script:
>
> importClass(Packages.ij.IJ)
> importClass(Packages.ij.plugin.filter.ParticleAnalyzer)
> importClass(Packages.ij.measure.Measurements)
> importClass(Packages.ij.measure.ResultsTable)
>
> imp = IJ.openImage(imagePath);
> IJ.run(imp, "8-bit", "");
> IJ.setThreshold(imp, 1, 255);
> IJ.run(imp, "Convert to Mask", ""); // How to use ij.plugin.Thresholder?
> table = ResultsTable()
> pa = ParticleAnalyzer(0, Measurements.CENTER_OF_MASS, table, 300,
> Number.POSITIVE_INFINITY, 0.4, 1.0)
> pa.analyze(imp)
> print(table.getCounter())
>
> This does work headless. However, maybe you meant running the Analyse
> particle command through IJ.run() will not work. In that case, ignore
> this comment.
>
>> Actually, my preferred way to run scripts is to call
>>
>>          ImageJ-<platform> script.bsh
>>
>> or
>>
>>          ImageJ-<platform> script.js
>
> Running scripts this way in pure ImageJ has been giving this exception
> for me for a few weeks. However, it does work in the daily Fiji build.
>
> Exception in thread "main" java.lang.NullPointerException
>          at net.imagej.launcher.ClassLauncher.launch(ClassLauncher.java:248)
>          at net.imagej.launcher.ClassLauncher.run(ClassLauncher.java:184)
>          at net.imagej.launcher.ClassLauncher.main(ClassLauncher.java:76)
>
>> directly. This will not spin up a context or for that matter, ImageJ, for
>> you, but makes it possible to use ImageJ truly as a library. You would
>> probably want to start up a context anyway, like this:
>>
>>          importClass(Packages.net.imagej.ImageJ);
>>
>>          ij = ImageJ();
>>          ij.command().run(...);
>>
>
> I'm not sure what you mean by this latest example. In which case would
> you like to use this?
>
> Thank you,
>
> Paul
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html