Re: Parsing formulas/macro into ImageJ

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

Re: Parsing formulas/macro into ImageJ

ctrueden
Hi Max,

> I am trying to build a plugin for ImageJ where the user can input a
> function as text and the program will parse it and it will output an
> image.

There are several things like this in ImageJ2.

You can generate an image using a JavaScript formula:
https://nbviewer.jupyter.org/github/imagej/tutorials/blob/ma
ster/notebooks/1_-_Using_ImageJ/2_-_Introduction_to_ImageJ_O
ps.ipynb#Generating-an-image-from-a-formula

You can also evaluate an expression involving variables:
https://nbviewer.jupyter.org/github/imagej/tutorials/blob/ma
ster/notebooks/1_-_Using_ImageJ/2_-_Introduction_to_ImageJ_O
ps.ipynb#Evaluating-expressions

You can also execute arbitrary scripts in any SciJava-supported script
language using the ScriptService:
https://nbviewer.jupyter.org/github/imagej/tutorials/blob/
master/notebooks/1_-_Using_ImageJ/1_-_Fundamentals_of_ImageJ.ipynb#Scripts

Even if you decide you want to do your own thing instead of using Ops or
SciJava scripting, I advise you not to write your own parser, but rather
use Parsington:
https://github.com/scijava/parsington

Regards,
Curtis

--
Curtis Rueden
LOCI software architect - https://loci.wisc.edu/software
ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
Did you know ImageJ has a forum? http://forum.imagej.net/


On Fri, Dec 8, 2017 at 4:27 PM, Maximilian Maske <
[hidden email]> wrote:

> Hi there,
>
> I am trying to build a plugin for ImageJ where the user can input a
> function as text and the program will parse it and it will output an
> image. My question is how to parse a string to either Macro code or Java
> code to process it. My solution right now is quite slow because I
> transform the function for every pixel into a String and get the result
> as a string from IJ.macro() which I have to parse to a double again.
>
> This is my general idea in Java code:
>
> // example input
> String formula = "sin(x)*sin(x)+sin(y)*sin(y)";
> int width = 128, height = 128;
>
> int[][] image = new int[width][height];
>
> for(int y=0; y<height; y++) {for(int x=0; x<width; x++) {
> String curr_formula = formula.replace("x", "" +   x).replace("y", "" + y);
>
>         String val = IJ.runMacro("return '' + " + curr_formula + ";");
>         double value = Double.parseDouble(val);
>
>         image[x][y] = value ;
>     }
> }
>
> Does somebody have a better idea or knows another framework to parse
> functions?
>
> Thank you for the help,
> Max
>
> PS: If need to compile the code you can use this method to see what
> actually happens:
>
> public void functionToImage(int width, int height, int slices, double min,
> double max, String formula) {
>
>         ImagePlus imagePlus = IJ.createImage("test", "32-Bit", width,
> height, slices);
>         ImageProcessor processor = imagePlus.getProcessor();
>
>         double range = Math.abs(min - max);
>
>         String curr_formula;
>
>      for(int y_=0; y_<height; y_++) {
>
>             double y = (double) y_ / height; // 0..1 double dy = range * y
> + min; // min..max for (int x_ = 0; x_ < width; x_++) {
>
>                 double x = (double) x_ / width; // 0..1 double dx = range
> * x + min; //min..max curr_formula = formula.replace("x", "" +
>  dx).replace("y", "" + dy);
>
>                 String val = IJ.runMacro("return '' + " + curr_formula +
> ";");
>                 double val_ = Double.parseDouble(val);processor.putPixelValue(x_,
> y_, val_);
>             }
>         }
>
>         imagePlus.show();
> }
>
>
> --
> 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: Parsing formulas/macro into ImageJ

Herbie
Maximilian,

did you have a look at:

<https://imagej.nih.gov/ij/macros/examples/MathMacroDemo.txt>

Regards

Herbie

::::::::::::::::::::::::::::::::::::::::::::::
Am 08.12.17 um 16:27 schrieb Maximilian Maske:

> Hi there,
>
> I am trying to build a plugin for ImageJ where the user can input a
> function as text and the program will parse it and it will output an
> image. My question is how to parse a string to either Macro code or Java
> code to process it. My solution right now is quite slow because I
> transform the function for every pixel into a String and get the result
> as a string from IJ.macro() which I have to parse to a double again.
>
> This is my general idea in Java code:
>
> // example input
> String formula = "sin(x)*sin(x)+sin(y)*sin(y)";
> int width = 128, height = 128;
>
> int[][] image = new int[width][height];
>
> for(int y=0; y<height; y++) {for(int x=0; x<width; x++) {
> String curr_formula = formula.replace("x", "" +   x).replace("y", "" + y);
>
>          String val = IJ.runMacro("return '' + " + curr_formula + ";");
>          double value = Double.parseDouble(val);
>
>          image[x][y] = value ;
>      }
> }
>
> Does somebody have a better idea or knows another framework to parse functions?
>
> Thank you for the help,
> Max
>
> PS: If need to compile the code you can use this method to see what actually happens:
>
> public void functionToImage(int width, int height, int slices, double min, double max, String formula) {
>
>          ImagePlus imagePlus = IJ.createImage("test", "32-Bit", width, height, slices);
>          ImageProcessor processor = imagePlus.getProcessor();
>
>          double range = Math.abs(min - max);
>
>          String curr_formula;
>
>       for(int y_=0; y_<height; y_++) {
>
>              double y = (double) y_ / height; // 0..1 double dy = range * y + min; // min..max for (int x_ = 0; x_ < width; x_++) {
>
>                  double x = (double) x_ / width; // 0..1 double dx = range * x + min; //min..max curr_formula = formula.replace("x", "" +   dx).replace("y", "" + dy);
>
>                  String val = IJ.runMacro("return '' + " + curr_formula + ";");
>                  double val_ = Double.parseDouble(val);processor.putPixelValue(x_, y_, val_);
>              }
>          }
>
>          imagePlus.show();
> }
>
>
> --
> 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: Parsing formulas/macro into ImageJ

Michael Schmid
Hi Maximilian,

just a bit more background to Herbie's answer:
The MathMacroDemo is based on the ImageJ Process>Math>Macro command,
   
https://imagej.nih.gov/ij/docs/guide/146-29.html#toc-Subsubsection-29.9.19

The Java code for it is in ij.plugin.filter.ImageMath
In java or JavaScript, you can also call the ImageMath method
   public static void applyMacro(ImageProcessor ip, String macro, boolean
showProgress)
to apply the macro code to an image.
The code is quite nicely optimized, e.g. it does not calculate values
such as distance from the center (d) and angle (a) if they are not used
by the macro.
Of course, it uses the same Macro Interpreter for all pixels.
A separate call to IJ.runMacro for each pixel would create a new
Interpreter for each pixel (I guess that creating Macro Interpreters is
the main reason why your macro is slow).

Michael
________________________________________________________________




On 2017-12-08 18:47, Herbie wrote:

> Maximilian,
>
> did you have a look at:
>
> <https://imagej.nih.gov/ij/macros/examples/MathMacroDemo.txt>
>
> Regards
>
> Herbie
>
> ::::::::::::::::::::::::::::::::::::::::::::::
> Am 08.12.17 um 16:27 schrieb Maximilian Maske:
>> Hi there,
>>
>> I am trying to build a plugin for ImageJ where the user can input a
>> function as text and the program will parse it and it will output an
>> image. My question is how to parse a string to either Macro code or
>> Java
>> code to process it. My solution right now is quite slow because I
>> transform the function for every pixel into a String and get the
>> result
>> as a string from IJ.macro() which I have to parse to a double again.
>>
>> This is my general idea in Java code:
>>
>> // example input
>> String formula = "sin(x)*sin(x)+sin(y)*sin(y)";
>> int width = 128, height = 128;
>>
>> int[][] image = new int[width][height];
>>
>> for(int y=0; y<height; y++) {for(int x=0; x<width; x++) {
>> String curr_formula = formula.replace("x", "" +   x).replace("y", "" +
>> y);
>>
>>          String val = IJ.runMacro("return '' + " + curr_formula +
>> ";");
>>          double value = Double.parseDouble(val);
>>
>>          image[x][y] = value ;
>>      }
>> }
>>
>> Does somebody have a better idea or knows another framework to parse
>> functions?
>>
>> Thank you for the help,
>> Max
>>
>> PS: If need to compile the code you can use this method to see what
>> actually happens:
>>
>> public void functionToImage(int width, int height, int slices, double
>> min, double max, String formula) {
>>
>>          ImagePlus imagePlus = IJ.createImage("test", "32-Bit", width,
>> height, slices);
>>          ImageProcessor processor = imagePlus.getProcessor();
>>
>>          double range = Math.abs(min - max);
>>
>>          String curr_formula;
>>
>>       for(int y_=0; y_<height; y_++) {
>>
>>              double y = (double) y_ / height; // 0..1 double dy =
>> range * y + min; // min..max for (int x_ = 0; x_ < width; x_++) {
>>
>>                  double x = (double) x_ / width; // 0..1 double dx =
>> range * x + min; //min..max curr_formula = formula.replace("x", "" +  
>> dx).replace("y", "" + dy);
>>
>>                  String val = IJ.runMacro("return '' + " +
>> curr_formula + ";");
>>                  double val_ =
>> Double.parseDouble(val);processor.putPixelValue(x_, y_, val_);
>>              }
>>          }
>>
>>          imagePlus.show();
>> }
>>
>>
>> --
>> 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