Posted by
Burger Wilhelm on
Apr 11, 2016; 11:26am
URL: http://imagej.273.s1.nabble.com/Iterator-over-Roi-points-anywhere-tp5016087p5016095.html
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