# Iterator over Roi points anywhere? Classic List Threaded 11 messages Open this post in threaded view
|

## Iterator over Roi points anywhere?

 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 } Any help appreciated! --Wilhelm -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Open this post in threaded view
|

## Re: Iterator over Roi points anywhere?

 I posted a similar solution in Jython here: http://forum.imagej.net/t/pointroi-input-different-from-output-point-positions/1038So you can get, e.g., points, etc., in Java with: Roi roi = im.getRoi(); int [] xArr = roi.getPolygon().xpoints int [] yArr = roi.getPolygon().ypoints See: http://javadoc.imagej.net/ImageJ1/ij/gui/PolygonRoi.html#getNCoordinates%28%29
Open this post in threaded view
|

## Re: Iterator over Roi points anywhere?

 In reply to this post by Burger Wilhelm > 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
Open this post in threaded view
|

## Re: Iterator over Roi points anywhere?

 In reply to this post by Burger Wilhelm Following up on this, does anyone have a plugin or algo to do the opposite, namely, collect a group of contiguous points into an ImageJ ROi? Eric Sent from [ProtonMail](https://protonmail.ch), encrypted email based in Switzerland. ---------------------------------------------------------------------- Date: Sun, 10 Apr 2016 15:08:40 +0000 From: Burger Wilhelm <[hidden email]> Subject: Iterator over Roi points anywhere? 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 } Any help appreciated! --Wilhelm -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html------------------------------ Date: Sun, 10 Apr 2016 09:21:57 -0700 From: Bio7 <[hidden email]> Subject: Re: Iterator over Roi points anywhere? I posted a similar solution in Jython here: http://forum.imagej.net/t/pointroi-input-different-from-output-point-positions/1038So you can get, e.g., points, etc., in Java with: Roi roi = im.getRoi(); int [] xArr = roi.getPolygon().xpoints int [] yArr = roi.getPolygon().ypoints See: http://javadoc.imagej.net/ImageJ1/ij/gui/PolygonRoi.html#getNCoordinates%28%29-- ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Open this post in threaded view
|

## Re: Iterator over Roi points anywhere?

 In reply to this post by Rasband, Wayne (NIH/NIMH) [E] 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
Open this post in threaded view
|

## Re: Iterator over Roi points anywhere?

Open this post in threaded view
|

## Re: Iterator over Roi points anywhere?

Open this post in threaded view
|

## Re: Iterator over Roi points anywhere?

 In reply to this post by Burger Wilhelm > 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
Open this post in threaded view
|

## Re: Iterator over Roi points anywhere?

 Hi, Great question. Is there any chance there is an equally nice solution on macro writing level? Currently I use a (time consuming) workaround by making all pixels outside the ROI black and subtracting 1 from all pixels in the image. Then I iterate through the bounding box of the ROI and use an if statement to check if the value of the pixel. If -1, then we must be outside the ROI... A nice solution like the getcontainedpoints method would be great (and much faster, I assume). Kind greetings, Dimitri Vanhecke -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html