Why ROI is not selecting based on the name?

Here is the ROI:
1
and I want to delete “quarter_1” from the ROI.
I use this code:

j=1;
symmetry_type="quarter";
Roiname=symmetry_type+"_"+j; 
	roiManager("select",Roiname);

But it does not select the “quarter_1” and it selects the first item in the ROI.
Any idea to solve the issue/

Hi there.

The ‘select’ command from the RoiManager only takes indexes (0,1,2,…) as per the documentation in Help > Macro Functions...

Selects an item in the ROI Manager list, where index must be greater than or equal zero and less than the value returned by roiManager(“count”) .

If you want to select Rois by name, you would need to make your own function ( Or add this functionality to the RoiManager).

We had such a need a while back and made these functions that return the index of the roi with the given name.

/* 
 * Returns index of first ROI that matches  
 * the given regular expression 
 */ 
function findRoiWithName(roiName) { 
	nR = roiManager("Count"); 
 
	for (i=0; i<nR; i++) { 
		roiManager("Select", i); 
		rName = Roi.getName(); 
		if (matches(rName, roiName)) { 
			return i; 
		} 
	} 
	return -1; 
} 
 
/* 
 * Returns an array of indexes of ROIs that match  
 * the given regular expression 
 */ 
function findRoisWithName(roiName) { 
	nR = roiManager("Count"); 
	roiIdx = newArray(nR); 
	k=0; 
	clippedIdx = newArray(0); 
	 
	for (i=0; i<nR; i++) { 
		roiManager("Select", i); 
		rName = Roi.getName(); 
		if (matches(rName, roiName) ) { 
			roiIdx[k] = i; 
			k++; 
		} 
	} 
	if (k>0) { 
		clippedIdx = Array.trim(roiIdx,k); 
	} 
	 
	return clippedIdx; 
} 

So using these functions your code could look like this

j=1;
symmetry_type = "quarter";
roi_name =symmetry_type +"_" + j; 
roi_idx = findRoiWithName( roi_name );
roiManager( "Select", roi_idx );
...

Best

3 Likes

@oburri: Thanks Olivier. I have not used functions in imagej j macro. Should I add it to the end of current macro? or separately to an other macro?

Hi @Zeynab_Mousavi,

Yes, you can add them at the bottom of your code. The whole file is parsed before running. Glad it helps!

1 Like

@oburri: Was very helpful. Thanks so much Olivier.

1 Like