Rotate ROI outline with selected object

Hello all,

I recently posted a macro I wrote that rotates selected objects using the ferets angle - see link.

Briefly this macro: 1) selects an object using a threshold; 2) rotates the wing on its’ ferets angle; 3) re-selectes the wing using another threshold (which is where the issue lies); 4) measures three sections of the re-selected and rotated object.

However, in this macro (at step 3) I reselect the wing and make a new ROI using a threshold method.
However, for some images the threshold method does not work well on the rotated image. Perhaps because that threshold has already been applied.

This leads me to my question:
Is there a way to rotate the ROI outline of the selected image with the image?? - i.e. the outline would move with the object when rotated and update in the ROI manager.
OR
Is there a way to remove a previously applied threshold? SO I can then use the same original threshold to re-sekect the rotated wing?

Example images and the macro I am using can be found following the link provided (instead of re-posting it all again).

Thank you for your help in advance.
Eve

Hi @EveTC

Indeed:
you can rotate the image (in this example,15 degress), first removing the ROI:

run("Select None");
run("Rotate... ", "angle=15 interpolation=Bilinear");

Then restore the ROI, and rotate the ROI by the same amount, around the center of image:

run("Restore Selection");
run("Rotate...", "rotate angle=15");

Cheers,
Nico

Hi @NicoDF

Once I have run this, when I then re-click on the ROI in the ROI manager the outline is still where the wing was previously located?

Also this is probably another issue but once rotated, some of the object is cut off the image- is there a way to stop this or do I need a different image?

Ignore the first issue @NicoDF - I can just add the new ROI. Sorry I am still getting use to ImageJ/FiJi.
Is there a way around the cutting some of the image out issue?

Thank you so much @NicoDF :smiley:

Could you post an example of this?

Yes of course.

For example I rotate this image by 45.36 (ferets angle):

Using this code

run("Select None");
run("Rotate...", "angle=45.36 interpolation=Bilinear fill");
run("Restore Selection");
run("Rotate...", "rotate angle=45.36");

Which results in this:

Is there anyway to stop it cutting off the left edge?

Thank you so much for your help.

Oh, I see! In order to avoid clipping, you should translate the object (and ROI) to the center of the image before rotating (as long as it fits within a circle of diameter = min(img_height, img_width))

This snippet should do it. You can use the one-line version, but will have to deal with the black border.

List.setMeasurements; // alternative to measure-getResult, no table involved
dx = round(getWidth()/2 - parseInt(List.get("X")));
dy = round(getHeight()/2 - parseInt(List.get("Y")));
roi_x = parseInt(List.get("BX"))+dx;
roi_y = parseInt(List.get("BY"))+dy;
feret_angle = parseInt(List.get("FeretAngle"));

//run("Translate...", "x=&dx y=&dy interpolation=None"); //this is the "standard" way, but for RGB the next lines actually use the background color	

run("Select All");
run("Cut");

run("Restore Selection"); //juggling the original roi, in other contexts can be called from the ROI manager
run("Select None");

run("Select All");
Roi.move(dx, dy);
run("Paste");

run("Restore Selection"); //juggling again
run("Select None");

run("Rotate...", "angle=&feret_angle interpolation=Bilinear fill");

run("Restore Selection");
Roi.move(roi_x, roi_y);
run("Rotate...", "rotate angle=&feret_angle");

I set the background color to the one you used, so that it matches after transforming.

Cheers,
Nico

2 Likes

@NicoDF - you are amazing!

Thank you so much, you have saved me a lot of time and confusion.
Eve

1 Like