Can't Recover ImagePlus after Stack Rotate

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Can't Recover ImagePlus after Stack Rotate

kacollins
I am writing a plugin that involves, amoung other things, rotating a stack. I have included the pertinent code below related to my problem. In ImageJ 1.42q, the code preserves the ImagePlus variable "imp" in line four (i.e. it is not null, and it contains the rotated data). In ImageJ 1.43u, the resulting value of imp is null. The only change is moving from 1.42q to 1.43u. I have gone back and forth and the problem only happens in 1.43u. How can I recover the imp related to s2 in version 1.43u?

------------------------

StackProcessor sp = new StackProcessor(imp.getStack(), ip);
ImageStack s2 = null;
s2 = sp.rotateLeft();
imp.setStack(null, s2);

if (imp == null)
     IJ.log("imp = null");

-------------------------


Thanks,
    Karen
Reply | Threaded
Open this post in threaded view
|

Re: Can't Recover ImagePlus after Stack Rotate

dscho
Hi,

On Fri, 13 Aug 2010, kacollins wrote:

> I am writing a plugin that involves, amoung other things, rotating a
> stack. I have included the pertinent code below related to my problem.
> In ImageJ 1.42q, the code preserves the ImagePlus variable "imp" in line
> four (i.e. it is not null, and it contains the rotated data). In ImageJ
> 1.43u, the resulting value of imp is null. The only change is moving
> from 1.42q to 1.43u. I have gone back and forth and the problem only
> happens in 1.43u. How can I recover the imp related to s2 in version
> 1.43u?
>
> ------------------------
>
> StackProcessor sp = new StackProcessor(imp.getStack(), ip);
> ImageStack s2 = null;
> s2 = sp.rotateLeft();
> imp.setStack(null, s2);
>
> if (imp == null)
>      IJ.log("imp = null");
>
> -------------------------

Probably I have put some different glue code around it, but it works here,
with ImageJA 1.44e:

-- snipsnap --
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;

import ij.plugin.PlugIn;

import ij.process.ImageProcessor;
import ij.process.StackProcessor;

public class Rotate_Test implements PlugIn {
        public void run(String arg) {
                ImagePlus imp = IJ.getImage();
                ImageProcessor ip = imp.getProcessor().duplicate();
                StackProcessor sp = new StackProcessor(imp.getStack(), ip);
                ImageStack s2 = null;
                s2 = sp.rotateLeft();
                imp.setStack(null, s2);

                if (imp == null)
                     IJ.log("imp = null");
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: Can't Recover ImagePlus after Stack Rotate

kacollins
Hi Johannes,
    The code you provided worked in a standalone plugin in 1.43u as well. Unfortunately, implementing the differences in my plugin still did not work in 1.43u (but works fine in 1.42q). It may have something to do with the fact that my plugin extends StackWindow, but I'm not sure why it would work in 1.42q.
    In any event, I have found a workaround that is not very elegant, but solves the null imp problem by grabbing the imp from the window after the setStack command. The code that works within my plugin for both 1.42q, and 1.43u (and I assume 1.44e, but haven't tested it yet) is:

--------------------------------------------
StackProcessor sp = new StackProcessor(imp.getStack(), ip);
ImageStack s2 = null;
s2 = sp.rotateLeft();
imp.setStack(null, s2);
imp = IJ.getImage();
--------------------------------------------

imp is now valid after the setStack and works as expected for the remaining part of the plugin (after creating a new instance of my extended StackWindow).

Thanks for the suggested code, it actually triggered the thought of re-grabbing the imp from the window using IJ.getImage().

Karen
Reply | Threaded
Open this post in threaded view
|

Re: Can't Recover ImagePlus after Stack Rotate

dscho
Hi Karen,

if you say "Hi Johannes", please send the mail to me, and Cc: the list.
Please do not address the list with "Hi Johannes". It is my name, not the
list's.

Besides, I am not the only Johannes on the list and if you would not have
answered in such a timely manner, I would have thought that you are
referring to another one and not read the mail.

On Fri, 13 Aug 2010, kacollins wrote:

>     The code you provided worked in a standalone plugin in 1.43u as well.

That is what I thought. I'd need a minimal example that reproduces the
issue to find out the root of the problem.

> Unfortunately, implementing the differences in my plugin still did not
> work in 1.43u (but works fine in 1.42q). It may have something to do
> with the fact that my plugin extends StackWindow, but I'm not sure why
> it would work in 1.42q.
>
>     In any event, I have found a workaround that is not very elegant,
> but solves the null imp problem by grabbing the imp from the window
> after the setStack command. The code that works within my plugin for
> both 1.42q, and 1.43u (and I assume 1.44e, but haven't tested it yet)
> is:
>
> --------------------------------------------
> StackProcessor sp = new StackProcessor(imp.getStack(), ip);
> ImageStack s2 = null;
> s2 = sp.rotateLeft();
> imp.setStack(null, s2);
> imp = IJ.getImage();
> --------------------------------------------

The real problem I have with your code is that there is no code in between
imp.getStack() and imp.setStack() that touches the value of "imp". And
even imp.setStack() cannot change the value of imp, unless you have a
faulty compiler.

Unless "imp" is a class field and either setStack() is really calling a
method of the same class or another method is run at the same time which
sets the field to null.

All of this is guess work, which is very tedious and time consuming,
because I can be correct in at most one hypothesis. It would be very easy
to see which one once I could reproduce your problem. Guessing (and being
incorrect all the time) is no fun.

Therefore it is crucial to provide the people you ask for help with enough
information (and in this case it is either your complete code or a
stripped down minimal example that shows the problem, not so much stripped
down that the problem does not occur anymore) so they can and want to
help you.

The easier and more fun you make it for others to help you, the more
likely you will receive the assistance you need.

> Thanks for the suggested code, it actually triggered the thought of
> re-grabbing the imp from the window using IJ.getImage().

I am sorry to convey this to you: IJ.getImage() is most likely wrong,
since it requires the user not to touch the keyboard and the mouse at all,
lest another image come to the foreground and be mistaken by your code as
the intended "imp". Your best bet if you want to have a reliable plugin in
the end is to find the cause of the issue.

Hth,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: Can't Recover ImagePlus after Stack Rotate

kacollins
Hi Wayne,
      The plugin under development is now working perfectly for both
Images and Stacks with the 1.44f9 daily build. Thank you for the fix!
Much appreciation!

Cheers,
       Karen

On 8/15/2010 8:26 PM, Rasband, Wayne (NIH/NIMH) [E] wrote:

> On Aug 15, 2010, at 3:29 AM, Karen Collins wrote:
>
>    
>> Hi Wayne,
>>       With 1.44f8, both my TestPlugin and full Astronomy_Tool plugin end
>> up with imp = null after the stackProcessor.rotateLeft() statement as
>> they did with 1.43u. However, Panel_Stack_Window does work with 1.44f8,
>> and I see that it does not work with 1.43u (i.e. logs null for the imp)
>> as expected, given the fix.
>>       My TestPlugin is below. If you don't see a problem with the code,
>> would you be able to test it on your end to check if you see the "imp =
>> null" result as I do in 1.44f8? If I pass imp to buildAstroWindow as a
>> parameter, it works properly.
>>      
> Hi Karen,
>
> Your code is fine. The problem was with ImageJ and it is fixed in the 1.44f9 daily build. SetStack() no longer triggers the construction of another ImageWindow or StackWindow when switching between one image and two image stacks. A StackWindow can now display a single image with no scrollbar.
>
> Best regards,
>
> -wayne
>
>    
>> Best regards,
>>               Karen
>>
>> -----------------------------------------------------------------------
>>
>> import ij.IJ;
>> import ij.*;
>> import ij.ImagePlus;
>> import ij.ImageStack;
>> import ij.gui.ImageCanvas;
>> import ij.gui.StackWindow;
>> import ij.plugin.PlugIn;
>> import ij.process.ImageProcessor;
>> import ij.process.StackProcessor;
>>
>> import java.awt.*;
>> import java.awt.Graphics.*;
>>
>>
>>
>> public class TestPlugin implements PlugIn {
>>          public void run(String arg) {
>>                  ImagePlus imp = IJ.getImage();
>>                  ImageCanvas ic = imp.getCanvas();
>>                  AstroStackWindow astroWindow = new
>> AstroStackWindow(imp, ic);
>>          }
>>
>> class AstroStackWindow extends StackWindow {
>>
>>          AstroStackWindow(ImagePlus imp, ImageCanvas ic) {
>>
>>                  super(imp, ic);
>>
>>                  buildAstroWindow();
>>          }
>>
>>                  void buildAstroWindow() {
>>
>>                      ImageProcessor ip = imp.getProcessor();
>>                      StackProcessor sp = new
>> StackProcessor(imp.getStack(), ip);
>>                      ImageStack s2 = null;
>>                      s2 = sp.rotateLeft();
>>                      imp.setStack(null, s2);
>>
>>                      if (imp == null)
>>                           IJ.log("imp = null");
>>                      else
>>                           IJ.log("imp = something");
>>                      }
>>          }
>>
>> }
>>
>> On 8/15/2010 1:37 AM, Rasband, Wayne (NIH/NIMH) [E] wrote:
>>      
>>> Hi Karen,
>>>
>>> The ImageJ 1.44f8 daily build fixes a bug that caused ImageJ to unnecessarily create a replacement StackWindow when a setStack() call changed both the image size and the stack size. This caused the 'imp' instance variable in the original StackWindow to be set to null. I attached the plugin I used for testing.
>>>
>>> Best regards,
>>>
>>> -wayne
>>>
>>>        
>
>    
Reply | Threaded
Open this post in threaded view
|

Re: Can't Recover ImagePlus after Stack Rotate

dscho
Hi,

On Sun, 15 Aug 2010, Karen Collins wrote:

>      The plugin under development is now working perfectly for both
> Images and Stacks with the 1.44f9 daily build. Thank you for the fix!
> Much appreciation!

It is great that it is working again, because there might be other code
which relied on imp not being changed.

Having said that, I still suggest to write programs in such a way that the
member field of a superclass is not used, unless you have to (I would call
this "defensive programming").

Ciao,
Dscho