Question about user controlled code advance


Hi there,

I am still very new to making macros in imageJ, and I have a small question about my code. I know the imageJ macro language doesn’t have a “GoTo”, but i want advice on how to proceed with my code.

Overall goal: Make a skeleton of cell membrane image, isolate a single branch and use as a mask to measure intensity of different tagged proteins that associate with membrane.

I have a macro created that makes the skeletonized image, and a macro that can analyze intensity of a define region… but my issue is isolating specific branches to make a mask.

input: Binary skeleton image stack of time points

What i want to do: load image> use multipoint to select a point(s) to “cut” (clear) > binarize > analyse skeleton > isolate the branch I “cut” > create new file with isolated branch to use as mask… and I want this to repeat for each time point in the stack.

So far, I have been able to to use the multipoint tool to isolate a membrane, but the issue is proceeding to next step in code. I dont know to tell the code that I am done using the “Circle on mouse click” function and to go on with the rest of the code… can anyone help me?

Code (i know it is messy and not efficient… sorry…):

//retrieves image info and creates save directory for images
dir = getDirectory(“Select Main Save Location”);
path= dir + “test2”+ File.separator;
getDimensions(w, h, channels, slices, frames);

//establish loop
for (q = 1; q <= slices; q++){
u = IJ.pad(q, 3);
run(“Duplicate…”, " ");
// allows for user to click on area, and generate ciricle ROI that will clear pixels inside
// Need to be able to make multiple ROI to isolate branch
//right click stops whole macro…
macro CircleOn_MouseClick{
setOption(“DisablePopupMenu”, false);
getPixelSize(unit, pixelWidth, pixelHeight);
height = 2pixelHeightradius;
width = 2pixelWidthradius;
x2=-1; y2=-1; z2=-1; flags2=-1;
getCursorLoc(x, y, z, flags);
while (flags&rightButton==0){
getCursorLoc(x, y, z, flags);
if (flags&leftButton!=0) {
if (x!=x2 || y!=y2 || z!=z2 || flags!=flags2) {
x = x - width/2;
y = y - height/2;
makeOval(x, y, width, height);
x2=x; y2=y; z2=z; flags2=flags;

                              run("Clear", "slice");


////MAGIC needs to happen here//////

//turns cut image into binary
run(“Make Binary”);
//analyse skeleton peices
run(“Analyze Skeleton (2D/3D)”, “prune=[shortest branch] display”);
selectWindow(“Tagged skeleton”);
waitForUser(“check isolation”)
// isolate and save image
run(“Make Binary”);
saveAs(“tiff”, path+ fileName+"_skeleton"+u);

Thanks for your help!!!.

I have included a link to a sample image for people to use.


I might be missing something but it looks like you should exit the while-loop once you click the right mouse button. Does that not work?

Some stylistic observations. It seems to me that checking for a different z-value is pointless as you are operating on a single-slice image. Therefore, that value will never change (it should always be 0). Given the current structure of your code, I am also not convinced that the “Circle on mouse click” needs to be a separate macro.


That is the issue, i don’t know how to exit the loop on right click. When i hit right click, it ends the entire macro completely and does not proceed to the next set of code.

As for the circle on mouse click thing, I copied it from other code and I am trying t get it to work in my code… so it may not be the best way to use it…

I am still very new to this kind of stuff so I don’t know about optimization.


Perhaps the issue is that you have it enclosed as a separate macro? I am not sure but try just removing “macro CircleOn_MouseClick {” and the corresponding “}”.


OMG IT WORKED!!! Thank you soo much!!!

I still have a lot to learn!

thank you again!