irregular ROIs

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

irregular ROIs

Tony Shepherd
can anyone tell me how to loop over an irregular (polygonal) ROI ?

I tried taking a control loop from the tutorial called Inverter_ (pasted below)

At the moment it only seems to loop over the bounding rectangle.

Thanks,

Tony.
..............................................

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]);
      }
   }
}
Reply | Threaded
Open this post in threaded view
|

Re: irregular ROIs

dscho
Hi,

On Wed, 23 Aug 2006, Tony Shepherd wrote:

> can anyone tell me how to loop over an irregular (polygonal) ROI ?
>
> I tried taking a control loop from the tutorial called Inverter_ (pasted
> below)
>
> At the moment it only seems to loop over the bounding rectangle.
>
> Thanks,
>
> Tony.
> ..............................................
>
> public void run(ImageProcessor ip) {

You need to store a reference to the image, not the image processor. The
standard method is to add an ImagePlus member "image" and set that from
the setup() method.

Then, you can just do

        PolygonRoi roi = (PolygonRoi)image.getRoi();

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

Re: irregular ROIs

Wayne Rasband
In reply to this post by Tony Shepherd
Get the ROI's mask and only process pixels that correspond to non-zero
pixels in the mask. Here is an example:

public void run(ImageProcessor ip) {
    byte[] pixels = (byte[])ip.getPixels();
    int width = ip.getWidth();
    Rectangle r = ip.getRoi();
    ImageProcessor mask = ip.getMask();
    for (int y=r.y; y<(r.y+r.height); y++) {
       for (int x=r.x; x<(r.x+r.width); x++) {
          if (mask==null || mask.getPixel(x-r.x,y-r.y)!=0)
              ip.putPixel(x,y,255-ip.getPixel(x,y));
       }
    }
}

-wayne


On Aug 23, 2006, at 11:19 AM, Tony Shepherd wrote:

> can anyone tell me how to loop over an irregular (polygonal) ROI ?
>
> I tried taking a control loop from the tutorial called Inverter_
> (pasted below)
>
> At the moment it only seems to loop over the bounding rectangle.
>
> Thanks,
>
> Tony.
> ..............................................
>
> 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]);
>       }
>    }
> }
>