Class ImageUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Class<? extends WritableImage>The default image class to work with, if the user has no Preference. -
Method Summary
Modifier and TypeMethodDescriptionstatic WritableImageautoCrop(ReadableImage image) Automatically crop an image to remove regions of background.static WritableImage[]autoCrop(ReadableImage[] images) Automatically crops an array of images to remove regions which are background in any of the images.static WritableImage[]autoCrop(ReadableImage[] images, MonitorWorker worker, boolean verbose) Automatically crops an array of images to remove regions which are background in any of the images.static WritableImage[]autoCrop(ReadableImage[] images, Double threshold) Automatically crops an array of images to remove regions which are background in any of the images.static WritableImage[]autoCrop(ReadableImage[] images, Double threshold, MonitorWorker worker, boolean verbose) Automatically crops an array of images to remove regions which are background in any of the images.static WritableImageautoCrop(ReadableImage image, MonitorWorker worker, boolean verbose) Automatically crop an image to remove regions of background.static WritableImageautoCrop(ReadableImage image, Double threshold) Automatically crop an image to remove regions of background.static WritableImageautoCrop(ReadableImage image, Double threshold, MonitorWorker worker, boolean verbose) Automatically crop an image to remove regions of background.static WritableImageautoCropSlices(ReadableImage image) Automatically crop an image to remove slices that only contain background.static WritableImageautoCropSlices(ReadableImage image, MonitorWorker worker, boolean verbose) Automatically crop an image to remove slices that only contain background.static WritableImageautoCropSlices(ReadableImage image, Double threshold) Automatically crop an image to remove slices that only contain background.static WritableImageautoCropSlices(ReadableImage image, Double threshold, MonitorWorker worker, boolean verbose) Automatically crop an image to remove slices that only contain background.static WritableImageautoResample(ReadableImage image, int maxNVoxels, SubSampleType subSampleType, boolean allowSliceResample, boolean verbose) Automatically resamples a ReadableImage so that the resulting resampled image has no more than the given maximum number of voxels.static voidcheckIndices(ReadableImage image, int[] pixIdx) Checks compatibility of the pixel indices that reference a particular pixel of an image.static voidcheckMinMax(int min, int max, PixelDataType dataType) Checks the integrity of the minimum and maximum pixel values taking into account the data type of the image.static WritableImagecrop(ReadableImage image, int[][] cropDims, MonitorWorker worker, boolean verbose) Crops aWritableImageto produce a new image.static booleandeleteImage(String filename, Class<? extends ReadableImage> c) Attempts to delete an image of the givenClass<? extends WritableImage>with the given name.export(ReadableImage image, String suffix, ColourMapping cm, double displayMin, double displayMax, ComplexMode complexMode, int slicesPerRow) Returns anInputStreamfrom which an external format image (such as JPEG, GIF etc.) can be read, producing the external format image from thisWritableImage, by drawing all of the slices in a raster of slices.static voidexport(ReadableImage image, String suffix, String filename, ColourMapping cm, double displayMin, double displayMax, ComplexMode complexMode, int slicesPerRow) Writes an image to a disk file in a picture format (such as JPEG, GIF etc.), creating the disk image from thisWritableImage, by drawing all of the slices in a raster of slices.static voidexport(ReadableImage image, String suffix, ImageOutputStream outputStream, ColourMapping cm, double displayMin, double displayMax, ComplexMode complexMode, int slicesPerRow) Writes an image to anImageOutputStreamin an external format (such as JPEG, GIF etc.), creating the disk image from thisReadableImage, by drawing all of the slices in a raster of slices.static WritableImageflip(ReadableImage image, boolean flipHorizontally, boolean flipVertically, boolean flipSlices, MonitorWorker worker, boolean verbose) Flips the image data in aReadableImage.static org.jogamp.vecmath.Point3fReturns the coordinates of the center of the block of slices of an image, in mm, in patient coordinates.static intgetNPixels(int nDim, int[] dims) Returns the total number of picture elements for an image with the given dimensions.static Class<? extends WritableImage>Returns theWritableImageclass set in the user's Preferences.static ReadableImageReturns a ReadableImage, reading the image data from a file.static ReadableImagegetReadableImage(String fileName) Returns a ReadableImage, reading the image data from a file.static booleanTests whether the supplied image has slices which are in reverse order compared to standard radiological presentation order.static WritableImagegetWritableImage(ReadableImage templateImage) Returns an in-memory WritableImage that is a copy of the image supplied.static WritableImagegetWritableImage(ReadableImage templateImage, int nSlices) Returns an in-memory WritableImage that is a copy of the image supplied except that the number of image slices may be changed.static WritableImagegetWritableImage(ReadableImage templateImage, int nDim, int[] dims) Returns an in-memory WritableImage that is a copy of the image supplied except that the image dimensionality and dimensions may be changed.static WritableImagegetWritableImage(ReadableImage templateImage, PixelDataType storedDataType) Returns an in-memoryWritableImagethat is a copy of the image supplied except that the storedPixelDataTypemay be changed.static WritableImagegetWritableImage(ReadableImage templateImage, Class<? extends WritableImage> toImageClass) Returns an in-memory WritableImage that is a copy of the image supplied except that the class of the image may be changed.static WritableImagegetWritableImage(ReadableImage templateImage, Class<? extends WritableImage> toImageClass, int nDim, int[] dims, PixelDataType storedDataType) Creates a new in-memoryWritableImagewith the class ofWritableImageclass given by the argument, possibly changing the number of slices in the image and the PixelDataType.static WritableImagegetWritableImage(ReadableImage templateImage, Class<? extends WritableImage> toImageClass, int nSlices, PixelDataType storedDataType) Creates a new in-memoryWritableImagewith the class ofWritableImageclass given by the argument, possibly changing the number of slices in the image and the PixelDataType.static WritableImageReturns a WritableImage (opened read-write), reading the image data from a file.static WritableImagegetWritableImage(String fileName) Returns a WritableImage (opened read-write), reading the image data from a file.static Class<? extends WritableImage>getWritableImageClass(Class clazz) Returns the WritableImage Class that is assignable from the supplied class, or if not assignable, the WritableImage Class set in the user's Preferences.static Class<? extends WritableImage>getWritableImageClass(Object image) Returns the WritableImage Class that can be deduced from the supplied image, or the WritableImage Class set in the user's Preferences.static Class<? extends WritableImage>getWritableImageClass(String className) Returns the WritableImage Class that is assignable from the supplied class name.static List<Class<? extends WritableImage>>Returns a List of classes that are known to implementWritableImage.static StringgetWritableImageCommonName(Class<? extends WritableImage> imageClass) Returns the "common name" (for example, "NIFTI-1") for a Class of WritableImage.static booleanimageExists(String filename, Class<? extends WritableImage> imageClass) Tests whether aWritableImagewith the given name already exists on disk.static booleanTests whether the suppliedFilerefers to aWritableImagefile.static voidRun the self-test of this class.static WritableImagepad(ReadableImage image, int[] padDims, MonitorWorker worker, boolean verbose) Pads aWritableImagewith zero-intensity pixels around the edge of the image.static WritableImagereorient(ReadableImage image, MostLikePlane newOrientation, MostLikePlane currentOrientation, Boolean slicesAreReversed, MonitorWorker worker, boolean verbose) Reorients aReadableImage.static voidreorientRadiological(ReadableImage image, Object pix) Reorients a set of pixels for an image so that they will be presented in standard radiological orientation.static WritableImagereSample(ReadableImage image, float[] newPixelSizes, InterpolationType interpolationType, SubSampleType subSampleType, MonitorWorker worker, boolean verbose) Resamples an existingReadableImageto have different pixel sizes.static WritableImagereSample(ReadableImage image, int[] newDims, InterpolationType interpolationType, SubSampleType subSampleType, MonitorWorker worker, boolean verbose) Resamples an existing image to have different numbers of pixels.static voidreverseSlices(WritableImage image, MonitorWorker worker, boolean verbose) Reverses the slice order of a WritableImage in place.static WritableImagerotate(ReadableImage image, RotationAngle rotationAngle, MonitorWorker worker, boolean verbose) Rotates aReadableImagein multiples of 90 degrees.static voidsavePreferredImageClass(Class<? extends WritableImage> imageClass) Saves the supplied class to the user's Preferences.static voidFor an image which contains multiple slices with all slices parallel and regularly-spaced, this method will modify the image position (patient coords), image orientation (patient coords) and pixel spacing (pixel sizes) according to the block.static voidsetOrientationPositionRadiological(org.jogamp.vecmath.Vector3f[] orientation, org.jogamp.vecmath.Point3f position, int nCols, int nRows, float pixelXSize, float pixelYSize) Reorientates the image orientation vectors, and repositions an image position so that the positions and orientations refer to blocks of image pixels that have been gotten in an anatomical orientation.static BufferedImagetoBufferedImage(ReadableImage image, ColourMapping cm, double displayMin, double displayMax, ComplexMode complexMode, int slicesPerRow) Create aBufferedImagefrom aReadableImage, by drawing all of the slices in a raster of slices.
-
Field Details
-
DEFAULT_IMAGE_CLASS
The default image class to work with, if the user has no Preference.
-
-
Method Details
-
getPreferredImageClass
Returns theWritableImageclass set in the user's Preferences.- Returns:
- the
WritableImageclass that has been set in the user's Preferences as the one that the user most often works with.
-
savePreferredImageClass
Saves the supplied class to the user's Preferences.- Parameters:
imageClass- the {Class<? extends WritableImage>that the user most often works with, and which will be set as the default to use when this Class loads.
-
getWritableImageClasses
Returns a List of classes that are known to implementWritableImage.- Returns:
- a List of classes that implement
WritableImage.
-
getWritableImageCommonName
Returns the "common name" (for example, "NIFTI-1") for a Class of WritableImage.- Parameters:
imageClass- the WritableImage Class.- Returns:
- the common name for the image class.
-
getWritableImageClass
Returns the WritableImage Class that can be deduced from the supplied image, or the WritableImage Class set in the user's Preferences.- Parameters:
image- the Object from which to deduce the WritableImage Class.- Returns:
- the WritableImage Class deduced from the supplied Object, or the user's preferred WritableImage Class.
-
getWritableImageClass
public static Class<? extends WritableImage> getWritableImageClass(String className) throws ClassNotFoundException, ClassCastException Returns the WritableImage Class that is assignable from the supplied class name.- Parameters:
className- the fully-qualified name of the Class to be assigned, or its "common name" (the name returned by theWritableImage.getImageTypeName()method).- Returns:
- the WritableImage Class that is assignable from the supplied class name.
- Throws:
ClassCastException- if the Class name supplied cannot be assigned to WritableImage class.ClassNotFoundException- if a Class with the supplied name cannot be found.
-
getWritableImageClass
public static Class<? extends WritableImage> getWritableImageClass(Class clazz) throws ClassCastException Returns the WritableImage Class that is assignable from the supplied class, or if not assignable, the WritableImage Class set in the user's Preferences.- Parameters:
clazz- the Class to be assigned.- Returns:
- the WritableImage Class that is assignable from the supplied class, or the user's preferred WritableImage Class.
- Throws:
ClassCastException
-
getReadableImage
Returns a ReadableImage, reading the image data from a file.- Parameters:
fileName- the name (path) of the image file.- Returns:
- a WritableImage obtained from the file.
- Throws:
InvalidImageException- if the file does not yield a valid WritableImage.
-
getWritableImage
Returns a WritableImage (opened read-write), reading the image data from a file.- Parameters:
fileName- the name (path) of the image file.- Returns:
- a WritableImage obtained from the file.
- Throws:
InvalidImageException- if the file does not yield a valid WritableImage.
-
getReadableImage
Returns a ReadableImage, reading the image data from a file.- Parameters:
f- the image file.- Returns:
- a ReadableImage obtained from the file.
- Throws:
InvalidImageException- if the file does not yield a valid WritableImage.
-
getWritableImage
Returns a WritableImage (opened read-write), reading the image data from a file.- Parameters:
f- the image file.- Returns:
- a WritableImage obtained from the file.
- Throws:
InvalidImageException- if the file does not yield a valid WritableImage.
-
getWritableImage
public static WritableImage getWritableImage(ReadableImage templateImage) throws InvalidImageException, IOException Returns an in-memory WritableImage that is a copy of the image supplied.- Parameters:
templateImage- theReadableImageon which to base the copy.- Returns:
- the new
WritableImagethat is created. - Throws:
InvalidImageException- if the supplied ReadableImage is invalid.IOException- occurs while reading the supplied ReadableImage.
-
getWritableImage
public static WritableImage getWritableImage(ReadableImage templateImage, PixelDataType storedDataType) throws InvalidImageException, IOException Returns an in-memoryWritableImagethat is a copy of the image supplied except that the storedPixelDataTypemay be changed.- Parameters:
templateImage- theReadableImageon which to base the copy.storedDataType- the storedPixelDataTypefor the newWritableImage. The pixel values from the templateImage will be coerced to the storedDataType and clipped as necessary to fit within the intensity range of the storedDataType.- Returns:
- the new
WritableImagethat is created. - Throws:
InvalidImageException- if the supplied ReadableImage is invalid.IOException- occurs while reading the supplied ReadableImage.
-
getWritableImage
public static WritableImage getWritableImage(ReadableImage templateImage, int nSlices) throws InvalidImageException, IOException Returns an in-memory WritableImage that is a copy of the image supplied except that the number of image slices may be changed.- Parameters:
templateImage- theReadableImageon which to base the copy.nSlices- the number of slices for the new image.- Returns:
- the new
WritableImagethat is created. - Throws:
InvalidImageException- if the supplied ReadableImage is invalid.IOException- occurs while reading the supplied ReadableImage.
-
getWritableImage
public static WritableImage getWritableImage(ReadableImage templateImage, int nDim, int[] dims) throws InvalidImageException, IOException Returns an in-memory WritableImage that is a copy of the image supplied except that the image dimensionality and dimensions may be changed.- Parameters:
templateImage- theReadableImageon which to base the copy.nDim- the dimensionality for the new image.dims- the dimensions of the new image.- Returns:
- the new
WritableImagethat is created. - Throws:
InvalidImageException- if the supplied ReadableImage is invalid.IOException- occurs while reading the supplied ReadableImage.
-
getWritableImage
public static WritableImage getWritableImage(ReadableImage templateImage, Class<? extends WritableImage> toImageClass) throws InvalidImageException, IOException Returns an in-memory WritableImage that is a copy of the image supplied except that the class of the image may be changed.- Parameters:
templateImage- the image on which to base the copy.toImageClass- the class of the new image.- Returns:
- the new
WritableImagethat is created. - Throws:
InvalidImageException- if the supplied image is invalid.IOException- occurs while reading the supplied image.
-
getWritableImage
public static WritableImage getWritableImage(ReadableImage templateImage, Class<? extends WritableImage> toImageClass, int nSlices, PixelDataType storedDataType) throws InvalidImageException, IOException Creates a new in-memoryWritableImagewith the class ofWritableImageclass given by the argument, possibly changing the number of slices in the image and the PixelDataType.If the number of slices requested for the new image is is less than the original, only the first slices will be copied to the new image. If the number of slices requested for the new image is more than the original, the additional slices will be filled with zero pixel values, and the slice-specific information will be empty.
- Parameters:
templateImage- the image on which to base the copy.toImageClass- the class of the new image.nSlices- the number of slices for the new image.storedDataType- the storedPixelDataTypefor the newWritableImage. The pixel values from the templateImage will be coerced to the storedDataType and clipped as necessary to fit within the intensity range of the storedDataType.- Returns:
- the new
WritableImagethat is created. - Throws:
InvalidImageException- if the supplied image is invalid.IOException- occurs while reading the supplied image or writing the WritableImage.
-
getWritableImage
public static WritableImage getWritableImage(ReadableImage templateImage, Class<? extends WritableImage> toImageClass, int nDim, int[] dims, PixelDataType storedDataType) throws InvalidImageException, IOException Creates a new in-memoryWritableImagewith the class ofWritableImageclass given by the argument, possibly changing the number of slices in the image and the PixelDataType.If the number of slices requested for the new image is is less than the original, only the first slices will be copied to the new image. If the number of slices requested for the new image is more than the original, the additional slices will be filled with zero pixel values, and the slice-specific information will be empty.
- Parameters:
templateImage- the image on which to base the copy.toImageClass- the class of the new image.nDim- the dimensionality for the new image.dims- the dimensions of the new image.storedDataType- the storedPixelDataTypefor the newWritableImage. The pixel values from the templateImage will be coerced to the storedDataType and clipped as necessary to fit within the intensity range of the storedDataType.- Returns:
- the new
WritableImagethat is created. - Throws:
InvalidImageException- if the supplied image is invalid.IOException- occurs while reading the supplied image or writing the WritableImage.
-
checkIndices
Checks compatibility of the pixel indices that reference a particular pixel of an image. Checking ensures that the pixel addressed by the indices is within the bounds of the image.- Parameters:
image- the image against which the indices will be checked.pixIdx- an array where each element of the array is an index to one dimension of the image.- Throws:
IllegalArgumentException- if the supplied indices are bad for this image.
-
getNPixels
Returns the total number of picture elements for an image with the given dimensions.- Parameters:
nDim- the dimensionality of the image.dims- the size of the image in each dimension.- Returns:
- the total number of pixels for an image with the given dimensions.
- Throws:
IllegalArgumentException- if the dimensionality is illegal, or if the image is too large (has more than 231-1 pixels).
-
checkMinMax
public static void checkMinMax(int min, int max, PixelDataType dataType) throws InvalidImageException Checks the integrity of the minimum and maximum pixel values taking into account the data type of the image.- Parameters:
min- the putative minimum pixel value.max- the putative maximum pixel value.dataType- the PixelDataType for the image.- Throws:
InvalidImageException- if the min/max values are unsuitable for this image.
-
isImage
Tests whether the suppliedFilerefers to aWritableImagefile.- Parameters:
f- theFileto be tested.- Returns:
trueif the supplied File refers an image file;falseotherwise.- Throws:
FileNotFoundException- if the file does not exist.
-
imageExists
Tests whether aWritableImagewith the given name already exists on disk. Analyze/NIFTI/DICOM file extensions are handled in a smart fashion.- Parameters:
filename- the name of the image file to test for existence.imageClass- the Class ofWritableImagefor which to test existence.- Returns:
trueif an image with the specified file name exists on disk;falseotherwise.
-
deleteImage
Attempts to delete an image of the givenClass<? extends WritableImage>with the given name.- Parameters:
filename- the name of the image file that is to be deleted.c- the class of image to delete; used to help find which file of is correct one to delete, which may be useful if the file name is given without extension and multiple images with the same base name exist.- Returns:
trueif the image file is successfully deleted, or if the image file did not exist in the first place.
-
getImageCentrePatient
public static org.jogamp.vecmath.Point3f getImageCentrePatient(ReadableImage image) throws InvalidImageException Returns the coordinates of the center of the block of slices of an image, in mm, in patient coordinates.- Parameters:
image- the image for which to find the centre location.- Returns:
- a the
Point3fcorresponding to the Left,Posterior,Superior (LPS) coordinates of the centre of the block of slices for the image. Returnsnullif the position cannot be retrieved from the image. - Throws:
InvalidImageException- if the image is invalid.
-
reorientRadiological
Reorients a set of pixels for an image so that they will be presented in standard radiological orientation. This is:- For axial images, increasing left coordinate with column number, and increasing posterior coordinate with row number.
- For coronal images, increasing left coordinate with column number, and increasing inferior coordinate with row number.
- For sagittal images, increasing posterior coordinate with column number, and increasing inferior coordinate with row number.
- Parameters:
image- theReadableImagefor which the pixels are to be reoriented.pix- the array of pixels to reorient. The reorientation is done in-place.
-
setOrientationPositionRadiological
public static void setOrientationPositionRadiological(org.jogamp.vecmath.Vector3f[] orientation, org.jogamp.vecmath.Point3f position, int nCols, int nRows, float pixelXSize, float pixelYSize) Reorientates the image orientation vectors, and repositions an image position so that the positions and orientations refer to blocks of image pixels that have been gotten in an anatomical orientation.This method works out the way in which an image slice with the supplied orientation and position will be presented radiologically, and sets the orientation and position "in place" correctly for the image slice as presented radiologically.
- Parameters:
orientation- the current orientation of the image slice.position- the current position of the image slice.nCols- the number of columns in the image.nRows- the number of rows in the image.pixelXSize- the pixel size along a row.pixelYSize- the pixel size down a column.
-
getSlicesAreReversed
Tests whether the supplied image has slices which are in reverse order compared to standard radiological presentation order.- Parameters:
image- the image for which to test the slice order.- Returns:
trueif the slices are in reverse order compared to standard radiological presentation order;falseotherwise.
-
set3DBlockParameters
public static void set3DBlockParameters(WritableImage image) throws IOException, InvalidImageException For an image which contains multiple slices with all slices parallel and regularly-spaced, this method will modify the image position (patient coords), image orientation (patient coords) and pixel spacing (pixel sizes) according to the block.Images that do not consist of a single block of parallel, equally-spaced slices are unaffeted by this method.
- Parameters:
image- the image for which the parameters are to be set.- Throws:
InvalidImageException- if the image information cannot be modified.IOException- if an I/O error occurs.
-
reSample
public static WritableImage reSample(ReadableImage image, int[] newDims, InterpolationType interpolationType, SubSampleType subSampleType, MonitorWorker worker, boolean verbose) throws InvalidArgumentException, InvalidImageException, IOException, CancelledException Resamples an existing image to have different numbers of pixels. The newDims array specifies the new number of pixels in each dimension of the image.- Parameters:
image- the image to be resampled.newDims- the number of samples in each of the dimensions of the image. newDims must have the same number of elements as there are dimensions in this image. The integer in each element of the array specifies the number of samples in each dimension for the resampled image. The column dimension is the last array element; the row dimension is the next-to-last; the slice dimension is the second-from-last, and the time dimension is the third-from-last. Any dimensions that are below the time dimension are ignored, and no resampling will occur in these dimensions.interpolationType- the type of interpolation to used if the number of samples increases in any dimension.subSampleType- the type of sub-sampling to be used if the pixel size is increased in any dimension.worker- if non-null, aProgressMonitormay be created, showing the progress of the resampling.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImagebased on the supplied image, but resampled to the specified number of samples. - Throws:
InvalidArgumentException- if the new dimensions are unsuitable for resampling the supplied image.InvalidImageException- if any of the new number of samples is less than or equal to zero, or if the length of the newDims array is incorrect for this image.IOException- if an I/O error occurs.CancelledException- if resampling is cancelled via theProgressMonitorthat may pop up.
-
autoResample
public static WritableImage autoResample(ReadableImage image, int maxNVoxels, SubSampleType subSampleType, boolean allowSliceResample, boolean verbose) throws InvalidImageException, IOException, CancelledException Automatically resamples a ReadableImage so that the resulting resampled image has no more than the given maximum number of voxels.The numbers of samples will be calculated to give a more isotropic resolution. The algorithm will reduce the number of samples (if necessary) in any or all dimensions so that the resulting image has no more than the specified number of voxels, but the number of samples in any direction will never be increased to try to achieve isotropic resolution.
The number of voxels may be less than the maximum because the resulting image will always have the same field of view as this image, and the number of samples in each dimension must (of course) be a whole number.
Resampling of the fourth dimension of 4-D images does not take place, only of the three spatial dimensions. Images with dimensionality greater than 4 may not ne resampled with this method.
- Parameters:
image- the image to be resampled.maxNVoxels- maximum number of voxels allowed in the resampled image.subSampleType- the type of sub-sampling to be used if the pixel size is increased in any dimension.allowSliceResample- iftrue, resampling of pixels in the slice direction is allowed; iffalse, resampling in the slice direction is disallowed.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a resampled image with no more than the specified maximum number of voxels, or the original image if it already has fewer voxels than the specified maximum.
- Throws:
InvalidImageException- if the image dimensionality is greater than 4. or equal to zero, or if the length of the newDims array is incorrect for this image.IOException- if an I/O error occurs.CancelledException- if resampling is cancelled via theProgressMonitorthat may pop up.
-
reSample
public static WritableImage reSample(ReadableImage image, float[] newPixelSizes, InterpolationType interpolationType, SubSampleType subSampleType, MonitorWorker worker, boolean verbose) throws InvalidArgumentException, InvalidImageException, IOException, CancelledException Resamples an existingReadableImageto have different pixel sizes. The newPixelSizes array specifies the physical size of pixels in each dimension of the image that has a physical "size". If, for any dimension of this image, the pixel size cannot be found, or if the pixel size is invalid, that dimension is not resampled. reSample does not know how to resample in any dimension greater than 4; in any image with dimensions higher than 4, higher dimensions are are not resampled, since reSample cannot find the pixel size in these dimensions. Any new pixel sizes you set for these higher dimensions will be ignored.- Parameters:
image- the image to be resampled.newPixelSizes- the physical pixel size in each of the dimensions of the image. newPixelSizes must have the same number of elements as there are dimensions in this image. The floating point number in each element of the array specifies the pixel size in each dimension for the resampled image. The column dimension is the last array element; the row dimension is the next-to-last; the slice dimension is the second-from-last, and the time dimension is the third-from-last. Any other dimensions are ignored.interpolationType- the type of interpolation to be used if the pixel size is reduced in any dimension.subSampleType- the type of sub-sampling to be used if the pixel size is increased in any dimension.worker- if non-null, aProgressMonitormay be created, showing the progress of the resampling.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImagelike the supplied image, but resampled to the specified pixel sizes. - Throws:
InvalidArgumentException- if the array of pixel sizes is unsuitable for resampling the supplied image.InvalidImageException- if any of the new pixel sizes is not positive, or if the length of the newPixelSizes array is insufficient for this image.IOException- if an I/O error occurs.CancelledException- if resampling is cancelled via theProgressMonitorthat may pop up.
-
rotate
public static WritableImage rotate(ReadableImage image, RotationAngle rotationAngle, MonitorWorker worker, boolean verbose) throws InvalidArgumentException, InvalidImageException, IOException, CancelledException Rotates aReadableImagein multiples of 90 degrees.- Parameters:
image- the image to be rotated.rotationAngle- theRotationAngle.worker- if non-null, aProgressMonitormay be created, showing the progress of the rotation.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also a WritableImage), but rotated. - Throws:
InvalidArgumentException- if the image is unsuitable for rotating.InvalidImageException- if the supplied image is invalid.IOException- if an I/O error occurs.CancelledException- if rotation is cancelled via theProgressMonitorthat may pop up.
-
flip
public static WritableImage flip(ReadableImage image, boolean flipHorizontally, boolean flipVertically, boolean flipSlices, MonitorWorker worker, boolean verbose) throws InvalidArgumentException, InvalidImageException, IOException, CancelledException Flips the image data in aReadableImage. Only the image data are affected by the flip operation. Any orientation information stored in the image header is copied to the output image unchanged. Thus, this method is suitable for correcting the orientation of images where it is known that the orientation stored in the image header does not match that displayed on screen, and the error can be corrected by simple flips- Parameters:
image- the image to be flipped.flipHorizontally- if the image is to be flipped about a vertical axis.flipVertically- if the image is to be flipped about a horizontal axis.flipSlices- if the slice order is to be reversed.worker- if non-null, aProgressMonitormay be created, showing the progress of the flipping.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also a WritableImage), but with the image data flipped as specified. - Throws:
InvalidArgumentException- if the image is unsuitable for flipping.InvalidImageException- if the supplied image is invalid.IOException- if an I/O error occurs.CancelledException- if flipping is cancelled via theProgressMonitorthat may pop up.
-
reorient
public static WritableImage reorient(ReadableImage image, MostLikePlane newOrientation, MostLikePlane currentOrientation, Boolean slicesAreReversed, MonitorWorker worker, boolean verbose) throws InvalidImageException, IOException, CancelledException Reorients aReadableImage. The newOrientation sets the orientation of the reoriented image. If non-null, currentOrientation specifies the orientation of this image. If currentOrientation is null, this method will try to determine the orientation of this image.- Parameters:
image- the image to be reoriented.newOrientation- theMostLikePlaneof the required new orientation.currentOrientation- theMostLikePlaneof the current orientation, ornullto force an attempt to determine the orientation automatically.slicesAreReversed- if non-null, specifies whether the slices are in reverse order compared to standard radiological order (which is:- axial: increasing superior coordinate with slice number
- coronal: increasing posterior coordinate with slice number
- sagittal: increasing left coordinate with slice number
null, the slice order is determined from the image header.worker- if non-null, aProgressMonitormay be created, showing the progress of the reorientation.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also a WritableImage), but reoriented to the specified new orientation. - Throws:
InvalidImageException- if currentOrientation is null, and the orientation of this image cannot be determined.IOException- if an I/O error occurs.CancelledException- if reorienting is cancelled via theProgressMonitorthat may pop up.
-
reverseSlices
public static void reverseSlices(WritableImage image, MonitorWorker worker, boolean verbose) throws InvalidImageException, IOException, CancelledException Reverses the slice order of a WritableImage in place. The only operation performed is to replace the pixel data for the first slice with that for the last slice; the image data for the second slice with that for the next-to-last slice, etc.- Parameters:
image- the image to have its slice order reversed.worker- if non-null, aProgressMonitormay be created, showing the progress of the slice reversal..verbose- if true, progress messages will be written toSystem.out.- Throws:
InvalidImageException- if the supplied image is invalid.IOException- if an I/O error occurs.CancelledException- if reorienting is cancelled via theProgressMonitorthat may pop up.
-
crop
public static WritableImage crop(ReadableImage image, int[][] cropDims, MonitorWorker worker, boolean verbose) throws InvalidArgumentException, InvalidImageException, CancelledException Crops aWritableImageto produce a new image. The cropDims array specifies the range of pixel indices in each dimension of the image from which to select a block of pixels.- Parameters:
image- the image to crop.cropDims- the high and low indices (referenced from zero) of samples in each of the dimensions of the image. cropDims must have the same number of elements as there are dimensions in this image, and each element must be an array of length 2. The first element of each array specifies the low pixel index for that dimension, and the second element of the array specifies the high pixel index to select for the cropped image. The column dimension is the last array element; the row dimension is the next-to-last; the slice dimension is the second-from-last etc.worker- if non-null, aProgressMonitormay be created, showing the progress of the cropping.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also a WritableImage), but cropped to the specified range of pixels. - Throws:
InvalidArgumentException- if any of the indices is less than zero, or greater than the number of samples, or if the size of the cropDims array is incorrect for this image.InvalidImageException- if the cropped image cannot be created.CancelledException- if cropping is cancelled via theProgressMonitorthat may pop up.
-
autoCrop
Automatically crop an image to remove regions of background. The threshold for separating the foreground from the background is selected using the Otsu method.- Parameters:
image- the image to crop.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also aWritableImage), but cropped to remove the background. - Throws:
InvalidImageException- if the cropped image cannot be created.
-
autoCrop
public static WritableImage autoCrop(ReadableImage image, Double threshold) throws InvalidImageException Automatically crop an image to remove regions of background.- Parameters:
image- the image to crop.threshold- if non-null, the intensity threshold that separates the foreground from the background. Ifnull, the threshold is selected using the Otsu method.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also a WritableImage), but cropped to remove the background. - Throws:
InvalidImageException- if the cropped image cannot be created.
-
autoCrop
public static WritableImage autoCrop(ReadableImage image, MonitorWorker worker, boolean verbose) throws InvalidImageException, CancelledException Automatically crop an image to remove regions of background. The threshold for separating the foreground from the background is selected using the Otsu method.- Parameters:
image- the image to crop.worker- if non-null, theMonitorWorkerthat can be used to cancel the operation.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also a WritableImage), but cropped to remove the background. - Throws:
InvalidImageException- if the cropped image cannot be created.CancelledException- if cropping is cancelled via theProgressMonitorthat may pop up.
-
autoCrop
public static WritableImage autoCrop(ReadableImage image, Double threshold, MonitorWorker worker, boolean verbose) throws InvalidImageException, CancelledException Automatically crop an image to remove regions of background.- Parameters:
image- the image to crop.threshold- if non-null, the intensity threshold that separates the foreground from the background. Ifnull, the threshold is selected using the Otsu method.worker- if non-null, theMonitorWorkerthat can be used to cancel the operation.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also a WritableImage), but cropped to remove the background. - Throws:
InvalidImageException- if the cropped image cannot be created.CancelledException- if cropping is cancelled via theProgressMonitorthat may pop up.
-
autoCropSlices
Automatically crop an image to remove slices that only contain background. The threshold for separating the foreground from the background is selected using the Otsu method.- Parameters:
image- the image from which to crop slices with only background.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also aWritableImage), but cropped to remove the background slices. - Throws:
InvalidImageException- if the cropped image cannot be created.
-
autoCropSlices
public static WritableImage autoCropSlices(ReadableImage image, Double threshold) throws InvalidImageException Automatically crop an image to remove slices that only contain background.- Parameters:
image- the image from which to crop slices with only background.threshold- if non-null, the intensity threshold that separates the foreground from the background. Ifnull, the threshold is selected using the Otsu method.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also aWritableImage), but cropped to remove the background slices. - Throws:
InvalidImageException- if the cropped image cannot be created.
-
autoCropSlices
public static WritableImage autoCropSlices(ReadableImage image, MonitorWorker worker, boolean verbose) throws InvalidImageException, CancelledException Automatically crop an image to remove slices that only contain background. The threshold for separating the foreground from the background is selected using the Otsu method.- Parameters:
image- the image from which to crop slices with only background.worker- if non-null, theMonitorWorkerthat can be used to cancel the operation.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also aWritableImage), but cropped to remove the background slices. - Throws:
InvalidImageException- if the cropped image cannot be created.CancelledException- if cropping is cancelled via theProgressMonitorthat may pop up.
-
autoCropSlices
public static WritableImage autoCropSlices(ReadableImage image, Double threshold, MonitorWorker worker, boolean verbose) throws InvalidImageException, CancelledException Automatically crop an image to remove slices that only contain background.- Parameters:
image- the image from which to crop slices with only background.threshold- if non-null, the intensity threshold that separates the foreground from the background. Ifnull, the threshold is selected using the Otsu method.worker- if non-null, theMonitorWorkerthat can be used to cancel the operation.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also aWritableImage), but cropped to remove the background slices. - Throws:
InvalidImageException- if the cropped image cannot be created.CancelledException- if cropping is cancelled via theProgressMonitorthat may pop up.
-
autoCrop
Automatically crops an array of images to remove regions which are background in any of the images. All input images must be of the same dimensions. The threshold for separating the foreground from the background is selected using the Otsu method.- Parameters:
images- the images to crop.- Returns:
- a new in-memory
WritableImagearray of the same classes as the input images (if the input image is also a WritableImage), but cropped to remove the background. All returned images will have the same dimensions, and regions which are background in any of the imnput images will be cropped. - Throws:
InvalidImageException- if the cropped images cannot be created.
-
autoCrop
public static WritableImage[] autoCrop(ReadableImage[] images, Double threshold) throws InvalidImageException Automatically crops an array of images to remove regions which are background in any of the images. All input images must be of the same dimensions.- Parameters:
images- the images to crop.threshold- if non-null, the intensity threshold that separates the foreground from the background. Ifnull, the threshold is selected using the Otsu method.- Returns:
- a new in-memory
WritableImagearray of the same classes as the input images (if the input image is also a WritableImage), but cropped to remove the background. All returned images will have the same dimensions, and regions which are background in any of the imnput images will be cropped. - Throws:
InvalidImageException- if the cropped images cannot be created.
-
autoCrop
public static WritableImage[] autoCrop(ReadableImage[] images, MonitorWorker worker, boolean verbose) throws InvalidImageException, CancelledException Automatically crops an array of images to remove regions which are background in any of the images. All input images must be of the same dimensions. The threshold for separating the foreground from the background is selected using the Otsu method.- Parameters:
images- the images to crop.worker- if non-null, theMonitorWorkerthat can be used to cancel the operation.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImagearray of the same classes as the input images (if the input image is also aWritableImage), but cropped to remove the background. All returned images will have the same dimensions, and regions which are background in any of the imnput images will be cropped. - Throws:
InvalidImageException- if the cropped images cannot be created.CancelledException- if cropping is cancelled via theProgressMonitorthat may pop up.
-
autoCrop
public static WritableImage[] autoCrop(ReadableImage[] images, Double threshold, MonitorWorker worker, boolean verbose) throws InvalidImageException, CancelledException Automatically crops an array of images to remove regions which are background in any of the images. All input images must be of the same dimensions.- Parameters:
images- the images to crop.threshold- if non-null, the intensity threshold that separates the foreground from the background. Ifnull, the threshold is selected using the Otsu method.worker- if non-null, theMonitorWorkerthat can be used to cancel the operation.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also aWritableImage), but cropped to remove the background. - Throws:
InvalidImageException- if the cropped images cannot be created.CancelledException- if cropping is cancelled via theProgressMonitorthat may pop up.
-
pad
public static WritableImage pad(ReadableImage image, int[] padDims, MonitorWorker worker, boolean verbose) throws InvalidArgumentException, InvalidImageException, CancelledException Pads aWritableImagewith zero-intensity pixels around the edge of the image. The padDims array specifies the number of samples in each dimension of the padded image. The zero-intensity pixels are distributed as evenly as possible to either side of the current field of view (within the constrains imposed by odd/even numbers of pixel samples).- Parameters:
image- the image to pad.padDims- the number of samples in each dimension of the image for the padded image. padDims must have the same number of elements as there are dimensions in this image. The column dimension is the last array element; the row dimension is the next-to-last; the slice dimension is the second-from-last etc. If any value is less than the number of samples in the current image, then the number of samples in that dimension is unchanged in the new image.worker- if non-null, aProgressMonitormay be created, showing the progress of the padding.verbose- if true, progress messages will be written toSystem.out.- Returns:
- a new in-memory
WritableImageof the same class as the input image (if the input image is also a WritableImage), but padded to the specified number of pixel samples. - Throws:
InvalidArgumentException- if the size of the padDims array is incorrect for this image.InvalidImageException- if the padded image cannot be created.CancelledException- if padding is cancelled via theProgressMonitorthat may pop up.
-
export
public static void export(ReadableImage image, String suffix, String filename, ColourMapping cm, double displayMin, double displayMax, ComplexMode complexMode, int slicesPerRow) throws IOException, InvalidImageException Writes an image to a disk file in a picture format (such as JPEG, GIF etc.), creating the disk image from thisWritableImage, by drawing all of the slices in a raster of slices.The first slice will be at the top left of the image, and the image is filled across the row of slices before moving down to the next row. The width of the disk image (in pixels) will either be the number of columns in the
WritableImagemultiplied by the number ofslicesPerRow, or 1024, whichever is the smaller.- Parameters:
image- the image to export.suffix- the suffix describing the type of image to be created. Currently, must be one of:- "gif"
- "jpeg" or "jpg"
- "bmp"
- "png"
filename- the name of the disk file to which the image will be written.cm- theColourMappingto use when converting the pixel values to colours in the image. Thecmis ignored forPixelDataTypes that have the R,G,B components specified.displayMin- the image pixel value that maps to the first entry in theColourMapping. All pixel values belowdisplayMinwill also be mapped to the first entry.displayMax- the image pixel value that maps to the last entry in theColourMapping. All pixel values abovedisplayMaxwill also be mapped to the last entry.complexMode- the display mode for complex images. May benullfor non-complex images.slicesPerRow- the number of image slices to put into one row of the image raster.- Throws:
IOException- an error occurs writing to the file, or if an image of the type requested cannot be created.InvalidImageException- if thisWritableImagehas bad dimensionality for this operation.
-
export
public InputStream export(ReadableImage image, String suffix, ColourMapping cm, double displayMin, double displayMax, ComplexMode complexMode, int slicesPerRow) throws IOException, InvalidImageException Returns anInputStreamfrom which an external format image (such as JPEG, GIF etc.) can be read, producing the external format image from thisWritableImage, by drawing all of the slices in a raster of slices. The first slice will be at the top left of the image, and the image is filled across the row of slices before moving down to the next row. The width of the image will be either be the number of columns in theWritableImagemultiplied by the number ofslicesPerRow, or 1024, whichever is the smaller.- Parameters:
image- the image to export.suffix- the suffix describing the type of image to be created. Currently, must be one of:- "gif"
- "jpeg" or "jpg"
- "bmp"
- "png"
cm- theColourMappingto use when converting the pixel values to colours in the image. Thecmis ignored forPixelDataTypes that have the R,G,B components specified.displayMin- the image pixel value that maps to the first entry in theColourMapping. All pixel values belowdisplayMinwill also be mapped to the first entry.displayMax- the image pixel value that maps to the last entry in theColourMapping. All pixel values abovedisplayMaxwill also be mapped to the last entry.complexMode- the display mode for complex images. May benullfor non-complex images.slicesPerRow- the number of image slices to put into one row of the image raster.- Returns:
- an
InputStreamfrom which the resulting external format image can be read. - Throws:
IOException- an error occurs writing the image, or if an image of the type requested cannot be created.InvalidImageException- if thisWritableImagehas bad dimensionality for this operation.
-
export
public static void export(ReadableImage image, String suffix, ImageOutputStream outputStream, ColourMapping cm, double displayMin, double displayMax, ComplexMode complexMode, int slicesPerRow) throws IOException, InvalidImageException Writes an image to anImageOutputStreamin an external format (such as JPEG, GIF etc.), creating the disk image from thisReadableImage, by drawing all of the slices in a raster of slices. The first slice will be at the top left of the image, and the image is filled across the row of slices before moving down to the next row. The width of the image will be either be the number of columns in theWritableImagemultiplied by the number ofslicesPerRow, or 1024, whichever is the smaller.- Parameters:
image- the image to export.suffix- the suffix describing the type of image to be created. Currently, must be one of:- "gif"
- "jpeg" or "jpg"
- "bmp"
- "png"
outputStream- theImageOutputStreamto which the image will be written.cm- theColourMappingto use when converting the pixel values to colours in the image. Thecmis ignored forPixelDataTypes that have the R,G,B components specified.displayMin- the image pixel value that maps to the first entry in theColourMapping. All pixel values belowdisplayMinwill also be mapped to the first entry.displayMax- the image pixel value that maps to the last entry in theColourMapping. All pixel values abovedisplayMaxwill also be mapped to the last entry.complexMode- the display mode for complex images. May benullfor non-complex images.slicesPerRow- the number of image slices to put into one row of the image raster.- Throws:
IOException- an error occurs writing to theImageOutputStream, or if an image of the type requested cannot be created.InvalidImageException- if thisWritableImagehas bad dimensionality for this operation.
-
toBufferedImage
public static BufferedImage toBufferedImage(ReadableImage image, ColourMapping cm, double displayMin, double displayMax, ComplexMode complexMode, int slicesPerRow) throws IllegalArgumentException, InvalidImageException Create aBufferedImagefrom aReadableImage, by drawing all of the slices in a raster of slices. The first slice will be at the top left of theBufferedImage, and theBufferedImageis filled across the row of slices before moving down to the next row. The width of theBufferedImagewill be either the number of columns in theReadableImagemultiplied by the number ofslicesPerRow, or 1048, whichever is the smaller.- Parameters:
image- the image to be drawn.cm- theColourMappingto use when converting the pixel values to colours in theBufferedImage. Thecmis ignored forPixelDataTypes that have the R,G,B components specified.displayMin- the image pixel value that maps to the first entry in theColourMapping. All pixel values belowdisplayMinwill also be mapped to the first entry.displayMax- the image pixel value that maps to the last entry in theColourMapping. All pixel values abovedisplayMaxwill also be mapped to the last entry.complexMode- the display mode for complex images. May benullfor non-complex images.slicesPerRow- the number of image slices to put into one row of theBufferedImageraster.- Returns:
- a
BufferedImageshowing all the slices of thisWritableImage. - Throws:
IllegalArgumentException- if this image cannot be converted to aBufferedImage.InvalidImageException- if thisWritableImagehas bad dimensionality for this operation.
-
main
Run the self-test of this class.- Parameters:
args- the program arguments.
-