Hi,
I'm new to imageJ. I have played with PlugInFilter these past days, solved the usual PATH problems and managed to do simple tricks. But there is one elementary thing that I can't get going, it's calling a built-in function in a PlugInFilter. From the many docs I have read, the following minimal example should work, I think: import ij.*; import ij.process.*; import ij.plugin.filter.PlugInFilter; import java.awt.*; public class callPlugin_PIF implements PlugInFilter { public int setup(String arg, ImagePlus imp) { return DOES_ALL; } public void run(ImageProcessor ip) { IJ.run("Mean...", "radius=2"); } } When I open imageJ on a sample image, then call Plugins->callPlugin PIF, nothing happens, the image remains the same. Whereas if I use Process->Filters->Mean..., the effect is visible. I tried adding IJ.getImage().updateAndDraw(); after IJ.run, but it didn't change the outcome. Obviously I'm missing something, possibly something self-evident that would be intuitive if I had more experience working with imageJ. Could someone point me to the right direction? Best regards, Sébastien Desreux. |
Hi Seb,
On Fri, 30 Sep 2011, Seb wrote: > public class callPlugin_PIF implements PlugInFilter { > > public int setup(String arg, ImagePlus imp) { > return DOES_ALL; > } Here you get already the current image. So you should add a field, say protected ImagePlus image; and then store it in setup(): this.image = imp; > public void run(ImageProcessor ip) { > IJ.run("Mean...", "radius=2"); > } Here you should use the "image" field: IJ.run(image, "Mean...", "radius=2"); This is just so much safer because then you definitely run on the correct image, even if you updateAndDraw() when the user switched to another window. Having said that, the real problem is that the plugin filter will "lock" the image. Unfortunately, since IJ.run() will not throw an exception, it will only update the status to say that the image is locked. Further, ImageJ will then update the status right away to tell you that your plugin finished, so you cannot see the "locked" message. You could actually see it if you insert an "IJ.wait(500);" at the end of the run() method. The trick is to insert an "image.unlock();" at the beginning of the run() method (and now it is _really_ important that you do not just expect IJ.getImage() to magically choose the right image). So this is the version that works for me: -- snipsnap -- import ij.IJ; import ij.ImagePlus; import ij.plugin.filter.PlugInFilter; import ij.process.ImageProcessor; public class Seb implements PlugInFilter { protected ImagePlus image; public int setup(String arg, ImagePlus imp) { image = imp; return DOES_ALL; } public void run(ImageProcessor ip) { IJ.run(image, "Mean...", "radius=20"); } } |
In reply to this post by Sébastien Desreux
On Sep 30, 2011, at 11:18 AM, Seb wrote:
> Hi, > > I'm new to imageJ. I have played with PlugInFilter these past days, solved > the usual PATH problems and managed to do simple tricks. > > But there is one elementary thing that I can't get going, it's calling a > built-in function in a PlugInFilter. From the many docs I have read, the > following minimal example should work, I think: > > import ij.*; > import ij.process.*; > import ij.plugin.filter.PlugInFilter; > import java.awt.*; > > public class callPlugin_PIF implements PlugInFilter { > > public int setup(String arg, ImagePlus imp) { > return DOES_ALL; > } > > public void run(ImageProcessor ip) { > IJ.run("Mean...", "radius=2"); > } > } > > When I open imageJ on a sample image, then call Plugins->callPlugin PIF, > nothing happens, the image remains the same. Whereas if I use > Process->Filters->Mean..., the effect is visible. > > I tried adding > IJ.getImage().updateAndDraw(); > after IJ.run, but it didn't change the outcome. > > Obviously I'm missing something, possibly something self-evident that > would be intuitive if I had more experience working with imageJ. Could > someone point me to the right direction? Make the plugin a PlugIn instead of a PlugInFilter and it will work. You should only use the PlugInFilter interface when you are working with ImageProcessors and are not making IJ.run() calls. import ij.*; import ij.process.*; import ij.plugin.PlugIn; public class Plugin_PIF implements PlugIn { public void run(String arg) { ImagePlus imp = IJ.getImage(); IJ.run(imp, "Mean...", "radius=5"); } } -wayne |
Hello,
Thanks Johannes for the careful explanation of what was missing from my code. You made it work in a PlugInFilter and now I understand. I will follow Wayne's advice and use a PlugIn whenever IJ.run is called. Best regards, Sébastien. |
Free forum by Nabble | Edit this page |