Rectangle roi from ImageStack

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

Rectangle roi from ImageStack

Michael Doube
Hi all

I want to get the Rectangle ROI from an ImageStack that I have passed to
a method, as in

public void myMethod(ImageStack stack){
        Rectangle r = stack.getRoi();
        //do something with r
        return;
}

This is fine when there is a rectangle drawn, but if there is not an ROI
drawn, I get a null pointer exception.

Interestingly, if I set r to be a class variable

Public Rectangle r;

Then in the run() method set it with

r = ip.getRoi();

All the 'r's in my other methods behave sensibly and I don't get a null
pointer exception when I haven't drawn an ROI on my stack.

What is going on here?

Mike
Reply | Threaded
Open this post in threaded view
|

Re: Rectangle roi from ImageStack

Michael Schmid
Hi Michael,

a few points about rois, maybe some of this helps you solve your  
problem.

ImageProcessor.getRoi gives the roi bounds (rectangle), also for non-
rectangular rois. You can get the mask for non-rectangular rois.

Unless you are in the run(ip) method of a PlugInFilter, the rois of  
an ImageProcessor do not necessarily reflect the roi of the ImagePlus  
that the the ImageProcessor is part of. This is for avoiding  
unnecessary calculations (calculating the mask takes some time).
See the PlugInFilterRunner in the ImageJ source code on how to  
transfer the roi from an ImagePlus to the roi rectangle/mask of an  
ImageProcessor.

Also note that the roi of an ImagePlus may be null. Then, in the run
(ip) method of a PlugInFilter you will get a roi rectangle enclosing  
the whole image. This greatly facilitates programming plugin filters.

Hope this helps a bit.

Michael
________________________________________________________________

On 18 Mar 2009, at 14:09, Michael Doube wrote:

> Hi all
>
> I want to get the Rectangle ROI from an ImageStack that I have  
> passed to a method, as in
>
> public void myMethod(ImageStack stack){
> Rectangle r = stack.getRoi();
> //do something with r
> return;
> }
>
> This is fine when there is a rectangle drawn, but if there is not  
> an ROI drawn, I get a null pointer exception.
>
> Interestingly, if I set r to be a class variable
>
> Public Rectangle r;
>
> Then in the run() method set it with
>
> r = ip.getRoi();
>
> All the 'r's in my other methods behave sensibly and I don't get a  
> null pointer exception when I haven't drawn an ROI on my stack.
>
> What is going on here?
>
> Mike
Reply | Threaded
Open this post in threaded view
|

Re: Rectangle roi from ImageStack

Michael Doube
Michael,

Thanks for this explanation.  Turns out that the issue that I was having
trouble with was the way IJ handled a null return from
ImageStack.getRoi(); I was expecting that it would be the same as
ImagePlus.getRoi() (i.e. the ROI has the image bounds).  Wayne
identified this as a bug and fixed it in a daily build some time last
week - so now ImageStack.getRoi() returns the image size and (0,0)
origin instead of null when no ROI is drawn.

Cheers

Mike



Michael Schmid wrote:

> Hi Michael,
>
> a few points about rois, maybe some of this helps you solve your  
> problem.
>
> ImageProcessor.getRoi gives the roi bounds (rectangle), also for non-
> rectangular rois. You can get the mask for non-rectangular rois.
>
> Unless you are in the run(ip) method of a PlugInFilter, the rois of  
> an ImageProcessor do not necessarily reflect the roi of the ImagePlus  
> that the the ImageProcessor is part of. This is for avoiding  
> unnecessary calculations (calculating the mask takes some time).
> See the PlugInFilterRunner in the ImageJ source code on how to  
> transfer the roi from an ImagePlus to the roi rectangle/mask of an  
> ImageProcessor.
>
> Also note that the roi of an ImagePlus may be null. Then, in the run
> (ip) method of a PlugInFilter you will get a roi rectangle enclosing  
> the whole image. This greatly facilitates programming plugin filters.
>
> Hope this helps a bit.
>
> Michael
> ________________________________________________________________
>
> On 18 Mar 2009, at 14:09, Michael Doube wrote:
>
>> Hi all
>>
>> I want to get the Rectangle ROI from an ImageStack that I have  
>> passed to a method, as in
>>
>> public void myMethod(ImageStack stack){
>> Rectangle r = stack.getRoi();
>> //do something with r
>> return;
>> }
>>
>> This is fine when there is a rectangle drawn, but if there is not  
>> an ROI drawn, I get a null pointer exception.
>>
>> Interestingly, if I set r to be a class variable
>>
>> Public Rectangle r;
>>
>> Then in the run() method set it with
>>
>> r = ip.getRoi();
>>
>> All the 'r's in my other methods behave sensibly and I don't get a  
>> null pointer exception when I haven't drawn an ROI on my stack.
>>
>> What is going on here?
>>
>> Mike