Hi,
I'm writing a javascript that uses overlays to draw regions on a stack. What I do is that I set each slice for the ImagePlus, then create a ROI from a coordinates array, then add that roi the overlay. In the end I affect the Overlay to the stack and display the stack with its overlay. The problem is that it doesn't keep each overlay associated with its slice: I get a single-slice overlay with all ROIs. How can I get overlays to stay associated with the stack slices, like what happens when manually adding ROIs to an overlay in an stack ? Is there a way to use something like over.add(roi) that takes into account which slice this particular overlay element has to be associated with ? Here is a sample code I use : importClass(Packages.ij.gui.Overlay); var imp = IJ.createImage("Stack", "8-bit Black", 200, 200, 2); var rm = new RoiManager(); var over = new Overlay(); imp.setSlice(1); xpoints = [26,120,88,161]; ypoints = [38,44,108,158]; var roi1 = new PolygonRoi(convertArrayI(xpoints), convertArrayI(ypoints), xpoints.length, Roi.POLYLINE); over.add(roi1); imp.setSlice(2); xpoints = [116,54,72,152]; ypoints = [38,88,157,123]; var roi2 = new PolygonRoi(convertArrayI(xpoints), convertArrayI(ypoints), xpoints.length, Roi.POLYLINE); over.add(roi2); imp.setOverlay(over); imp.show(); function convertArrayI(arr) { var jArr = java.lang.reflect.Array.newInstance(java.lang.Integer.TYPE, arr.length); for (var i = 0; i < arr.length; i++) { jArr[i] = arr[i]; } return jArr; } |
On Jan 3, 2012, at 8:07 AM, Christophe Leterrier wrote:
> Hi, > > I'm writing a javascript that uses overlays to draw regions on a stack. What I do is that I set each slice for the ImagePlus, then create a ROI from a coordinates array, then add that roi the overlay. In the end I affect the Overlay to the stack and display the stack with its overlay. > > The problem is that it doesn't keep each overlay associated with its slice: I get a single-slice overlay with all ROIs. How can I get overlays to stay associated with the stack slices, like what happens when manually adding ROIs to an overlay in an stack ? Is there a way to use something like over.add(roi) that takes into account which slice this particular overlay element has to be associated with ? Use the setPosition() method to associate an ROI to a particular stack slice. Use the PolygonRoi constructor that accepts a Polygon as the first argument to avoid the convertArrayI() function. Here is an updated version of your example: var imp = IJ.createImage("Stack", "8-bit Black", 200, 200, 2); var over = new Overlay(); xpoints = [26,120,88,161]; ypoints = [38,44,108,158]; p = new Polygon(xpoints, ypoints, xpoints.length); var roi1 = new PolygonRoi(p, Roi.POLYLINE); roi1.setPosition(1); over.add(roi1); xpoints = [116,54,72,152]; ypoints = [38,88,157,123]; p = new Polygon(xpoints, ypoints, xpoints.length); var roi2 = new PolygonRoi(p, Roi.POLYLINE); roi2.setPosition(2); over.add(roi2); imp.setOverlay(over); imp.show(); -wayne > > Here is a sample code I use : > > importClass(Packages.ij.gui.Overlay); > > var imp = IJ.createImage("Stack", "8-bit Black", 200, 200, 2); > var rm = new RoiManager(); > var over = new Overlay(); > > imp.setSlice(1); > xpoints = [26,120,88,161]; > ypoints = [38,44,108,158]; > var roi1 = new PolygonRoi(convertArrayI(xpoints), convertArrayI(ypoints), xpoints.length, Roi.POLYLINE); > over.add(roi1); > > imp.setSlice(2); > xpoints = [116,54,72,152]; > ypoints = [38,88,157,123]; > var roi2 = new PolygonRoi(convertArrayI(xpoints), convertArrayI(ypoints), xpoints.length, Roi.POLYLINE); > over.add(roi2); > > imp.setOverlay(over); > imp.show(); > > function convertArrayI(arr) { > var jArr = java.lang.reflect.Array.newInstance(java.lang.Integer.TYPE, arr.length); > for (var i = 0; i < arr.length; i++) { > jArr[i] = arr[i]; > } > return jArr; > } > > > |
Free forum by Nabble | Edit this page |