Trainable Weka script - load FeatureStack from TIFF and apply classifier


::Final Edit:: I solved my own problem (thanks to Ignacios’ comments here: I didn’t realize there is a separately applyClassifier() that doesn’t require the imp input: prob = seg.applyClassifier( testFeatureStackArray 0, true ); Editing that line of code accordingly resolved the below issue.

I’m having some issues applying a classifier to saved FeatureStacks.

To save some time during development, I am creating stacks of Features up front and saving them to a directory. I then load them one by one and apply the classifier against them, but I get the “Could not apply classifier” popup eight times. I have verified that each feature in my model exists in the stack by loading the TIF and checking the label of each slice and comparing it to the attribute names present in the model.

I adapted this code from Ignacio’s examples, but clearly I am doing somethign wrong. Is there anything obvious?

::edited:: I realized there is a method where you can directly define the FeatureStack, but I am still getting the same error. The Log claims the classification was .

batchClassifier() {

// create segmentator
segmentator = new WekaSegmentation();
// load classifier
segmentator.loadClassifier( super.modelPath.getCanonicalPath() );

// get list of input images
//listOfFiles = super.inputDir.listFiles();

listOfFiles = super.inputDir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".tif");
for ( i = 0; i < listOfFiles.length; i++ )
// process only files (do not go into sub-folders)
if( listOfFiles[ i ].isFile() )
// try to read file as image
image = IJ.openImage( listOfFiles[i].getCanonicalPath() );
if( image != null )
inputImageStack = image.getStack();
inputImage= new ImagePlus( “OrigImage”, inputImageStack.getProcessor( 1 ) );
width = image.getWidth();
height = image.getHeight();
featuresArray = new FeatureStackArray(1);
finalFeatureStack = new FeatureStack( width, height, false );
featuresArray.set(finalFeatureStack, 0);

          // apply classifier and get results (0 indicates number of threads is auto-detected)
          result = segmentator.applyClassifier( inputImage, featuresArray, 0, true );

          if( !getProbs )
              // assign same LUT as in GUI
              result.setLut( Utils.getGoldenAngleLUT() );
          // save result as TIFF in output folder
          outputFileName = listOfFiles[ i ].getName().replaceFirst("[.][^.]+$", "") + "_Result.tif";
          new File( super.rootDir + File.separator + "Results" ).mkdirs();
          new FileSaver( result ).saveAsTiff( super.rootDir + File.separator + "Results" + File.separator + outputFileName );

          // force garbage collection (important for large images)
          result = null; 
          image = null;
          tempFeatureStack = null;


The error message:

Started MasterSegmenter1.bsh at Wed Sep 12 11:55:06 PDT 2018
Sourced file: inline evaluation of: import ij.IJ; import ij.ImagePlus; import ij.ImageStack; import ij.gui . . . '' : Method Invocation segmentator.applyClassifier : at Line: 310 : in file: inline evaluation of: import ij.IJ; import ij.ImagePlus; import ij.ImageStack; import ij.gui . . . ‘’ : segmentator .applyClassifier ( inputImage , featuresArray , 0 , true )

Target exception: java.lang.NullPointerException

at bsh.BSHMethodInvocation.eval(

Hi, I was wondering if you ever figured this out and if you have the updated code? I am facing a similar problem.


Did AWag’s solution not work?