ROI manager "Apply changes to all selections" problem

Hello, I made a script that is outlining my embryos and scanning mean intensity of the flow inside several ROIs. It supposes to pick up “.tif” files from folders and save them to a subfolder with the same name automatically, but the window “Apply changes to N selections?” keeps poping up from ROI manager each second file. Can someone help me out with that please?

/*
 * Macro template to process multiple images in a folder
 */

#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
		
	}
}

function processFile(input, output, file) {
	// Do the processing here by adding your own code.
	// Leave the print statements until things work, then remove them.
	//Angle measurement
	setBatchMode(true);
	open(input);
	selectImage(1);
	 imagename=File.nameWithoutExtension;
	 File.makeDirectory(output +File.separator+imagename);
	run("Deinterleave", "how=3 keep");
close();
selectImage(2);
rename("#1");
selectImage(3);
rename("#2");
run("Merge Channels...", "c1=#1 c2=#2 create");
run("Re-order Hyperstack ...", "channels=[Frames (t)] slices=[Channels (c)] frames=[Slices (z)]");
rename("Hyperstack");
run("Z Project...", "projection=[Max Intensity] all");
run("8-bit");
rename("Zstack");
run("Z Project...", "projection=[Max Intensity]");
rename("Zstack_1");
run("Duplicate...", " ");
run("Gaussian Blur...", "sigma=2");
run("Auto Threshold", "method=Triangle white");
//cycle for finding ROI
checker = 0;
doWand(checker, checker);
run("Measure");
n =0;
a1 = getResult("Area", n);
b=pow(0.1, 7);
areaMEASURE = 5.0195308 * b;
while(abs(areaMEASURE - a1) / areaMEASURE > 0.2)
{
doWand(checker/2 + checker, checker);
checker = checker +50;
n=n+1;
run("Measure");
a1 = getResult("Area", n);
}
run("ROI Manager...");
roiManager("Add");
roiManager("Select", 0);
run("Fit Ellipse");
roiManager("Add");
roiManager("Select", 1);
roiManager("Measure");
roiManager("Deselect");
roiManager("Set Color", "yellow");
roiManager("Set Line Width", 20);
selectImage("Zstack_1");
roiManager("Select", 0);
roiManager("Draw");

//Second half

run("Clear Results");
selectImage("Zstack");
frames1=nSlices;
meanint = 0;
roiManager("Select", 1);

getSelectionCoordinates(xpoints, ypoints);
coordrrX=xpoints;
coordrrY=ypoints;

if(coordrrX[0]<coordrrX[coordrrX.length/2])
{
	Array.rotate(coordrrX, round(coordrrX.length/2));
	Array.rotate(coordrrY, round(coordrrX.length/2));
}

Array.print(coordrrX);
Array.print(coordrrY);
 	makeSelection(1, coordrrX, coordrrY);
	run("Scale... ", "x=0.1 y=0.1");
	run("Rotate...", "  angle=90");
	setSelectionLocation(coordrrX[round(coordrrX.length/4)], coordrrY[0]);
    run("Measure Stack...");
    for(i=0;i<frames1;i++)
    {
    	meanint = meanint + getResult("Mean", i);
    }
meanint=meanint/frames1;
print("meannt= " + meanint);
// second half
run("Clear Results");
 	makeSelection(1, coordrrX, coordrrY);
	run("Scale... ", "x=0.1 y=0.1");
	run("Rotate...", "  angle=90");
	setSelectionLocation(coordrrX[10], coordrrY[0]);
	run("Measure Stack...");
	counter = 0;
	for(i=0;i<frames1;i++)
    {

    	counter = counter + getResult("Mean", i);
    }
counter=counter/frames1;
print("counter= " + counter);
	if ( counter > meanint)
	{ 
	print("Anterior");



	for(i=0; i<=3;i++)
	 { 

			if (i==3)
				{
					selectImage("Zstack");
 					fitx= Array.getSequence(nSlices);
					fity= newArray(nSlices);
 					makeSelection(1, coordrrX, coordrrY);	 	
					run("Scale... ", "x=0.40 y=0.50");
					run("Rotate...", "  angle=90");
					setSelectionLocation(coordrrX[round(coordrrX.length*0.45)], coordrrY[round(0.65*coordrrY.length)]);
					
					run("Clear Results");
					run("Measure Stack...");
					fity= Table.getColumn("Mean", "Results");
					 Fit.doFit("8th Degree Polynomial", fitx, fity);
					Plot.create("Starting point "+ i, "x", "Mean");
					Plot.add("Line ", Table.getColumn("Mean", "Results"));
					Plot.setStyle(0, "blue,#a0a0ff,1.0,Line");
					Plot.show();
					save(output +File.separator+imagename+ File.separator +  "Start point"+ i+"_.tif");
					Fit.plot;
					save(output +File.separator+imagename+ File.separator + i + "_fit.tif");
				}
			else{
				selectImage("Zstack");
 				fitx= Array.getSequence(nSlices);
				fity= newArray(nSlices);
 				makeSelection(1, coordrrX, coordrrY);
 	 			getSelectionBounds(xRECT, yRECT, widthRECT, heightRECT);
 				makeRotatedRectangle(xRECT, yRECT, xRECT+widthRECT, yRECT, heightRECT);
				run("Scale... ", "x=0.40 y=0.25");
				run("Rotate...", "  angle=90");
				setSelectionLocation(coordrrX[round(coordrrX.length/4)+i]-heightRECT*0.25, coordrrY[round(0.60*coordrrY.length)]);
				run("Clear Results");
				run("Measure Stack...");
				roiManager("Add", "ffff00", 0);
				
					fity= Table.getColumn("Mean", "Results");
		 		Fit.doFit("8th Degree Polynomial", fitx, fity);
				Plot.create("End point "+ i, "x", "Mean");
				Plot.add("Line ", Table.getColumn("Mean", "Results"));
				Plot.setStyle(0, "blue,#a0a0ff,1.0,Line");
				Plot.show();
				save(output +File.separator+imagename+ File.separator +  "End points "+ i+"_.tif");
				Fit.plot;
				save(output +File.separator+imagename+ File.separator + i + "_fit.tif");
}
	 }



	
	}




	
	else
	{

	for(i=0; i<=3;i++)
	 { 

			if (i==3)
				{
					selectImage("Zstack");
 					fitx= Array.getSequence(nSlices);
					fity= newArray(nSlices);
 					makeSelection(1, coordrrX, coordrrY);	 	
					run("Scale... ", "x=0.40 y=0.50");
					run("Rotate...", "  angle=90");
					setSelectionLocation(coordrrX[round(coordrrX.length*0.2)], coordrrY[round(0.83*coordrrY.length)]);
					
					run("Clear Results");
					run("Measure Stack...");
					fity= Table.getColumn("Mean", "Results");
				roiManager("Add", "ffff00", 0);
					
					Fit.doFit("8th Degree Polynomial", fitx, fity);
					Plot.create("Starting point "+ i, "x", "Mean");
					Plot.add("Line ", Table.getColumn("Mean", "Results"));
					Plot.setStyle(0, "blue,#a0a0ff,1.0,Line");
					Plot.show();
					save(output +File.separator+imagename+ File.separator +  "Start point "+ i+"_.tif");
					Fit.plot;
					save(output +File.separator+imagename+ File.separator + i + "_fit.tif");
				}
			else{
				selectImage("Zstack");
 				fitx= Array.getSequence(nSlices);
				fity= newArray(nSlices);
 				makeSelection(1, coordrrX, coordrrY);
 	 			getSelectionBounds(xRECT, yRECT, widthRECT, heightRECT);
 				makeRotatedRectangle(xRECT, yRECT, xRECT+widthRECT, yRECT, heightRECT);
				run("Scale... ", "x=0.40 y=0.25");
				run("Rotate...", "  angle=90");
				setSelectionLocation(coordrrX[round(coordrrX.length/4)-i], coordrrY[round(0.6*coordrrY.length)]);
				
				run("Clear Results");
				run("Measure Stack...");
				
					fity= Table.getColumn("Mean", "Results");
				roiManager("Add", "ffff00", 0);
				
		 		Fit.doFit("8th Degree Polynomial", fitx, fity);
				Plot.create("End points "+ i, "x", "Mean");
				Plot.add("Line ", Table.getColumn("Mean", "Results"));
				Plot.setStyle(0, "blue,#a0a0ff,1.0,Line");
				Plot.show();
				save(output +File.separator+imagename+ File.separator + "End points "+ i+"_.tif");
				Fit.plot;
				save(output +File.separator+imagename+ File.separator + i + "_fit.tif");
}
	 }
		}
		
selectImage("Zstack_1");
rename(File.nameWithoutExtension + "___1");
save(output +File.separator+imagename+ File.separator + File.nameWithoutExtension + "___1.tif");
selectImage("Zstack");
rename(File.nameWithoutExtension + "___2");
save(output +File.separator+imagename+ File.separator + File.nameWithoutExtension + "___2.tif");
//selectImage("PLOT");
//save(output);
run("Clear Results");
run("Close All");
roiManager("deselect");
roiManager("delete");
close("ROI manager");
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output);
}

Hi @11129,
Could it be that you only want one selection to change and have all/none/multiple selections active in the RoiManager, or did not apply a change?
A sample image or line number where you get the error would make things easier.

Dear eljonco,

I think it might be exactly what you mentioned, can you write down the code that will solve this, please?

I was talking about an input image to test your code on, not a screenshot of your code :slight_smile: If you step through the code, you will know where the error occurs. That should allow you to put in a command that either performs the change to N selections, or selects one ROI or allows you to remove the offending command…

moment, uploading them

https://drive.google.com/open?id=19anu1mbw1lfG1q37SuNPCjHzqGnbenhm
https://drive.google.com/open?id=1HVqWw-d82Wa5_l22WgaPvLmGvocLHwSQ
https://drive.google.com/open?id=1ejrXsfqVdVW2L5H-wJqQjlfYjDDINO3f
https://drive.google.com/open?id=1-jNwD0D5XNa7HAx6Drx0yfB4TMYdagZU

put them all in the same folder please

Dear Eljonco,

I added my samples, could you be so kind and help me?

https://forum.image.sc/t/roi-manager-apply-changes-to-all-selections-problem/26383/5

With best regards,

Nikita

I would love to, so I just tried. After downloading the images, loading the macro in Fiji (which isn’t just ImageJ) and trying to debug in Fiji, that fails. When I try to run the macro with a few adaptations in ImageJ1, ImageJ crashes with the following error, so I think you are left on your own, at least as far as I’m concerned; sorry.
Error:

Process:               JavaApplicationStub [19351]
Path:                  /Applications/ImageJ's Fiji/*/ImageJ.app/Contents/MacOS/JavaApplicationStub
Identifier:            gov.nih.info.rsb.ImageJ
Version:               10.2
Code Type:             X86-64 (Native)
Parent Process:        ??? [1]
Responsible:           JavaApplicationStub [19351]
User ID:               502

Date/Time:             2019-06-09 19:34:53.054 +0200
OS Version:            Mac OS X 10.12.6 (16G29)
Report Version:        12
Anonymous UUID:        2532FC5B-A4A0-58A7-D068-3B92FC3BE146

Sleep/Wake UUID:       439CAF57-8BB0-47B2-9F88-DA3A0B9730F5

Time Awake Since Boot: 840000 seconds
Time Since Wake:       1900 seconds

System Integrity Protection: enabled

Crashed Thread:        17  Java: Java2D Disposer

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000047
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [0]

VM Regions Near 0x47:
--> 
    __TEXT                 0000000100000000-0000000100001000 [    4K] r-x/rwx SM=COW  /Applications/ImageJ's Fiji/*/ImageJ.app/Contents/MacOS/JavaApplicationStub

Application Specific Information:
Java information:
 Exception type: Bus Error (0xa) at pc=7fffb07a6a16
 
 Java VM: Java HotSpot(TM) 64-Bit Server VM (20.65-b04-468 mixed mode macosx-amd64)