Hello all,
My Question: Is there a way of creating a new image (in a reasonable amount of time) from the atan2 value calculated from the pixel values of two input images, in an imageJ macro? Can this be done within a macro or will this require a script or plugin? Background on what I'm doing and what I've attempted: I have been writing a macro to calculate intensity (I), degree of linear polarization (p), and angle of polarization (Ψ) from a set of four photographs taken with a polarizing filter at different angles (0, 45, 90, 135) ( addition info on polarimetry measurements). I have created the macro work on multiple sets of such images. I am able to calculate I & p without to much trouble using the image calculator and the process>math functions. I have hit a roadblock with calculating Ψ though. The formula to calculate Ψ is: Ψ = 0.5*atan(U/Q) U & Q are stokes parameters calculated from intensity values from the images (see link above) Unfortunately atan have different values depending on the sign of the two inputs, so I cannot simply divide U by Q and take the atan of the value like this: imageCalculator("Divide create 32-bit", "U","Q"); selectWindow("Result of U"); run("Macro...", "code=v=0.5*atan(v)"); The atan2 function takes the different values into account but requires two inputs and must be iterated over each pixel. I've tried the code below but it is incredibly slow. selectImage(U); w = getWidth(); h = getHeight(); newImage("Ψ", "32-bit", w, h, 1) for (y=0; y<h; y++) { for (x=0; x<w; x++) { selectImage(U) Upixel=getPixel(x, y); selectImage(Q) Qpixel=getPixel(x, y); setPixel(x, y, 0.5*atan2(Upixel, Qpixel)) } } updateDisplay(); Fiji has the Image Expression Parser which does exactly what I want (0.5*atan2(A,B)), but at least I haven't figured out a way to pass arguments to the Parser from a macro. I know this is possible to calculate within a plugin, but I have yet to find one that I can incorporate into my macro. Any idea how to calculate this in my macro would be appreciated. Would this particular task be better suited to a script or plugin? I can post my code thus far if it would be helpful. Thanks, Adam Blake |
Hi Adam,
There is likely a more elegant solution to your question, but until it's posted a way to speed up your macro is to put the pixels values for each image into arrays and then index them based on the image dimensions. It's one of the work arounds suggested for the macro language limitation to 1D arrays. Using the arrays avoids switching between the three images. It will still be limited in speed compared to a plugin and may require a lot of memory for the arrays (dependent on your image size). But it will be faster than switching between the images and it might get you by until a better solution is posted. I've pasted an edited version of your code below. Best, George selectImage(U); w = getWidth(); h = getHeight(); UArray=newArray(w*h); QArray=newArray(w*h); selectImage(U); for (y=0; y<h; y++) { for (x=0; x<w; x++) { UArray[x+(h*y)]=getPixel(x, y); } } selectImage(Q); for (y=0; y<h; y++) { for (x=0; x<w; x++) { QArray[x+(h*y)]=getPixel(x, y); } } newImage("Ψ", "32-bit", w, h, 1); for (y=0; y<h; y++) { for (x=0; x<w; x++) { setPixel(x, y, 0.5*atan2(UArray[x+(h*y)],QArray[x+(h*y)])); } } updateDisplay(); On Thu, May 15, 2014 at 2:46 PM, [hidden email] <[hidden email]>wrote: > Hello all, > > *My Question:* > Is there a way of creating a new image (in a reasonable amount of time) > from > the atan2 value calculated from the pixel values of two input images, in an > imageJ macro? Can this be done within a macro or will this require a script > or plugin? > > *Background on what I'm doing and what I've attempted:* > I have been writing a macro to calculate intensity (/I/), degree of linear > polarization (/p/), and angle of polarization (Ψ) from a set of four > photographs taken with a polarizing filter at different angles (0, 45, 90, > 135) ( addition info on polarimetry measurements > < > https://casper.berkeley.edu/astrobaki/index.php/Polarization#Stokes_Q__U:_linear_polarization > > > ). I have created the macro work on multiple sets of such images. > > I am able to calculate /I/ & /p/ without to much trouble using the image > calculator and the process>math functions. I have hit a roadblock with > calculating Ψ though. The formula to calculate Ψ is: > > Ψ = 0.5*atan(/U///Q/) > > U & Q are stokes parameters calculated from intensity values from the > images (see link above) > > Unfortunately atan have different values depending on the sign of the two > inputs, so I cannot simply divide U by Q and take the atan of the value > like > this: > > imageCalculator("Divide create 32-bit", "U","Q"); > selectWindow("Result of U"); > run("Macro...", "code=v=0.5*atan(v)"); > > The atan2 function takes the different values into account but requires two > inputs and must be iterated over each pixel. I've tried the code below but > it is incredibly slow. > > selectImage(U); > w = getWidth(); > h = getHeight(); > newImage("Ψ", "32-bit", w, h, 1) > for (y=0; y<h; y++) { > for (x=0; x<w; x++) { > selectImage(U) > Upixel=getPixel(x, y); > selectImage(Q) > Qpixel=getPixel(x, y); > setPixel(x, y, 0.5*atan2(Upixel, Qpixel)) > } > } > updateDisplay(); > > Fiji has the Image Expression Parser which does exactly what I want > (0.5*atan2(A,B)), but at least I haven't figured out a way to pass > arguments > to the Parser from a macro. > > I know this is possible to calculate within a plugin, but I have yet to > find > one that I can incorporate into my macro. Any idea how to calculate this in > my macro would be appreciated. Would this particular task be better suited > to a script or plugin? I can post my code thus far if it would be helpful. > > Thanks, > Adam Blake > > > > > > > > > -- > View this message in context: > http://imagej.1557.x6.nabble.com/Calculating-atan2-on-pixels-from-two-input-images-in-a-macro-tp5007758.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 |
Hi Adam
You could go to one of the scripting languages, jython, jruby, beanshell... depends what you are comfortable with. I find most of the work is in googling the API functions anyway, figuring out how to get access to the images and buffers you need. Something like the below beanshell may work (obviously where it says "clown.jpg" you replace with the name of your images). ip1=WindowManager.getImage("clown.jpg (red)").getProcessor(); ip2=WindowManager.getImage("clown.jpg (green)").getProcessor(); width = ip1.getWidth(); height = ip1.getHeight(); out = NewImage.createFloatImage("out", width, height, 1, 0); out_ip=out.getProcessor(); ip1Buffer=ip1.getPixels(); ip2Buffer=ip2.getPixels(); outBuffer=out_ip.getPixels(); for (x=0;x<width;x++) { for (y =0;y<height;y++) { p1=ip1Buffer[x+y*width]; p2=ip2Buffer[x+y*width]; value=java.lang.Math.atan2((double)p1, (double)p2); outBuffer[x+y*width]=(float)value; } } out.show(); On Thu, May 15, 2014 at 4:09 PM, George Patterson <[hidden email]>wrote: > Hi Adam, > There is likely a more elegant solution to your question, but until it's > posted a way to speed up your macro is to put the pixels values for each > image into arrays and then index them based on the image dimensions. It's > one of the work arounds suggested for the macro language limitation to 1D > arrays. > Using the arrays avoids switching between the three images. It will still > be limited in speed compared to a plugin and may require a lot of memory > for the arrays (dependent on your image size). But it will be faster than > switching between the images and it might get you by until a better > solution is posted. > I've pasted an edited version of your code below. > Best, > George > > > selectImage(U); > w = getWidth(); > h = getHeight(); > > UArray=newArray(w*h); > QArray=newArray(w*h); > > selectImage(U); > for (y=0; y<h; y++) { > for (x=0; x<w; x++) { > UArray[x+(h*y)]=getPixel(x, y); > } > } > > selectImage(Q); > for (y=0; y<h; y++) { > for (x=0; x<w; x++) { > QArray[x+(h*y)]=getPixel(x, y); > } > } > > newImage("Ψ", "32-bit", w, h, 1); > for (y=0; y<h; y++) { > for (x=0; x<w; x++) { > setPixel(x, y, 0.5*atan2(UArray[x+(h*y)],QArray[x+(h*y)])); > } > } > updateDisplay(); > > > On Thu, May 15, 2014 at 2:46 PM, [hidden email] <[hidden email] > >wrote: > > > Hello all, > > > > *My Question:* > > Is there a way of creating a new image (in a reasonable amount of time) > > from > > the atan2 value calculated from the pixel values of two input images, in > an > > imageJ macro? Can this be done within a macro or will this require a > script > > or plugin? > > > > *Background on what I'm doing and what I've attempted:* > > I have been writing a macro to calculate intensity (/I/), degree of > linear > > polarization (/p/), and angle of polarization (Ψ) from a set of four > > photographs taken with a polarizing filter at different angles (0, 45, > 90, > > 135) ( addition info on polarimetry measurements > > < > > > https://casper.berkeley.edu/astrobaki/index.php/Polarization#Stokes_Q__U:_linear_polarization > > > > > ). I have created the macro work on multiple sets of such images. > > > > I am able to calculate /I/ & /p/ without to much trouble using the image > > calculator and the process>math functions. I have hit a roadblock with > > calculating Ψ though. The formula to calculate Ψ is: > > > > Ψ = 0.5*atan(/U///Q/) > > > > U & Q are stokes parameters calculated from intensity values from the > > images (see link above) > > > > Unfortunately atan have different values depending on the sign of the two > > inputs, so I cannot simply divide U by Q and take the atan of the value > > like > > this: > > > > imageCalculator("Divide create 32-bit", "U","Q"); > > selectWindow("Result of U"); > > run("Macro...", "code=v=0.5*atan(v)"); > > > > The atan2 function takes the different values into account but requires > two > > inputs and must be iterated over each pixel. I've tried the code below > but > > it is incredibly slow. > > > > selectImage(U); > > w = getWidth(); > > h = getHeight(); > > newImage("Ψ", "32-bit", w, h, 1) > > for (y=0; y<h; y++) { > > for (x=0; x<w; x++) { > > selectImage(U) > > Upixel=getPixel(x, y); > > selectImage(Q) > > Qpixel=getPixel(x, y); > > setPixel(x, y, 0.5*atan2(Upixel, Qpixel)) > > } > > } > > updateDisplay(); > > > > Fiji has the Image Expression Parser which does exactly what I want > > (0.5*atan2(A,B)), but at least I haven't figured out a way to pass > > arguments > > to the Parser from a macro. > > > > I know this is possible to calculate within a plugin, but I have yet to > > find > > one that I can incorporate into my macro. Any idea how to calculate this > in > > my macro would be appreciated. Would this particular task be better > suited > > to a script or plugin? I can post my code thus far if it would be > helpful. > > > > Thanks, > > Adam Blake > > > > > > > > > > > > > > > > > > -- > > View this message in context: > > > http://imagej.1557.x6.nabble.com/Calculating-atan2-on-pixels-from-two-input-images-in-a-macro-tp5007758.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 > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
On 5/15/2014 1:21 PM, Brian Northan wrote:
> Hi Adam > > You could go to one of the scripting languages, jython, jruby, beanshell... > depends what you are comfortable with. > > I find most of the work is in googling the API functions anyway, figuring > out how to get access to the images and buffers you need. Something like > the below beanshell may work (obviously where it says "clown.jpg" you > replace with the name of your images). > > ip1=WindowManager.getImage("clown.jpg (red)").getProcessor(); > ip2=WindowManager.getImage("clown.jpg (green)").getProcessor(); > > width = ip1.getWidth(); > height = ip1.getHeight(); > > out = NewImage.createFloatImage("out", width, height, 1, 0); > out_ip=out.getProcessor(); > > ip1Buffer=ip1.getPixels(); > ip2Buffer=ip2.getPixels(); > outBuffer=out_ip.getPixels(); > > for (x=0;x<width;x++) > { > for (y =0;y<height;y++) > { > p1=ip1Buffer[x+y*width]; > p2=ip2Buffer[x+y*width]; > > value=java.lang.Math.atan2((double)p1, (double)p2); > > outBuffer[x+y*width]=(float)value; > } > } In writing some Java code for Micro-manager, I found a 5-10x speed up by replacing a nested loop like this one by a single loop, where you just loop over the whole 1D array: int length = oldPixels.length; for (int index = 0; index < length; index++){ newPixels[index] = (byte) ( (float) oldPixels[index] * flatFieldImage_[index]); } Code from https://valelab.ucsf.edu/svn/micromanager2/trunk/plugins/MultiChannelShading/src/org/micromanager/multichannelshading/BFProcessor.java Kurt > > out.show(); > > > On Thu, May 15, 2014 at 4:09 PM, George Patterson <[hidden email]>wrote: > >> Hi Adam, >> There is likely a more elegant solution to your question, but until it's >> posted a way to speed up your macro is to put the pixels values for each >> image into arrays and then index them based on the image dimensions. It's >> one of the work arounds suggested for the macro language limitation to 1D >> arrays. >> Using the arrays avoids switching between the three images. It will still >> be limited in speed compared to a plugin and may require a lot of memory >> for the arrays (dependent on your image size). But it will be faster than >> switching between the images and it might get you by until a better >> solution is posted. >> I've pasted an edited version of your code below. >> Best, >> George >> >> >> selectImage(U); >> w = getWidth(); >> h = getHeight(); >> >> UArray=newArray(w*h); >> QArray=newArray(w*h); >> >> selectImage(U); >> for (y=0; y<h; y++) { >> for (x=0; x<w; x++) { >> UArray[x+(h*y)]=getPixel(x, y); >> } >> } >> >> selectImage(Q); >> for (y=0; y<h; y++) { >> for (x=0; x<w; x++) { >> QArray[x+(h*y)]=getPixel(x, y); >> } >> } >> >> newImage("Ψ", "32-bit", w, h, 1); >> for (y=0; y<h; y++) { >> for (x=0; x<w; x++) { >> setPixel(x, y, 0.5*atan2(UArray[x+(h*y)],QArray[x+(h*y)])); >> } >> } >> updateDisplay(); >> >> >> On Thu, May 15, 2014 at 2:46 PM, [hidden email] <[hidden email] >>> wrote: >>> Hello all, >>> >>> *My Question:* >>> Is there a way of creating a new image (in a reasonable amount of time) >>> from >>> the atan2 value calculated from the pixel values of two input images, in >> an >>> imageJ macro? Can this be done within a macro or will this require a >> script >>> or plugin? >>> >>> *Background on what I'm doing and what I've attempted:* >>> I have been writing a macro to calculate intensity (/I/), degree of >> linear >>> polarization (/p/), and angle of polarization (Ψ) from a set of four >>> photographs taken with a polarizing filter at different angles (0, 45, >> 90, >>> 135) ( addition info on polarimetry measurements >>> < >>> >> https://casper.berkeley.edu/astrobaki/index.php/Polarization#Stokes_Q__U:_linear_polarization >>> ). I have created the macro work on multiple sets of such images. >>> >>> I am able to calculate /I/ & /p/ without to much trouble using the image >>> calculator and the process>math functions. I have hit a roadblock with >>> calculating Ψ though. The formula to calculate Ψ is: >>> >>> Ψ = 0.5*atan(/U///Q/) >>> >>> U & Q are stokes parameters calculated from intensity values from the >>> images (see link above) >>> >>> Unfortunately atan have different values depending on the sign of the two >>> inputs, so I cannot simply divide U by Q and take the atan of the value >>> like >>> this: >>> >>> imageCalculator("Divide create 32-bit", "U","Q"); >>> selectWindow("Result of U"); >>> run("Macro...", "code=v=0.5*atan(v)"); >>> >>> The atan2 function takes the different values into account but requires >> two >>> inputs and must be iterated over each pixel. I've tried the code below >> but >>> it is incredibly slow. >>> >>> selectImage(U); >>> w = getWidth(); >>> h = getHeight(); >>> newImage("Ψ", "32-bit", w, h, 1) >>> for (y=0; y<h; y++) { >>> for (x=0; x<w; x++) { >>> selectImage(U) >>> Upixel=getPixel(x, y); >>> selectImage(Q) >>> Qpixel=getPixel(x, y); >>> setPixel(x, y, 0.5*atan2(Upixel, Qpixel)) >>> } >>> } >>> updateDisplay(); >>> >>> Fiji has the Image Expression Parser which does exactly what I want >>> (0.5*atan2(A,B)), but at least I haven't figured out a way to pass >>> arguments >>> to the Parser from a macro. >>> >>> I know this is possible to calculate within a plugin, but I have yet to >>> find >>> one that I can incorporate into my macro. Any idea how to calculate this >> in >>> my macro would be appreciated. Would this particular task be better >> suited >>> to a script or plugin? I can post my code thus far if it would be >> helpful. >>> Thanks, >>> Adam Blake >>> >>> >>> >>> >>> >>> >>> >>> >>> -- >>> View this message in context: >>> >> http://imagej.1557.x6.nabble.com/Calculating-atan2-on-pixels-from-two-input-images-in-a-macro-tp5007758.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 >> > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > > -- Kurt Thorn Director, Nikon Imaging Center http://nic.ucsf.edu/blog/ -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by bnorthan
I did look into creating arrays from the images and that method is still very slow.
I think I'll try to create a jython script to perform the atan2 calculation, as I've done some python programing. Would I be able to call this script from my existing macro or would I need to convert the entire macro to a jython script? Thanks, Adam |
Hi Adam
You can try runMacro("nameofmacro"). If your script is in the fiji macros directory it should find it without the directory name... if it is somewhere else you have to include the directory name. On Fri, May 16, 2014 at 7:15 PM, [hidden email] <[hidden email]>wrote: > I did look into creating arrays from the images and that method is still > very > slow. > > I think I'll try to create a jython script to perform the atan2 > calculation, > as I've done some python programing. Would I be able to call this script > from my existing macro or would I need to convert the entire macro to a > jython script? > > Thanks, > Adam > > > > -- > View this message in context: > http://imagej.1557.x6.nabble.com/Calculating-atan2-on-pixels-from-two-input-images-in-a-macro-tp5007758p5007772.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 |
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, 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. 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(); } } |
On Thursday 21 Aug 2014 01:54:24 [hidden email] wrote:
> 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); > } > } If you are looking for speeding it up further, it is probably quicker if you processed the images as a unidimensional arrays. Cheers Gabriel -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by adam@ajblake.info
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 |
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. 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 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("a1", newIp).show(); } }</b> |
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("a1", newIp).show(); > } > > }</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 |
Restarting imageJ seems to have resolved the issue.
Thanks again! |
Free forum by Nabble | Edit this page |