Re: Parsing formulas/macro into ImageJ

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

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