Run trainable Weka Segmentation over multiple images after training from multiple images

Hello All,
I am working on segmenting a series of images, basically stills from a video, and I have trained a model (.arff data file) using multiple of these images following the guidance from this forum post (Train weka segmentation classifier on many images) and the default classifier “FastRandomForest”. So far, I have been trying to loop through the images based on the Scripting Wiki to apply the model to each of the images and export the resulting segmentation as an image.
I am more familiar with macros than the BeanShell scripting but it does not seem like Macros will do what I need it to.
The Issue: I seem to be missing a way to bring in the model (.arff file) to apply it to the segmentation. Is this even nessesary? Did I miss a step along the way?

Below is the BeanShell Script with little modification from the forum:

#@ File(label=“Input directory”, description=“Select the directory with input images”, style=“directory”) inputDir
#@ File(label=“Output directory”, description=“Select the output directory”, style=“directory”) outputDir
#@ File(label=“Weka model”, description=“Select the Weka model to apply”) modelPath
#@ File(label=“Weka trained data”, description=“Select the Weka trained data to apply”) dataPath
#@ String(label=“Result mode”,choices={“Labels”,“Probabilities”}) resultMode

import trainableSegmentation.WekaSegmentation;
import trainableSegmentation.utils.Utils;
import ij.IJ;
import ij.ImagePlus;

// starting time
startTime = System.currentTimeMillis();

// caculate probabilities?
getProbs = resultMode.equals( “Probabilities” );

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

// get list of input images
listOfFiles = inputDir.listFiles();
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 )
print("Processing image: "+image);
// apply classifier and get results (0 indicates number of threads is auto-detected)
result = segmentator.applyClassifier( image, 0, getProbs );

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

        // force garbage collection (important for large images)
        result = null; 
        image = null;
// print elapsed time
estimatedTime = System.currentTimeMillis() - startTime;
IJ.log( “** Finished processing folder in " + estimatedTime + " ms **” );

This line should load the classifier from the selected path from here:

#@ File(label=“Weka model”, description=“Select the Weka model to apply”) modelPath

Are you sure the .arff file is brought in as the “classifier” instead of the .model file?

When I input the .arff file it errors out “Error while loading train header”

For the segmentation the classifier is needed, this is the model so to speak.

The data (stored in the .arff file) is the data that has been used to generate the model / classifier. So these are the traces you draw on your training image, the features and other settings you used.

Have a look at the documentation:

To answer your question correctly then, the .arff data is not needed for the script above.

1 Like

I suppose I’m feeling dense today. I have been all through the documentation wiki on the TWS and the scripting of TWS but have not been able to get a segmentation result with just importing the classifier, I’ve always had to import the training data (both GUI and Macro).

If I use the script as is from here:

A quick model for the AuPbSn40.tif sample image: (19.6 KB)

I get the following result:
AuPbSn40.tif (243.1 KB)


Is this what you want to achieve? Maybe you can guide us through your usage and were it does not work.

I based the code I initially copied off that version, but I suppose I messed something up. When copied directly from the site, it worked. Thanks @schmiedc, I’ve got some BeanShell learning to do…

1 Like