setBatchMode(true) in ImageJ Macro produces different result

Hi all,

I put together an ImageJ macro to analyze cell area and skewness in single cells over time. It produces the expected result, however it is quite slow so I tried to speed it up by using:

setBatchMode(true);

in the beginning of the script (I made no other change).
It does indeed speed up, however the results are in this case for only half the segmented cells and every cell is measured twice. What could be the issue here and how do I fix it? Is there anything I need to pay attention to when using this command?

In case it is helpful, this is the macro I am using:

//clean up
roiManager("Reset");

//input file and output folder
run("Open...");
orgName =getTitle();
output_path = getDirectory("Choose output folder");

setBatchMode(true);

//illumination correction
run("Select None");
run("Duplicate...", "title=smoothed");
smoothed = getTitle();
run("Gaussian Blur...", "sigma=3");
run("Duplicate...", "title=illumination_correction_image");
illumination_correction_image = getTitle();
run("Gaussian Blur...", "sigma=100");
imageCalculator("Divide create 32-bit", smoothed, illumination_correction_image);
close(smoothed);
close(illumination_correction_image);

// create segmentation and selection mask
selectWindow("Result of smoothed");
run("Despeckle");
getStatistics(x, Mean, Min, Max, SD);
a = Mean + 0.5 * SD;
setThreshold(a, Max);
setOption("BlackBackground", true);
run("Create Mask");
run("Watershed Irregular Features", "erosion=1 convexity_threshold=0 separator_size=35-55 exclude");

//single cell analysis
run("Analyze Particles...", "size=20-70 display exclude clear add");
run("Clear Results");
selectWindow("mask");
close();
selectWindow("Result of smoothed");
close();
run("Set Measurements...", "area mean standard modal min integrated skewness kurtosis redirect=None decimal=3");
selectWindow(orgName);

//loop over cells
nROIs=roiManager("count");
for (ROI = 0; ROI < nROIs; ROI++) {
	selectWindow(orgName);
	roiManager("Select", ROI);
	run("Fit Rectangle");
	run("Enlarge...", "enlarge=1.5");
	run("Fit Rectangle");
	run("Duplicate...", "duplicate");
	currentROI = getTitle();
	print("the current ROI is:", ROI);

//loop over frames
	print("It has", nSlices, " slices.");
	setSlice(1);
	run("Duplicate...", "title=forStatistics");
	forStatistics = getTitle();
	run("Gaussian Blur...", "sigma=2");
	getStatistics(x2, Mean2, Min2, Max2, SD2);
	a2 = Mean2-0.3*SD2;
	close(forStatistics);
	selectWindow(currentROI);
	numberSlices=nSlices;
	for(slice = 1; slice<=numberSlices; slice++) {
		setSlice(slice);
		run("Measure");
		setResult("Frame", nResults-1, slice);
		setResult("CellID", nResults-1, ROI);
		
		//measure area and skewness thresholded in each frame
		selectWindow(currentROI);
		run("Duplicate...", "use");
		ThresholdImage = getTitle();
		run("Gaussian Blur...", "sigma=2");
		getMinAndMax(min, max);
		setThreshold(a2, max);
		setOption("BlackBackground", true);
		run("Convert to Mask", "method=Default background=Dark calculate black");
		run("Fill Holes");
		run("Create Selection");

		//jump to next loop if nothing is selected
		if(selectionType()==-1) {
			print("slice ", slice, "could not be thresholded.");
			selectWindow(ThresholdImage);
			close();
			selectWindow(currentROI);
			run("Select None");
			slice=numberSlices+1;
			updateResults();
			
		} else {
			selectWindow(currentROI);
			run("Restore Selection");
			List.setMeasurements;
			skewness = List.get("Skew");
			AreaThresholded = List.get("Area");
			run("Select None");
			selectWindow(ThresholdImage);
			close();
			selectWindow(currentROI);
			setResult("CellArea", nResults-1, AreaThresholded);
			setResult("CellSkewness", nResults-1, skewness);
			updateResults();
		}
	}
	
	selectWindow(currentROI);
	close();
}
selectWindow("Results");
saveAs("results",  output_path + orgName + "2.csv");

if (isOpen("Log")) {
         selectWindow("Log");
         run("Close" );
}
if (isOpen("Results")) {
         selectWindow("Results"); 
         run("Close" );
}
if (isOpen(orgName)) {
         selectWindow(orgName); 
         run("Close" );
}

setBatchMode(false);

Best,
Anne