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; 3:46pm
URL: http://imagej.273.s1.nabble.com/Calculating-atan2-on-pixels-from-two-input-images-in-a-macro-tp5007758p5009292.html

On Aug 21, 2014, at 4:54 AM, [hidden email] wrote:

> I thought I would give some followup for anyone who might be running into a
> similar problem.
>
> I was able to get the beanshell code from bnorthan working. This code ran
> much faster than for loops in the macro language but still required several
> minutes for a ~12 megapixel image. I was able to find a java plugin
> performing a similar calculation here (
> http://www.physics.umanitoba.ca/~westjl/thesis/chapter0.pdf
> <http://www.physics.umanitoba.ca/~westjl/thesis/chapter0.pdf>  , Appendix 2,
> pg 200). I was able to modify the code to serve my needs. This code runs in
> less than a second. I have posted the modified code below.

The following is a simpler and faster version of this plugin that displays an error message if either the "Q" or "U" image is missing rather than throwing a null pointer exception.

-wayne

import ij.*;
import ij.process.*;
import java.awt.*;
import ij.plugin.*;

/**
* Produces a polarized intensity or 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("a1", newIp).show();
    }

}


>
> import ij.*;
> import ij.process.*;
> import java.awt.*;
> import ij.plugin.filter.*;
>
> /**
> * Produces a polarized intensity or polarization angle image given
> * Stokes Q and Stokes U images as input
> */
>
> public class atan_ implements PlugInFilter {
> ImagePlus imp;
>
> public int setup(String arg, ImagePlus imp) {
> this.imp = imp;
> return DOES_ALL;
> }
>
> public void run(ImageProcessor ip) {
> ImageProcessor ipQ = WindowManager.getImage("Q").getProcessor();
> ImageProcessor ipU = WindowManager.getImage("U").getProcessor();
>
> double ratio =0;
> double value =0;
> int offset, i, x, y;
> int width = ipQ.getWidth();
> int height = ipQ.getHeight();
> ImageProcessor newIp = new FloatProcessor(width, height);
> for (y=0; y<height; y++) {
> for (x=0; x<width; x++) {
>                value = 0.5*Math.atan2
> (ipU.getPixelValue(x,y),
> ipQ.getPixelValue(x,y));
> newIp.putPixelValue(x,y,value);
> }
> }
> String imTitle = "a1";
> ImagePlus newImp = new ImagePlus(imTitle, newIp);
> newImp.show();
> }
>
> }
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Calculating-atan2-on-pixels-from-two-input-images-in-a-macro-tp5007758p5009285.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