# Serial ROI creation

#1

Hi Guys!

I’m trying to write a few macros to help me with some analysis. Basically, in an image where I already drew a ROI I want to make a macro to command the creation of consecutive ROIs in the same shape as the initial but increasing in size with constant increments (based on the scale of the image, not pixels). So, as an exemple, if I have a circle in the middle of the image of a diameter of 100 microns and my set constant is 20, I would have next 120, 140, 160, 180, etc until the ROI reaches the border of the image.

Is it clear? Could you help me with that?? I’m a very beginner in programming, so I have no idea of how.

Thank you so much in advance!!
Marcos

#2

Good day Marcos,

here is a demo macro that does what you wish:

inc = 20;
sInc = inc;
coord = newArray(2);
newImage("Untitled", "8-bit black", 512, 512, 1);
w = getWidth();
h = getHeight();
mouseClickCoord( coord );
makeOval(coord[0], coord[1], 50, 50);
run("Set Scale...", "distance=2 known=1 unit=µm");
getSelectionBounds(x, y, ww, hh);
toScaled(sInc);
while (x-inc>0 && x+ww+inc<w && y-inc>0 && y+hh+inc<h) {
run("Enlarge...", "enlarge="+sInc);
getSelectionBounds(x, y, ww, hh);
}
roiManager("Show All");
exit();
function mouseClickCoord( a ) {
a[0] = 0;
while (a[0]==0) {
getCursorLoc( x, y, z, mdfrs );
if (mdfrs&16!=0) { a[0] = x; a[1] = y; }
}
}

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it.

Click somwhere in the image canvas which defines the left-top position of the first/inner circle.

Regards

Herbie

#3

Hi Herbie,

Many thanks!! I do have one question, is there any way that the ROI could be a free design and not only circles? I mean, the idea was to draw an ROI over a region, and then having this ROI (same shape) increased sequentially. That’s because the region of interest won’t likely be a circle. The circle was the easiest example I could describe).So, the idea is to have a image from a microscope and draw first the ROI, then in a non destructive way, having the new ROIs as overlays.

Many thanks again!!
Marcos

#4

Marcos,

if you understand the provided ImageJ macro you could easily generalize it to your liking …

Here is—at no extra charge—what you want:

inc = 20;
sInc = inc;
coord = newArray(2);
newImage("Untitled", "8-bit black", 512, 512, 1);
w = getWidth();
h = getHeight();
waitForUser("Draw a selection of your choice!");
run("Set Scale...", "distance=2 known=1 unit=µm");
getSelectionBounds(x, y, ww, hh);
toScaled(sInc);
while (x-inc>0 && x+ww+inc<w && y-inc>0 && y+hh+inc<h) {
run("Enlarge...", "enlarge="+sInc);
getSelectionBounds(x, y, ww, hh);
}
run("From ROI Manager");
run("Select None");
run("Close");
exit();

Please learn how to write macros. It is really easy.

Reagrds

Herbie

#5

Thanks Herbie,

Learning to write is my intention, however I still have many gaps on my java language knowledge.

Before you sent me the last version I managed to edit the original and made it work. However there is only one thing I couldn’t. The increments are always based on pixel number and not microns… I’m trying to add a constant based on the information on the set scale dialog to multiple to the increments and correct it, but I still couldn’t do it. Am I thinking wrong?

Thanks
M

#6

Hey Herbie,

I managed! Many thanks though!
M

#7

I still have many gaps on my java language knowledge.

ImageJ-macros are written in an easy to learn macro-language.
No Java needed!
https://imagej.nih.gov/ij/developer/macro/macros.html
https://imagej.nih.gov/ij/developer/macro/functions.html

Regarding scale (I refer to my first macro):

1. Please click somewhere in the empty canvas.
2. Go to the ROI-manager and select the first item
3. Move the innermost circle with the mouse and observe the ImageJ status bar
4. The status bar shows the pixel values in parentheses: Diameter 25µm (50pel)
5. Go to the ROI-manager and select the second item
6. Move the second circle with the mouse and observe the ImageJ status bar
7. The status bar shows the pixel values in parentheses: Diameter 45µm (90pel)

Consequently the diameter increment is 20µm.
I guess this is what you wanted.

Regards

Herbie

#8

In the first it works because we set directly the scale in the macro. If that is removed from the macro to not change the original scale it starts working only based on the pixels. However, in the command toScaled(sInc) if I change to toScaled(inc) it starts to work!

I do have one last question though. If my ROI is in the corner and I still want to have the other ROIs towards the other end of the image, is there anyway to solve this? Because, right now if I draw and ROI in the corner it won’t work because it will hit the boundaries of the image…

I’m trying to solve a few issues before implementing on my analysis… I know I could just play with the canvas, but there maight be a way in the macro so I can avoid that…

Many many thanks,
Marcos

#9

Marcos,

you are changing the game because initially you wrote:

ROIs in the same shape as the initial but increasing in size with constant increments […] until the ROI reaches the border of the image.

Now you want it differently:

I still want to have the other ROIs towards the other end of the image

Yes this is possible. You will find out!

However, in the command toScaled(sInc) if I change to toScaled(inc) it starts to work!

If you do this, it works differently.
You will have to learn why.

Good luck

Herbie

PS:
We are here to help but not to do your work for you!