Restore rectangle selection - macro

fiji
imagej
macro
Tags: #<Tag:0x00007fb8823e3ae0> #<Tag:0x00007fb8823e3928> #<Tag:0x00007fb8823e36d0>

#1

Hello everyone.

I am trying to copy a rectangle selection to another slice of a stack in a Fiji macro, but after other selections have been made, to add to the ROI Manager. Currently I am adding the rectangle selection to the ROI Manager also but this is producing unwanted results when it comes to measuring the ROIs.

As far as I am aware, Edit > Selection > Restore Selection cannot be used as other selections have been made since? Perhaps the SaveAs(xyCoordinates) could be implemented somehow, or to get the coordinates of the rectangle, and then use the makeRectangle function? This should be very simple.

Thank you in advance!


#2

Hi @camlloyd

Ok… so I haven’t really done this myself before - but just playing a bit with the Macro Recorder and a sample image - I think you can use this call after moving to your new slice:

roiManager("Deselect"); // ensure no other ROI is selected
roiManager("Select", 1); // pick the ROI you want to display/measure/etc.

Where the number (in this case 1) is the ROI you are interested in… (Note: the count starts with 0 in the ROI Manager).

Would you be able to use that call?

eta :slight_smile:


#3

Hi Eta,

thank you for your reply!

My apologies, I was trying not to flood this thread with code but I should have posted my current method beforehand. I have written it below, with some steps omitted.

To give some background, I am processing confocal microscopy files and measuring fluorescence intensities of specific ROIs. Before the intensities can be measured, some processing must be done, and this involves subtracting the background. I am trying to subtract the same region on both slices.

The problem with my current method is I don’t believe it is very efficient, as the ROIs created for the background subtraction selection will mix with my experiment results, producing unwanted entries in the results table. My macro also exports the results table to a CSV file so it would be good to find a solution to this.

Currently, this means that the rows in the results table that I do not need can be removed afterwards manually, or via a macro command.

My other option when I want to measure the oval ROIs is not to deselect the ROIs and then measure all, but to measure only the ROIs I want. However, as the number of ROIs will vary from image to image, I believe this is more difficult…

// Wait for user to select background for subtraction.
while (selectionType==-1)
    waitForUser("Select background for subtraction, then click OK.");

// Add background selection to ROI Manager.
roiManager("Add");

// Measure background fluorescence intensity.
run("Measure");

// Clear rectangle tool background selection.
run("Select None");

// Subtract background fluorescence intensity.
BG = getResult("Mean");
run("Subtract...", "value=BG slice");

// Set oval tool for ROI selection.
setTool("oval");

// Wait for user to add all selections to ROI Manager.
waitForUser("Add all selections to ROI Manager [t], then click OK.");

// Measure all ROI fluorescence intensities for 488 channel.
roiManager("deselect");
roiManager("Measure");

// Select background ROI (0);
roiManager("select", 0);
    
// Display slice 2 of the active stack (647 channel).
setSlice(2);

// Measure background fluorescence intensity.
run("Measure");

// Deselect background ROI (0).
roiManager("deselect");

// Clear rectangle tool background selection.
run("Select None");