How to select ROIs as equal square rectangles?

My goal is to select 4 equal rectangular ROIs on image. This ROIs should be equal distance away (lets say 500 pixels away) from one selected landmark on image. How can I create such 4 equal ROIs? So far I can only to create one (using rectangle tool). How to create all 4? And then to create 4 separate images of those selected ROIs (for further statistical analysis)?

@1timur1

You can script it. Here are some helpful links to get you going with scripting in ImageJ:

To start - just open the Macro Recorder… then you can start saving your commands in code - and then start piecing things together. So for example - this code lets the user select a point on an opened image and then draws 4 rectangles at equal distance around it:

waitForUser("Draw a point on the image.");
roiManager("Add");
Roi.getBounds(x, y, width, height);
buffer = 10;
rectWidth = 50;
rectHeight = 30;
//setTool("rectangle");
makeRectangle(x+buffer, y+buffer, rectWidth, rectHeight);
roiManager("Add");
makeRectangle(x-buffer-rectWidth, y-buffer-rectHeight, rectWidth, rectHeight);
roiManager("Add");
makeRectangle(x+buffer, y-buffer-rectHeight, rectWidth, rectHeight);
roiManager("Add");
makeRectangle(x-buffer-rectWidth, y+buffer, rectWidth, rectHeight);
roiManager("Add");

Just as a simple example…

2 Likes

Hi,

following what Ellen said, to get the images, you can select each ROI and perform a duplicate (Image>Duplicate…)

Nico

1 Like

Thank you. It works on my images. These rectangles appear diagonally from each other. Is it possible to position them up and down an

d left and right? Something like in the image attached.

The first two arguments of makeRectangle are the x and y coordinates of the position where the rectangle’s upper left corner will appear. The rectangles are placed ‘diagonal’ because in @etadobson’s example macro buffer is added to both x and y. That is a ‘right down’ shift. Then buffer is subtracted from both x and y. That is a ‘left, up’ shift. (subtracting the rectWidth from the x coordinate makes sure the rectangle is placed left of the first rectangle.)

It probably helps to spell out your upper left coordinates of the rectangles you desire, using the variables in @etadobson’s macro.

Subsequently buffer is added to x, but subtracted from y. That is a ‘right up’ (y-coordinates in ImageJ run from top to bottom). So to only go up or down, you leave the x coordinate as is, and only add buffer to the y component (or subtract it from the y coordinate) and in order to go left (or right) you leave the y as is and add (or subtract) buffer to the x value.

Likewise, if you need the rectangles rather than squares, and you require them to be ‘in portrait’ when shifted horizontally and ‘in landscape’ when shifting up or down, have different values for buffer; e.g. aBuffer=30 and bBuffer=20. On the horizontal shifts you use bBuffer for the Width argument, on the vertical shifts you use aBuffer for the Width arguments.

BTW, your image looks like a stitched image with quite a bit fo shading. Don’t let shading interfere with your (statistical) analysis.
Another thing to keep in mind: IF statistical analysis needs random sampling consider that the leftmost rectangle in the attached image is not placed randomly. You probably manually shifted the rectangle up to avoid the tear that comes in from the lower left, but manually adjusting the location does away with sampling still being random. You introduce a bias. Stereology theory helps you to avoid this bias.

1 Like