Posted by
Rasband, Wayne (NIH/NIMH) [E] on
Jul 01, 2010; 11:58pm
URL: http://imagej.273.s1.nabble.com/Overlays-from-plugin-example-tp3687749p3687750.html
On Jul 1, 2010, at 12:04 PM, Kenneth Sloan wrote:
> I'm writing a plugin, in java (i.e., NOT a macro), and feel the need to
> display results (for now, several sets of points - later, perhaps several
> polylines) as an overlay. I prefer not to munge the underlying image.
>
> Google tells me that someone implemented "ImageCanvasWithOverlay" about a year ago.
>
> But, I also see references to "Overlay" in the ImageJ documentation, and a flurry of
> changes (as recent as 1 June) that seem to be related to overlays. From what I can
> gather, this overlay feature is closely coupled with ROI's. It's not entirely clear to me
> that my point sets are "ROIs" - perhaps I'm confused about that?
>
> I checked the Examples, and found a *macro* that uses the ROI manager to create an overlay.
> What I need is an example that does this from a (java) plugin.
>
> My target code for this would look something like:
>
> createOverlay();
> for(int i = 0; i < numPointSets; i++)
> {
> setColor(color[i]);
> for(int p = 0; p < numPoints[i]; p++)
> plotPoint(points[i][p];
> }
> showOverlay();
>
> Bottom line: I want to display several collections of POINTS (perhaps later POLYLINES) as
> an overlay.
>
> Perhaps I need a good "plugin developers' reference manual"? Feel free to point me at one...please!
> My profound apologies if such a thing exists in plain sight and I've simply missed it.
An overlay is just a list of one or more ROIs that is non-destructively displayed on an image. To display a collection of points you can create a PointRoi (a multi-point selection) and display it as an overlay. Or create a ShapeRoi containing custom points and display it as an overlay. Here is an example plugin that does both.
-wayne
import ij.*;
import ij.gui.*;
import java.awt.*;
import java.util.Random;
import ij.plugin.*;
import java.awt.geom.*;
public class Overlay_Points implements PlugIn {
public void run(String arg) {
ImagePlus imp = IJ.getImage();
int n = 100;
int[] xpoints = new int[n];
int[] ypoints = new int[n];
Random ran = new Random();
for (int i=0; i<n; i++) {
xpoints[i] = (int)(ran.nextDouble()*imp.getWidth());
ypoints[i] = (int)(ran.nextDouble()*imp.getWidth());
}
Roi roi = new PointRoi(xpoints, ypoints, n);
roi.setStrokeColor(Color.red);
Overlay overlay = new Overlay(roi);
imp.setOverlay(overlay);
IJ.wait(4000);
int size = 10;
int lineWidth = 3;
GeneralPath path = new GeneralPath();
for (int i=0; i<n; i++) {
path.moveTo(xpoints[i]-size,ypoints[i]);
path.lineTo(xpoints[i]+size,ypoints[i]);
path.moveTo(xpoints[i],ypoints[i]-size);
path.lineTo(xpoints[i],ypoints[i]+size);
}
roi = new ShapeRoi(path);
roi.setStrokeColor(Color.blue);
roi.setStrokeWidth(lineWidth);
overlay = new Overlay(roi);
imp.setOverlay(overlay);
}
}