Login  Register

Re: irregular ROIs

Posted by Wayne Rasband on Aug 23, 2006; 5:21pm
URL: http://imagej.273.s1.nabble.com/irregular-ROIs-tp3701774p3701775.html

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]);
>       }
>    }
> }
>