Re: Parsing formulas/macro into ImageJ

Posted by ctrueden on
URL: http://imagej.273.s1.nabble.com/Re-Parsing-formulas-macro-into-ImageJ-tp5019747.html

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