Skip to content

Instantly share code, notes, and snippets.

@iarganda
Created November 4, 2014 17:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iarganda/c7fc0a88b8d2737c9d3d to your computer and use it in GitHub Desktop.
Save iarganda/c7fc0a88b8d2737c9d3d to your computer and use it in GitHub Desktop.
import ij.IJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.process.FloatProcessor;
// assume probability image as the currently selected one
probs = IJ.getImage();
// read image dimensions
width = probs.getWidth();
height = probs.getHeight();
nSlices = probs.getNSlices();
nClasses = probs.getNChannels();
// create stack to store segmentation
segmentation = new ImageStack( width, height );
// for each slice
for( s = 1; s <= nSlices; s++ )
{
probs.setSlice( s );
probStack = probs.getImageStack();
segSlice = new FloatProcessor( width, height );
for( x=0; x<width; x++ )
for( y=0; y<height; y++ )
{
max = 0f;
cIndex = 0;
// select class with higher probability
for( c=0; c<nClasses; c++ )
if( probStack.getVoxel( x, y, c ) > max )
{
max = probStack.getVoxel( x, y, c );
cIndex = c;
}
segSlice.setf( x, y, (float) cIndex );
}
segmentation.addSlice( "", segSlice );
}
// create and display segmentation image
result = new ImagePlus( "Thresholded probabilities", segmentation );
result.show();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment