launch functions in java

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

launch functions in java

CARL Philippe (LBP)
Dear all,

In a plugin the function crop can be launched through: IJ.run("Crop");

But given that the crop corresponds (Ctrl +l) to the function:
ij.plugin.Resizer("crop")

How can it be launched through a call?

And is it better to use a call rather than a IJ.run instruction or inverse?

I thank you very much in advance for your lightnings.

My best regards,

Philippe

 

Philippe CARL

Laboratoire de Biophotonique et Pharmacologie

UMR 7213 CNRS - Université de Strasbourg

Faculté de Pharmacie

74 route du Rhin

67401 ILLKIRCH

Tel : +33(0)3 68 85 41 84


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: launch functions in java

Rasband, Wayne (NIH/NIMH) [E]
> On Jun 10, 2016, at 11:11 AM, Philippe CARL <[hidden email]> wrote:
>
> Dear all,
>
> In a plugin the function crop can be launched through: IJ.run("Crop”);

It is best to use the recorder (Plugins>Macros>Record) to generate code. This is the code recorded when you open the Boats sample image and crop it:

  imp = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
  imp.setRoi(136,185,125,177);
  IJ.run(imp, "Crop", "”);
  imp.show();

> But given that the crop corresponds (Ctrl +l) to the function:
> ij.plugin.Resizer("crop")
>
> How can it be launched through a call?

The Resizer class is not designed to be called directly. Instead, call the ImageProcessor.crop() method. Here is an example:

  imp = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
  ip = imp.getProcessor();
  ip.setRoi(136,185,125,177);
  ip = ip.crop();
  new ImagePlus("Lighthouse",ip).show();

> And is it better to use a call rather than a IJ.run instruction or inverse?

It is better to use IJ.run(imp,"Crop","”); since it works with stacks and overlays, and it preserves spatial calibration.

-wayne

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: launch functions in java

CARL Philippe (LBP)
Hi Wayne,

How are you doing?

I thank you very much for your answer and I indeed confirm (i.e. by checking it) all what you had written.
Nevertheless, please find the following java code of a quite small plugin which had as well been generated by using the recorder (i.e. by using Plugins>Macros>Record):
        ImagePlus imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
        imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
        IJ.run(imp, "Images to Stack", "name=Stack title=[] use");
        imp.show();
Which is given the error message: "No images are open.". What is the issue about it?

Besides this, within a macro, it is possible to import data from a launched plugin by using a defined "Ext." through the macro extensions features.
But how can this be done by launching the plugin from an another plugin?

I thank you very much in advance fir your help and wish you a nice end of week-end.

Kindest regards,

Philippe

Le Samedi 11 Juin 2016 17:14 CEST, "Rasband, Wayne (NIH/NIMH) [E]" <[hidden email]> a écrit:

> > On Jun 10, 2016, at 11:11 AM, Philippe CARL <[hidden email]> wrote:
> >
> > Dear all,
> >
> > In a plugin the function crop can be launched through: IJ.run("Crop”);
>
> It is best to use the recorder (Plugins>Macros>Record) to generate code. This is the code recorded when you open the Boats sample image and crop it:
>
>   imp = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
>   imp.setRoi(136,185,125,177);
>   IJ.run(imp, "Crop", "”);
>   imp.show();
>
> > But given that the crop corresponds (Ctrl +l) to the function:
> > ij.plugin.Resizer("crop")
> >
> > How can it be launched through a call?
>
> The Resizer class is not designed to be called directly. Instead, call the ImageProcessor.crop() method. Here is an example:
>
>   imp = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
>   ip = imp.getProcessor();
>   ip.setRoi(136,185,125,177);
>   ip = ip.crop();
>   new ImagePlus("Lighthouse",ip).show();
>
> > And is it better to use a call rather than a IJ.run instruction or inverse?
>
> It is better to use IJ.run(imp,"Crop","”); since it works with stacks and overlays, and it preserves spatial calibration.
>
> -wayne
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html





--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: launch functions in java

Michael Schmid
Hi Philippe,

for "Images to Stack", you have to show the images first.
This command uses the open images, i.e., those that are shown.

   imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
   //imp.setTitle("blobs1.gif"); //would be nicer with this
   imp.show();
   imp2 = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
   imp2.show();
   IJ.run(imp, "Images to Stack", "name=Stack title=[] use");

As an alternative, you can do it like the following without showing the
images:

   imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
   is = imp.getStack();
   imp2 = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
   ip = imp2.getProcessor();
   is.addSlice(ip);
   IJ.log("Stack size: "+is.getSize());
   stackImp = new ImagePlus("The stack", is);
   stackImp.show();

---

 > Besides this, within a macro, it is possible to import data from a
 > launched plugin by using a defined "Ext." through the macro extensions
 > features.
 > But how can this be done by launching the plugin from an another
 > plugin?

Usually, the easiest way is having a public method of the plugin that
gets called.  If you can't modify that plugin, have a look at
   ij.macro.ExtensionDescriptor
and the
   ij.macro.Functions.doExt() method
to get some inspirations on how the macro extensions it can be called.
(I have no experience with this, however).

Michael
________________________________________________________________
On 2016-06-12 23:43, CARL Philippe (PHA) wrote:

> Hi Wayne,
>
> How are you doing?
>
> I thank you very much for your answer and I indeed confirm (i.e. by
> checking it) all what you had written.
> Nevertheless, please find the following java code of a quite small
> plugin which had as well been generated by using the recorder (i.e. by
> using Plugins>Macros>Record):
> ImagePlus imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
> imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
> IJ.run(imp, "Images to Stack", "name=Stack title=[] use");
> imp.show();
> Which is given the error message: "No images are open.". What is the
> issue about it?
>
> Besides this, within a macro, it is possible to import data from a
> launched plugin by using a defined "Ext." through the macro extensions
> features.
> But how can this be done by launching the plugin from an another
> plugin?
>
> I thank you very much in advance fir your help and wish you a nice
> end  of week-end.
>
> Kindest regards,
>
> Philippe
>
> Le Samedi 11 Juin 2016 17:14 CEST, "Rasband, Wayne (NIH/NIMH) [E]"  <[hidden email]> a écrit:
>
>>> On Jun 10, 2016, at 11:11 AM, Philippe CARL  <[hidden email]> wrote:
>>>
>>> Dear all,
>>>
>>> In a plugin the function crop can be launched through:
>>> IJ.run("Crop”);
>>
>> It is best to use the recorder (Plugins>Macros>Record) to generate
>> code. This is the code recorded when you open the Boats sample image and
>> crop it:
>>
>>   imp = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
>>   imp.setRoi(136,185,125,177);
>>   IJ.run(imp, "Crop", "”);
>>   imp.show();
>>
>>> But given that the crop corresponds (Ctrl +l) to the function:
>>> ij.plugin.Resizer("crop")
>>>
>>> How can it be launched through a call?
>>
>> The Resizer class is not designed to be called directly. Instead,
>> call the ImageProcessor.crop() method. Here is an example:
>>
>>   imp = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
>>   ip = imp.getProcessor();
>>   ip.setRoi(136,185,125,177);
>>   ip = ip.crop();
>>   new ImagePlus("Lighthouse",ip).show();
>>
>>> And is it better to use a call rather than a IJ.run instruction or inverse?
>>
>> It is better to use IJ.run(imp,"Crop","”); since it works with
>> stacks  and overlays, and it preserves spatial calibration.
>>
>> -wayne
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
>
>
>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: launch functions in java

CARL Philippe (LBP)
Hi Michael,
How are you doing?
I had finally figured out the first version (i.e. with the imp.show() added after each IJ.openImage) but the alternative version is what I was actually looking for (to be faster since the pictures are not shown).
Nevertheless, I didn't expect the needed code to be so complicated, thus thanks a lot for sharing it!!!
I can modify the plugin I working on, thus all options are open and I would prefer to choose the most adapted and efficient one.
Also would you have an example to link me with where there is a "public method of the plugin that gets called"?
The issue is that the plugin has already " ij.macro.MacroExtension" features and when I try to launch it through a "IJ.run" I get the "Macro must be running to install macro extensions" error which corresponds to the case (interp==null) of the "registerExtensions" method within the " ij.macro.Functions.java" class.
Which obviously means that I'm doing something wrong here and I'm kind of stuck...
I thank you very much in advance for your help.
Kindest regards,
Philippe

-----Message d'origine-----
De : ImageJ Interest Group [mailto:[hidden email]] De la part de Michael Schmid
Envoyé : lundi 13 juin 2016 11:22
À : [hidden email]
Objet : Re: launch functions in java

Hi Philippe,

for "Images to Stack", you have to show the images first.
This command uses the open images, i.e., those that are shown.

   imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
   //imp.setTitle("blobs1.gif"); //would be nicer with this
   imp.show();
   imp2 = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
   imp2.show();
   IJ.run(imp, "Images to Stack", "name=Stack title=[] use");

As an alternative, you can do it like the following without showing the
images:

   imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
   is = imp.getStack();
   imp2 = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
   ip = imp2.getProcessor();
   is.addSlice(ip);
   IJ.log("Stack size: "+is.getSize());
   stackImp = new ImagePlus("The stack", is);
   stackImp.show();

---

 > Besides this, within a macro, it is possible to import data from a  > launched plugin by using a defined "Ext." through the macro extensions  > features.
 > But how can this be done by launching the plugin from an another  > plugin?

Usually, the easiest way is having a public method of the plugin that gets called.  If you can't modify that plugin, have a look at
   ij.macro.ExtensionDescriptor
and the
   ij.macro.Functions.doExt() method
to get some inspirations on how the macro extensions it can be called.
(I have no experience with this, however).

Michael
________________________________________________________________
On 2016-06-12 23:43, CARL Philippe (PHA) wrote:

> Hi Wayne,
>
> How are you doing?
>
> I thank you very much for your answer and I indeed confirm (i.e. by
> checking it) all what you had written.
> Nevertheless, please find the following java code of a quite small
> plugin which had as well been generated by using the recorder (i.e. by
> using Plugins>Macros>Record):
> ImagePlus imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
> imp = IJ.openImage("http://wsr.imagej.net/images/blobs.gif");
> IJ.run(imp, "Images to Stack", "name=Stack title=[] use");
> imp.show();
> Which is given the error message: "No images are open.". What is the
> issue about it?
>
> Besides this, within a macro, it is possible to import data from a
> launched plugin by using a defined "Ext." through the macro extensions
> features.
> But how can this be done by launching the plugin from an another
> plugin?
>
> I thank you very much in advance fir your help and wish you a nice end  
> of week-end.
>
> Kindest regards,
>
> Philippe
>
> Le Samedi 11 Juin 2016 17:14 CEST, "Rasband, Wayne (NIH/NIMH) [E]"  <[hidden email]> a écrit:
>
>>> On Jun 10, 2016, at 11:11 AM, Philippe CARL  <[hidden email]> wrote:
>>>
>>> Dear all,
>>>
>>> In a plugin the function crop can be launched through:
>>> IJ.run("Crop”);
>>
>> It is best to use the recorder (Plugins>Macros>Record) to generate
>> code. This is the code recorded when you open the Boats sample image
>> and crop it:
>>
>>   imp = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
>>   imp.setRoi(136,185,125,177);
>>   IJ.run(imp, "Crop", "”);
>>   imp.show();
>>
>>> But given that the crop corresponds (Ctrl +l) to the function:
>>> ij.plugin.Resizer("crop")
>>>
>>> How can it be launched through a call?
>>
>> The Resizer class is not designed to be called directly. Instead,
>> call the ImageProcessor.crop() method. Here is an example:
>>
>>   imp = IJ.openImage("http://wsr.imagej.net/images/boats.gif");
>>   ip = imp.getProcessor();
>>   ip.setRoi(136,185,125,177);
>>   ip = ip.crop();
>>   new ImagePlus("Lighthouse",ip).show();
>>
>>> And is it better to use a call rather than a IJ.run instruction or inverse?
>>
>> It is better to use IJ.run(imp,"Crop","”); since it works with stacks  
>> and overlays, and it preserves spatial calibration.
>>
>> -wayne
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
>
>
>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: launch functions in java

Michael Schmid
Hi Philippe,

as I said, I have no experience with macro extensions. There is a bit of
documentation at
   https://imagej.nih.gov/ij/macros/MacroExtensions.txt

One obvious way of accessing macro extensions in a plugin would be
calling a short macro by
   IJ.runMacro(String macro)


Concerning public methods: For calling a plugin from other plugins, I
think this should be obvious. A macro can call only static methods:
   https://imagej.nih.gov/ij/developer/macro/functions.html#call
This method can also return a String.

An example for calling a static function is my Versatile_Wand:
 
http://imagejdocu.tudor.lu/doku.php?id=plugin:segmentation:versatile_wand:start

It can be called via
   call("Versatile_Wand_Tool.doWand", x, y, tolerance, colorSensitivity,
gradientTolerance, "options");

and the plugin has a method

   public static void doWand(String xStartS, String yStartS,
     String toleranceS, String colorS, String gradientS, String options)

In this method, the strings are converted (parsed) to numbers where
appropriate, and then a new instance of the plugin is created and gets
the arguments to do the work.

Michael
________________________________________________________________
On 2016-06-13 14:50, Philippe CARL wrote:
> Hi Michael,
> How are you doing?
> I had finally figured out the first version (i.e. with the
> imp.show()
added after each IJ.openImage) but the alternative version is what I was
actually looking for (to be faster since the pictures are not shown).
> Nevertheless, I didn't expect the needed code to be so complicated,
thus thanks a lot for sharing it!!!
> I can modify the plugin I working on, thus all options are open and
> Iwould prefer to choose the most adapted and efficient one.
> Also would you have an example to link me with where there is a
"public method of the plugin that gets called"?
> The issue is that the plugin has already " ij.macro.MacroExtension"
features and when I try to launch it through a "IJ.run" I get the "Macro
must be running to install macro extensions" error which corresponds to
the case (interp==null) of the "registerExtensions" method within the "
ij.macro.Functions.java" class.
> Which obviously means that I'm doing something wrong here and I'm
> kindof stuck...
> I thank you very much in advance for your help. > Kindest regards,
> Philippe

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: launch functions in java

CARL Philippe (LBP)
Hi Michael,
How are you doing? (I hope you have a better weather than what we get here!!!)
I thank you very much for your answer that helped me to understand a couple of things.
So first to solve the macro extension issue (i.e. get rid of the "Macro must be running to install macro extensions" error) when launching the plugin form another plugin, I have added a "Interpreter.getInstance()" condition and now everything works fine by using the following code:
                if (!IJ.macroRunning() || Interpreter.getInstance() == null)
                {
                        IJ.error("Cannot install macro extensions from outside a macro!");
                        return;
                }
                else
                        Functions.registerExtensions(this);
Nevertheless, I still have some issues for "calling a plugin from other plugins".
Indeed, if I have the following plugin file (Call_Demo.java):
                public class Call_Demo
                {
                        public static String method1(String arg1)
                        {
                                return "method1"+"("+arg1+")";
                        }
                }
That I call with the following plugin:
                import ij.*;
                import ij.plugin.*;

                public class My_Plugin implements PlugIn
                {
                        public void run(String arg)
                        {
                                IJ.log(Call_Demo.method1("arg1"));
                        }
                }
Everything is working fine: i.e. I get the result "method1(arg1)" within the log window.
But when I put the Call_Demo.java file in a folder named "test" and add the line:
                package test;
within the plugin code.
In this case, I'm able to correctly compile the new Call_Demo.java file, but as I expected that I need to change the
IJ.log(Call_Demo.method1("arg1"));
line into
IJ.log(test.Call_Demo.method1("arg1"));
this is not working and giving me the error: "package test does not exist".
Knowing that the final goal is to launch the Call_Demo.method1 from within a jar file similar to the one you can find under the link:
http://punias.free.fr/ImageJ/arf_.jar
Do you have an idea of what I'm doing wrong and should correct?
I thank you very much in advance for your help.
Kindest regards,
Philippe

-----Message d'origine-----
De : ImageJ Interest Group [mailto:[hidden email]] De la part de Michael Schmid
Envoyé : mercredi 15 juin 2016 10:23
À : [hidden email]
Objet : Re: launch functions in java

Hi Philippe,

as I said, I have no experience with macro extensions. There is a bit of documentation at
   https://imagej.nih.gov/ij/macros/MacroExtensions.txt

One obvious way of accessing macro extensions in a plugin would be calling a short macro by
   IJ.runMacro(String macro)


Concerning public methods: For calling a plugin from other plugins, I
think this should be obvious. A macro can call only static methods:
   https://imagej.nih.gov/ij/developer/macro/functions.html#call
This method can also return a String.

An example for calling a static function is my Versatile_Wand:
 
http://imagejdocu.tudor.lu/doku.php?id=plugin:segmentation:versatile_wand:start

It can be called via
   call("Versatile_Wand_Tool.doWand", x, y, tolerance, colorSensitivity,
gradientTolerance, "options");

and the plugin has a method

   public static void doWand(String xStartS, String yStartS,
     String toleranceS, String colorS, String gradientS, String options)

In this method, the strings are converted (parsed) to numbers where
appropriate, and then a new instance of the plugin is created and gets
the arguments to do the work.

Michael
________________________________________________________________
On 2016-06-13 14:50, Philippe CARL wrote:
> Hi Michael,
> How are you doing?
> I had finally figured out the first version (i.e. with the
> imp.show()
added after each IJ.openImage) but the alternative version is what I was
actually looking for (to be faster since the pictures are not shown).
> Nevertheless, I didn't expect the needed code to be so complicated,
thus thanks a lot for sharing it!!!
> I can modify the plugin I working on, thus all options are open and
> Iwould prefer to choose the most adapted and efficient one.
> Also would you have an example to link me with where there is a
"public method of the plugin that gets called"?
> The issue is that the plugin has already " ij.macro.MacroExtension"
features and when I try to launch it through a "IJ.run" I get the "Macro
must be running to install macro extensions" error which corresponds to
the case (interp==null) of the "registerExtensions" method within the "
ij.macro.Functions.java" class.
> Which obviously means that I'm doing something wrong here and I'm
> kindof stuck...
> I thank you very much in advance for your help. > Kindest regards,
> Philippe

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

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