Using ImageJ in another java pp in the existing jre, and batch mode on linux

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Using ImageJ in another java pp in the existing jre, and batch mode on linux

Jared Hodge
Hi, I'm new to ImageJ, so forgive me if this has been addressed before.
I have a java application where I am wanting to use ImageJ.  I would
think running it in my existing jre would be the most efficient way to
process lots of images.  I mostly want to just be able to run macros,
which I'm can do by calling ij.IJ runMacro.  The problem is I want them
to run without any feedback to the monitor (similar to -batch, but
actually processing the script I pass in), but runMacro displays a
"results" GUI on the screen.  Also, if I ever construct an "ij.ImageJ"
in my jre to display the GUI, all macros thereafter display even more
stuff on the screen, even if I've closed the ImageJ GUI (until I restart
my application).  Looking at the code a bit, it looks like the run
method which shuts down the ImageJ GUI is not cleaning up the static
references to itself in the IJ object.

 

I've also noticed a difference in the behavior of macros on Windows and
Linux.  On Windows if I run a batch script, no results are displayed to
the screen.  On Linux, the GUI briefly pops up a "results" window, which
I think tends to slow things down as rather annoying.  Is this a bug?
My test case of my application above is on Linux, so I'm not sure if
these are related.

 

Thanks for any help.

-Jared

 

 
Reply | Threaded
Open this post in threaded view
|

Re: Using ImageJ in another java pp in the existing jre, and batch mode on linux

Jared Hodge
After a little more testing, I think this perceived difference in their
behavior may be due to windows being so much slower to start up the GUI
than Linux (at least for my test systems).  It takes about 3 seconds to
start the GUI under windows and about 1 second under Linux.  The macro
I'm running only takes just over a second to complete, and then I guess
the "System.exit" is reached before the GUI even started on Windows.

 

Either way, this seems like a bug, or am I just not doing something
right?  How do I completely suppress the display of the results window?

 

The macro component that seems to be doing the output is run("Analyze
Particles...").  Is this component broken for batch-mode?

-Jared

 

From: Jared Hodge
Sent: Friday, February 12, 2010 10:44 AM
To: '[hidden email]'
Subject: Using ImageJ in another java pp in the existing jre, and batch
mode on linux

 

Hi, I'm new to ImageJ, so forgive me if this has been addressed before.
I have a java application where I am wanting to use ImageJ.  I would
think running it in my existing jre would be the most efficient way to
process lots of images.  I mostly want to just be able to run macros,
which I'm can do by calling ij.IJ runMacro.  The problem is I want them
to run without any feedback to the monitor (similar to -batch, but
actually processing the script I pass in), but runMacro displays a
"results" GUI on the screen.  Also, if I ever construct an "ij.ImageJ"
in my jre to display the GUI, all macros thereafter display even more
stuff on the screen, even if I've closed the ImageJ GUI (until I restart
my application).  Looking at the code a bit, it looks like the run
method which shuts down the ImageJ GUI is not cleaning up the static
references to itself in the IJ object.

 

I've also noticed a difference in the behavior of macros on Windows and
Linux.  On Windows if I run a batch script, no results are displayed to
the screen.  On Linux, the GUI briefly pops up a "results" window, which
I think tends to slow things down as rather annoying.  Is this a bug?
My test case of my application above is on Linux, so I'm not sure if
these are related.

 

Thanks for any help.

-Jared

 

 
Reply | Threaded
Open this post in threaded view
|

Re: Using ImageJ in another java pp in the existing jre, and batch mode on linux

Jared Hodge
In reply to this post by Jared Hodge
OK, based on the lack of responses, and my own testing, I'm thinking the
behavior that I'm after is either not implemented or broken.  It was
actually trivial to fix once I started working on the java itself, so
here are my additions.  Can someone with commit permissions review these
and commit them or respond back to me with any issues:

 

In ij/measure/ResultsTable.java one added line (shown in context):

public void show(String windowTitle) {

    if(IJ.getInstance() == null) return;

    if (!windowTitle.equals("Results") &&
this==Analyzer.getResultsTable())

 

That fixes the issue with the popup results window in batch mode.

Next to fix the issues with reusing the jre, I made the following two
changes:

In ij/IJ.java, one new function:

static void cleanup() {

    ij=null; applet=null; progressBar=null; textPanel=null;

}

 

And I call that function at the end of the run() function in
ImageJ.java:

    dispose();

    if (exitWhenQuitting)

        System.exit(0);

    IJ.cleanup(); //Clear up the references in case anything else is
done in this jvm

}

 

Hope this is helpful.  Thanks.

-Jared

 

 

From: Jared Hodge
Sent: Friday, February 12, 2010 1:36 PM
To: '[hidden email]'
Subject: RE: Using ImageJ in another java pp in the existing jre, and
batch mode on linux

 

After a little more testing, I think this perceived difference in their
behavior may be due to windows being so much slower to start up the GUI
than Linux (at least for my test systems).  It takes about 3 seconds to
start the GUI under windows and about 1 second under Linux.  The macro
I'm running only takes just over a second to complete, and then I guess
the "System.exit" is reached before the GUI even started on Windows.

 

Either way, this seems like a bug, or am I just not doing something
right?  How do I completely suppress the display of the results window?

 

The macro component that seems to be doing the output is run("Analyze
Particles...").  Is this component broken for batch-mode?

-Jared

 

From: Jared Hodge
Sent: Friday, February 12, 2010 10:44 AM
To: '[hidden email]'
Subject: Using ImageJ in another java pp in the existing jre, and batch
mode on linux

 

Hi, I'm new to ImageJ, so forgive me if this has been addressed before.
I have a java application where I am wanting to use ImageJ.  I would
think running it in my existing jre would be the most efficient way to
process lots of images.  I mostly want to just be able to run macros,
which I'm can do by calling ij.IJ runMacro.  The problem is I want them
to run without any feedback to the monitor (similar to -batch, but
actually processing the script I pass in), but runMacro displays a
"results" GUI on the screen.  Also, if I ever construct an "ij.ImageJ"
in my jre to display the GUI, all macros thereafter display even more
stuff on the screen, even if I've closed the ImageJ GUI (until I restart
my application).  Looking at the code a bit, it looks like the run
method which shuts down the ImageJ GUI is not cleaning up the static
references to itself in the IJ object.

 

I've also noticed a difference in the behavior of macros on Windows and
Linux.  On Windows if I run a batch script, no results are displayed to
the screen.  On Linux, the GUI briefly pops up a "results" window, which
I think tends to slow things down as rather annoying.  Is this a bug?
My test case of my application above is on Linux, so I'm not sure if
these are related.

 

Thanks for any help.

-Jared

 

 
Reply | Threaded
Open this post in threaded view
|

Re: Using ImageJ in another java pp in the existing jre, and batch mode on linux

Adam Waite
Hi Jared,

I'm glad you wrote to the list.  I have a similar problem that I asked about on February 7th, and also received no response.  My issue also involves trying to use ImageJ as a library for a Java-based image analysis program.  Specifically, my problem is the requirement of the RoiManager class to show a window in order to add a selection.  This forces me to show an image, which prevents this program from running completely in the background, and slows down analysis.

Have you tried to use RoiManager in your programs?  If so, have you found a way to circumvent this requirement?  I'm including the relevant part of my first email below:



Here's what I need to do:
1) Threshold a 16-bit grayscale image. (Using setAutoThreshold).
2) Make a mask out of this threshold. (Using IJ.run(img, "Convert to Mask","");)
3) Make a selection out of this mask.
4) Apply this selection to the original image.

To accomplish the transition between steps 3 and 4, I am using the following code:

<code>

RoiManager rm = new RoiManager(false);

    IJ.run(bgmask, "Create Selection","");
    bgmask.show();
    rm.runCommand("Add");
    bgmask.close();

    Roi[] roi = rm.getRoisAsArray();
    bs.setRoi(roi[0]);
    ImageStatistics fg_stats = bs.getStatistics(ImageStatistics.MEDIAN);
</code>

The problem is that if I don't run "bgmask.show()" the roi manager complains that "there are no images open" and aborts the run.

Thanks for any insights you may have,
Adam

 =====
Science is more of an art than a science.
Reply | Threaded
Open this post in threaded view
|

Re: Using ImageJ in another java pp in the existing jre, and batch mode on linux

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Jared Hodge
On Feb 12, 2010, at 11:43 AM, Jared Hodge wrote:

> Hi, I'm new to ImageJ, so forgive me if this has been addressed before.
> I have a java application where I am wanting to use ImageJ.  I would
> think running it in my existing jre would be the most efficient way to
> process lots of images.  I mostly want to just be able to run macros,
> which I'm can do by calling ij.IJ runMacro.  The problem is I want them
> to run without any feedback to the monitor (similar to -batch, but
> actually processing the script I pass in), but runMacro displays a
> "results" GUI on the screen.  Also, if I ever construct an "ij.ImageJ"
> in my jre to display the GUI, all macros thereafter display even more
> stuff on the screen, even if I've closed the ImageJ GUI (until I restart
> my application).  Looking at the code a bit, it looks like the run
> method which shuts down the ImageJ GUI is not cleaning up the static
> references to itself in the IJ object.

These bugs are fixed in the 1.43r3 daily build.

-wayne
Reply | Threaded
Open this post in threaded view
|

Re: Using ImageJ in another java pp in the existing jre, and batch mode on linux

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Adam Waite
On Feb 13, 2010, at 12:27 AM, Adam wrote:

> Hi Jared,
>
> I'm glad you wrote to the list.  I have a similar problem that I asked about on February 7th, and also received no response.  My issue also involves trying to use ImageJ as a library for a Java-based image analysis program.  Specifically, my problem is the requirement of the RoiManager class to show a window in order to add a selection.  This forces me to show an image, which prevents this program from running completely in the background, and slows down analysis.
>
> Have you tried to use RoiManager in your programs?  If so, have you found a way to circumvent this requirement?  I'm including the relevant part of my first email below:
>
> Here's what I need to do:
> 1) Threshold a 16-bit grayscale image. (Using setAutoThreshold).
> 2) Make a mask out of this threshold. (Using IJ.run(img, "Convert to Mask","");)
> 3) Make a selection out of this mask.
> 4) Apply this selection to the original image.
>
> To accomplish the transition between steps 3 and 4, I am using the following code:
>
> <code>
>
> RoiManager rm = new RoiManager(false);
>
>    IJ.run(bgmask, "Create Selection","");
>    bgmask.show();
>    rm.runCommand("Add");
>    bgmask.close();
>
>    Roi[] roi = rm.getRoisAsArray();
>    bs.setRoi(roi[0]);
>    ImageStatistics fg_stats = bs.getStatistics(ImageStatistics.MEDIAN);
> </code>
>
> The problem is that if I don't run "bgmask.show()" the roi manager complains that "there are no images open" and aborts the run.

Use

   rm.addRoi(bgmask.getRoi());

instead of

   rm.runCommand("Add");

-wayne