Roi restore selection

imagej

#1

Goodmorning

I have two sets of images: set 1 is made of original images with a ROI selected, set 2 by binarized images without ROI. Each image of set 1 has got the same name of the correspondent image in set 2. How can I restore the ROI from each image of set 1 to correspondent in set 2 in an automatic fashion?

Thanks in advance


#2

The macro language has several ROI functions. You can use those to select the ROI from set 1 and use this in set 2.

One of the functions is
Roi.getName

This gets the name of the ROI. In this case you can make an library (key, value) with the specific file name as key and the value for the ROI name. You can return the ROIname whenever you want to select that in the future during your image analysis.


#3

Hi @a.rabiolo ,

I tried to build a simple macro showing you how to use the “Restore selection” command of imageJ:

run("Blobs (25K)");
makeRectangle(77, 32, 88, 98);
imageName1 = getTitle();

run("Blobs (25K)");
imageName2 = getTitle();

// at this point, we have two images open and the first one has an ROI


// select the second image
selectWindow(imageName2);

// and transfer the ROI
run("Restore Selection");

Does that work?

Cheers,
Robert


#4

Dear Robert
Actually I have two sets of images. Each set is made of about 20 images. Each image has got a unique ROI manually defined before. I would like to restore the ROI if the 2 images are correspondent (they have same name, but different extension).


#5

Huh,

I don’t know how a “set of images” looks like. Is it a folder with image files? How convenient are you with ImageJ macro programming? You would need functions for opening all images in a folder (getFileList() and open()). You could then switch from a file ending with something (endsWith(filename, "_a.tif)) to another file ending similarily like this:

selectWindow(filename);
selectWindow(replace(filename, "_a.tif", "_b.tif"));
run("Restore Selection");

You can also consider replacing the selectWindow function in this example with the open function…

https://imagej.nih.gov/ij/developer/macro/functions.html

I hope that helps.

Robert


#6

As far as I understand the problem, this macro should do what you want:

// Choose your input directory
dirInput = getDirectory("Choose your input directory");

// Loop through all files in the directory
setBatchMode(true);
files = getFileList(dirInput);
for (i = 0; i < files.length; i++) {
	fileName = files[i];
	// Only process TIFF files
	if (endsWith(fileName, ".tif")) {
		// Look for a corresponding PNG file
		fileName2 = replace(fileName, ".tif", ".png");
		// Warn if no PNG file found
		if (!File.exists(dirInput + fileName2)) {
			print("Warning! No matching PNG file found for " + fileName);
			continue;
		}
		// Open both files & transfer ROI
		print("Transferring ROI from " + fileName + " to " + fileName2);
		open(dirInput + fileName);
		idOrig = getImageID();
		open(dirInput + fileName2);
		run("Restore Selection");
		// Save the PNG file - need to save as TIFF to include ROI
		saveAs("Tiff", dirInput + fileName2 + "-binary.tif");
		// Close both images
		close();
		selectImage(idOrig);
		close();
	}
}
setBatchMode(false);

This assumes your binary images are PNG and your other images (with ROIs) are TIFF.

With that in mind, here’s a bonus macro that generates data in this format for testing:

// Choose your output directory
dirOutput = getDirectory("Choose your output directory");

// Get an image stack
run("Confocal Series (2.2MB)");
Stack.setChannel(2)
run("Delete Slice", "delete=channel");
run("Gaussian Blur...", "sigma=2 stack");

// Loop through slices, getting an example with a ROI (TIFF) 
// and a binary image as well
idOrig = getImageID();
setBatchMode(true)
for (slice = 1; slice <= nSlices(); slice++) {
	run("Select None");
	setSlice(slice);
	run("Duplicate...", " ");
	idDuplicate = getImageID();

	setAutoThreshold("Otsu dark");
	run("Create Selection");
	saveAs("Tiff", dirOutput + "test_image_" + slice + ".tif");

	run("Select None");
	setAutoThreshold("Triangle dark");
	run("Create Mask");
	saveAs("PNG", dirOutput + "test_image_" + slice + ".png");
	
	close();
	selectImage(idOrig);
}
setBatchMode(false)