Login  Register

Re: How to return a value from a plugin to a macro?

Posted by ce01 on Mar 13, 2014; 4:52pm
URL: http://imagej.273.s1.nabble.com/How-to-return-a-value-from-a-plugin-to-a-macro-tp3690723p5006903.html

Hi Michael,

I read this topic since it is exactly what I need to perform, and I'm trying to implement your method with static variable and call function from the macro.
However, I got the following message error:

cannot load class My_Plugin

Any hints? Should I somehow "import" this class into my macro?

Thank you
Stefano

Here is my plugin code:

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.*;
import java.io.*;

public class Filter_CountWhitePoints implements PlugInFilter {
        ImagePlus imp;

        static int myResult;
       
        public int setup(String arg, ImagePlus imp) {
                this.imp = imp;
                return DOES_ALL;
        }

        public void run(ImageProcessor ip) {

                byte[] pixels = (byte[])ip.getPixels();
                int[] pix = new int[pixels.length];
                int counter=0;
               
                for (int i=0; i<(pixels.length); i++){
                pix[i] = pixels[i] & 0xff;
      if (pix[i]==0){
                        counter++;
                }

                }
                myResult = counter;
        }

        static String getResult() {
        return Integer.toString(myResult);
     }
}

and the call from the macro (I run the plugin before asking for the return value):

run("Filter CountWhitePoints");
var white = call("Filter CountWhitePoints.getResult");






Michael Schmid-3 wrote
Hi Bill, Gabe,

ok, a bit more specific:

For transferring parameters/data *to* a plugin, the usual way is via  
GenericDialog (also recordable in a macro), as almost all built-in  
ImageJ commands do.
Of course, you can also use static methods that set static variables  
for this purpose.

For transferring data *from* a plugin, setting a static variable and  
having a static method to return it is a reasonable choice  
(especially if there are reasons for not using the Results Table).

Of course, static methods are not multithread-safe.

Example:
In your plugin, you have, e.g.:

public class My_Plugin extends ... {
     static double myResult;

     void run(ImageProcessor ip) {
         ...
         myResult = ...;
     }

     static String getResult() {
         return myResult.toString();
     }
}

In the macro
   myResult = parseFloat(call("My_Plugin.getResult"));

Michael
________________________________________________________________
>
> Michael Schmid wrote:
>>
>> Hi Gabe, there are several possibilities: - have a static method  
>> of your plugin that returns a String. You can access it by the  
>> call macro function - via the Results Table - by setting pixels in  
>> an image created for this putpose (if you want to access an array)  
>> - via a file Michael  
>> ________________________________________________________________  
>> On 16 Oct 2009, at 14:20, gabejackson wrote:
>>>
>>> I've been searching around for this a bit and can't find  
>>> anything. How can i return a value from a plugin to a macro? I've  
>>> got a lot of analysis plugins and would like to perform various  
>>> things with the return values in a macro. thanks in advance Gabe