Login  Register

Re: Iterator over Roi points anywhere?

Posted by Michael Schmid on Apr 11, 2016; 12:46pm
URL: http://imagej.273.s1.nabble.com/Iterator-over-Roi-points-anywhere-tp5016087p5016097.html

Hi Wilhelm,

Wayne's code should work for rectangles. They do not have a masks - the
if (mask==null...) clause is meant for that case.

For non-area rois, I fear that it is not so easy to get all points.

For lines, you can use the 'Line to Area' command first. For Multi-Point
Rois, you can use Roi.contains, or take the detour via 'Create Mask' and
'Create Selection'.

The 'Roi.contains' method seems to be meant for this purpose, but
unfortunately it does not work with lines. An implementation to make it
work for lines would be nice (anyone out there who could do this?)

A nasty problem with Roi.contains: For polygons, the pixels reported by
'Roi.contains' and those affected by 'Fill' and are not the same. It
seems that there is a half-pixel offset in x and y between them. The
reason is that PolygonRoi.contains uses FloatPolygon.contains instead of
the mask.
(The mask, which uses the ImageJ PolygonFiller, better fits the ImageJ
conventions, i.e., it better fits what you see at high magnification).

Another small inconsistency, if someone should find time to look at
this: Both 'Fill' and 'Roi.contains' for rectangular rois with
non-integer position do not round coordinates to the nearest integer but
truncate them. For rectangles with positions like 9.99, this will cause
an operation to be almost 1 pixel off from what you see at the screen.
This seems to be a remainder from the days when ImageJ Rois could only
have integer coordinates. Nothing is perfect...

Michael
________________________________________________________________

On 2016-04-11 13:36, Burger Wilhelm wrote:

> Thanks Wayne,
>
> this works nicely for some types of ROIs (polygon, ellipse, freehand). However, ROIs of sub-type rectangle, line, point and multi-point do not seem to define a mask.
>
> --Wilhelm
>
>
> -----Original Message-----
> From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Rasband, Wayne (NIH/NIMH) [E]
> Sent: Montag, 11. April 2016 04:48
> To: [hidden email]
> Subject: Re: Iterator over Roi points anywhere?
>
>> On Apr 10, 2016, at 11:08 AM, Burger Wilhelm <[hidden email]> wrote:
>>
>> Hello all,
>>
>> does anyone know of a straight way to enumerate/iterate over all points contained in an (arbitrary) Roi? That is (in Java terms), something like this:
>>
>> ...
>> Roi roi = im.getRoi();
>> for (Point p : roi.getContainedPoints()) {
>>    ... // process p
>> }
>
> Get the ROI mask and use it to determine which pixels are contained in the ROI. The following JavaScript/BeanShell code calculates the mean value of the pixels contained in the current ROI or the mean of the entire image if there is no ROI.
>
> -wayne
>
>    img = IJ.getImage();
>    ip = img.getProcessor();
>    bounds = ip.getRoi();
>    mask = img.getMask();
>    sum = 0;
>    count = 0;
>    for (y=0; y<bounds.height; y++) {
>       for (x=0; x<bounds.width; x++) {
>          if (mask==null||mask.getPixel(x,y)!=0) {
>             count++;
>             sum += ip.getf(x+bounds.x, y+bounds.y);
>          }
>       }
>    }
>    IJ.log("mean,count: "+IJ.d2s(sum/count,4)+" "+count);
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html