Posted by
Michael Schmid on
URL: http://imagej.273.s1.nabble.com/Getting-coordinates-inside-a-ROI-tp5003250p5003282.html
Hi Peter,
if you create a new Point object for each point in the ROI and put them into an array or vector, this will be slow - the Garbage Collector gets a lot of work to get rid of all the unnecessary Point objects (can be millions) after use.
You can have int[][] roiPoints = getRoiPoints(Roi roi), which gives you two arrays, roiPoints[0] with all the x coordinates and roiPoints[1] with all the y coordinates.
For creating these arrays, as Wayne said, it would be best to step through the mask, avoiding what may be millions of calls to the contains() method, with all the overhead of a method call.
Something like the following (I did not try, so there may be lots of mistakes in it):
int n=0; //number of points in the selection
byte[] mask;
Rectangle rect = roi.getBounds();
if (roi.getType()==Roi.RECTANGLE) {
mask = null;
n = rect.width*rect.height;
} else {
mask = (byte[])(roi.getMask().getPixels());
for (int i=0; i<mask.length; i++)
if (mask[i] != 0) n++; //count points in mask
}
int[][] points = new int[2][n];
int i=0;
for (int y=rect.y; y<rect.y+rect.height; y++)
for (int x=rect.x; x<rect.x+rect.width; x++)
if (mask==null || mask[i]!=0) {
points[0][i] = x;
points[1][i] = y;
i++;
}
It might be worthwhile having something like this as method of Roi (and maybe even as a macro function getRoiPoints(xArray, yArray).
If one does not care about the potentially huge memory consumption by the array, it could provide a simple way to write code that steps through all points of the roi.
Michael
________________________________________________________________
On Jun 7, 2013, at 16:06, Peter Mc wrote:
> Thanks Wayne;
> I am afraid cannot get why is much faster...
> I do not need to get the the pixel value(just X,Y coordinates),so getting
> the bounds is enough for the coordinates (Rectangle coordinates). Using some
> ROIs as ovals or ellipses some points inside the rectangle could not be
> inside the ROI but the method /contains()/ fixes this issue quite good; sure
> I could avoid the method contains() establishing some restrictions over X
> and Y in the loop,but I think is not necessary get more complicated the code
> in my case.
>
> Actually I was looking for a direct way, don't know something close to :
>
> Point getROIPoints(Roi roi) { }
>
>
> Thank you for you answer and please let me know if you have a better idea
> than mine,haha.
>
>
>
> --
> View this message in context:
http://imagej.1557.x6.nabble.com/Getting-coordinates-inside-a-ROI-tp5003250p5003280.html> Sent from the ImageJ mailing list archive at Nabble.com.
>
> --
> ImageJ mailing list:
http://imagej.nih.gov/ij/list.html--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html