Re: How ImageJ process non-rectangular ROIs?

Posted by Wayne Rasband on
URL: http://imagej.273.s1.nabble.com/How-ImageJ-process-non-rectangular-ROIs-tp3701627p3701629.html

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
>