Login  Register

Re: Iterator over Roi points anywhere?

Posted by Rasband, Wayne (NIH/NIMH) [E] on Apr 12, 2016; 1:06am
URL: http://imagej.273.s1.nabble.com/Iterator-over-Roi-points-anywhere-tp5016087p5016101.html

> 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
> }

The latest ImageJ daily build (1.51a5) adds an Roi.getContainedPoints() method which works with arbitrary ROIs, including point and line selections. Here is example Java code that calculates the mean pixel value of the current ROI:

 ImagePlus img = IJ.getImage();
 ImageProcessor ip = img.getProcessor();
 Roi roi = img.getRoi();
 double sum=0;
 int count=0;
 for (Point p : roi.getContainedPoints()) {
    count++;
    sum += ip.getf(p.x, p.y);
 }
 IJ.log("mean,count: "+IJ.d2s(sum/count,4)+" "+count);

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