[macro-ImageJ] roiManager "the image doesn't have a selection"

Hello everyone,
I am writing a macro to analyse in repetitive manner some plants in tray pots.

The macro first threshold the images according the plants, make a selection and add it to the roiManager (ROI number 0).

Then it create different rectangle (one for each pot in the tray) and add it to the roiManager (ROI number 1,2,…x).

Then it select always the ROI number 0 and ADD it to the rectangles made previously.

Some time, in some pots there isn’t a plant so while going through it’s work, the macro stop and say “the image does not have a selection”.

I imagine that I can solve my problem using if/else and tell the macro to switch to the next images, but I have not idea which condition use.

Hereafter I report the code:

run("Color Threshold..."); // thresholding
[.....recorded threshold....]
run("Create Selection"); roiManager("Add"); close();

//create the rectangle on pot position
for (i = 0; i < 6; i++) { 
	x=100;
	y=200-(i*120);
	while(x>580){
		makeRectangle(x, y, 80, 80);
		roiManager("add")
		x=x-84;				
	}
}

//And here the for cycle that give me inconvenience
nROI=roiManager("count")
for (i = 1; i <= nROI; i++) {
	roiManager("select", newArray(0,i));
	roiManager("and");
	roiManager("add");
}

Thank you for the help,
Regards!
L.

[EDIT]: added images

Hi @kost
Could you post an image ?

Hi,

simple just use:

if (nROI > 0) {

print("ROI found");
for (i = 1; i <= nROI; i++) {
    roiManager("select", newArray(0,i));
    roiManager("and");
    roiManager("add");
    }
} else {

print("No ROI found");

}

Unfortunatly it does not solve the problem. When the macro do the AND between the plant threshold and the position where there isn’t a plant, the result of the AND is null so when it try to add it still return “the active image does not have a selection”

Do you think is possible to deal with dialogbox of the roimanager or with the result of the AND? Like
a) detect the opening of a dialog box with “the active image does not have a selection”
b) detect that the ROI resulting from the AND operation is 0

PS: I attach an image where the macro block.

[EDIT: I add the full code and also an original images]

setBatchMode(true)

run("Duplicate...", "title=z");
selectImage("z");
min=newArray(3);max=newArray(3);filter=newArray(3);a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoLab");
run("RGB Stack");run("Convert Stack to Images");
selectWindow("Red");close();
selectWindow("Green");close();
selectWindow("Blue"); rename("2");
min[2]=142;max[2]=255;filter[2]="pass";
  selectWindow(2);
  setThreshold(min[2], max[2]);
selectImage("2"); 
run("Create Selection");
roiManager("Add"); 
close();


for (i = 0; i < 6; i++) {
	x=1056;
	y=1324-(i*175);
	while(x>460){
		makeRectangle(x, y, 82, 88);
		roiManager("add");
		x=x-84;				
	}
}

nROI = roiManager("count")

for (i = 1; i <= nROI; i++) {
    roiManager("select", newArray(0,i));
    roiManager("and");
    roiManager("add");
}

Cheers!
L.
as

Could you explain why you do this operation?
What is the goal to get these two ROIs combined?

Maybe there is easier way around this.

Cheers,
Christopher

Hey yes for sure,

I am trying to write a macro for some experiments that will be repeated.
The macro should evaluate for each plant present in the pots the area and some RGB indexes. Some time, due to the treatment, the plant might not grow and the macro should be able to return a NA value for the empty pots or just pass to the next.

Right now I “bypassed” the issue in a inelegant way by adding a dialog box at the beginning of the script where to insert in which pot plants are missing.

Do you know if it is possible to save selection polygon as variable? So I could avoid using the roiManager

Btw thanks for the interest, meanwhile I will continue to search for an alternative.

Regards,

L.

Ah ok so you need a proper readout for absent plants.

I think within the macro solution manually outlining the missing plants and adding them to the ROI manager is an ok solution. The more fine grained control over the ROIs will only be possible in a proper scripting language. You could save ROIs as different sets and reload them again. If you worry to get ROIs that are false positives you can always try to remove them by the size and circularity filter of the Particle Analyzer.

I still don’t understand why you need the following commands then? This is solving a more specific problem I suspect:

roiManager("and");
roiManager("add");

I think just having manual ROIs and the automatically generated ones and then do the measurement in all of them should be fine…

For a more automatic detection you could try the weka machine learning to segment the earth as well. It should be very different from the “no earth” and “plant” containing pots: https://imagej.net/Trainable_Weka_Segmentation

Hi
@kost
I propose a solution with morpholib.
The result must be improved!

Hi guys,
Thanks for the answer! I am finishing some work for a deadline and soon I will be back on this problem and will try to post a solution :muscle:

@Mathew
Thanks you it does look promising for avoiding to add manualy which pot is empty

@schmiedc
I do “and” + “add” to select a specific plant according the pot in the tray (it probably doesn’t sound clear with this word so I am going to publish a picture later to explain more correctly ))

Regards,
L

1 Like

Hello everyone!
So I managed to “explain” to the macro how it have to deal whereas there is not a plant.
I used the selectiontype() comand.
The selection type return the type of selection, in my case I use it after the AND, if there is no selection (because of an absence of plant) it does return -1
In the case the AND produce a selection (because there is a plant in the subseted ROI) it will return a value different than -1.

I attach the code with one image if you want to see how it work (you can just run it, I left all the ROI produced so if you scroll them you will see what the macro does).

setBatchMode(true);
run("ROI Manager...");

run("Duplicate...", "title=z");
selectImage("z");
min=newArray(3);max=newArray(3);filter=newArray(3);a=getTitle();
call("ij.plugin.frame.ColorThresholder.RGBtoLab");
run("RGB Stack");run("Convert Stack to Images");
selectWindow("Red");close();
selectWindow("Green");close();
selectWindow("Blue"); rename("2");
min[2]=142;max[2]=255;filter[2]="pass";
  selectWindow(2);
  setThreshold(min[2], max[2]);
selectImage("2"); 
run("Create Selection");
roiManager("Add"); 
close();

for (i = 0; i < 6; i++) {
	x=1056;
	y=1324-(i*175);
	while(x>460){
		makeRectangle(x, y, 82, 88);
		roiManager("add")
		x=x-84;				
	}
}


nROI = roiManager("count");
for (i = 1; i < nROI; i++) {
	roiManager("select", newArray(0,i));
        roiManager("and");
        type=selectionType();
            if (type==-1) {
		print("null");
         	} else {
    			roiManager("add");
   			}
}

Thank for the help, with this work I learnt correctly use the if/else and organize the order of the command inside a loop cycles.

Cheers!

L.

3 Likes

Hi
@kost @schmiedc
My macro is not great. I learn…
I am waiting for remarks. Thank you in advance.

img=getImageID();
run("Duplicate...", "title=temp");
close("\\Others");
//setTool("rectangle");
makeRectangle(434, 353, 1643, 1094);
for(i=0;i<2; i++)
{
run("Duplicate...", "title="+"0"+i);
}
selectImage("01");
run("Gaussian Blur...", "sigma=5");
run("8-bit");
run("Statistical Region Merging", "q=2 showaverages");
//run("Threshold...");
setAutoThreshold("Mean");
run("Convert to Mask");
run("Fill Holes");
run("Analyze Particles...", "add");
close("01 (SRM Q=2.0)");
close("01");
roi_counts=roiManager("count");
for(i=0;i<roi_counts;i++)
{
selectImage("00");
roiManager("select",i);
run("Duplicate...","title="+i);
roiManager("Rename", i);
run("Select None");
}
close("00");
close("temp");
run("Tile");
roiManager("reset");
wait(5000);

//------------------------------------

for(j=0;j<2;j++)
{
selectWindow(j);
Apply();
}
close("Summary");
close("Results");
close("Log");
close("Roi manager");
run("Tile");

function   Apply()
{
run("32-bit");
run("Morphological Segmentation");
wait(3000);
//setTool("multipoint");
call("inra.ijpb.plugins.MorphologicalSegmentation.setInputImageType", "object");
call("inra.ijpb.plugins.MorphologicalSegmentation.setGradientRadius", "1");
call("inra.ijpb.plugins.MorphologicalSegmentation.setGradientType", "Morphological");
wait(1000);
call("inra.ijpb.plugins.MorphologicalSegmentation.segment", "tolerance=6.0", "calculateDams=true", "connectivity=4");
wait(10000);
call("inra.ijpb.plugins.MorphologicalSegmentation.setDisplayFormat", "Watershed lines");
call("inra.ijpb.plugins.MorphologicalSegmentation.createResultImage");
wait(1000);
selectWindow("Morphological Segmentation");
close();

run("Erode");
run("Analyze Particles...", "size=6000-6220 display summarize add");
roiManager("Set Fill Color", "blue");
run("Flatten");
roiManager("reset");
}