Re: Parsing formulas/macro into ImageJ

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

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