How ImageJ process non-rectangular ROIs?

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

How ImageJ process non-rectangular ROIs?

panovr
Hi,
  a simple inverter plugin which can deal with ROIs may like these:

===========================================================================
public class Simple_Inverter implements PlugInFilter {
        ImagePlus imp;

        public int setup(String arg, ImagePlus imp) {
                if (arg.equals("about")) {
                        showAbout();
                        return DONE;
                }
                this.imp = imp;
                return DOES_8G + DOES_STACKS + SUPPORTS_MASKING;
        }

        public void run(ImageProcessor ip) {
                byte[] pixels = (byte[])ip.getPixels();
                int width = ip.getWidth();
                Rectangle r = ip.getRoi();
                int offset, i;
               
                for (int y = r.y; y < (r.y + r.height); y++) {
                        offset = y * width;
                        for (int x = r.x; x < (r.x + r.width); x++) {
                                i = offset + x;
                                pixels[i] = (byte)(255 - pixels[i]);
                        }
                }
        }
    }
}
===========================================================================
 
  When I use "Elliptical" tool to select a non-rectangular ROI in the
image, then invoke this plugin, and ImageJ can process the non-rectangular
ROI correctly (the elliptical region was inverted).

  My question is: in my program, I use "Rectangle" to represent general
ROIs including rectangular and non-rectangular. But how ImageJ can process
non-rectangular ROI by "Rectangle"?

  Thanks!

Yili Zhao
Reply | Threaded
Open this post in threaded view
|

Re: How ImageJ process non-rectangular ROIs?

Wayne Rasband
Pixels outside the selection and inside the bounding rectangle are  
restored when a PlugInFilter returns the SUPPORTS_MASKING flag.  
Processing can also be limited to a non-rectangular area by masking.  
Here is an example:

public class Masking_Example implements PlugInFilter {
    public int setup(String arg, ImagePlus imp) {
       return DOES_8G;
    }
    public void run(ImageProcessor ip) {
       byte[] pixels = (byte[])ip.getPixels();
       int width = ip.getWidth();
       Rectangle r = ip.getRoi();
       int offset, i, j=0;
       byte[] mask = ip.getMaskArray();
       for (int y = r.y; y < (r.y + r.height); y++) {
          offset = y * width;
          for (int x = r.x; x < (r.x + r.width); x++) {
             i = offset + x;
             if (mask==null || mask[j++]!=0)
                pixels[i] = (byte)(255 - pixels[i]);
             }
          }
       }
}

-wayne

On Sep 5, 2006, at 7:50 AM, Yili Zhao wrote:

> Hi,
>   a simple inverter plugin which can deal with ROIs may like these:
>
> =======================================================================
> ====
> public class Simple_Inverter implements PlugInFilter {
> ImagePlus imp;
>
> public int setup(String arg, ImagePlus imp) {
> if (arg.equals("about")) {
> showAbout();
> return DONE;
> }
> this.imp = imp;
> return DOES_8G + DOES_STACKS + SUPPORTS_MASKING;
> }
>
> public void run(ImageProcessor ip) {
> byte[] pixels = (byte[])ip.getPixels();
> int width = ip.getWidth();
> Rectangle r = ip.getRoi();
> int offset, i;
>
> for (int y = r.y; y < (r.y + r.height); y++) {
> offset = y * width;
> for (int x = r.x; x < (r.x + r.width); x++) {
> i = offset + x;
> pixels[i] = (byte)(255 - pixels[i]);
> }
> }
> }
>     }
> }
> =======================================================================
> ====
>
>   When I use "Elliptical" tool to select a non-rectangular ROI in the
> image, then invoke this plugin, and ImageJ can process the  
> non-rectangular
> ROI correctly (the elliptical region was inverted).
>
>   My question is: in my program, I use "Rectangle" to represent general
> ROIs including rectangular and non-rectangular. But how ImageJ can  
> process
> non-rectangular ROI by "Rectangle"?
>
>   Thanks!
>
> Yili Zhao
>
Reply | Threaded
Open this post in threaded view
|

Re: How ImageJ process non-rectangular ROIs?

panovr
In reply to this post by panovr
Hi wayne,
  does ImageJ treate non-rectangular ROI like these (a guess):
  If a PlugInFilter returns the SUPPORTS_MASKING flag, then one
selected a non-rectangular ROI in the image. When ImageJ process the
pixels in the ROI, it will test the mask for the ROI: if the mask for
the pixel is zero, then ImageJ will not process this pixel, else
ImageJ will do the process of the plugin.
  By the way, how does ImageJ create the mask for the ROI (rectangular
and non-rectangular ROIs)? Does ImageJ first calculate the bounding
rectangular for the non-rectangular ROI and then use some algorithems
to create the mask for the bounding rectangular? And, if the pixel is
in the bounding rectangular and not in the ROI, then the mask will be
zero (invalid)? If the pixel is in the non-rectangular ROI, then the
mask will be non-zero (valid).

Best regards,
Yili Zhao