Novice with an exception error in macro

macro
Tags: #<Tag:0x00007fb87d65d780>

#1

Hello all,

I am running the following macro in batch mode and getting an exception that I can’t even begin to understand. It cycles through the first image and makes the measurement but doesn’t open the next image in the “input” folder. My goal is to be able to watch the image processing as it progresses but have each image set close after the analysis so that I don’t have hundreds of image windows open at the end. Thanks in advance for your suggestions.

setBatchMode(“show”);
T = getTitle();
selectWindow(T);
run(“Split Channels”);
selectWindow(T+" (blue)");
setBatchMode(“show”);
run(“Subtract Background…”, “rolling=50”);
waitForUser(“select area then click edit–>selection–>create selection then click ok”);
selectWindow(T+" (red)");
setBatchMode(“show”);
run(“Subtract Background…”, “rolling=30”);
setAutoThreshold(“Default dark”);
setThreshold(31, 217);
run(“Restore Selection”);
run(“Analyze Particles…”, “size=50-100 pixel summarize”);
run(“Close All”);

Exception:
ImageJ 1.48v; Java 1.6.0_65 [64-bit]; Mac OS X 10.12.3; 142MB of 658MB (21%)

java.lang.NullPointerException
at ij.process.ColorProcessor.(ColorProcessor.java:30)
at ij.ImagePlus.setupProcessor(ImagePlus.java:715)
at ij.ImagePlus.getFileInfo(ImagePlus.java:1780)
at ij.io.FileSaver.(FileSaver.java:34)
at ij.IJ.saveAsTiff(IJ.java:1736)
at ij.IJ.saveAs(IJ.java:1665)
at ij.plugin.BatchProcessor.processFolder(BatchProcessor.java:200)
at ij.plugin.BatchProcessor.run(BatchProcessor.java:91)
at ij.IJ.runPlugIn(IJ.java:169)
at ij.Executer.runCommand(Executer.java:131)
at ij.Executer.run(Executer.java:64)
at java.lang.Thread.run(Thread.java:695)


#2

Hi,

Do you have other files in the input folder other than images? You should only have images in that folder which imagej should analyze.


#3

Hi,

The only other thing in the folder with the images is an output folder for the post-processing file that is generated.


#4

I am not able to reliably reproduce this exception but I suspect it occurs because the macro closes the image which the Process>Batch>Macro command is trying to save. You can avoid this problem by leaving the “Output…” field blank. It’s probably better, however, to avoid the Process>Batch>Macro command, which is not designed to work with displayed images or with the waitForUser() function. Instead, use a simple macro like the following that iterates through all the images in a folder.

  dir = getDirectory("Choose Source Directory ");
  list = getFileList(dir);
  for (i=0; i<list.length; i++) {
     open(dir+list[i]);
     T = getTitle(); 
     run("Split Channels"); 
     selectWindow(T+" (blue)");
     run("Subtract Background...", "rolling=50");
     waitForUser("select area then click edit>selection>create selection then click ok");
     selectWindow(T+" (red)");
     run("Subtract Background...", "rolling=30");
     setAutoThreshold("Default dark");
     setThreshold(31, 217);
     run("Restore Selection");
     run("Analyze Particles...", "size=50-100 pixel summarize");
     run("Close All");
  }