Skip to content

Instantly share code, notes, and snippets.

@bogovicj
Last active March 11, 2016 15:02
Show Gist options
  • Save bogovicj/42d6f8581ff71a1bba76 to your computer and use it in GitHub Desktop.
Save bogovicj/42d6f8581ff71a1bba76 to your computer and use it in GitHub Desktop.
Reading a 3d Nifti image into an ImageJ ImagePlus
import java.io.IOException;
import loci.formats.FormatException;
import loci.formats.in.NiftiReader;
import loci.plugins.util.ImageProcessorReader;
import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
import ij.ImageStack;
public class TestNiiReader
{
/**
* Needs theses two dependencies in maven:
* groupId: net.imagej
* artifactId: ij
*
* groupId: ome
* artifactId: bio-formats_plugins
*/
public static void main( String[] args ) throws FormatException, IOException
{
String f = "path-to-my-nifti-file.nii";
/**
* The two lines below yields:
* "Unsupported format or not found"
* at the time of this writing
*/
// ImagePlus aa = IJ.openImage( f );
// System.out.println( aa );
NiftiReader reader = new NiftiReader();
reader.setId( f );
int width = reader.getSizeX();
int height = reader.getSizeY();
int depth = reader.getSizeZ();
ImageProcessorReader ipr = new ImageProcessorReader( reader );
ImageStack stack = new ImageStack( width, height );
for( int z = 0; z < depth; z++ )
stack.addSlice( ipr.openProcessors( z )[0] );
reader.close();
ipr.close();
ImagePlus ip = new ImagePlus( "foo", stack );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment