Login  Register

Re: Calculating atan2 on pixels from two input images in a macro

Posted by Rasband, Wayne (NIH/NIMH) [E] on Aug 21, 2014; 9:22pm
URL: http://imagej.273.s1.nabble.com/Calculating-atan2-on-pixels-from-two-input-images-in-a-macro-tp5007758p5009296.html

On Aug 21, 2014, at 3:21 PM, [hidden email] wrote:
> Hi Wayne,
>
> Thank you for your helpful input. I've worked your suggested changes into
> the plugin. I didn't notice an increase in speed with this size of image but
> the result was appearing so quickly before I don't think I would be able to
> discern any differences.

You probably will not see much increase in speed because the plugin spends most of its time calculating atan2. You could pre-calculate the atan2 values but that would only be feasible for 8-bit images, and would require a 64K (256x256) table.

> I seem to get some odd results when I try and test the error message part of
> the plugin. If the Q or U image is not encountered I get the following error
> dialog pop up (for reference U1 was the title of the active image):
>
> *Title: Unlock image?
> The image 'U1'appears to be locked... Unlock?
> Cancel OK*

You are getting this error because the previous version of the plugin left one of the images in a locked state. You can work around this error by closing all images and re-open the two input images. The previous version of the plugin implemented the PlugInFilter interface, which, by default, locks the current image. The new version implements the PlugIn interface, which does not automatically lock images.

You need to also make sure you are actually running the new version of the plugin. The stack trace below shows that ImageJ's PlugInFilterRunner was called, which would not be the case if you were running the new version of the plugin, which is not a PlugInFilter.

-wayne

> Pressing OK would result in this null pointer exception:
>
> *java.lang.NullPointerException
> at atan_.run(atan_.java:20)
> at
> ij.plugin.filter.PlugInFilterRunner.processOneImage(PlugInFilterRunner.java:262)
> at ij.plugin.filter.PlugInFilterRunner.<init>(PlugInFilterRunner.java:111)
> at net.imagej.legacy.IJ1Helper.run(IJ1Helper.java:544)
> at net.imagej.legacy.LegacyJavaRunner.run(LegacyJavaRunner.java:54)
> at
> org.scijava.plugins.scripting.java.DefaultJavaService.run(DefaultJavaService.java:61)
> at org.scijava.plugins.scripting.java.JavaEngine.eval(JavaEngine.java:178)
> at org.scijava.script.ScriptModule.run(ScriptModule.java:175)
> at org.scijava.module.ModuleRunner.run(ModuleRunner.java:167)
> at org.scijava.module.ModuleRunner.call(ModuleRunner.java:126)
> at org.scijava.module.ModuleRunner.call(ModuleRunner.java:65)
> at
> org.scijava.thread.DefaultThreadService$2.call(DefaultThreadService.java:164)
> at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
> at java.util.concurrent.FutureTask.run(FutureTask.java:138)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
> at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
> at java.lang.Thread.run(Thread.java:695)*
>
> I have included the exact code I am using below. I believe the only change
> is to the public class on line 11. I have made it lower case to match the
> filename I was using. Thank you again for your input.
>
> *import ij.*;
> import ij.process.*;
> import java.awt.*;
> import ij.plugin.*;
>
> /**
> * Produces a polarization angle image given
> * Stokes Q and Stokes U images as input
> */
>
> public class atan_ implements PlugIn {  
>
>    public void run(String info) {
>        ImagePlus Q = WindowManager.getImage("Q");
>        ImagePlus U = WindowManager.getImage("U");
>        if (Q==null || U==null) {
>            IJ.error("ATAN","'Q' or 'U' image not found");
>            return;
>        }
>        int width = Q.getWidth();
>        int height = Q.getHeight();
>        ImageProcessor ipQ = Q.getProcessor();
>        ImageProcessor ipU = U.getProcessor();
>        ImageProcessor newIp = new FloatProcessor(width, height);
>        for (int i=0; i<width*height; i++) {
>            double value = 0.5*Math.atan2(ipU.getf(i),ipQ.getf(i));
>            newIp.setf(i,(float)value);
>        }
> new ImagePlus(&quot;a1&quot;, newIp).show();
> }
>
> }&lt;/b>
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Calculating-atan2-on-pixels-from-two-input-images-in-a-macro-tp5007758p5009295.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html