Login  Register

Re: suggestion: scaleRectangle()

Posted by vischer on Jul 28, 2013; 10:40pm
URL: http://imagej.273.s1.nabble.com/suggestion-scaleRectangle-tp5004148p5004195.html

> I often want to paste a plot into a larger image and maintain correct
> status display of coordinates while the cursor is moved across the plot.

Meanwhile, I managed to write a scaleRectangle() function (see below).
This can be useful whenever a plot needs to be part of a larger image
with correct interaction of the mouse cursor.
My first trial was not successful because I was not aware that
ImageJ accepts negative values for pixel width and height.

Norbert Vischer



//Pastes a plot frame into an existing window
//and adjusts scaling so that ImageJ status shows correct cursor coordinates.

macro "Scale Rectangle Demo" {
        close("*Plot*");
        newImage("Plot-2", "RGB white", 800, 600, 1);
        setForegroundColor(200, 200, 200);
        run("Select All");
        run("Fill", "slice");
        xMin = 30;
        xMax = 40;
        yMin = 10;
        yMax = 20;
        frameWidth = 400;
        frameHeight = 100;
        Plot.create("Simple Plot", "X", "Y");
        Plot.setFrameSize(frameWidth, frameHeight);
        Plot.setLimits(xMin, xMax, yMin, yMax);
        Plot.show;


        run("Select All");
        run("Copy");
        selectImage("Plot-2");
        run("Paste");
  getSelectionBounds(x, y, w, h);
        //ImageJ uses frameTop = 60, frameLeft = 15
        makeRectangle(x + 60, y + 15, frameWidth, frameHeight);
        scaleRectangle(xMin, yMax, xMax, yMin);
}

//scales boundary of current roi
function scaleRectangle(scaledLeft, scaledTop, scaledRight, scaledBottom){
  getSelectionBounds(x, y, frameWidth, frameHeight);
        xRange = scaledRight - scaledLeft;
        yRange = scaledTop - scaledBottom;
        origX = x - scaledLeft * frameWidth/xRange;
        origY = frameHeight + y + scaledBottom * frameHeight/yRange;
        pxWidth = xRange/frameWidth;
        pxHeight = -yRange/frameHeight;
        run("Properties...", "pixel_width=" + pxWidth + " pixel_height=" +  pxHeight + " origin=" +  origX +"," + origY);
}

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html