Automated addition to ROI manager. Any sample code?

fiji
imagej
macro

#1

I am trying to develop a code to add a selection to ROI with macro language code. I used for loop to add one by one but it does only add the second one. here is the code:

for (j=1;j<2; j=j+1){
	yn="y"+j;
	xn="x"+j;
  x1 = newArray(nResults);
  y1 = newArray(nResults);
  size=nResults;
for (ii=0; ii<size; ii++) {
y1[ii] = getResult(xn, ii);
x1[ii] = getResult(yn, ii);
}
     
makeSelection("freehand", x1, y1);
roiManager("Add");

}
}
close(); ```

#2

Hi @Zeynab_Mousavi,

your outer for-loop starts at 1 (so x1 and y1 are processed) and continues as long as j < 2. Thus, it ends immediately after one turn. Furthermore, your code contains some errors in the last lines. There is a curly bracket too much closing.

Try this snippet:

for (j = 1; j <= 2; j = j + 1){
	yn = "y" + j;
	xn = "x" + j;
	x1 = newArray(nResults);
	y1 = newArray(nResults);
	size = nResults;
	for (ii = 0; ii < size; ii++) {
		y1[ii] = getResult(xn, ii);
		x1[ii] = getResult(yn, ii);
	}
	
	makeSelection("freehand", x1, y1);
	roiManager("Add");
}

I also added a lot of spaces to make it easier to read. The human brain is made for reading sentences with spacesbetweenwords. :wink:

Furthermore, your code would become easier to read here in the forum if you put ``` above and below code blocks.

Cheers,
Robert


#3

Thanks so much for your notes and quick helps. I actually tried j<=4 in my code but forgot to edit it here and this did not solve the problem. I tried your changes but again I had the same issue. I think I need to add a line of “selection” but dont know how.
By the way I edited the code with ```. Great idea!


The active image does not have selection
#4

So my test CSV file looks like this:

image

This is the code I run:

for (j = 1; j <= 2; j = j + 1){
	yn = "y" + j;
	xn = "x" + j;
	x1 = newArray(nResults);
	y1 = newArray(nResults);
	size = nResults;
	for (ii = 0; ii < size; ii++) {
		y1[ii] = getResult(xn, ii);
		x1[ii] = getResult(yn, ii);
	}
	
	makeSelection("freehand", x1, y1);
	roiManager("Add");
}

And afterwards my ROI manager looks like that:
image

How does your table/roi-manager/code look like? It’s hard to guess what goes wrong if we don’t see what you see :wink:


#5

Here is what looks like:
image
If I run the program with no open image, it will get error. So, I just open my first image and then run the program.
Here is the code just in case:
image


#6

Could you also show us the csv file please? Just drag&drop it on Fiji…


#7

It is a messy long csv file like this:


Do you need the whole file?


#8

Maybe I indeed need the whole file. I guess it has something to do with it. It’s working for x1/y1 and the contour makes sense?


#9

@haesleinhuepf Yes it works for x1/y1 perfectly.


#10

@haesleinhuepf But it does not work for more than one.


#11

Would you mind sharing the csv file? Otherwise it’s hard to guess…


#12

@haesleinhuepf ola.csv (50.7 KB)
By the way I have an other question. Would you please help me with that? here is the link: How to add a string to a path of file which is changing in each loop in imagej macro language?


#13

Hey @Zeynab_Mousavi,

I guess it has something to do with the different length of the polygons. Furthermore, it’s thousands of points. Do you see a chance to simplifying the polygon before exporting it to csv? Alternatively, would it be possible to export the segmentation as binary image? It might be easier to export/import it.

Cheers,
Robert


#14

Thanks for your quick response. What would be the problem of having too many point? and how do you say the current file is/is not binary? Because I have certain points that show the edge so we can assign those points to be black or white.


#15

Dear Mousavi,

If the number of points is not equal for all ROIs the non existing points will become NaN, so you should check for this. Try:

for (j = 1; j <= 3; j = j + 1){
yn = “y” + j;
xn = “x” + j;
x1 = newArray(nResults);
y1 = newArray(nResults);
size = nResults; print(size);
for (ii = 0; ii < size; ii++) {

	if(isNaN(getResult(xn, ii))==1) continue;
	
	y1[ii] = getResult(xn, ii); 
	x1[ii] = getResult(yn, ii);
	
	print(y1[ii],x1[ii]);
}

makeSelection("freehand", x1, y1);
roiManager("Add");

}

Best wishes

Kees

Dr Ir K.R. Straatman
Senior Experimental Officer
Advanced Imaging Facility
Centre for Core Biotechnology Services
University of Leicester
www.le.ac.uk/advanced-imaging-facility