Adding macro to weka segmentation classifer

Hello all!
Sorry for being such a novice here…
I am trying to add a threshold and “Analyze Particles” macro to the weka classifier I have created. Using the following code, I am having trouble running it post weka results and outputting data into a certain choice file.

Thanks in advance!

-Jonathan

           // IJ.run(result, "8-bit");
           // IJ.run( result, "Make Binary" );

            IJ.run( result, "8-bit","");
            IJ.run(results, "Auto Threshold", "method=Default ignore_black white");
            IJ.run(results, "Make Binary", "method=Default background=Dark calculate black");
            Prefs.blackBackground = true;
            IJ.run(results, "Convert to Mask", "method=Default background=Dark calculate black");
            IJ.run(results, "Watershed", "slice");
            // IJ.run (results, "Analyze Particles" );
            IJ.run(results, "Analyze Particles...", "size=400-900 circularity=0.00-1.10 show=Outlines display exclude clear add slice");

Whole code:

// @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
// @String(label="Result mode",choices={"Labels","Probabilities"}) resultMode
 
import trainableSegmentation.WekaSegmentation;
import ij.io.FileSaver;
import ij.IJ;
import ij.ImagePlus;
 
// starting time
startTime = System.currentTimeMillis();
 
// caculate probabilities?
getProbs = false;
 
// 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 = new ImagePlus( listOfFiles[i].getCanonicalPath() );
        if( image != null )
        {       
            // create segmentator
            segmentator = new WekaSegmentation( image );
            // load classifier
            segmentator.loadClassifier( modelPath.getCanonicalPath() );
            // apply classifier and get results
            segmentator.applyClassifier( getProbs );
            result = segmentator.getClassifiedImage();
            // IJ.run(result, "8-bit");
           // IJ.run( result, "Make Binary" );

            IJ.run( result, "8-bit","");
            IJ.run(results, "Auto Threshold", "method=Default ignore_black white");
            IJ.run(results, "Make Binary", "method=Default background=Dark calculate black");
            Prefs.blackBackground = true;
            IJ.run(results, "Convert to Mask", "method=Default background=Dark calculate black");
            IJ.run(results, "Watershed", "slice");
            // IJ.run (results, "Analyze Particles" );
            IJ.run(results, "Analyze Particles...", "size=400-900 circularity=0.00-1.10 show=Outlines display exclude clear add slice");
            // 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)
            segmentator = null;
            result = null; 
            image = null;
            System.gc();
        }
    }
}
// print elapsed time
estimatedTime = System.currentTimeMillis() - startTime;
IJ.log( "** Finished processing folder in " + estimatedTime + " ms **" );

Thank you for providing the code, but you haven’t indicated what the problem is (error?) or given any sample files to reproduce the problem with. Without either the error or the ability to test the code on your data, it will be challenging (probably for someone else) to troubleshoot.

Sorry for the confusion. I know my question was very vague.

Acually figured it out. Not sure how to delete this post.

Thanks in advance.

1 Like