// "AdvancedPlots" macros // These macros demonstrate how to use the toUnscaled() // and toScaled() functions with plot windows. // They require ImageJ 1.45n or later. // // There are five macros: // "Highlight and Annotate Area [F1]" // "Qualify Roi [F2]" // "Filled Plot [F3]" // "Custom Markers [F4]" // "Zoom in Plot [F5]" // // vischer at science.uva.nl 05.09.11 22:41 // Michel Schmid, updated for new plot features in ImageJ 1.49t, 03.05.15 macro "Highlight and Annotate Area [F1]" { requires("1.45n"); left = -1; right = 5; bottom = -0.2; top = 1.2; nPoints = 200; range = right - left; xValues = newArray(nPoints); yValues = newArray(nPoints); Plot.create("Gauss-Ellipse-Match", "X", "Y"); Plot.setLimits(left, right, bottom, top ); sigma = 2; ampl = 1; x0 = 2; radiusX = sigma /sqrt(2);//horizontal radius for (p = 0; p < nPoints; p++){ x = p/nPoints * range + left; y = ampl * exp(-pow(x - x0, 2)/(sigma * sigma * 2)); xValues[p] = x; yValues[p] = y; } Plot.setColor("blue"); Plot.add("line", xValues, yValues); Plot.setColor("red"); Plot.drawLine(x0, ampl/2, x0+radiusX, ampl/2); Plot.show; hookLeft = x0-radiusX; hookTop = ampl; hookBottom = 0; hookRight = x0+radiusX; toUnscaled(hookLeft, hookTop); toUnscaled(hookRight, hookBottom); makeOval(hookLeft, hookTop, hookRight - hookLeft, hookBottom - hookTop); changeValues(0x00ffffff, 0x00ffffff, 0x00ccddff); run("Select None"); setColor(0xff0000); x = x0; y = ampl/2; toUnscaled(x, y); drawString("r = sigma / sqrt(2)", x, y-2); if (getVersion()>="1.49t") Plot.freeze(); //avoid removing the oval or annotation by resizing or zooming } macro "Qualify ROI [F2]" { k1000 = 1000; xValues = newArray(k1000); yValues = newArray(k1000); for (jj = 0; jj ="1.49t") Plot.freeze(); //avoid removing the fill color by resizing or zooming } macro "Custom Markers [F4]"{ makeCustomIcons(); waitForUser("Using selection in RGB image as custom marker..."); getSelectionBounds(left, top, ww, hh); run("Copy"); xValues = newArray(11, 20, 33, 46, 61, 82); yValues = newArray(1, 10, 18, 22, 25, 28); Plot.create("Custom Markers", "X", "Y"); Plot.setLimits(0, 90, 0, 40 ); Plot.setColor("lightGray"); Plot.add("line", xValues, yValues); Plot.setColor("red"); //make sure the plot uses an RGB image Plot.show; selectImage(nImages); toUnscaled(xValues, yValues); for (jj = 0; jj < xValues.length; jj++){ makeRectangle(round(xValues[jj] -ww/2), round(yValues[jj] -hh/2), ww, hh); setPasteMode("Transparent-white"); run("Paste"); } setPasteMode("Copy"); run("Select None"); selectImage("Custom Icons"); close(); if (getVersion()>="1.49t") Plot.freeze(); //avoid removing the custom markers by resizing or zooming } macro "Zoom in Plot [F5]"{ left = 0; right = 4; bottom = 0; top = 1.2; nPoints = 2000; range = right - left; xValues = newArray(nPoints); yValues = newArray(nPoints); Plot.create("Zoom in Plot", "X", "Y"); Plot.setLimits(left, right, bottom, top ); sigma = 0.05; ampl = 1; x0 = 2; for (p = 0; p < nPoints; p++){ x = p/nPoints * range + left; y = ampl * exp(-pow(x - x0, 2)/(sigma * sigma * 2)); xValues[p] = x; yValues[p] = y; } Plot.setColor("blue"); Plot.add("line", xValues, yValues); Plot.show; if (getVersion()>="1.49t") { tx1 = "Click ok and try zooming with\n"; tx = "- the commands of the Image>Zoom menu\n"; tx = tx + "- the '+' and '-' keys,\n"; tx = tx + " with the cursor inside the plot (zoom on point),\n"; tx = tx + " with the cursor left of the y axis/below the x axis,\n"; tx = tx + "- with the mouse wheel while pressing SHIFT or CTRL\n"; tx = tx + "- with the commands in the More>> menu of the plot.\n"; tx = tx + "Try panning with\n- with the mouse while pressing the space bar\n"; tx = tx + "- with the scrolling tool (hand tool) of the toolbar,\n"; tx = tx + "- or with the mouse wheel (press space bar for horizontal scrooling)."; showMessage(tx1+tx); print("Try zooming with\n"+tx); selectWindow("Zoom in Plot"); exit(); } //The following code is for previous ImageJ versions without built-in zooming waitForUser("Create ROI for zooming, then click OK"); if (selectionType == -1) exit ("No Selection found"); getSelectionBounds(xmin, ymin, w, h); ymin += h; xmax = xmin + w; ymax = ymin - h; toScaled(xmin, ymin); toScaled(xmax, ymax); Plot.create("Zoom to ROI", "X", "Y"); Plot.setLimits(xmin, xmax, ymin, ymax ); Plot.setColor("blue"); Plot.add("line", xValues, yValues); Plot.show; } macro "Arrow Plot [F6]" { // This macro demonstrates how to plot a field of arrows // It also shows that arrows can be combined with other plot objects requires("1.49t"); gridSize = 20; n = gridSize*gridSize; increment = 1.0/(gridSize-1); xS = newArray(n); yS = newArray(n); xE = newArray(n); yE = newArray(n); i = 0; for (y=0; y<=1.000001; y+=increment) { for (x=0; x<=1.000001; x+=increment) { xS[i] = x; yS[i] = y; dx = (0.45+0.45*sin(2*PI*y))/gridSize; dy = 0.9*cos(2*PI*x+0.75*PI*y)/gridSize; xE[i] = x + dx; yE[i] = y + dy; i++; } } Plot.create("Arrow Field Plot","X Axis","Y Axis"); Plot.setColor("green"); Plot.setColor("blue"); Plot.drawVectors(xS, yS, xE, yE); xLine = newArray(-1, 2); yLine = newArray(0.75, 0.75); Plot.setColor("red"); Plot.add("line", xLine, yLine); Plot.setColor("black"); //legend frame Plot.setLegend("x component vanishes here", "top-left erase"); Plot.show(); } function makeCustomIcons(){ newImage("Custom Icons", "RGB-Color White", 500, 21, 1); random("seed", 5); for (x = 10; x<=240; x+= 30){ setForegroundColor(random*256, random*256, random*256); fillOval(x-8, 10-1, 17, 3); fillOval(x-1, 10-8, 3, 17); } setFont("Monospaced", 12, "antialiased"); drawString("A B C D E F", 248, 17); makeRectangle(0, 0, 21, 21); }