suggestion: scaleRectangle()

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

suggestion: scaleRectangle()

vischer
Hi Wayne and others,

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.

My favorite built-in function would be:

        scaleRectangle(scaledLeft, scaledTop, scaledBottom, scaledRight);
        //scales corner points of current rectangular ROI

As such a function does not exist, I tried to emulate it - a clumsy business
and finally without success.
Obviously it is not enough to calculate pixel size and offsets.

The macro below shows this:
it copies a plot frame and applies same properties to the copy.
While "Simple Plot" behaves correctly, "Copied Plot" shows negative y-coordinates
in the ImageJ status display.
(I know I could use "Duplicate" instead "Copy", but I need the freedom to
apply a custom scale to any image)




macro "Copy a Plot" {
        xMin = 30;
        xMax = 50;
        yMin = 6;
        yMax = 9;
        frameWidth = 400;
        frameHeight = 200;
        Plot.create("Simple Plot", "X", "Y");
        Plot.setFrameSize(frameWidth, frameHeight);
        Plot.setLimits(xMin, xMax, yMin, yMax);
        Plot.show;//from here, I copied properties
        run("Select All");
        run("Copy");
        run("Internal Clipboard");
        rename("Copied Plot");
        run("Properties...", "pixel_width=0.050 pixel_height=0.0150 origin=-540,615");
}




Norbert Vischer

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: suggestion: scaleRectangle()

vischer
> 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