///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Original code by Wayne Rasband, improved by Frank Sprenger // and deposited on the ImageJ mailing server: // (http://imagej.588099.n2.nabble.com/Overlay-Scalebar-Plugins-td6380378.html#a6394996). // I added choise of font size, scale bar height, option to choose any position for scale bar and some // options that allow to set the image calibration (only for overlay, not in Meta data). // Kees Straatman, CBS, University of Leicester, May 2011 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// macro "scalebar_overlay"{ pixelH = getHeight(); pixelW = getWidth(); getPixelSize(scale, pixelSize, y); //ratio = pixelSize/y; sbHeight = pixelH / 100; Dialog.create("Scale Bar Parameters"); Dialog.addNumber("pixelSize:", pixelSize); //Dialog.addNumber("Pixel aspect ratio:", ratio); Dialog.addString("Unit of length:", scale); Dialog.addNumber("Length of scale bar in current units:", 10); Dialog.addNumber("hight of scale bar;", sbHeight); choice1 = newArray("white", "black", "red", "green", "blue", "yellow", "magenta", "cyan"); Dialog.addChoice("Scale bar color:", choice1); choice2 = newArray("Top Left", "Top Right", "Bottom Left", "Bottom Right", "Move Free"); Dialog.addChoice("Scale bar position:", choice2); Dialog.addCheckbox("Hide text", false); Dialog.addNumber("Font size:", 12); Dialog.show(); pixelSize = Dialog.getNumber(); //pixelRation = Dialog.getNumber(); units = Dialog.getString(); inLength = Dialog.getNumber(); sbHeight = Dialog.getNumber(); inColor = Dialog.getChoice(); inPos = Dialog.getChoice(); if (Dialog.getCheckbox()==true) ht=1; else ht=0; fontSize = Dialog.getNumber(); sbLength = inLength / pixelSize; if (inColor == "white") setColor(255,255,255); else if (inColor == "black") setColor(0,0,0); else if (inColor == "red") setColor(255,0,0); else if (inColor == "green") setColor(0,255,0); else if (inColor == "blue") setColor(0,0,255); else if (inColor == "yellow") setColor(255,255,0); else if (inColor == "magenta") setColor(255,0,255); else if (inColor == "cyan") setColor(0,255,255); if (inPos == "Top Left") { sbXcoord = pixelW / 30; sbYcoord = pixelH / 30; } else if (inPos == "Top Right") { sbXcoord = pixelW - sbLength - (pixelW / 30); sbYcoord = pixelH / 30; } else if (inPos == "Bottom Left") { sbXcoord = pixelW / 30; sbYcoord = pixelH - sbHeight - (pixelH / 30); } else if (inPos == "Bottom Right") { sbXcoord = pixelW - sbLength - (pixelW / 30); sbYcoord = pixelH - sbHeight - (pixelH / 30); } else if (inPos=="Move Free"){ setTool("rectangle"); title="position"; msg = "draw a box in the image where you want the scale bar to appear"; waitForUser(title, msg); getSelectionBounds(sbXcoord, sbYcoord, width, height); } makeRectangle(sbXcoord, sbYcoord, sbLength, sbHeight); run("Add Selection...", "stroke=" + inColor + " width=1 fill=" + inColor); if (ht==0){ textYcoord = sbYcoord - (pixelH / 200); label = toString(inLength) + " " + scale; setFont("SansSerif",fontSize); Overlay.drawString(label, sbXcoord, textYcoord); Overlay.add; Overlay.show; } run("Select None"); }