Download ImageCanvasWithOverlay.zip. Unzip into your plugins folder. Delete the sample newOverlay_Test files if you don't need them.
(source: ImageCanvasWithOverlay.java)
ImageJ is a versatile tool with many utilities. However, it does not have an "overlay" method. I needed this for the "Census" plugin, among other things, so I wrote some modifications to the ImageJ source code. With help from Johannes Schindelin and Samuel Moll, it finally became what it is now. Basically, ImageCanvasWithOverlay is a class that expands ImageCanvas, adding an "overlay" structure. The overlay is a transparent layer that is above an image. It is possible to write on this layer, and the changes do not modify the underlying image. Also, any changes to the image will not affect the overlay layer.
I've made a sample plugin that uses the ImageCanvasWithOverlay class, called newOverlay_Test. It is included in with the package above. Just open an image and run the plugin. A cross will be placed at the upper left of the image. Try to zoom in and out: you will notice the cross zooms with the image. Try to change the brightness and contrast of your image: you will notice that this does not affect the cross. Also notice that one of the bars uses the alpha channel, and as such is semi-transparent.
The source for the sample is below. The key lines are:
ImageCanvasWithOverlay icwo = new ImageCanvasWithOverlay(imp);
imp.setWindow(new ImageWindow(imp, icwo));
These commands replace the existing ImageCanvas with ImageCanvasWithOverlay.
import ij.*; import ij.gui.*; import java.awt.Color; import ij.plugin.PlugIn; import ij_ImageCanvasWithOverlay.ImageCanvasWithOverlay; /* * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA * * * Peter Sebastian Masny * ps-contact@masny.dk * * Parts of this code were adapted from Wayne Rasband, Sergio * Caballero Jr., Michael A. Miller, Philippe Thevenaz, * Samuel Moll, and Johannes Schindelin. Content derived from * these authors follow their corresponding copyrights. * * @version 2.2 26 Jan 2009 * @author Peter Sebastian Masny */ public class newOverlay_Test implements PlugIn { public void run(String arg) { ImagePlus imp = WindowManager.getCurrentImage(); ImageCanvasWithOverlay icwo = new ImageCanvasWithOverlay(imp); imp.setWindow(new ImageWindow(imp, icwo)); icwo.overlaySetLine(10, 15, 20, 15, new Color(0,0,255,128)); for (int i = 10; i < 20; i++) { icwo.overlaySetPixel(15, i, new Color(255,0,0)); } icwo.overlayFillRoi( new Color(0,255,0)); icwo.repaint(); } } |
(Try running it with and without having a Roi selected when running the plugin).