Running macro in headless mode on - Error

Dear all,

I “installed” Fiji/ImageJ on a Linux/Debian system (no GUI) to run some macros we have created in the lab in headless mode. These macros work when we test them in Fiji that is “installed” of a Windows machine. However, when we run the same exact macro (only the input file paths were changed) on the Linux/Debian system, as such

imagej --headless -macro 2d_foci_counting_rolling_ball.ijm

then we get the following error

\Clear
java.awt.HeadlessException
        at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
        at java.awt.Window.<init>(Window.java:432)
        at java.awt.Frame.<init>(Frame.java:403)
        at ij.text.TextWindow.<init>(TextWindow.java:52)
        at ij.text.TextWindow.<init>(TextWindow.java:40)
        at ij.plugin.NewPlugin.createTable(NewPlugin.java:97)
        at ij.plugin.NewPlugin.run(NewPlugin.java:73)
        at ij.IJ.runPlugIn(IJ.java:182)
        at ij.Executer.runCommand(Executer.java:132)
        at ij.Executer.run(Executer.java:65)
        at ij.IJ.run(IJ.java:292)
        at ij.macro.Functions.doRun(Functions.java:601)
        at ij.macro.Functions.doFunction(Functions.java:96)
        at ij.macro.Interpreter.doStatement(Interpreter.java:227)
        at ij.macro.Interpreter.doStatements(Interpreter.java:215)
        at ij.macro.Interpreter.run(Interpreter.java:112)
        at ij.macro.Interpreter.run(Interpreter.java:82)
        at ij.macro.Interpreter.run(Interpreter.java:93)
        at ij.plugin.Macro_Runner.runMacro(Macro_Runner.java:153)
        at ij.plugin.Macro_Runner.runMacroFile(Macro_Runner.java:137)
        at ij.IJ.runMacroFile(IJ.java:143)
        at net.imagej.legacy.IJ1Helper$4.call(IJ1Helper.java:933)
        at net.imagej.legacy.IJ1Helper$4.call(IJ1Helper.java:930)
        at net.imagej.legacy.IJ1Helper.runMacroFriendly(IJ1Helper.java:880)
        at net.imagej.legacy.IJ1Helper.runMacroFile(IJ1Helper.java:930)
        at net.imagej.legacy.LegacyCommandline$Macro.handle(LegacyCommandline.java:188)
        at org.scijava.console.DefaultConsoleService.processArgs(DefaultConsoleService.java:93)
        at net.imagej.legacy.LegacyConsoleService.processArgs(LegacyConsoleService.java:76)
        at net.imagej.Main.launch(Main.java:64)
        at net.imagej.Main.main(Main.java:73)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at net.imagej.launcher.ClassLauncher.launch(ClassLauncher.java:258)
        at net.imagej.launcher.ClassLauncher.run(ClassLauncher.java:184)
        at net.imagej.launcher.ClassLauncher.main(ClassLauncher.java:76)

The macro script is as follows (and running it with setBatchMode on does not make a difference in terms of the error messages that we get)

print("\\Clear");

run("New... ", "name=results-nuclei type=Table");
print("[results-nuclei]","\\Headings:\t \tIntDen\tRawIntDen\tseriesNumber");

run("New... ", "name=results-foci type=Table");
print("[results-foci]","\\Headings:\t \tArea\tMean\tStdDev\tX\tY\tnucleusNumber\tseriesNumber");

run("Bio-Formats Macro Extensions"); 
Ext.setId("test.lif");
Ext.getSeriesCount(seriesCount);
print("there are "+seriesCount+" series in your lif file");

for (seriesNumber=1;seriesNumber<seriesCount+1;seriesNumber++){
	roiManager ("reset");
	run("Bio-Formats Importer", "open=[test.lif] color_mode=Colorized view=Hyperstack stack_order=XYCZT series_"+seriesNumber);
	print("opened series "+seriesNumber);
	rename("stack");
	
	run("Z Project...", "projection=[Max Intensity]");
	run("Split Channels");
	close("C3-AVG_stack");
	selectWindow("C1-MAX_stack");
	rename("foci");
	selectWindow("C2-MAX_stack");
	rename("nuclei");
	
	selectWindow("nuclei");
	run("FeatureJ Laplacian", "compute smoothing=3");
	setAutoThreshold("MaxEntropy");
	run("Convert to Mask");
	run("Set Measurements...", "area redirect=None decimal=3");
	run("Analyze Particles...", "size=1.5-Infinity add display exclude clear");
	close("nuclei");
	print("nuclei segmentation finished");
	numberNuclei=roiManager("count");
	print(""+numberNuclei+" nuclei found");

	if (numberNuclei>0) {
	
		selectWindow("foci");
		run("Duplicate...","title=foci-segmented");
		run("Gaussian Blur...", "sigma=0.50");
		run("Subtract Background...", "rolling=1");
		setAutoThreshold("Yen dark");
		setOption("BlackBackground", true);
		run("Convert to Mask");
		run("Watershed");
		print("foci segmentation finished");	

		if (isOpen("Results")) {
			selectWindow("Results");
			run("Close");
		}
	
		run("Set Measurements...", "area mean standard centroid redirect=foci decimal=8");
		resultsLine=0;
		for (i=0;i<numberNuclei;i++) {
			selectWindow("foci-segmented");
			roiManager("select",i);
			run("Analyze Particles...", "size=0-Infinity display");
			for (k=resultsLine;k<nResults;k++ )
				setResult("nucleusNumber", k, i+1);
			resultsLine=nResults;
		}
		numberFoci=nResults();
		print("foci quantification finished");  
		print(""+numberFoci+ " foci found");
	
		if (numberFoci>0) {
        		for	(k=0;k<numberFoci;k++) 
	     			setResult("seriesNumber", k, seriesNumber);
     			updateResults();
        		String.copyResults();
        		print("[results-foci]",String.paste);
        	     			
			selectWindow("foci");
			run("Duplicate...","title=foci-center");
			setBackgroundColor(0, 0, 0);
			run("Select All");
			run("Clear");
			for (i=0;i<numberFoci;i++) { 
				x=getResult("X", i);
				y=getResult("Y", i);
				toUnscaled(x,y);
				setPixel(x,y,1);
			}
			run("Clear Results");
			run("Set Measurements...", "integrated redirect=None decimal=3");
			for (i=0;i<numberNuclei;i++) {
				selectWindow("foci-center");
				roiManager("select",i);
				run("Measure");
				setResult("seriesNumber", i, seriesNumber);
			}
	
			updateResults();
        		String.copyResults();
        		print("[results-nuclei]",String.paste);
       			run("Clear Results");
     	
    			print("foci counting finished");

	
		}
	} 
	run("Close All");
}

Does anybody know what the issue is?

Thanks in advance.

Nicola

Perhaps you installed imagej from the Debian package manager? That package is only ImageJ 1.x, not ImageJ2. And it is not maintained by the core ImageJ team.

Only ImageJ2 has support for headless operation. We advise against using the Debian package manager to install ImageJ, but instead suggest you download it manually.

No, I didn’t. I manually “installed” the Linux distribution.

In fact, after some discussion with our HPC manager I managed to make the whole thing to work by starting a graphical environment from terminal, as such

Xvnc4 :$UID 2> /dev/null & export DISPLAY=:$UID

before running

imagej -macro 2d_foci_counting_rolling_ball.ijm

without the --headless option.

Thanks again.

Nicola

2 Likes