This example shows you how to create a simple programme to extract a single image slice from a multi-slice image. The image can be in UNC, Analyze, NIFTI-1, NIFTI-2 or DICOM format. To try out the programme, save the following text to a file called ExtractSlice.java. You need to make sure that the following files from the Jim installation folder are in your classpath:
javac ExtractSlice.java
Note: you must use javac version 17.java ExtractSlice 2 input-image output-image
where input-image is the name of the image from which to extract the slice, and output-image is the image to create. This example would extract slice 2 from the input image and create a single slice output image containing just slice 2.
<--Java code Starts Here -->
import java.io.IOException;
import com.xinapse.image.ReadableImage;
import com.xinapse.image.WritableImage;
import com.xinapse.image.ImageUtils;
import com.xinapse.image.InvalidImageException;
import com.xinapse.multisliceimage.InfoStorer;
import com.xinapse.util.InfoList;
public class ExtractSlice {
/**
Java program to extract a single slice from a multi-slice image.
*/
public static void main(String argv[]) {
int sliceToExtract = 0;
int nFiles = argv.length - 1;
if (nFiles != 2) {
System.err.println("Usage: ExtractSlice slice-number input-file output-file");
System.err.println(" - extract a slice from a multi-slice image and create a new image");
System.exit(-1);
}
try {
// The user will have supplied a slice indexed from 1.
// getSlice works with indices from zero.
sliceToExtract = Integer.parseInt(argv[0]) - 1;
}
catch (NumberFormatException e) {
System.err.println("Couldn't get slice number to extract from " + argv[0]);
System.exit(-1);
}
// Create a ReadableImage instance from the supplied file name.
ReadableImage inputImage = null;
try {
inputImage = ImageUtils.getReadableImage(argv[1]);
}
catch (InvalidImageException iiE) {
System.err.println("Error reading input image: " + iiE.getMessage());
System.exit(-1);
}
String outputImageName = argv[2];
// Get the image dimensionality.
int nDim = inputImage.getNDim();
if (nDim < 3) {
System.err.println("Can't extract a slice from a " + nDim + "-dimensional image");
System.exit(-1);
}
int nRows = inputImage.getNRows();
int nCols = inputImage.getNCols();
int nSlices = inputImage.getTotalNSlices();
if (sliceToExtract < 0 || sliceToExtract >= nSlices) {
System.err.println("Slice to extract out of range: " + (sliceToExtract + 1));
System.exit(-1);
}
// Create the output image, with the same format as the input image, but with only one
// slice. There are other methods to change the format, change the pixel data type etc.
try {
WritableImage outputImage = ImageUtils.getWritableImage(inputImage, 1);
// Get the one slice-worth of pixels from the input image ...
Object slicePixels = inputImage.getSlice(sliceToExtract);
// ... and put them to slice 0 of the output image.
outputImage.putSlice(slicePixels, 0);
if (inputImage instanceof InfoStorer && outputImage instanceof InfoStorer) {
// Copy any slice-specific information from the right slice in the input image to
// the general information of the output image.
InfoList sliceInfoList = ((InfoStorer) inputImage).getInfoList(nDim-3, sliceToExtract);
((InfoStorer) outputImage).appendInfoList(sliceInfoList);
// Remove any slice-specific information from the output image (for slice 0).
((InfoStorer) outputImage).setSliceInfoList(new InfoList(), 0);
}
// Save the output image to disk.
outputImage.write(outputImageName);
// It's good practice to close the images when we're finished with them, although
// in this case they will be closed when the program exits.
inputImage.close();
outputImage.close();
}
catch (IOException ioE) {
System.err.println("Error writing output image: " + ioE.getMessage());
System.exit(-1);
}
catch (InvalidImageException iiE) {
System.err.println("Error writing output image: " + iiE.getMessage());
System.exit(-1);
}
System.exit(0);
}
}