How to combine masked sub images of ROI Manager into one image

fiji
imagej
macro
Tags: #<Tag:0x00007fb87e2753d8> #<Tag:0x00007fb87e275270> #<Tag:0x00007fb87e2750e0>

#1

I have image generated with native command like this:

run("Blobs (25K)");
run("Duplicate...", "title=ImageCOPY");
setAutoThreshold("Default");
run("Analyze Particles...", "display exclude include summarize add");

z=9 // This is ROI id
//z=5
setSlice(1);
run("Duplicate...", "title="+z);
roiManager("Select", z);
setAutoThreshold("Yen");
setThreshold(190, 249);
run("Analyze Particles...", "size=0-Infinity show=Masks display exclude include summarize");
selectWindow("Mask of "+z);

The idea is to iterate each member of ROI and then masked them then put it into one image.
Currently with ROI ids z=5 and z=9. I get this image separately.

How can I combine all ROI ids z from ROI manager into one image?

I tried the following loop but the final image is black.

////TRY LOOPING THROUGH THE ROI MANAGER HERE
for (z=0;z<roiManager("count"); z++){

	setSlice(1);
	run("Duplicate...", "title="+z);
	roiManager("Select", z);
	setAutoThreshold("Yen");
    setThreshold(190, 249);
	run("Analyze Particles...", "size=0-Infinity show=Masks display exclude include summarize");
	selectWindow("Mask of "+z);
	run("Copy");
	close();
	selectWindow(""+z);
	close();

    selectWindow("ImageCOPY");
    setSlice(2);
	setPasteMode("Divide");
    run("Paste");
}

run("Select None");
rename("Composite Image");
saveAs("Jpeg", "/Users/pdubois/Desktop/CompositeImage.jpg");

#2

Perhaps you could keep your mask windows open during the loop, then make them into a stack and make a maximum-intensity projection.

In this macro I’ve created 2 different masks, as well as an unwanted image, to illustrate how you can select images by name (using the title parameter) to convert to a stack.

The MAX_Stack image has the combined particles from all the “blobs” images.

Hope it helps.

run("Blobs (25K)");
setOption("BlackBackground", true);
run("Make Binary");
run("Duplicate...", " ");
run("Duplicate...", "title=unwanted_image.gif"); // will not be included in the stack
selectWindow("blobs-1.gif");
run("Rotate... ", "angle=90 grid=1 interpolation=Bilinear enlarge"); // make a different particle pattern
run("Images to Stack", "method=[Copy (center)] name=Stack title=blobs use"); // only windows with "blobs" in the name will be included 
run("Z Project...", "projection=[Max Intensity]");

#3

Usually the way to combine two binary images into one is using the Image Calculator with the OR operator:

imageCalculator("OR create", "Mask of 9","Mask of 5");

#4

Hi Wayne and imagejan,

Thank you for your suggestion, but I need to go through loop. Because I want to perform Threshold and Analyze Particle to count particle for each blob. How can I do it?

Essentially, I want to replicate this process, but with black and white image run("Blobs (25K)");.


#5

Hi @Peverall_Dubois ,

BTW, I’m not Wayne – sorry if the handle is confusing.

The strategy I recommended above should work with your loop. You need to adapt it to your code. Remove the commands to close the Masks, and then after your measurement loop is over, stack and project the masks.

Alternatively, you could use the Image Calculator as @imagejan suggested. It works on only 2 images at a time, as far as I know. So to use that method, you would need to make another loop to “OR” the masks, or even better, do it within the measurement loop as you go along.

Either method should work for your purpose.